1 // Copyright 2014 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_CALAYER_MAC_H_
6 #define CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_CALAYER_MAC_H_
8 #include "base/mac/scoped_nsobject.h"
9 #include "content/common/gpu/image_transport_surface_fbo_mac.h"
10 #include "ui/base/cocoa/remote_layer_api.h"
11 #include "ui/gl/gpu_switching_observer.h"
12 #include "ui/gl/scoped_cgl.h"
14 // Interface to the CALayer that will draw the content, and will be sent to the
15 // browser process via a CAContext.
16 @protocol ImageTransportLayer
17 // Draw a new frame whenever is appropriate (for pull-based systems, this may
18 // result in the frame being drawn at some point in the future).
19 - (void)drawNewFrame
:(gfx::Rect
)dirtyRect
;
20 // Draw the frame immediately (force pull models to do a pull immedately).
21 - (void)drawPendingFrameImmediately
;
22 // This is called when the layer is no longer being used by the
23 // CALayerStorageProvider.
24 - (void)resetStorageProvider
;
29 // Allocate CAOpenGLLayer-backed storage for an FBO image transport surface.
30 class CALayerStorageProvider
31 : public ImageTransportSurfaceFBO::StorageProvider
,
32 public ui::GpuSwitchingObserver
{
34 CALayerStorageProvider(ImageTransportSurfaceFBO
* transport_surface
);
35 ~CALayerStorageProvider() override
;
37 // ImageTransportSurfaceFBO::StorageProvider implementation:
38 gfx::Size
GetRoundedSize(gfx::Size size
) override
;
39 bool AllocateColorBufferStorage(
40 CGLContextObj context
, const base::Closure
& context_dirtied_callback
,
41 GLuint texture
, gfx::Size pixel_size
, float scale_factor
) override
;
42 void FreeColorBufferStorage() override
;
43 void FrameSizeChanged(
44 const gfx::Size
& pixel_size
, float scale_factor
) override
;
45 void SwapBuffers(const gfx::Rect
& dirty_rect
) override
;
46 void WillWriteToBackbuffer() override
;
47 void DiscardBackbuffer() override
;
48 void SwapBuffersAckedByBrowser(bool disable_throttling
) override
;
50 // Interface to ImageTransportLayer:
51 CGLContextObj
LayerShareGroupContext();
52 base::Closure
LayerShareGroupContextDirtiedCallback();
53 bool LayerHasPendingDraw() const;
54 void LayerDoDraw(const gfx::Rect
& dirty_rect
);
56 // ui::GpuSwitchingObserver implementation.
57 void OnGpuSwitched() override
;
60 void DrawImmediatelyAndUnblockBrowser();
62 // The browser will be blocked while there is a frame that was sent to it but
63 // hasn't drawn yet. This call will un-block the browser.
64 void UnblockBrowserIfNeeded();
66 // Inform the layer that it is no longer being used, and reset the layer.
69 ImageTransportSurfaceFBO
* transport_surface_
;
71 // Used to determine if we should use setNeedsDisplay or setAsynchronous to
72 // animate. If vsync is disabled, an immediate setNeedsDisplay and
73 // displayIfNeeded are called.
74 const bool gpu_vsync_disabled_
;
76 // Used also to determine if we should wait for CoreAnimation to call our
77 // drawInCGLContext, or if we should force it with displayIfNeeded.
78 bool throttling_disabled_
;
80 // Set when a new swap occurs, and un-set when |layer_| draws that frame.
81 bool has_pending_draw_
;
83 // The texture with the pixels to draw, and the share group it is allocated
85 base::ScopedTypeRef
<CGLContextObj
> share_group_context_
;
86 base::Closure share_group_context_dirtied_callback_
;
88 gfx::Size fbo_pixel_size_
;
89 float fbo_scale_factor_
;
91 // State for the Core Profile code path.
93 GLuint vertex_shader_
;
94 GLuint fragment_shader_
;
95 GLuint position_location_
;
97 GLuint vertex_buffer_
;
100 // The CALayer that the current frame is being drawn into.
101 base::scoped_nsobject
<CAContext
> context_
;
102 base::scoped_nsobject
<CALayer
<ImageTransportLayer
>> layer_
;
104 // When a CAContext is destroyed in the GPU process, it will become a blank
105 // CALayer in the browser process. Put retains on these contexts in this queue
106 // when they are discarded, and remove one item from the queue as each frame
108 std::list
<base::scoped_nsobject
<CAContext
> > previously_discarded_contexts_
;
110 // Indicates that the CALayer should be recreated at the next swap. This is
111 // to ensure that the CGLContext created for the CALayer be on the right GPU.
112 bool recreate_layer_after_gpu_switch_
;
114 // Weak factory against which a timeout task for forcing a draw is created.
115 base::WeakPtrFactory
<CALayerStorageProvider
> pending_draw_weak_factory_
;
117 DISALLOW_COPY_AND_ASSIGN(CALayerStorageProvider
);
120 } // namespace content
122 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_CALAYER_MAC_H_