OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_LAYER_MAC_H_ | |
6 #define CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_LAYER_MAC_H_ | |
7 | |
8 #import <Cocoa/Cocoa.h> | |
9 | |
10 #include "base/mac/scoped_cftyperef.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/timer/timer.h" | |
13 | |
14 @class CompositingIOSurfaceLayer; | |
15 | |
16 namespace content { | |
17 class CompositingIOSurfaceMac; | |
18 class CompositingIOSurfaceContext; | |
19 | |
20 // The interface through which the CompositingIOSurfaceLayer calls back into | |
21 // the structrue that created it (RenderWidgetHostViewMac or | |
22 // BrowserCompositorViewMac). | |
23 class CompositingIOSurfaceLayerClient { | |
24 public: | |
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 | |
27 // immediately. | |
28 virtual bool AcceleratedLayerShouldAckImmediately() const = 0; | |
29 | |
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; | |
33 | |
34 // Called when an error prevents the frame from being drawn. | |
35 virtual void AcceleratedLayerHitError() = 0; | |
36 }; | |
37 | |
38 // CompositingIOSurfaceLayerHelper provides C++ functionality needed for the | |
39 // CompositingIOSurfaceLayer class, and does most of the heavy lifting for the | |
40 // class. | |
41 // TODO(ccameron): This class should own CompositingIOSurfaceLayer, rather than | |
42 // vice versa. | |
43 class CompositingIOSurfaceLayerHelper { | |
44 public: | |
45 CompositingIOSurfaceLayerHelper(CompositingIOSurfaceLayerClient* client, | |
46 CompositingIOSurfaceLayer* layer); | |
47 ~CompositingIOSurfaceLayerHelper(); | |
48 | |
49 // Called when the CompositingIOSurfaceLayer gets a new frame. | |
50 void GotNewFrame(); | |
51 | |
52 // Called whenever -[CompositingIOSurfaceLayer setNeedsDisplay] is called. | |
53 void SetNeedsDisplay(); | |
54 | |
55 // Called whenever -[CompositingIOSurfaceLayer canDrawInCGLContext] is called, | |
56 // to determine if a new frame should be drawn. | |
57 bool CanDraw(); | |
58 | |
59 // Called whenever -[CompositingIOSurfaceLayer drawInCGLContext] draws a | |
60 // frame. | |
61 void DidDraw(bool success); | |
62 | |
63 // Immediately re-draw the layer, even if the content has not changed, and | |
64 // ensure that the frame be acked. | |
65 void SetNeedsDisplayAndDisplayAndAck(); | |
66 | |
67 // Immediately draw the layer, only if one is pending, and ensure that the | |
68 // frame be acked. | |
69 void DisplayIfNeededAndAck(); | |
70 | |
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(); | |
76 | |
77 private: | |
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 | |
80 // compositor). | |
81 void AckPendingFrame(bool success); | |
82 | |
83 void TimerFired(); | |
84 | |
85 // The client that the owning layer was created with. | |
86 content::CompositingIOSurfaceLayerClient* const client_; | |
87 | |
88 // The layer that owns this helper. | |
89 CompositingIOSurfaceLayer* const layer_; | |
90 | |
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. | |
94 bool needs_display_; | |
95 | |
96 // This is set when a frame is received, and un-set when the frame is drawn. | |
97 bool has_pending_frame_; | |
98 | |
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_; | |
102 | |
103 // Set when inside a BeginPumpingFrames/EndPumpingFrames block. | |
104 bool is_pumping_frames_; | |
105 | |
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_; | |
111 }; | |
112 | |
113 } // namespace content | |
114 | |
115 // The CoreAnimation layer for drawing accelerated content. | |
116 @interface CompositingIOSurfaceLayer : CAOpenGLLayer { | |
117 @private | |
118 scoped_refptr<content::CompositingIOSurfaceMac> iosurface_; | |
119 scoped_refptr<content::CompositingIOSurfaceContext> context_; | |
120 | |
121 scoped_ptr<content::CompositingIOSurfaceLayerHelper> helper_; | |
122 } | |
123 | |
124 - (content::CompositingIOSurfaceMac*)iosurface; | |
125 - (content::CompositingIOSurfaceContext*)context; | |
126 | |
127 - (id)initWithIOSurface:(scoped_refptr<content::CompositingIOSurfaceMac>) | |
128 iosurface | |
129 withScaleFactor:(float)scale_factor | |
130 withClient:(content::CompositingIOSurfaceLayerClient*)client; | |
131 | |
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. | |
134 - (void)resetClient; | |
135 | |
136 // Called when a new frame is received. | |
137 - (void)gotNewFrame; | |
138 | |
139 // Force a draw immediately (even if this means re-displaying a previously | |
140 // displayed frame). | |
141 - (void)setNeedsDisplayAndDisplayAndAck; | |
142 | |
143 // Force a draw immediately, but only if one was requested. | |
144 - (void)displayIfNeededAndAck; | |
145 | |
146 // Mark a bracket in which new frames are being pumped in a restricted nested | |
147 // run loop. | |
148 - (void)beginPumpingFrames; | |
149 - (void)endPumpingFrames; | |
150 @end | |
151 | |
152 #endif // CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_LAYER_MAC_H_ | |
OLD | NEW |