OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/compositor/io_surface_layer_mac.h" | 5 #include "content/browser/compositor/io_surface_layer_mac.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #include <OpenGL/CGLIOSurface.h> | 8 #include <OpenGL/gl.h> |
9 #include <OpenGL/CGLRenderers.h> | |
10 #include <OpenGL/OpenGL.h> | |
11 | 9 |
12 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
13 #include "base/mac/sdk_forward_declarations.h" | 11 #include "base/mac/sdk_forward_declarations.h" |
14 #include "content/browser/renderer_host/render_widget_host_impl.h" | 12 #include "content/browser/renderer_host/render_widget_host_impl.h" |
15 #include "content/browser/renderer_host/render_widget_host_view_mac.h" | 13 #include "content/browser/renderer_host/render_widget_host_view_mac.h" |
| 14 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h" |
| 15 #include "content/browser/renderer_host/compositing_iosurface_mac.h" |
16 #include "ui/base/cocoa/animation_utils.h" | 16 #include "ui/base/cocoa/animation_utils.h" |
17 #include "ui/gfx/size_conversions.h" | 17 #include "ui/gfx/size_conversions.h" |
18 #include "ui/gl/scoped_cgl.h" | |
19 #include "ui/gl/gpu_switching_manager.h" | 18 #include "ui/gl/gpu_switching_manager.h" |
20 | 19 |
21 // Convenience macro for checking errors in the below code. | |
22 #define CHECK_GL_ERROR() do { \ | |
23 GLenum gl_error = glGetError(); \ | |
24 LOG_IF(ERROR, gl_error != GL_NO_ERROR) << "GL Error: " << gl_error; \ | |
25 } while (0) | |
26 | |
27 //////////////////////////////////////////////////////////////////////////////// | |
28 // IOSurfaceLayer(Private) | |
29 | |
30 @interface IOSurfaceLayer(Private) | |
31 // Force a draw immediately, but only if one was requested. | |
32 - (void)displayIfNeededAndAck; | |
33 | |
34 // Called when it has been a fixed interval of time and a frame has yet to be | |
35 // drawn. | |
36 - (void)timerFired; | |
37 @end | |
38 | |
39 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
40 // IOSurfaceLayerHelper | 21 // IOSurfaceLayerHelper |
41 | 22 |
42 namespace content { | 23 namespace content { |
43 | 24 |
44 // IOSurfaceLayerHelper provides C++ functionality needed for the | |
45 // IOSurfaceLayer class (interfacing with base::DelayTimer). | |
46 class IOSurfaceLayerHelper { | |
47 public: | |
48 IOSurfaceLayerHelper(IOSurfaceLayer* layer); | |
49 ~IOSurfaceLayerHelper(); | |
50 void ResetTimer(); | |
51 | |
52 private: | |
53 void TimerFired(); | |
54 | |
55 // The layer that owns this helper. | |
56 IOSurfaceLayer* const layer_; | |
57 | |
58 // The browser places back-pressure on the GPU by not acknowledging swap | |
59 // calls until they appear on the screen. This can lead to hangs if the | |
60 // view is moved offscreen (among other things). Prevent hangs by always | |
61 // acknowledging the frame after timeout of 1/6th of a second has passed. | |
62 base::DelayTimer<IOSurfaceLayerHelper> timer_; | |
63 }; | |
64 | |
65 | |
66 IOSurfaceLayerHelper::IOSurfaceLayerHelper( | 25 IOSurfaceLayerHelper::IOSurfaceLayerHelper( |
| 26 IOSurfaceLayerClient* client, |
67 IOSurfaceLayer* layer) | 27 IOSurfaceLayer* layer) |
68 : layer_(layer), | 28 : client_(client), |
| 29 layer_(layer), |
| 30 needs_display_(false), |
| 31 has_pending_frame_(false), |
| 32 did_not_draw_counter_(0), |
| 33 is_pumping_frames_(false), |
69 timer_( | 34 timer_( |
70 FROM_HERE, | 35 FROM_HERE, |
71 base::TimeDelta::FromSeconds(1) / 6, | 36 base::TimeDelta::FromSeconds(1) / 6, |
72 this, | 37 this, |
73 &IOSurfaceLayerHelper::TimerFired) {} | 38 &IOSurfaceLayerHelper::TimerFired) {} |
74 | 39 |
75 IOSurfaceLayerHelper::~IOSurfaceLayerHelper() { | 40 IOSurfaceLayerHelper::~IOSurfaceLayerHelper() { |
| 41 // Any acks that were waiting on this layer to draw will not occur, so ack |
| 42 // them now to prevent blocking the renderer. |
| 43 AckPendingFrame(true); |
76 } | 44 } |
77 | 45 |
78 void IOSurfaceLayerHelper::ResetTimer() { | 46 void IOSurfaceLayerHelper::GotNewFrame() { |
79 timer_.Reset(); | |
80 } | |
81 | |
82 void IOSurfaceLayerHelper::TimerFired() { | |
83 [layer_ timerFired]; | |
84 } | |
85 | |
86 } // namespace content | |
87 | |
88 //////////////////////////////////////////////////////////////////////////////// | |
89 // IOSurfaceLayer | |
90 | |
91 @implementation IOSurfaceLayer | |
92 | |
93 - (id)initWithClient:(content::IOSurfaceLayerClient*)client | |
94 withScaleFactor:(float)scale_factor { | |
95 if (self = [super init]) { | |
96 client_ = client; | |
97 helper_.reset(new content::IOSurfaceLayerHelper(self)); | |
98 needs_display_ = false; | |
99 has_pending_frame_ = false; | |
100 did_not_draw_counter_ = 0; | |
101 is_pumping_frames_ = false; | |
102 io_surface_texture_ = 0; | |
103 io_surface_texture_dirty_ = false; | |
104 cgl_renderer_id_ = 0; | |
105 | |
106 [self setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)]; | |
107 [self setAnchorPoint:CGPointMake(0, 0)]; | |
108 // Setting contents gravity is necessary to prevent the layer from being | |
109 // scaled during dyanmic resizes (especially with devtools open). | |
110 [self setContentsGravity:kCAGravityTopLeft]; | |
111 if ([self respondsToSelector:(@selector(setContentsScale:))]) | |
112 [self setContentsScale:scale_factor]; | |
113 } | |
114 return self; | |
115 } | |
116 | |
117 - (void)dealloc { | |
118 DCHECK(!helper_ && !client_); | |
119 [super dealloc]; | |
120 } | |
121 | |
122 - (float)scaleFactor { | |
123 if ([self respondsToSelector:(@selector(contentsScale))]) | |
124 return [self contentsScale]; | |
125 return 1; | |
126 } | |
127 | |
128 - (int)rendererID { | |
129 return cgl_renderer_id_; | |
130 } | |
131 | |
132 - (void)timerFired { | |
133 [self displayIfNeededAndAck]; | |
134 } | |
135 | |
136 - (void)resetClient { | |
137 // Any acks that were waiting on this layer to draw will not occur, so ack | |
138 // them now to prevent blocking the renderer. | |
139 [self ackPendingFrame]; | |
140 helper_.reset(); | |
141 client_ = NULL; | |
142 } | |
143 | |
144 - (void)gotFrameWithIOSurface:(IOSurfaceID)io_surface_id | |
145 withPixelSize:(gfx::Size)pixel_size | |
146 withScaleFactor:(float)scale_factor { | |
147 // A trace value of 2 indicates that there is a pending swap ack. See | 47 // A trace value of 2 indicates that there is a pending swap ack. See |
148 // canDrawInCGLContext for other value meanings. | 48 // canDrawInCGLContext for other value meanings. |
149 TRACE_COUNTER_ID1("browser", "PendingSwapAck", self, 2); | 49 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 2); |
| 50 |
150 has_pending_frame_ = true; | 51 has_pending_frame_ = true; |
151 needs_display_ = true; | 52 needs_display_ = true; |
152 helper_->ResetTimer(); | 53 timer_.Reset(); |
153 | |
154 frame_pixel_size_ = pixel_size; | |
155 | |
156 // If this is a new IOSurface, open the IOSurface and mark that the | |
157 // GL texture needs to bind to the new surface. | |
158 if (!io_surface_ || io_surface_id != IOSurfaceGetID(io_surface_)) { | |
159 io_surface_.reset(IOSurfaceLookup(io_surface_id)); | |
160 io_surface_texture_dirty_ = true; | |
161 if (!io_surface_) { | |
162 LOG(ERROR) << "Failed to open IOSurface for frame"; | |
163 if (client_) | |
164 client_->IOSurfaceLayerHitError(); | |
165 } | |
166 } | |
167 | 54 |
168 // If reqested, draw immediately and don't bother trying to use the | 55 // If reqested, draw immediately and don't bother trying to use the |
169 // isAsynchronous property to ensure smooth animation. If this is while | 56 // isAsynchronous property to ensure smooth animation. If this is while |
170 // frames are being pumped then ack and display immediately to get a | 57 // frames are being pumped then ack and display immediately to get a |
171 // correct-sized frame displayed as soon as possible. | 58 // correct-sized frame displayed as soon as possible. |
172 if (is_pumping_frames_ || | 59 if (is_pumping_frames_ || client_->IOSurfaceLayerShouldAckImmediately()) { |
173 (client_ && client_->IOSurfaceLayerShouldAckImmediately())) { | 60 SetNeedsDisplayAndDisplayAndAck(); |
174 [self setNeedsDisplayAndDisplayAndAck]; | |
175 } else { | 61 } else { |
176 if (![self isAsynchronous]) | 62 if (![layer_ isAsynchronous]) |
177 [self setAsynchronous:YES]; | 63 [layer_ setAsynchronous:YES]; |
178 } | 64 } |
179 } | 65 } |
180 | 66 |
181 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext | 67 void IOSurfaceLayerHelper::SetNeedsDisplay() { |
182 pixelFormat:(CGLPixelFormatObj)pixelFormat | 68 needs_display_ = true; |
183 forLayerTime:(CFTimeInterval)timeInterval | 69 } |
184 displayTime:(const CVTimeStamp*)timeStamp { | 70 |
| 71 bool IOSurfaceLayerHelper::CanDraw() { |
185 // If we return NO 30 times in a row, switch to being synchronous to avoid | 72 // If we return NO 30 times in a row, switch to being synchronous to avoid |
186 // burning CPU cycles on this callback. | 73 // burning CPU cycles on this callback. |
187 if (needs_display_) { | 74 if (needs_display_) { |
188 did_not_draw_counter_ = 0; | 75 did_not_draw_counter_ = 0; |
189 } else { | 76 } else { |
190 did_not_draw_counter_ += 1; | 77 did_not_draw_counter_ += 1; |
191 if (did_not_draw_counter_ == 30) | 78 if (did_not_draw_counter_ == 30) |
192 [self setAsynchronous:NO]; | 79 [layer_ setAsynchronous:NO]; |
193 } | 80 } |
194 | 81 |
195 // Add an instantaneous blip to the PendingSwapAck state to indicate | 82 // Add an instantaneous blip to the PendingSwapAck state to indicate |
196 // that CoreAnimation asked if a frame is ready. A blip up to to 3 (usually | 83 // that CoreAnimation asked if a frame is ready. A blip up to to 3 (usually |
197 // from 2, indicating that a swap ack is pending) indicates that we | 84 // from 2, indicating that a swap ack is pending) indicates that we |
198 // requested a draw. A blip up to 1 (usually from 0, indicating there is no | 85 // requested a draw. A blip up to 1 (usually from 0, indicating there is no |
199 // pending swap ack) indicates that we did not request a draw. This would | 86 // pending swap ack) indicates that we did not request a draw. This would |
200 // be more natural to do with a tracing pseudo-thread | 87 // be more natural to do with a tracing pseudo-thread |
201 // http://crbug.com/366300 | 88 // http://crbug.com/366300 |
202 TRACE_COUNTER_ID1("browser", "PendingSwapAck", self, needs_display_ ? 3 : 1); | 89 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, needs_display_ ? 3 : 1); |
203 TRACE_COUNTER_ID1("browser", "PendingSwapAck", self, | 90 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, |
204 has_pending_frame_ ? 2 : 0); | 91 has_pending_frame_ ? 2 : 0); |
205 | 92 |
206 return needs_display_; | 93 return needs_display_; |
207 } | 94 } |
208 | 95 |
209 - (void)ackPendingFrame { | 96 void IOSurfaceLayerHelper::DidDraw(bool success) { |
| 97 needs_display_ = false; |
| 98 AckPendingFrame(success); |
| 99 } |
| 100 |
| 101 void IOSurfaceLayerHelper::AckPendingFrame(bool success) { |
210 if (!has_pending_frame_) | 102 if (!has_pending_frame_) |
211 return; | 103 return; |
212 has_pending_frame_ = false; | 104 has_pending_frame_ = false; |
213 if (client_) | 105 if (success) |
214 client_->IOSurfaceLayerDidDrawFrame(); | 106 client_->IOSurfaceLayerDidDrawFrame(); |
| 107 else |
| 108 client_->IOSurfaceLayerHitError(); |
215 // A trace value of 0 indicates that there is no longer a pending swap ack. | 109 // A trace value of 0 indicates that there is no longer a pending swap ack. |
216 TRACE_COUNTER_ID1("browser", "PendingSwapAck", self, 0); | 110 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 0); |
217 } | 111 } |
218 | 112 |
219 - (void)setNeedsDisplayAndDisplayAndAck { | 113 void IOSurfaceLayerHelper::SetNeedsDisplayAndDisplayAndAck() { |
220 // Drawing using setNeedsDisplay and displayIfNeeded will result in | 114 // Drawing using setNeedsDisplay and displayIfNeeded will result in |
221 // subsequent canDrawInCGLContext callbacks getting dropped, and jerky | 115 // subsequent canDrawInCGLContext callbacks getting dropped, and jerky |
222 // animation. Disable asynchronous drawing before issuing these calls as a | 116 // animation. Disable asynchronous drawing before issuing these calls as a |
223 // workaround. | 117 // workaround. |
224 // http://crbug.com/395827 | 118 // http://crbug.com/395827 |
225 if ([self isAsynchronous]) | 119 if ([layer_ isAsynchronous]) |
226 [self setAsynchronous:NO]; | 120 [layer_ setAsynchronous:NO]; |
227 | 121 |
228 [self setNeedsDisplay]; | 122 [layer_ setNeedsDisplay]; |
229 [self displayIfNeededAndAck]; | 123 DisplayIfNeededAndAck(); |
230 } | 124 } |
231 | 125 |
232 - (void)displayIfNeededAndAck { | 126 void IOSurfaceLayerHelper::DisplayIfNeededAndAck() { |
233 if (!needs_display_) | 127 if (!needs_display_) |
234 return; | 128 return; |
235 | 129 |
236 // As in SetNeedsDisplayAndDisplayAndAck, disable asynchronous drawing before | 130 // As in SetNeedsDisplayAndDisplayAndAck, disable asynchronous drawing before |
237 // issuing displayIfNeeded. | 131 // issuing displayIfNeeded. |
238 // http://crbug.com/395827 | 132 // http://crbug.com/395827 |
239 if ([self isAsynchronous]) | 133 if ([layer_ isAsynchronous]) |
240 [self setAsynchronous:NO]; | 134 [layer_ setAsynchronous:NO]; |
241 | 135 |
242 // Do not bother drawing while pumping new frames -- wait until the waiting | 136 // Do not bother drawing while pumping new frames -- wait until the waiting |
243 // block ends to draw any of the new frames. | 137 // block ends to draw any of the new frames. |
244 if (!is_pumping_frames_) | 138 if (!is_pumping_frames_) |
245 [self displayIfNeeded]; | 139 [layer_ displayIfNeeded]; |
246 | 140 |
247 // Calls to setNeedsDisplay can sometimes be ignored, especially if issued | 141 // Calls to setNeedsDisplay can sometimes be ignored, especially if issued |
248 // rapidly (e.g, with vsync off). This is unacceptable because the failure | 142 // rapidly (e.g, with vsync off). This is unacceptable because the failure |
249 // to ack a single frame will hang the renderer. Ensure that the renderer | 143 // to ack a single frame will hang the renderer. Ensure that the renderer |
250 // not be blocked by lying and claiming that we drew the frame. | 144 // not be blocked by lying and claiming that we drew the frame. |
251 [self ackPendingFrame]; | 145 AckPendingFrame(true); |
| 146 } |
| 147 |
| 148 void IOSurfaceLayerHelper::TimerFired() { |
| 149 SetNeedsDisplayAndDisplayAndAck(); |
| 150 } |
| 151 |
| 152 void IOSurfaceLayerHelper::BeginPumpingFrames() { |
| 153 is_pumping_frames_ = true; |
| 154 } |
| 155 |
| 156 void IOSurfaceLayerHelper::EndPumpingFrames() { |
| 157 is_pumping_frames_ = false; |
| 158 DisplayIfNeededAndAck(); |
| 159 } |
| 160 |
| 161 } // namespace content |
| 162 |
| 163 //////////////////////////////////////////////////////////////////////////////// |
| 164 // IOSurfaceLayer |
| 165 |
| 166 @implementation IOSurfaceLayer |
| 167 |
| 168 - (content::CompositingIOSurfaceMac*)iosurface { |
| 169 return iosurface_.get(); |
| 170 } |
| 171 |
| 172 - (content::CompositingIOSurfaceContext*)context { |
| 173 return context_.get(); |
| 174 } |
| 175 |
| 176 - (id)initWithClient:(content::IOSurfaceLayerClient*)client |
| 177 withScaleFactor:(float)scale_factor { |
| 178 if (self = [super init]) { |
| 179 helper_.reset(new content::IOSurfaceLayerHelper(client, self)); |
| 180 |
| 181 iosurface_ = content::CompositingIOSurfaceMac::Create(); |
| 182 context_ = content::CompositingIOSurfaceContext::Get( |
| 183 content::CompositingIOSurfaceContext::kCALayerContextWindowNumber); |
| 184 if (!iosurface_ || !context_) { |
| 185 LOG(ERROR) << "Failed create CompositingIOSurface or context"; |
| 186 [self resetClient]; |
| 187 [self release]; |
| 188 return nil; |
| 189 } |
| 190 |
| 191 [self setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)]; |
| 192 [self setAnchorPoint:CGPointMake(0, 0)]; |
| 193 // Setting contents gravity is necessary to prevent the layer from being |
| 194 // scaled during dyanmic resizes (especially with devtools open). |
| 195 [self setContentsGravity:kCAGravityTopLeft]; |
| 196 if ([self respondsToSelector:(@selector(setContentsScale:))]) { |
| 197 [self setContentsScale:scale_factor]; |
| 198 } |
| 199 } |
| 200 return self; |
| 201 } |
| 202 |
| 203 - (void)dealloc { |
| 204 DCHECK(!helper_); |
| 205 [super dealloc]; |
| 206 } |
| 207 |
| 208 - (bool)gotFrameWithIOSurface:(IOSurfaceID)io_surface_id |
| 209 withPixelSize:(gfx::Size)pixel_size |
| 210 withScaleFactor:(float)scale_factor { |
| 211 bool result = true; |
| 212 gfx::ScopedCGLSetCurrentContext scoped_set_current_context( |
| 213 context_->cgl_context()); |
| 214 result = iosurface_->SetIOSurfaceWithContextCurrent( |
| 215 context_, io_surface_id, pixel_size, scale_factor); |
| 216 return result; |
| 217 } |
| 218 |
| 219 - (void)poisonContextAndSharegroup { |
| 220 context_->PoisonContextAndSharegroup(); |
| 221 } |
| 222 |
| 223 - (bool)hasBeenPoisoned { |
| 224 return context_->HasBeenPoisoned(); |
| 225 } |
| 226 |
| 227 - (float)scaleFactor { |
| 228 return iosurface_->scale_factor(); |
| 229 } |
| 230 |
| 231 - (int)rendererID { |
| 232 return iosurface_->GetRendererID(); |
| 233 } |
| 234 |
| 235 - (void)resetClient { |
| 236 helper_.reset(); |
| 237 } |
| 238 |
| 239 - (void)gotNewFrame { |
| 240 helper_->GotNewFrame(); |
| 241 } |
| 242 |
| 243 - (void)setNeedsDisplayAndDisplayAndAck { |
| 244 helper_->SetNeedsDisplayAndDisplayAndAck(); |
| 245 } |
| 246 |
| 247 - (void)displayIfNeededAndAck { |
| 248 helper_->DisplayIfNeededAndAck(); |
252 } | 249 } |
253 | 250 |
254 - (void)beginPumpingFrames { | 251 - (void)beginPumpingFrames { |
255 is_pumping_frames_ = true; | 252 helper_->BeginPumpingFrames(); |
256 } | 253 } |
257 | 254 |
258 - (void)endPumpingFrames { | 255 - (void)endPumpingFrames { |
259 is_pumping_frames_ = false; | 256 helper_->EndPumpingFrames(); |
260 [self displayIfNeededAndAck]; | |
261 } | 257 } |
262 | 258 |
263 // The remaining methods implement the CAOpenGLLayer interface. | 259 // The remaining methods implement the CAOpenGLLayer interface. |
264 | 260 |
265 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { | 261 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { |
266 // Create the pixel format object for the context. | 262 if (!context_) |
267 std::vector<CGLPixelFormatAttribute> attribs; | 263 return [super copyCGLPixelFormatForDisplayMask:mask]; |
268 attribs.push_back(kCGLPFADepthSize); | 264 return CGLRetainPixelFormat(CGLGetPixelFormat(context_->cgl_context())); |
269 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0)); | 265 } |
270 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) { | 266 |
271 attribs.push_back(kCGLPFAAllowOfflineRenderers); | 267 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { |
272 attribs.push_back(static_cast<CGLPixelFormatAttribute>(1)); | 268 if (!context_) |
273 } | 269 return [super copyCGLContextForPixelFormat:pixelFormat]; |
274 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0)); | 270 return CGLRetainContext(context_->cgl_context()); |
275 GLint number_virtual_screens = 0; | |
276 base::ScopedTypeRef<CGLPixelFormatObj> pixel_format; | |
277 CGLError error = CGLChoosePixelFormat( | |
278 &attribs.front(), pixel_format.InitializeInto(), &number_virtual_screens); | |
279 if (error != kCGLNoError) { | |
280 LOG(ERROR) << "Failed to create pixel format object."; | |
281 return NULL; | |
282 } | |
283 return CGLRetainPixelFormat(pixel_format); | |
284 } | |
285 | |
286 - (void)releaseCGLContext:(CGLContextObj)glContext { | |
287 // Destroy the GL resources used by this context. | |
288 if (io_surface_texture_) { | |
289 gfx::ScopedCGLSetCurrentContext scoped_set_current_context(glContext); | |
290 glDeleteTextures(1, &io_surface_texture_); | |
291 io_surface_texture_ = 0; | |
292 } | |
293 io_surface_texture_dirty_ = true; | |
294 cgl_renderer_id_ = 0; | |
295 [super releaseCGLContext:(CGLContextObj)glContext]; | |
296 } | 271 } |
297 | 272 |
298 - (void)setNeedsDisplay { | 273 - (void)setNeedsDisplay { |
299 needs_display_ = true; | 274 if (helper_) |
| 275 helper_->SetNeedsDisplay(); |
300 [super setNeedsDisplay]; | 276 [super setNeedsDisplay]; |
301 } | 277 } |
302 | 278 |
| 279 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext |
| 280 pixelFormat:(CGLPixelFormatObj)pixelFormat |
| 281 forLayerTime:(CFTimeInterval)timeInterval |
| 282 displayTime:(const CVTimeStamp*)timeStamp { |
| 283 if (helper_) |
| 284 return helper_->CanDraw(); |
| 285 return NO; |
| 286 } |
| 287 |
303 - (void)drawInCGLContext:(CGLContextObj)glContext | 288 - (void)drawInCGLContext:(CGLContextObj)glContext |
304 pixelFormat:(CGLPixelFormatObj)pixelFormat | 289 pixelFormat:(CGLPixelFormatObj)pixelFormat |
305 forLayerTime:(CFTimeInterval)timeInterval | 290 forLayerTime:(CFTimeInterval)timeInterval |
306 displayTime:(const CVTimeStamp*)timeStamp { | 291 displayTime:(const CVTimeStamp*)timeStamp { |
307 TRACE_EVENT0("browser", "IOSurfaceLayer::drawInCGLContext"); | 292 TRACE_EVENT0("browser", "IOSurfaceLayer::drawInCGLContext"); |
308 | 293 |
309 // Create the texture if it has not been created in this context yet. | 294 if (!iosurface_->HasIOSurface() || context_->cgl_context() != glContext) { |
310 if (!io_surface_texture_) { | 295 glClearColor(1, 1, 1, 1); |
311 glGenTextures(1, &io_surface_texture_); | 296 glClear(GL_COLOR_BUFFER_BIT); |
312 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, io_surface_texture_); | 297 return; |
313 glTexParameteri( | |
314 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
315 glTexParameteri( | |
316 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
317 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | |
318 io_surface_texture_dirty_ = true; | |
319 } | 298 } |
320 | 299 |
321 // Associate the IOSurface with this texture, if the underlying IOSurface has | 300 // The correct viewport to cover the layer will be set up by the caller. |
322 // been changed. | 301 // Transform this into a window size for DrawIOSurface, where it will be |
323 if (io_surface_texture_dirty_ && io_surface_) { | 302 // transformed back into this viewport. |
324 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, io_surface_texture_); | 303 GLint viewport[4]; |
325 CGLError cgl_error = CGLTexImageIOSurface2D( | 304 glGetIntegerv(GL_VIEWPORT, viewport); |
326 glContext, | 305 gfx::Rect window_rect(viewport[0], viewport[1], viewport[2], viewport[3]); |
327 GL_TEXTURE_RECTANGLE_ARB, | 306 float window_scale_factor = 1.f; |
328 GL_RGBA, | 307 if ([self respondsToSelector:(@selector(contentsScale))]) |
329 IOSurfaceGetWidth(io_surface_), | 308 window_scale_factor = [self contentsScale]; |
330 IOSurfaceGetHeight(io_surface_), | 309 window_rect = ToNearestRect( |
331 GL_BGRA, | 310 gfx::ScaleRect(window_rect, 1.f/window_scale_factor)); |
332 GL_UNSIGNED_INT_8_8_8_8_REV, | |
333 io_surface_.get(), | |
334 0 /* plane */); | |
335 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | |
336 if (cgl_error != kCGLNoError) { | |
337 LOG(ERROR) << "CGLTexImageIOSurface2D failed with " << cgl_error; | |
338 glDeleteTextures(1, &io_surface_texture_); | |
339 io_surface_texture_ = 0; | |
340 if (client_) | |
341 client_->IOSurfaceLayerHitError(); | |
342 } | |
343 } else if (io_surface_texture_) { | |
344 glDeleteTextures(1, &io_surface_texture_); | |
345 io_surface_texture_ = 0; | |
346 } | |
347 | 311 |
348 // Fill the viewport with the texture. The viewport must be smaller or equal | 312 bool draw_succeeded = iosurface_->DrawIOSurface( |
349 // than the texture, because it is resized as frames arrive. | 313 context_, window_rect, window_scale_factor); |
350 if (io_surface_texture_) { | |
351 GLint viewport[4]; | |
352 glGetIntegerv(GL_VIEWPORT, viewport); | |
353 gfx::Size viewport_pixel_size(viewport[2], viewport[3]); | |
354 DCHECK_LE( | |
355 viewport_pixel_size.width(), | |
356 frame_pixel_size_.width()); | |
357 DCHECK_LE( | |
358 viewport_pixel_size.height(), | |
359 frame_pixel_size_.height()); | |
360 | 314 |
361 glMatrixMode(GL_PROJECTION); | 315 if (helper_) |
362 glLoadIdentity(); | 316 helper_->DidDraw(draw_succeeded); |
363 glOrtho(0, viewport_pixel_size.width(), | |
364 0, viewport_pixel_size.height(), -1, 1); | |
365 glMatrixMode(GL_MODELVIEW); | |
366 glLoadIdentity(); | |
367 | 317 |
368 glColor4f(1, 1, 1, 1); | |
369 glEnable(GL_TEXTURE_RECTANGLE_ARB); | |
370 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, io_surface_texture_); | |
371 glBegin(GL_QUADS); | |
372 glTexCoord2f(0, 0); | |
373 glVertex2f(0, 0); | |
374 glTexCoord2f(viewport[2], 0); | |
375 glVertex2f(frame_pixel_size_.width(), 0); | |
376 glTexCoord2f(viewport[2], viewport[3]); | |
377 glVertex2f(frame_pixel_size_.width(), frame_pixel_size_.height()); | |
378 glTexCoord2f(0, viewport[3]); | |
379 glVertex2f(0, frame_pixel_size_.height()); | |
380 glEnd(); | |
381 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | |
382 glDisable(GL_TEXTURE_RECTANGLE_ARB); | |
383 | |
384 // Workaround for issue 158469. Issue a dummy draw call with | |
385 // io_surface_texture_ not bound to a texture, in order to shake all | |
386 // references to the IOSurface out of the driver. | |
387 glBegin(GL_TRIANGLES); | |
388 glEnd(); | |
389 } else { | |
390 glClearColor(1, 1, 1, 1); | |
391 glClear(GL_COLOR_BUFFER_BIT); | |
392 } | |
393 | |
394 // Query the current GL renderer to send back to the GPU process. | |
395 { | |
396 CGLError cgl_error = CGLGetParameter( | |
397 glContext, kCGLCPCurrentRendererID, &cgl_renderer_id_); | |
398 if (cgl_error == kCGLNoError) { | |
399 cgl_renderer_id_ &= kCGLRendererIDMatchingMask; | |
400 } else { | |
401 LOG(ERROR) << "CGLGetParameter for kCGLCPCurrentRendererID failed with " | |
402 << cgl_error; | |
403 cgl_renderer_id_ = 0; | |
404 } | |
405 } | |
406 | |
407 // If we hit any errors, tell the client. | |
408 while (GLenum gl_error = glGetError()) { | |
409 LOG(ERROR) << "Hit GL error " << gl_error; | |
410 if (client_) | |
411 client_->IOSurfaceLayerHitError(); | |
412 } | |
413 | |
414 needs_display_ = false; | |
415 [super drawInCGLContext:glContext | 318 [super drawInCGLContext:glContext |
416 pixelFormat:pixelFormat | 319 pixelFormat:pixelFormat |
417 forLayerTime:timeInterval | 320 forLayerTime:timeInterval |
418 displayTime:timeStamp]; | 321 displayTime:timeStamp]; |
419 | |
420 [self ackPendingFrame]; | |
421 } | 322 } |
422 | 323 |
423 @end | 324 @end |
OLD | NEW |