1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_
6 #define CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_
11 #include "base/memory/linked_ptr.h"
12 #import "base/mac/scoped_nsobject.h"
13 #include "base/timer/timer.h"
14 #include "content/common/gpu/gpu_command_buffer_stub.h"
15 #include "content/common/gpu/image_transport_surface.h"
16 #include "ui/gl/gl_surface.h"
17 #include "ui/gl/gpu_switching_observer.h"
24 class ImageTransportSurfaceOverlayMac
: public gfx::GLSurface
,
25 public ImageTransportSurface
,
26 public ui::GpuSwitchingObserver
{
28 ImageTransportSurfaceOverlayMac(GpuChannelManager
* manager
,
29 GpuCommandBufferStub
* stub
,
30 gfx::PluginWindowHandle handle
);
32 // GLSurface implementation
33 bool Initialize() override
;
34 void Destroy() override
;
35 bool IsOffscreen() override
;
36 gfx::SwapResult
SwapBuffers() override
;
37 gfx::SwapResult
PostSubBuffer(int x
, int y
, int width
, int height
) override
;
38 bool SupportsPostSubBuffer() override
;
39 gfx::Size
GetSize() override
;
40 void* GetHandle() override
;
41 bool OnMakeCurrent(gfx::GLContext
* context
) override
;
42 bool SetBackbufferAllocation(bool allocated
) override
;
43 bool ScheduleOverlayPlane(int z_order
,
44 gfx::OverlayTransform transform
,
46 const gfx::Rect
& bounds_rect
,
47 const gfx::RectF
& crop_rect
) override
;
48 bool IsSurfaceless() const override
;
50 // ImageTransportSurface implementation
51 void OnBufferPresented(
52 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
) override
;
53 void OnResize(gfx::Size pixel_size
, float scale_factor
) override
;
54 void SetLatencyInfo(const std::vector
<ui::LatencyInfo
>&) override
;
55 void WakeUpGpu() override
;
57 // ui::GpuSwitchingObserver implementation.
58 void OnGpuSwitched() override
;
64 ~ImageTransportSurfaceOverlayMac() override
;
66 gfx::SwapResult
SwapBuffersInternal(const gfx::Rect
& pixel_damage_rect
);
68 void UpdateRootAndPartialDamagePlanes(
69 const std::vector
<linked_ptr
<OverlayPlane
>>& new_overlay_planes
,
70 const gfx::Rect
& dip_damage_rect
);
71 void UpdateOverlayPlanes(
72 const std::vector
<linked_ptr
<OverlayPlane
>>& new_overlay_planes
);
73 void UpdateCALayerTree();
75 // Returns true if the front of |pending_swaps_| has completed, or has timed
77 bool IsFirstPendingSwapReadyToDisplay(
78 const base::TimeTicks
& now
);
79 // Sets the CALayer contents to the IOSurface for the front of
80 // |pending_swaps_|, and removes it from the queue.
81 void DisplayFirstPendingSwapImmediately();
82 // Force that all of |pending_swaps_| displayed immediately, and the list be
84 void DisplayAndClearAllPendingSwaps();
85 // Callback issued during the next vsync period ofter a SwapBuffers call,
86 // to check if the swap is completed, and display the frame. Note that if
87 // another SwapBuffers happens before this callback, the pending swap will
88 // be tested at that time, too.
89 void CheckPendingSwapsCallback();
90 // Function to post the above callback. The argument |now| is passed as an
91 // argument to avoid redundant calls to base::TimeTicks::Now.
92 void PostCheckPendingSwapsCallbackIfNeeded(const base::TimeTicks
& now
);
94 // Return the time of |interval_fraction| of the way through the next
95 // vsync period that starts after |from|. If the vsync parameters are not
96 // valid then return |from|.
97 base::TimeTicks
GetNextVSyncTimeAfter(
98 const base::TimeTicks
& from
, double interval_fraction
);
100 scoped_ptr
<ImageTransportHelper
> helper_
;
101 base::scoped_nsobject
<CAContext
> ca_context_
;
102 base::scoped_nsobject
<CALayer
> ca_root_layer_
;
104 gfx::Size pixel_size_
;
106 std::vector
<ui::LatencyInfo
> latency_info_
;
108 // The renderer ID that all contexts made current to this surface should be
110 GLint gl_renderer_id_
;
112 // Overlay planes that have been scheduled, but have not had a subsequent
113 // SwapBuffers call made yet.
114 std::vector
<linked_ptr
<OverlayPlane
>> pending_overlay_planes_
;
116 // A queue of all frames that have been created by SwapBuffersInternal but
117 // have not yet been displayed. This queue is checked at the beginning of
118 // every swap and also by a callback.
119 std::deque
<linked_ptr
<PendingSwap
>> pending_swaps_
;
121 // The planes that are currently being displayed on the screen.
122 linked_ptr
<OverlayPlane
> current_root_plane_
;
123 std::list
<linked_ptr
<OverlayPlane
>> current_partial_damage_planes_
;
124 std::list
<linked_ptr
<OverlayPlane
>> current_overlay_planes_
;
126 // The time of the last swap was issued. If this is more than two vsyncs, then
127 // use the simpler non-smooth animation path.
128 base::TimeTicks last_swap_time_
;
130 // The vsync information provided by the browser.
131 bool vsync_parameters_valid_
;
132 base::TimeTicks vsync_timebase_
;
133 base::TimeDelta vsync_interval_
;
135 base::Timer display_pending_swap_timer_
;
136 base::WeakPtrFactory
<ImageTransportSurfaceOverlayMac
> weak_factory_
;
139 } // namespace content
141 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_