1 // Copyright 2013 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_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_LAYER_MAC_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_LAYER_MAC_H_
8 #import <Cocoa/Cocoa.h>
10 #include "base/mac/scoped_cftyperef.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/timer/timer.h"
14 @
class CompositingIOSurfaceLayer
;
17 class CompositingIOSurfaceMac
;
18 class CompositingIOSurfaceContext
;
20 // The interface through which the CompositingIOSurfaceLayer calls back into
21 // the structrue that created it (RenderWidgetHostViewMac or
22 // BrowserCompositorViewMac).
23 class CompositingIOSurfaceLayerClient
{
25 // Used to indicate that the layer should attempt to draw immediately and
26 // should (even if the draw is elided by the system), ack the frame
28 virtual bool AcceleratedLayerShouldAckImmediately() const = 0;
30 // Called when a frame is drawn or when, because the layer is not visible, it
31 // is known that the frame will never drawn.
32 virtual void AcceleratedLayerDidDrawFrame() = 0;
34 // Called when an error prevents the frame from being drawn.
35 virtual void AcceleratedLayerHitError() = 0;
38 // CompositingIOSurfaceLayerHelper provides C++ functionality needed for the
39 // CompositingIOSurfaceLayer class, and does most of the heavy lifting for the
41 // TODO(ccameron): This class should own CompositingIOSurfaceLayer, rather than
43 class CompositingIOSurfaceLayerHelper
{
45 CompositingIOSurfaceLayerHelper(CompositingIOSurfaceLayerClient
* client
,
46 CompositingIOSurfaceLayer
* layer
);
47 ~CompositingIOSurfaceLayerHelper();
49 // Called when the CompositingIOSurfaceLayer gets a new frame.
52 // Called whenever -[CompositingIOSurfaceLayer setNeedsDisplay] is called.
53 void SetNeedsDisplay();
55 // Called whenever -[CompositingIOSurfaceLayer canDrawInCGLContext] is called,
56 // to determine if a new frame should be drawn.
59 // Called whenever -[CompositingIOSurfaceLayer drawInCGLContext] draws a
61 void DidDraw(bool success
);
63 // Immediately re-draw the layer, even if the content has not changed, and
64 // ensure that the frame be acked.
65 void SetNeedsDisplayAndDisplayAndAck();
67 // Immediately draw the layer, only if one is pending, and ensure that the
69 void DisplayIfNeededAndAck();
71 // Mark a bracket in which new frames are being pumped in a restricted nested
72 // run loop. During this time frames are acked immediately and draws are
73 // deferred until the bracket ends.
74 void BeginPumpingFrames();
75 void EndPumpingFrames();
78 // Called whenever the frame provided in GotNewFrame should be acknowledged
79 // (this may be because it was drawn, or it may be to unblock the
81 void AckPendingFrame(bool success
);
85 // The client that the owning layer was created with.
86 content::CompositingIOSurfaceLayerClient
* const client_
;
88 // The layer that owns this helper.
89 CompositingIOSurfaceLayer
* const layer_
;
91 // Used to track when canDrawInCGLContext should return YES. This can be
92 // in response to receiving a new compositor frame, or from any of the events
93 // that cause setNeedsDisplay to be called on the layer.
96 // This is set when a frame is received, and un-set when the frame is drawn.
97 bool has_pending_frame_
;
99 // Incremented every time that this layer is asked to draw but does not have
100 // new content to draw.
101 uint64 did_not_draw_counter_
;
103 // Set when inside a BeginPumpingFrames/EndPumpingFrames block.
104 bool is_pumping_frames_
;
106 // The browser places back-pressure on the GPU by not acknowledging swap
107 // calls until they appear on the screen. This can lead to hangs if the
108 // view is moved offscreen (among other things). Prevent hangs by always
109 // acknowledging the frame after timeout of 1/6th of a second has passed.
110 base::DelayTimer
<CompositingIOSurfaceLayerHelper
> timer_
;
113 } // namespace content
115 // The CoreAnimation layer for drawing accelerated content.
116 @interface CompositingIOSurfaceLayer
: CAOpenGLLayer
{
118 scoped_refptr
<content::CompositingIOSurfaceMac
> iosurface_
;
119 scoped_refptr
<content::CompositingIOSurfaceContext
> context_
;
121 scoped_ptr
<content::CompositingIOSurfaceLayerHelper
> helper_
;
124 - (content::CompositingIOSurfaceMac
*)iosurface
;
125 - (content::CompositingIOSurfaceContext
*)context
;
127 - (id
)initWithIOSurface
:(scoped_refptr
<content::CompositingIOSurfaceMac
>)
129 withScaleFactor
:(float)scale_factor
130 withClient
:(content::CompositingIOSurfaceLayerClient
*)client
;
132 // Mark that the client is no longer valid and cannot be called back into. This
133 // must be called before the layer is destroyed.
136 // Called when a new frame is received.
139 // Force a draw immediately (even if this means re-displaying a previously
141 - (void)setNeedsDisplayAndDisplayAndAck
;
143 // Force a draw immediately, but only if one was requested.
144 - (void)displayIfNeededAndAck
;
146 // Mark a bracket in which new frames are being pumped in a restricted nested
148 - (void)beginPumpingFrames
;
149 - (void)endPumpingFrames
;
152 #endif // CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_LAYER_MAC_H_