Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(430)

Side by Side Diff: Source/core/platform/graphics/chromium/Canvas2DLayerBridge.cpp

Issue 16032003: Fixing Canvas2DLayerBridge to handle lost graphics contexts (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Added assertions on m_layer to verify that init was called Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 16 matching lines...) Expand all
27 27
28 #include "core/platform/graphics/chromium/Canvas2DLayerBridge.h" 28 #include "core/platform/graphics/chromium/Canvas2DLayerBridge.h"
29 29
30 #include "GrContext.h" 30 #include "GrContext.h"
31 #include "SkDevice.h" 31 #include "SkDevice.h"
32 #include "SkSurface.h" 32 #include "SkSurface.h"
33 #include "core/platform/chromium/TraceEvent.h" 33 #include "core/platform/chromium/TraceEvent.h"
34 #include "core/platform/graphics/GraphicsContext3D.h" 34 #include "core/platform/graphics/GraphicsContext3D.h"
35 #include "core/platform/graphics/chromium/Canvas2DLayerManager.h" 35 #include "core/platform/graphics/chromium/Canvas2DLayerManager.h"
36 #include "core/platform/graphics/chromium/GraphicsLayerChromium.h" 36 #include "core/platform/graphics/chromium/GraphicsLayerChromium.h"
37 #include "core/platform/graphics/gpu/SharedGraphicsContext3D.h"
37 #include <public/Platform.h> 38 #include <public/Platform.h>
38 #include <public/WebCompositorSupport.h> 39 #include <public/WebCompositorSupport.h>
39 #include <public/WebGraphicsContext3D.h> 40 #include <public/WebGraphicsContext3D.h>
40 41
41 using WebKit::WebExternalTextureLayer; 42 using WebKit::WebExternalTextureLayer;
42 using WebKit::WebGraphicsContext3D; 43 using WebKit::WebGraphicsContext3D;
43 using WebKit::WebTextureUpdater; 44 using WebKit::WebTextureUpdater;
44 45
45 namespace WebCore { 46 namespace WebCore {
46 47
47 Canvas2DLayerBridge::Canvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, SkDeferredCanvas* canvas, OpacityMode opacityMode, ThreadMode threadMode) 48 static SkSurface* createSurface(GraphicsContext3D* context3D, const IntSize& siz e)
49 {
50 ASSERT(!context3D->webContext()->isContextLost());
51 GrContext* gr = context3D->grContext();
52 if (!gr)
53 return 0;
54 gr->resetContext();
55 SkImage::Info info;
56 info.fWidth = size.width();
57 info.fHeight = size.height();
58 info.fColorType = SkImage::kPMColor_ColorType;
59 info.fAlphaType = SkImage::kPremul_AlphaType;
60 return SkSurface::NewRenderTarget(gr, info);
61 }
62
63 PassOwnPtr<Canvas2DLayerBridge> Canvas2DLayerBridge::create(PassRefPtr<GraphicsC ontext3D> context, const IntSize& size, OpacityMode opacityMode, ThreadMode thre ading)
64 {
65 SkAutoTUnref<SkSurface> surface(createSurface(context.get(), size));
66 if (!surface.get())
67 return PassOwnPtr<Canvas2DLayerBridge>();
68 SkDeferredCanvas* canvas = new SkDeferredCanvas(surface);
69 OwnPtr<Canvas2DLayerBridge> layerBridge = adoptPtr(new Canvas2DLayerBridge(c ontext, canvas));
70 layerBridge->init(opacityMode, threading);
71 return layerBridge.release();
72 }
73
74 Canvas2DLayerBridge::Canvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, SkDeferredCanvas* canvas)
48 : m_canvas(canvas) 75 : m_canvas(canvas)
49 , m_context(context) 76 , m_context(context)
50 , m_bytesAllocated(0) 77 , m_bytesAllocated(0)
51 , m_didRecordDrawCommand(false) 78 , m_didRecordDrawCommand(false)
52 , m_framesPending(0) 79 , m_framesPending(0)
53 , m_next(0) 80 , m_next(0)
54 , m_prev(0) 81 , m_prev(0)
55 #if ENABLE(CANVAS_USES_MAILBOX) 82 #if ENABLE(CANVAS_USES_MAILBOX)
56 , m_lastImageId(0) 83 , m_lastImageId(0)
57 #endif 84 #endif
58 { 85 {
86 }
87
88 Canvas2DLayerBridge::~Canvas2DLayerBridge()
89 {
90 ASSERT(m_layer);
91 GraphicsLayerChromium::unregisterContentsLayer(m_layer->layer());
92 Canvas2DLayerManager::get().layerToBeDestroyed(this);
93 m_canvas->setNotificationClient(0);
94 #if ENABLE(CANVAS_USES_MAILBOX)
95 m_mailboxes.clear();
96 #endif
97 m_layer->clearTexture();
98 }
99
100 void Canvas2DLayerBridge::init(OpacityMode opacityMode, ThreadMode threadMode)
101 {
59 ASSERT(m_canvas); 102 ASSERT(m_canvas);
60 // Used by browser tests to detect the use of a Canvas2DLayerBridge. 103 // Used by browser tests to detect the use of a Canvas2DLayerBridge.
61 TRACE_EVENT_INSTANT0("test_gpu", "Canvas2DLayerBridgeCreation"); 104 TRACE_EVENT_INSTANT0("test_gpu", "Canvas2DLayerBridge::init()");
62 m_canvas->setNotificationClient(this); 105 m_canvas->setNotificationClient(this);
Stephen White 2013/05/31 15:06:18 As discussed, let's make one last attempt to get r
63 #if ENABLE(CANVAS_USES_MAILBOX) 106 #if ENABLE(CANVAS_USES_MAILBOX)
64 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE xternalTextureLayerForMailbox(this)); 107 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE xternalTextureLayerForMailbox(this));
65 #else 108 #else
66 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE xternalTextureLayer(this)); 109 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE xternalTextureLayer(this));
67 m_layer->setRateLimitContext(threadMode == SingleThread); 110 m_layer->setRateLimitContext(threadMode == SingleThread);
68 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g etDevice()->accessRenderTarget()); 111 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g etDevice()->accessRenderTarget());
69 if (renderTarget) { 112 if (renderTarget) {
70 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle()); 113 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle());
71 } 114 }
72 #endif 115 #endif
73 m_layer->setOpaque(opacityMode == Opaque); 116 m_layer->setOpaque(opacityMode == Opaque);
74 GraphicsLayerChromium::registerContentsLayer(m_layer->layer()); 117 GraphicsLayerChromium::registerContentsLayer(m_layer->layer());
75 } 118 }
76 119
77 Canvas2DLayerBridge::~Canvas2DLayerBridge()
78 {
79 GraphicsLayerChromium::unregisterContentsLayer(m_layer->layer());
80 Canvas2DLayerManager::get().layerToBeDestroyed(this);
81 m_canvas->setNotificationClient(0);
82 #if ENABLE(CANVAS_USES_MAILBOX)
83 m_mailboxes.clear();
84 #endif
85 m_layer->clearTexture();
86 }
87
88 void Canvas2DLayerBridge::limitPendingFrames() 120 void Canvas2DLayerBridge::limitPendingFrames()
89 { 121 {
90 if (m_didRecordDrawCommand) { 122 if (m_didRecordDrawCommand) {
91 m_framesPending++; 123 m_framesPending++;
92 m_didRecordDrawCommand = false; 124 m_didRecordDrawCommand = false;
93 if (m_framesPending > 1) 125 if (m_framesPending > 1)
94 flush(); 126 flush();
95 } 127 }
96 } 128 }
97 129
98 void Canvas2DLayerBridge::prepareForDraw() 130 void Canvas2DLayerBridge::prepareForDraw()
99 { 131 {
132 ASSERT(m_layer);
133 if (!isValid()) {
134 if (m_canvas) {
135 // drop pending commands because there is no surface to draw to
136 m_canvas->silentFlush();
137 }
138 return;
139 }
100 #if !ENABLE(CANVAS_USES_MAILBOX) 140 #if !ENABLE(CANVAS_USES_MAILBOX)
101 m_layer->willModifyTexture(); 141 m_layer->willModifyTexture();
102 #endif 142 #endif
103 m_context->makeContextCurrent(); 143 m_context->makeContextCurrent();
104 } 144 }
105 145
106 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAlloca ted) 146 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAlloca ted)
107 { 147 {
108 intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated; 148 intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated;
109 m_bytesAllocated = bytesAllocated; 149 m_bytesAllocated = bytesAllocated;
(...skipping 26 matching lines...) Expand all
136 } 176 }
137 177
138 void Canvas2DLayerBridge::flush() 178 void Canvas2DLayerBridge::flush()
139 { 179 {
140 if (m_canvas->hasPendingCommands()) 180 if (m_canvas->hasPendingCommands())
141 m_canvas->flush(); 181 m_canvas->flush();
142 } 182 }
143 183
144 unsigned Canvas2DLayerBridge::prepareTexture(WebTextureUpdater& updater) 184 unsigned Canvas2DLayerBridge::prepareTexture(WebTextureUpdater& updater)
145 { 185 {
186 if (!isValid())
187 return 0;
146 #if ENABLE(CANVAS_USES_MAILBOX) 188 #if ENABLE(CANVAS_USES_MAILBOX)
147 ASSERT_NOT_REACHED(); 189 ASSERT_NOT_REACHED();
148 return 0; 190 return 0;
149 #else 191 #else
150 m_context->makeContextCurrent(); 192 m_context->makeContextCurrent();
151 193
152 TRACE_EVENT0("cc", "Canvas2DLayerBridge::SkCanvas::flush"); 194 TRACE_EVENT0("cc", "Canvas2DLayerBridge::SkCanvas::flush");
153 m_canvas->flush(); 195 m_canvas->flush();
154 m_context->flush(); 196 m_context->flush();
155 197
156 // Notify skia that the state of the backing store texture object will be to uched by the compositor 198 // Notify skia that the state of the backing store texture object will be to uched by the compositor
157 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g etDevice()->accessRenderTarget()); 199 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g etDevice()->accessRenderTarget());
158 if (renderTarget) { 200 if (renderTarget) {
159 GrTexture* texture = renderTarget->asTexture(); 201 GrTexture* texture = renderTarget->asTexture();
160 texture->invalidateCachedState(); 202 texture->invalidateCachedState();
161 return texture->getTextureHandle(); 203 return texture->getTextureHandle();
162 } 204 }
163 return 0; 205 return 0;
164 #endif // !ENABLE(CANVAS_USES_MAILBOX) 206 #endif // !ENABLE(CANVAS_USES_MAILBOX)
165 } 207 }
166 208
167 WebGraphicsContext3D* Canvas2DLayerBridge::context() 209 WebGraphicsContext3D* Canvas2DLayerBridge::context()
168 { 210 {
211 ASSERT(m_context.get());
169 return m_context->webContext(); 212 return m_context->webContext();
170 } 213 }
171 214
215 PassRefPtr<GraphicsContext3D> Canvas2DLayerBridge::getSharedContext() const
216 {
217 return SharedGraphicsContext3D::get();
218 }
219
220 bool Canvas2DLayerBridge::isValid()
221 {
222 ASSERT(m_layer);
223 RefPtr<GraphicsContext3D> sharedContext = getSharedContext();
224 if (!sharedContext || sharedContext->webContext()->isContextLost()) {
225 // Context was lost since last call to isValid and
226 // SharedGraphicsContext3D has not recovered.
227 m_context.clear();
228 m_layer->clearTexture();
229 return false;
230 }
231 if (m_context != sharedContext) {
232 ASSERT(sharedContext.get());
233 // Old context was lost and sharedContext has recovered.
234 // Use the new context to recover the layer
235 m_context = sharedContext;
236 m_layer->clearTexture();
237 IntSize size(m_canvas->getTopDevice()->width(), m_canvas->getTopDevice() ->height());
238 SkAutoTUnref<SkSurface> surface(createSurface(m_context.get(), size));
239 if (surface.get()) {
240 m_canvas->setSurface(surface.get());
241 // FIXME: draw sad canvas picture into new buffer crbug.com/243842
242 } else {
243 // Surface allocation failed. Reset m_context to trigger subsequent retry
244 m_context.clear();
245 return false;
246 }
247 #if !ENABLE(CANVAS_USES_MAILBOX)
248 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canva s->getDevice()->accessRenderTarget());
249 if (renderTarget)
250 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle()) ;
251 #endif
252 }
253 return true;
254 }
255
172 bool Canvas2DLayerBridge::prepareMailbox(WebKit::WebExternalTextureMailbox* outM ailbox) 256 bool Canvas2DLayerBridge::prepareMailbox(WebKit::WebExternalTextureMailbox* outM ailbox)
173 { 257 {
174 #if ENABLE(CANVAS_USES_MAILBOX) 258 #if ENABLE(CANVAS_USES_MAILBOX)
259 if (!m_canvas.get() || !isValid())
260 return false;
175 // Release to skia textures that were previouosly released by the 261 // Release to skia textures that were previouosly released by the
176 // compositor. We do this before acquiring the next snapshot in 262 // compositor. We do this before acquiring the next snapshot in
177 // order to cap maximum gpu memory consumption. 263 // order to cap maximum gpu memory consumption.
178 m_context->makeContextCurrent(); 264 m_context->makeContextCurrent();
179 Vector<MailboxInfo>::iterator mailboxInfo; 265 Vector<MailboxInfo>::iterator mailboxInfo;
180 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai lboxInfo++) { 266 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai lboxInfo++) {
181 if (mailboxInfo->m_status == MailboxReleased) { 267 if (mailboxInfo->m_status == MailboxReleased) {
182 if (mailboxInfo->m_mailbox.syncPoint) { 268 if (mailboxInfo->m_mailbox.syncPoint) {
183 context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint); 269 context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint);
184 mailboxInfo->m_mailbox.syncPoint = 0; 270 mailboxInfo->m_mailbox.syncPoint = 0;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 mailboxInfo->m_status = MailboxReleased; 343 mailboxInfo->m_status = MailboxReleased;
258 return; 344 return;
259 } 345 }
260 } 346 }
261 #endif 347 #endif
262 ASSERT_NOT_REACHED(); 348 ASSERT_NOT_REACHED();
263 } 349 }
264 350
265 WebKit::WebLayer* Canvas2DLayerBridge::layer() 351 WebKit::WebLayer* Canvas2DLayerBridge::layer()
266 { 352 {
353 ASSERT(m_layer);
267 return m_layer->layer(); 354 return m_layer->layer();
268 } 355 }
269 356
270 void Canvas2DLayerBridge::contextAcquired() 357 void Canvas2DLayerBridge::contextAcquired()
271 { 358 {
272 Canvas2DLayerManager::get().layerDidDraw(this); 359 Canvas2DLayerManager::get().layerDidDraw(this);
273 m_didRecordDrawCommand = true; 360 m_didRecordDrawCommand = true;
274 } 361 }
275 362
276 unsigned Canvas2DLayerBridge::backBufferTexture() 363 unsigned Canvas2DLayerBridge::backBufferTexture()
(...skipping 13 matching lines...) Expand all
290 // This copy constructor should only be used for Vector reallocation 377 // This copy constructor should only be used for Vector reallocation
291 // Assuming 'other' is to be destroyed, we swap m_image ownership 378 // Assuming 'other' is to be destroyed, we swap m_image ownership
292 // rather than do a refcount dance. 379 // rather than do a refcount dance.
293 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox)); 380 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox));
294 m_image.swap(const_cast<SkAutoTUnref<SkImage>*>(&other.m_image)); 381 m_image.swap(const_cast<SkAutoTUnref<SkImage>*>(&other.m_image));
295 m_status = other.m_status; 382 m_status = other.m_status;
296 } 383 }
297 #endif 384 #endif
298 385
299 } 386 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698