Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / accelerated_widget_mac / io_surface_layer.h
blobba48ff9d2698408a3827b7edcc30ee4746a86868
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;
17 namespace ui {
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 {
26 public:
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
29 // immediately.
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
42 // class.
43 // TODO(ccameron): This class should own IOSurfaceLayer, rather than
44 // vice versa.
45 class IOSurfaceLayerHelper {
46 public:
47 IOSurfaceLayerHelper(IOSurfaceLayerClient* client,
48 IOSurfaceLayer* layer);
49 ~IOSurfaceLayerHelper();
51 // Called when the IOSurfaceLayer gets a new frame.
52 void GotNewFrame();
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.
59 bool CanDraw();
61 // Called whenever -[IOSurfaceLayer drawInCGLContext] draws a
62 // frame.
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
70 // frame be acked.
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();
79 private:
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
82 // compositor).
83 void AckPendingFrame(bool success);
85 void TimerFired();
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.
96 bool needs_display_;
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_;
115 } // namespace ui
117 // The CoreAnimation layer for drawing accelerated content.
118 @interface IOSurfaceLayer : CAOpenGLLayer {
119 @private
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.
141 - (int)rendererID;
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.
145 - (void)resetClient;
147 // Called when a new frame is received.
148 - (void)gotNewFrame;
150 // Force a draw immediately (even if this means re-displaying a previously
151 // displayed frame).
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
158 // run loop.
159 - (void)beginPumpingFrames;
160 - (void)endPumpingFrames;
161 @end
163 #endif // UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_LAYER_H_