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

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: removed init, re-arranged constructor 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/GraphicsLayer.h" 35 #include "core/platform/graphics/GraphicsLayer.h"
36 #include "core/platform/graphics/chromium/Canvas2DLayerManager.h" 36 #include "core/platform/graphics/chromium/Canvas2DLayerManager.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
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 TRACE_EVENT_INSTANT0("test_gpu", "Canvas2DLayerBridge::Create");
66 SkAutoTUnref<SkSurface> surface(createSurface(context.get(), size));
67 if (!surface.get())
68 return PassOwnPtr<Canvas2DLayerBridge>();
69 SkDeferredCanvas* canvas = new SkDeferredCanvas(surface);
70 OwnPtr<Canvas2DLayerBridge> layerBridge = adoptPtr(new Canvas2DLayerBridge(c ontext, canvas, opacityMode, threading));
71 return layerBridge.release();
72 }
73
47 Canvas2DLayerBridge::Canvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, SkDeferredCanvas* canvas, OpacityMode opacityMode, ThreadMode threadMode) 74 Canvas2DLayerBridge::Canvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, SkDeferredCanvas* canvas, OpacityMode opacityMode, ThreadMode threadMode)
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 {
59 ASSERT(m_canvas); 86 ASSERT(m_canvas);
60 // Used by browser tests to detect the use of a Canvas2DLayerBridge. 87 // Used by browser tests to detect the use of a Canvas2DLayerBridge.
61 TRACE_EVENT_INSTANT0("test_gpu", "Canvas2DLayerBridgeCreation");
62 m_canvas->setNotificationClient(this);
63 #if ENABLE(CANVAS_USES_MAILBOX) 88 #if ENABLE(CANVAS_USES_MAILBOX)
64 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE xternalTextureLayerForMailbox(this)); 89 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE xternalTextureLayerForMailbox(this));
65 #else 90 #else
66 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE xternalTextureLayer(this)); 91 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE xternalTextureLayer(this));
67 m_layer->setRateLimitContext(threadMode == SingleThread); 92 m_layer->setRateLimitContext(threadMode == SingleThread);
68 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g etDevice()->accessRenderTarget()); 93 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g etDevice()->accessRenderTarget());
69 if (renderTarget) { 94 if (renderTarget) {
70 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle()); 95 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle());
71 } 96 }
72 #endif 97 #endif
73 m_layer->setOpaque(opacityMode == Opaque); 98 m_layer->setOpaque(opacityMode == Opaque);
74 GraphicsLayer::registerContentsLayer(m_layer->layer()); 99 GraphicsLayer::registerContentsLayer(m_layer->layer());
100 m_canvas->setNotificationClient(this);
75 } 101 }
76 102
77 Canvas2DLayerBridge::~Canvas2DLayerBridge() 103 Canvas2DLayerBridge::~Canvas2DLayerBridge()
78 { 104 {
105 ASSERT(m_layer);
79 GraphicsLayer::unregisterContentsLayer(m_layer->layer()); 106 GraphicsLayer::unregisterContentsLayer(m_layer->layer());
80 Canvas2DLayerManager::get().layerToBeDestroyed(this); 107 Canvas2DLayerManager::get().layerToBeDestroyed(this);
81 m_canvas->setNotificationClient(0); 108 m_canvas->setNotificationClient(0);
82 #if ENABLE(CANVAS_USES_MAILBOX) 109 #if ENABLE(CANVAS_USES_MAILBOX)
83 m_mailboxes.clear(); 110 m_mailboxes.clear();
84 #endif 111 #endif
85 m_layer->clearTexture(); 112 m_layer->clearTexture();
86 } 113 }
87 114
88 void Canvas2DLayerBridge::limitPendingFrames() 115 void Canvas2DLayerBridge::limitPendingFrames()
89 { 116 {
90 if (m_didRecordDrawCommand) { 117 if (m_didRecordDrawCommand) {
91 m_framesPending++; 118 m_framesPending++;
92 m_didRecordDrawCommand = false; 119 m_didRecordDrawCommand = false;
93 if (m_framesPending > 1) 120 if (m_framesPending > 1)
94 flush(); 121 flush();
95 } 122 }
96 } 123 }
97 124
98 void Canvas2DLayerBridge::prepareForDraw() 125 void Canvas2DLayerBridge::prepareForDraw()
99 { 126 {
127 ASSERT(m_layer);
128 if (!isValid()) {
129 if (m_canvas) {
130 // drop pending commands because there is no surface to draw to
131 m_canvas->silentFlush();
132 }
133 return;
134 }
100 #if !ENABLE(CANVAS_USES_MAILBOX) 135 #if !ENABLE(CANVAS_USES_MAILBOX)
101 m_layer->willModifyTexture(); 136 m_layer->willModifyTexture();
102 #endif 137 #endif
103 m_context->makeContextCurrent(); 138 m_context->makeContextCurrent();
104 } 139 }
105 140
106 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAlloca ted) 141 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAlloca ted)
107 { 142 {
108 intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated; 143 intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated;
109 m_bytesAllocated = bytesAllocated; 144 m_bytesAllocated = bytesAllocated;
(...skipping 26 matching lines...) Expand all
136 } 171 }
137 172
138 void Canvas2DLayerBridge::flush() 173 void Canvas2DLayerBridge::flush()
139 { 174 {
140 if (m_canvas->hasPendingCommands()) 175 if (m_canvas->hasPendingCommands())
141 m_canvas->flush(); 176 m_canvas->flush();
142 } 177 }
143 178
144 unsigned Canvas2DLayerBridge::prepareTexture(WebTextureUpdater& updater) 179 unsigned Canvas2DLayerBridge::prepareTexture(WebTextureUpdater& updater)
145 { 180 {
181 if (!isValid())
182 return 0;
146 #if ENABLE(CANVAS_USES_MAILBOX) 183 #if ENABLE(CANVAS_USES_MAILBOX)
147 ASSERT_NOT_REACHED(); 184 ASSERT_NOT_REACHED();
148 return 0; 185 return 0;
149 #else 186 #else
150 m_context->makeContextCurrent(); 187 m_context->makeContextCurrent();
151 188
152 TRACE_EVENT0("cc", "Canvas2DLayerBridge::SkCanvas::flush"); 189 TRACE_EVENT0("cc", "Canvas2DLayerBridge::SkCanvas::flush");
153 m_canvas->flush(); 190 m_canvas->flush();
154 m_context->flush(); 191 m_context->flush();
155 192
156 // Notify skia that the state of the backing store texture object will be to uched by the compositor 193 // 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()); 194 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g etDevice()->accessRenderTarget());
158 if (renderTarget) { 195 if (renderTarget) {
159 GrTexture* texture = renderTarget->asTexture(); 196 GrTexture* texture = renderTarget->asTexture();
160 texture->invalidateCachedState(); 197 texture->invalidateCachedState();
161 return texture->getTextureHandle(); 198 return texture->getTextureHandle();
162 } 199 }
163 return 0; 200 return 0;
164 #endif // !ENABLE(CANVAS_USES_MAILBOX) 201 #endif // !ENABLE(CANVAS_USES_MAILBOX)
165 } 202 }
166 203
167 WebGraphicsContext3D* Canvas2DLayerBridge::context() 204 WebGraphicsContext3D* Canvas2DLayerBridge::context()
168 { 205 {
206 ASSERT(m_context.get());
169 return m_context->webContext(); 207 return m_context->webContext();
170 } 208 }
171 209
210 PassRefPtr<GraphicsContext3D> Canvas2DLayerBridge::getSharedContext() const
211 {
212 return SharedGraphicsContext3D::get();
213 }
214
215 bool Canvas2DLayerBridge::isValid()
216 {
217 ASSERT(m_layer);
218 RefPtr<GraphicsContext3D> sharedContext = getSharedContext();
219 if (!sharedContext || sharedContext->webContext()->isContextLost()) {
220 // Context was lost since last call to isValid and
221 // SharedGraphicsContext3D has not recovered.
222 m_context.clear();
223 m_layer->clearTexture();
224 return false;
225 }
226 if (m_context != sharedContext) {
227 ASSERT(sharedContext.get());
228 // Old context was lost and sharedContext has recovered.
229 // Use the new context to recover the layer
230 m_context = sharedContext;
231 m_layer->clearTexture();
232 IntSize size(m_canvas->getTopDevice()->width(), m_canvas->getTopDevice() ->height());
233 SkAutoTUnref<SkSurface> surface(createSurface(m_context.get(), size));
234 if (surface.get()) {
235 m_canvas->setSurface(surface.get());
236 // FIXME: draw sad canvas picture into new buffer crbug.com/243842
237 } else {
238 // Surface allocation failed. Reset m_context to trigger subsequent retry
239 m_context.clear();
240 return false;
241 }
242 #if !ENABLE(CANVAS_USES_MAILBOX)
243 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canva s->getDevice()->accessRenderTarget());
244 if (renderTarget)
245 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle()) ;
246 #endif
247 }
248 return true;
249 }
250
172 bool Canvas2DLayerBridge::prepareMailbox(WebKit::WebExternalTextureMailbox* outM ailbox) 251 bool Canvas2DLayerBridge::prepareMailbox(WebKit::WebExternalTextureMailbox* outM ailbox)
173 { 252 {
174 #if ENABLE(CANVAS_USES_MAILBOX) 253 #if ENABLE(CANVAS_USES_MAILBOX)
254 if (!m_canvas.get() || !isValid())
255 return false;
175 // Release to skia textures that were previouosly released by the 256 // Release to skia textures that were previouosly released by the
176 // compositor. We do this before acquiring the next snapshot in 257 // compositor. We do this before acquiring the next snapshot in
177 // order to cap maximum gpu memory consumption. 258 // order to cap maximum gpu memory consumption.
178 m_context->makeContextCurrent(); 259 m_context->makeContextCurrent();
179 Vector<MailboxInfo>::iterator mailboxInfo; 260 Vector<MailboxInfo>::iterator mailboxInfo;
180 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai lboxInfo++) { 261 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai lboxInfo++) {
181 if (mailboxInfo->m_status == MailboxReleased) { 262 if (mailboxInfo->m_status == MailboxReleased) {
182 if (mailboxInfo->m_mailbox.syncPoint) { 263 if (mailboxInfo->m_mailbox.syncPoint) {
183 context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint); 264 context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint);
184 mailboxInfo->m_mailbox.syncPoint = 0; 265 mailboxInfo->m_mailbox.syncPoint = 0;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 mailboxInfo->m_status = MailboxReleased; 338 mailboxInfo->m_status = MailboxReleased;
258 return; 339 return;
259 } 340 }
260 } 341 }
261 #endif 342 #endif
262 ASSERT_NOT_REACHED(); 343 ASSERT_NOT_REACHED();
263 } 344 }
264 345
265 WebKit::WebLayer* Canvas2DLayerBridge::layer() 346 WebKit::WebLayer* Canvas2DLayerBridge::layer()
266 { 347 {
348 ASSERT(m_layer);
267 return m_layer->layer(); 349 return m_layer->layer();
268 } 350 }
269 351
270 void Canvas2DLayerBridge::contextAcquired() 352 void Canvas2DLayerBridge::contextAcquired()
271 { 353 {
272 Canvas2DLayerManager::get().layerDidDraw(this); 354 Canvas2DLayerManager::get().layerDidDraw(this);
273 m_didRecordDrawCommand = true; 355 m_didRecordDrawCommand = true;
274 } 356 }
275 357
276 unsigned Canvas2DLayerBridge::backBufferTexture() 358 unsigned Canvas2DLayerBridge::backBufferTexture()
(...skipping 13 matching lines...) Expand all
290 // This copy constructor should only be used for Vector reallocation 372 // This copy constructor should only be used for Vector reallocation
291 // Assuming 'other' is to be destroyed, we swap m_image ownership 373 // Assuming 'other' is to be destroyed, we swap m_image ownership
292 // rather than do a refcount dance. 374 // rather than do a refcount dance.
293 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox)); 375 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox));
294 m_image.swap(const_cast<SkAutoTUnref<SkImage>*>(&other.m_image)); 376 m_image.swap(const_cast<SkAutoTUnref<SkImage>*>(&other.m_image));
295 m_status = other.m_status; 377 m_status = other.m_status;
296 } 378 }
297 #endif 379 #endif
298 380
299 } 381 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698