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