Mac Overlays: Send vsync parameters directly
[chromium-blink-merge.git] / content / common / gpu / image_transport_surface_overlay_mac.h
blob9ad06750bd5dee61f7c5ed7caf3111474f87be66
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_
8 #include <deque>
10 #include "base/memory/linked_ptr.h"
11 #import "base/mac/scoped_nsobject.h"
12 #include "content/common/gpu/gpu_command_buffer_stub.h"
13 #include "content/common/gpu/image_transport_surface.h"
14 #include "ui/gl/gl_surface.h"
15 #include "ui/gl/gpu_switching_observer.h"
17 @class CAContext;
18 @class CALayer;
20 namespace content {
22 class ImageTransportSurfaceOverlayMac : public gfx::GLSurface,
23 public ImageTransportSurface,
24 public ui::GpuSwitchingObserver {
25 public:
26 ImageTransportSurfaceOverlayMac(GpuChannelManager* manager,
27 GpuCommandBufferStub* stub,
28 gfx::PluginWindowHandle handle);
30 // GLSurface implementation
31 bool Initialize() override;
32 void Destroy() override;
33 bool IsOffscreen() override;
34 gfx::SwapResult SwapBuffers() override;
35 gfx::SwapResult PostSubBuffer(int x, int y, int width, int height) override;
36 bool SupportsPostSubBuffer() override;
37 gfx::Size GetSize() override;
38 void* GetHandle() override;
39 bool OnMakeCurrent(gfx::GLContext* context) override;
40 bool ScheduleOverlayPlane(int z_order,
41 gfx::OverlayTransform transform,
42 gfx::GLImage* image,
43 const gfx::Rect& bounds_rect,
44 const gfx::RectF& crop_rect) override;
45 bool IsSurfaceless() const override;
47 // ImageTransportSurface implementation
48 void OnBufferPresented(
49 const AcceleratedSurfaceMsg_BufferPresented_Params& params) override;
50 void OnResize(gfx::Size pixel_size, float scale_factor) override;
51 void SetLatencyInfo(const std::vector<ui::LatencyInfo>&) override;
52 void WakeUpGpu() override;
54 // ui::GpuSwitchingObserver implementation.
55 void OnGpuSwitched() override;
57 private:
58 class PendingSwap;
60 ~ImageTransportSurfaceOverlayMac() override;
62 gfx::SwapResult SwapBuffersInternal(const gfx::Rect& pixel_damage_rect);
64 // Returns true if the front of |pending_swaps_| has completed, or has timed
65 // out by |now|.
66 bool IsFirstPendingSwapReadyToDisplay(
67 const base::TimeTicks& now);
68 // Sets the CALayer contents to the IOSurface for the front of
69 // |pending_swaps_|, and removes it from the queue.
70 void DisplayFirstPendingSwapImmediately();
71 // Force that all of |pending_swaps_| displayed immediately, and the list be
72 // cleared.
73 void FinishAllPendingSwaps();
74 // Callback issued during the next vsync period ofter a SwapBuffers call,
75 // to check if the swap is completed, and display the frame. Note that if
76 // another SwapBuffers happens before this callback, the pending swap will
77 // be tested at that time, too.
78 void CheckPendingSwapsCallback();
79 // Function to post the above callback. The argument |now| is passed as an
80 // argument to avoid redundant calls to base::TimeTicks::Now.
81 void PostCheckPendingSwapsCallbackIfNeeded(const base::TimeTicks& now);
83 // Return the time of |interval_fraction| of the way through the next
84 // vsync period that starts after |from|. If the vsync parameters are not
85 // valid then return |from|.
86 base::TimeTicks GetNextVSyncTimeAfter(
87 const base::TimeTicks& from, double interval_fraction);
89 scoped_ptr<ImageTransportHelper> helper_;
90 base::scoped_nsobject<CAContext> ca_context_;
91 base::scoped_nsobject<CALayer> layer_;
92 base::scoped_nsobject<CALayer> partial_damage_layer_;
94 gfx::Size pixel_size_;
95 float scale_factor_;
96 std::vector<ui::LatencyInfo> latency_info_;
98 // The renderer ID that all contexts made current to this surface should be
99 // targeting.
100 GLint gl_renderer_id_;
102 // Weak pointer to the image provided when ScheduleOverlayPlane is called. Is
103 // consumed and reset when SwapBuffers is called. For now, only one overlay
104 // plane is supported.
105 gfx::GLImage* pending_overlay_image_;
107 // A queue of all frames that have been created by SwapBuffersInternal but
108 // have not yet been displayed. This queue is checked at the beginning of
109 // every swap and also by a callback.
110 std::deque<linked_ptr<PendingSwap>> pending_swaps_;
112 // The union of the damage rects of SwapBuffersInternal since the last
113 // non-partial swap.
114 gfx::Rect accumulated_partial_damage_pixel_rect_;
116 // The vsync information provided by the browser.
117 bool vsync_parameters_valid_;
118 base::TimeTicks vsync_timebase_;
119 base::TimeDelta vsync_interval_;
121 // True if there is a pending call to CheckPendingSwapsCallback posted.
122 bool has_pending_callback_;
124 base::WeakPtrFactory<ImageTransportSurfaceOverlayMac> weak_factory_;
127 } // namespace content
129 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_