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

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: Response to senorblanco feedback Created 7 years, 7 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
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 0;
68 SkDeferredCanvas* canvas = new SkDeferredCanvas(surface);
69 return adoptPtr(new Canvas2DLayerBridge(context, canvas, opacityMode, thread ing));
70 }
71
72 PassOwnPtr<Canvas2DLayerBridge> Canvas2DLayerBridge::create(PassRefPtr<GraphicsC ontext3D> context, SkDeferredCanvas* canvas, OpacityMode opacityMode, ThreadMode threading)
73 {
74 // Factory overload used for unit testing
Stephen White 2013/05/28 14:23:10 Nit: this comment should probably be in the header
75 return adoptPtr(new Canvas2DLayerBridge(context, canvas, opacityMode, thread ing));
76 }
77
47 Canvas2DLayerBridge::Canvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, SkDeferredCanvas* canvas, OpacityMode opacityMode, ThreadMode threadMode) 78 Canvas2DLayerBridge::Canvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, SkDeferredCanvas* canvas, OpacityMode opacityMode, ThreadMode threadMode)
48 : m_canvas(canvas) 79 : m_canvas(canvas)
49 , m_context(context) 80 , m_context(context)
50 , m_bytesAllocated(0) 81 , m_bytesAllocated(0)
51 , m_didRecordDrawCommand(false) 82 , m_didRecordDrawCommand(false)
52 , m_framesPending(0) 83 , m_framesPending(0)
53 , m_next(0) 84 , m_next(0)
54 , m_prev(0) 85 , m_prev(0)
55 #if ENABLE(CANVAS_USES_MAILBOX) 86 #if ENABLE(CANVAS_USES_MAILBOX)
56 , m_lastImageId(0) 87 , m_lastImageId(0)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 if (m_didRecordDrawCommand) { 121 if (m_didRecordDrawCommand) {
91 m_framesPending++; 122 m_framesPending++;
92 m_didRecordDrawCommand = false; 123 m_didRecordDrawCommand = false;
93 if (m_framesPending > 1) 124 if (m_framesPending > 1)
94 flush(); 125 flush();
95 } 126 }
96 } 127 }
97 128
98 void Canvas2DLayerBridge::prepareForDraw() 129 void Canvas2DLayerBridge::prepareForDraw()
99 { 130 {
131 if (!isValid()) {
132 if (m_canvas) {
133 // drop pending commands because there is no surface to draw to
134 m_canvas->silentFlush();
135 }
136 return;
137 }
100 #if !ENABLE(CANVAS_USES_MAILBOX) 138 #if !ENABLE(CANVAS_USES_MAILBOX)
101 m_layer->willModifyTexture(); 139 m_layer->willModifyTexture();
102 #endif 140 #endif
103 m_context->makeContextCurrent(); 141 m_context->makeContextCurrent();
104 } 142 }
105 143
106 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAlloca ted) 144 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAlloca ted)
107 { 145 {
108 intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated; 146 intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated;
109 m_bytesAllocated = bytesAllocated; 147 m_bytesAllocated = bytesAllocated;
(...skipping 26 matching lines...) Expand all
136 } 174 }
137 175
138 void Canvas2DLayerBridge::flush() 176 void Canvas2DLayerBridge::flush()
139 { 177 {
140 if (m_canvas->hasPendingCommands()) 178 if (m_canvas->hasPendingCommands())
141 m_canvas->flush(); 179 m_canvas->flush();
142 } 180 }
143 181
144 unsigned Canvas2DLayerBridge::prepareTexture(WebTextureUpdater& updater) 182 unsigned Canvas2DLayerBridge::prepareTexture(WebTextureUpdater& updater)
145 { 183 {
184 if (!isValid())
185 return 0;
146 #if ENABLE(CANVAS_USES_MAILBOX) 186 #if ENABLE(CANVAS_USES_MAILBOX)
147 ASSERT_NOT_REACHED(); 187 ASSERT_NOT_REACHED();
148 return 0; 188 return 0;
149 #else 189 #else
150 m_context->makeContextCurrent(); 190 m_context->makeContextCurrent();
151 191
152 TRACE_EVENT0("cc", "Canvas2DLayerBridge::SkCanvas::flush"); 192 TRACE_EVENT0("cc", "Canvas2DLayerBridge::SkCanvas::flush");
153 m_canvas->flush(); 193 m_canvas->flush();
154 m_context->flush(); 194 m_context->flush();
155 195
156 // Notify skia that the state of the backing store texture object will be to uched by the compositor 196 // 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()); 197 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g etDevice()->accessRenderTarget());
158 if (renderTarget) { 198 if (renderTarget) {
159 GrTexture* texture = renderTarget->asTexture(); 199 GrTexture* texture = renderTarget->asTexture();
160 texture->invalidateCachedState(); 200 texture->invalidateCachedState();
161 return texture->getTextureHandle(); 201 return texture->getTextureHandle();
162 } 202 }
163 return 0; 203 return 0;
164 #endif // !ENABLE(CANVAS_USES_MAILBOX) 204 #endif // !ENABLE(CANVAS_USES_MAILBOX)
165 } 205 }
166 206
167 WebGraphicsContext3D* Canvas2DLayerBridge::context() 207 WebGraphicsContext3D* Canvas2DLayerBridge::context()
168 { 208 {
209 ASSERT(m_context.get());
169 return m_context->webContext(); 210 return m_context->webContext();
170 } 211 }
171 212
213 bool Canvas2DLayerBridge::isValid()
214 {
215 RefPtr<GraphicsContext3D> sharedContext = SharedGraphicsContext3D::get();
216 if (!sharedContext || sharedContext->webContext()->isContextLost()) {
217 // Context was lost since last call to isValid and
218 // SharedGraphicsContext3D has not recovered.
219 m_context.clear();
220 m_layer->clearTexture();
221 return false;
222 }
223 if (m_context != sharedContext) {
224 ASSERT(sharedContext.get());
225 // Old context was lost and sharedContext has recovered.
226 // Use the new context to recover the layer
227 m_context = sharedContext;
228 m_layer->clearTexture();
229 IntSize size(m_canvas->getTopDevice()->width(), m_canvas->getTopDevice() ->height());
230 SkAutoTUnref<SkSurface> surface(createSurface(m_context.get(), size));
231 if (surface.get()) {
232 m_canvas->setSurface(surface.get());
233 // FIXME: draw sad canvas picture into new buffer crbug.com/243842
234 } else {
235 // Surface allocation failed. Reset m_context to trigger subsequent retry
236 m_context.clear();
237 return false;
238 }
239 #if !ENABLE(CANVAS_USES_MAILBOX)
240 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canva s->getDevice()->accessRenderTarget());
241 if (renderTarget)
242 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle()) ;
Stephen White 2013/05/28 14:23:10 Not new to this patch, but I see this code in four
243 #endif
244 }
245 return true;
246 }
247
172 bool Canvas2DLayerBridge::prepareMailbox(WebKit::WebExternalTextureMailbox* outM ailbox) 248 bool Canvas2DLayerBridge::prepareMailbox(WebKit::WebExternalTextureMailbox* outM ailbox)
173 { 249 {
174 #if ENABLE(CANVAS_USES_MAILBOX) 250 #if ENABLE(CANVAS_USES_MAILBOX)
251 if (!m_canvas.get() || !isValid())
252 return false;
175 // Release to skia textures that were previouosly released by the 253 // Release to skia textures that were previouosly released by the
176 // compositor. We do this before acquiring the next snapshot in 254 // compositor. We do this before acquiring the next snapshot in
177 // order to cap maximum gpu memory consumption. 255 // order to cap maximum gpu memory consumption.
178 m_context->makeContextCurrent(); 256 m_context->makeContextCurrent();
179 Vector<MailboxInfo>::iterator mailboxInfo; 257 Vector<MailboxInfo>::iterator mailboxInfo;
180 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai lboxInfo++) { 258 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai lboxInfo++) {
181 if (mailboxInfo->m_status == MailboxReleased) { 259 if (mailboxInfo->m_status == MailboxReleased) {
182 if (mailboxInfo->m_mailbox.syncPoint) { 260 if (mailboxInfo->m_mailbox.syncPoint) {
183 context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint); 261 context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint);
184 mailboxInfo->m_mailbox.syncPoint = 0; 262 mailboxInfo->m_mailbox.syncPoint = 0;
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 // This copy constructor should only be used for Vector reallocation 368 // This copy constructor should only be used for Vector reallocation
291 // Assuming 'other' is to be destroyed, we swap m_image ownership 369 // Assuming 'other' is to be destroyed, we swap m_image ownership
292 // rather than do a refcount dance. 370 // rather than do a refcount dance.
293 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox)); 371 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox));
294 m_image.swap(const_cast<SkAutoTUnref<SkImage>*>(&other.m_image)); 372 m_image.swap(const_cast<SkAutoTUnref<SkImage>*>(&other.m_image));
295 m_status = other.m_status; 373 m_status = other.m_status;
296 } 374 }
297 #endif 375 #endif
298 376
299 } 377 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698