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 UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_LAYER_H_
6 #define UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_LAYER_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"
13 #include "ui/gfx/geometry/size.h"
15 @
class IOSurfaceLayer
;
19 class IOSurfaceTexture
;
20 class IOSurfaceContext
;
22 // The interface through which the IOSurfaceLayer calls back into
23 // the structrue that created it (RenderWidgetHostViewMac or
24 // BrowserCompositorViewMac).
25 class IOSurfaceLayerClient
{
27 // Used to indicate that the layer should attempt to draw immediately and
28 // should (even if the draw is elided by the system), ack the frame
30 virtual bool IOSurfaceLayerShouldAckImmediately() const = 0;
32 // Called when a frame is drawn or when, because the layer is not visible, it
33 // is known that the frame will never drawn.
34 virtual void IOSurfaceLayerDidDrawFrame() = 0;
36 // Called when an error prevents the frame from being drawn.
37 virtual void IOSurfaceLayerHitError() = 0;
40 // IOSurfaceLayerHelper provides C++ functionality needed for the
41 // IOSurfaceLayer class, and does most of the heavy lifting for the
43 // TODO(ccameron): This class should own IOSurfaceLayer, rather than
45 class IOSurfaceLayerHelper
{
47 IOSurfaceLayerHelper(IOSurfaceLayerClient
* client
,
48 IOSurfaceLayer
* layer
);
49 ~IOSurfaceLayerHelper();
51 // Called when the IOSurfaceLayer gets a new frame.
54 // Called whenever -[IOSurfaceLayer setNeedsDisplay] is called.
55 void SetNeedsDisplay();
57 // Called whenever -[IOSurfaceLayer canDrawInCGLContext] is called,
58 // to determine if a new frame should be drawn.
61 // Called whenever -[IOSurfaceLayer drawInCGLContext] draws a
63 void DidDraw(bool success
);
65 // Immediately re-draw the layer, even if the content has not changed, and
66 // ensure that the frame be acked.
67 void SetNeedsDisplayAndDisplayAndAck();
69 // Immediately draw the layer, only if one is pending, and ensure that the
71 void DisplayIfNeededAndAck();
73 // Mark a bracket in which new frames are being pumped in a restricted nested
74 // run loop. During this time frames are acked immediately and draws are
75 // deferred until the bracket ends.
76 void BeginPumpingFrames();
77 void EndPumpingFrames();
80 // Called whenever the frame provided in GotNewFrame should be acknowledged
81 // (this may be because it was drawn, or it may be to unblock the
83 void AckPendingFrame(bool success
);
87 // The client that the owning layer was created with.
88 IOSurfaceLayerClient
* const client_
;
90 // The layer that owns this helper.
91 IOSurfaceLayer
* const layer_
;
93 // Used to track when canDrawInCGLContext should return YES. This can be
94 // in response to receiving a new compositor frame, or from any of the events
95 // that cause setNeedsDisplay to be called on the layer.
98 // This is set when a frame is received, and un-set when the frame is drawn.
99 bool has_pending_frame_
;
101 // Incremented every time that this layer is asked to draw but does not have
102 // new content to draw.
103 uint64 did_not_draw_counter_
;
105 // Set when inside a BeginPumpingFrames/EndPumpingFrames block.
106 bool is_pumping_frames_
;
108 // The browser places back-pressure on the GPU by not acknowledging swap
109 // calls until they appear on the screen. This can lead to hangs if the
110 // view is moved offscreen (among other things). Prevent hangs by always
111 // acknowledging the frame after timeout of 1/6th of a second has passed.
112 base::DelayTimer
<IOSurfaceLayerHelper
> timer_
;
117 // The CoreAnimation layer for drawing accelerated content.
118 @interface IOSurfaceLayer
: CAOpenGLLayer
{
120 scoped_refptr
<ui::IOSurfaceTexture
> iosurface_
;
121 scoped_refptr
<ui::IOSurfaceContext
> context_
;
123 scoped_ptr
<ui::IOSurfaceLayerHelper
> helper_
;
126 - (id
)initWithClient
:(ui::IOSurfaceLayerClient
*)client
127 withScaleFactor
:(float)scale_factor
128 needsGLFinishWorkaround
:(bool)needs_gl_finish_workaround
;
130 - (bool)gotFrameWithIOSurface
:(IOSurfaceID
)io_surface_id
131 withPixelSize
:(gfx::Size
)pixel_size
132 withScaleFactor
:(float)scale_factor
;
134 // Context poison accessors.
135 - (void)poisonContextAndSharegroup
;
136 - (bool)hasBeenPoisoned
;
138 - (float)scaleFactor
;
140 // The CGL renderer ID.
143 // Mark that the client is no longer valid and cannot be called back into. This
144 // must be called before the layer is destroyed.
147 // Called when a new frame is received.
150 // Force a draw immediately (even if this means re-displaying a previously
152 - (void)setNeedsDisplayAndDisplayAndAck
;
154 // Force a draw immediately, but only if one was requested.
155 - (void)displayIfNeededAndAck
;
157 // Mark a bracket in which new frames are being pumped in a restricted nested
159 - (void)beginPumpingFrames
;
160 - (void)endPumpingFrames
;
163 #endif // UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_LAYER_H_