| OLD | NEW |
| 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 Loading... |
| 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/Platform.h" | 38 #include "public/platform/Platform.h" |
| 38 #include "public/platform/WebCompositorSupport.h" | 39 #include "public/platform/WebCompositorSupport.h" |
| 39 #include "public/platform/WebGraphicsContext3D.h" | 40 #include "public/platform/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", "Canvas2DLayerBridgeCreation"); |
| 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) |
| 79 , m_surfaceIsValid(true) |
| 52 , m_framesPending(0) | 80 , m_framesPending(0) |
| 53 , m_next(0) | 81 , m_next(0) |
| 54 , m_prev(0) | 82 , m_prev(0) |
| 55 #if ENABLE(CANVAS_USES_MAILBOX) | 83 #if ENABLE(CANVAS_USES_MAILBOX) |
| 56 , m_lastImageId(0) | 84 , m_lastImageId(0) |
| 57 #endif | 85 #endif |
| 58 { | 86 { |
| 59 ASSERT(m_canvas); | 87 ASSERT(m_canvas); |
| 60 // Used by browser tests to detect the use of a Canvas2DLayerBridge. | 88 // 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) | 89 #if ENABLE(CANVAS_USES_MAILBOX) |
| 64 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE
xternalTextureLayerForMailbox(this)); | 90 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE
xternalTextureLayerForMailbox(this)); |
| 65 #else | 91 #else |
| 66 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE
xternalTextureLayer(this)); | 92 m_layer = adoptPtr(WebKit::Platform::current()->compositorSupport()->createE
xternalTextureLayer(this)); |
| 67 m_layer->setRateLimitContext(threadMode == SingleThread); | 93 m_layer->setRateLimitContext(threadMode == SingleThread); |
| 68 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g
etDevice()->accessRenderTarget()); | 94 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g
etDevice()->accessRenderTarget()); |
| 69 if (renderTarget) { | 95 if (renderTarget) { |
| 70 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle()); | 96 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle()); |
| 71 } | 97 } |
| 72 #endif | 98 #endif |
| 73 m_layer->setOpaque(opacityMode == Opaque); | 99 m_layer->setOpaque(opacityMode == Opaque); |
| 74 GraphicsLayer::registerContentsLayer(m_layer->layer()); | 100 GraphicsLayer::registerContentsLayer(m_layer->layer()); |
| 101 m_canvas->setNotificationClient(this); |
| 75 } | 102 } |
| 76 | 103 |
| 77 Canvas2DLayerBridge::~Canvas2DLayerBridge() | 104 Canvas2DLayerBridge::~Canvas2DLayerBridge() |
| 78 { | 105 { |
| 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 m_layer->clearTexture(); | |
| 83 m_layer.clear(); | |
| 84 #if ENABLE(CANVAS_USES_MAILBOX) | 109 #if ENABLE(CANVAS_USES_MAILBOX) |
| 85 m_mailboxes.clear(); | 110 m_mailboxes.clear(); |
| 86 #endif | 111 #endif |
| 112 m_layer->clearTexture(); |
| 87 } | 113 } |
| 88 | 114 |
| 89 void Canvas2DLayerBridge::limitPendingFrames() | 115 void Canvas2DLayerBridge::limitPendingFrames() |
| 90 { | 116 { |
| 91 if (m_didRecordDrawCommand) { | 117 if (m_didRecordDrawCommand) { |
| 92 m_framesPending++; | 118 m_framesPending++; |
| 93 m_didRecordDrawCommand = false; | 119 m_didRecordDrawCommand = false; |
| 94 if (m_framesPending > 1) | 120 if (m_framesPending > 1) |
| 95 flush(); | 121 flush(); |
| 96 } | 122 } |
| 97 } | 123 } |
| 98 | 124 |
| 99 void Canvas2DLayerBridge::prepareForDraw() | 125 void Canvas2DLayerBridge::prepareForDraw() |
| 100 { | 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 } |
| 101 #if !ENABLE(CANVAS_USES_MAILBOX) | 135 #if !ENABLE(CANVAS_USES_MAILBOX) |
| 102 m_layer->willModifyTexture(); | 136 m_layer->willModifyTexture(); |
| 103 #endif | 137 #endif |
| 104 m_context->makeContextCurrent(); | 138 m_context->makeContextCurrent(); |
| 105 } | 139 } |
| 106 | 140 |
| 107 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAlloca
ted) | 141 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAlloca
ted) |
| 108 { | 142 { |
| 109 intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated; | 143 intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated; |
| 110 m_bytesAllocated = bytesAllocated; | 144 m_bytesAllocated = bytesAllocated; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 137 } | 171 } |
| 138 | 172 |
| 139 void Canvas2DLayerBridge::flush() | 173 void Canvas2DLayerBridge::flush() |
| 140 { | 174 { |
| 141 if (m_canvas->hasPendingCommands()) | 175 if (m_canvas->hasPendingCommands()) |
| 142 m_canvas->flush(); | 176 m_canvas->flush(); |
| 143 } | 177 } |
| 144 | 178 |
| 145 unsigned Canvas2DLayerBridge::prepareTexture(WebTextureUpdater& updater) | 179 unsigned Canvas2DLayerBridge::prepareTexture(WebTextureUpdater& updater) |
| 146 { | 180 { |
| 181 if (!isValid()) |
| 182 return 0; |
| 147 #if ENABLE(CANVAS_USES_MAILBOX) | 183 #if ENABLE(CANVAS_USES_MAILBOX) |
| 148 ASSERT_NOT_REACHED(); | 184 ASSERT_NOT_REACHED(); |
| 149 return 0; | 185 return 0; |
| 150 #else | 186 #else |
| 151 m_context->makeContextCurrent(); | 187 m_context->makeContextCurrent(); |
| 152 | 188 |
| 153 TRACE_EVENT0("cc", "Canvas2DLayerBridge::SkCanvas::flush"); | 189 TRACE_EVENT0("cc", "Canvas2DLayerBridge::SkCanvas::flush"); |
| 154 m_canvas->flush(); | 190 m_canvas->flush(); |
| 155 m_context->flush(); | 191 m_context->flush(); |
| 156 | 192 |
| 157 // 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 |
| 158 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g
etDevice()->accessRenderTarget()); | 194 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g
etDevice()->accessRenderTarget()); |
| 159 if (renderTarget) { | 195 if (renderTarget) { |
| 160 GrTexture* texture = renderTarget->asTexture(); | 196 GrTexture* texture = renderTarget->asTexture(); |
| 161 texture->invalidateCachedState(); | 197 texture->invalidateCachedState(); |
| 162 return texture->getTextureHandle(); | 198 return texture->getTextureHandle(); |
| 163 } | 199 } |
| 164 return 0; | 200 return 0; |
| 165 #endif // !ENABLE(CANVAS_USES_MAILBOX) | 201 #endif // !ENABLE(CANVAS_USES_MAILBOX) |
| 166 } | 202 } |
| 167 | 203 |
| 168 WebGraphicsContext3D* Canvas2DLayerBridge::context() | 204 WebGraphicsContext3D* Canvas2DLayerBridge::context() |
| 169 { | 205 { |
| 170 return m_context->webContext(); | 206 return isValid() ? m_context->webContext() : 0; |
| 207 } |
| 208 |
| 209 bool Canvas2DLayerBridge::isValid() |
| 210 { |
| 211 ASSERT(m_layer); |
| 212 if (m_context->webContext()->isContextLost() || !m_surfaceIsValid) { |
| 213 // Attempt to recover. |
| 214 m_layer->clearTexture(); |
| 215 RefPtr<GraphicsContext3D> sharedContext = SharedGraphicsContext3D::get()
; |
| 216 if (!sharedContext || sharedContext->webContext()->isContextLost()) { |
| 217 m_surfaceIsValid = false; |
| 218 return false; |
| 219 } |
| 220 m_context = sharedContext; |
| 221 IntSize size(m_canvas->getTopDevice()->width(), m_canvas->getTopDevice()
->height()); |
| 222 SkAutoTUnref<SkSurface> surface(createSurface(m_context.get(), size)); |
| 223 if (surface.get()) { |
| 224 m_canvas->setSurface(surface.get()); |
| 225 m_surfaceIsValid = true; |
| 226 // FIXME: draw sad canvas picture into new buffer crbug.com/243842 |
| 227 } else { |
| 228 // Surface allocation failed. Set m_surfaceIsValid to false to |
| 229 // trigger subsequent retry. |
| 230 m_surfaceIsValid = false; |
| 231 return false; |
| 232 } |
| 233 #if !ENABLE(CANVAS_USES_MAILBOX) |
| 234 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canva
s->getDevice()->accessRenderTarget()); |
| 235 if (renderTarget) |
| 236 m_layer->setTextureId(renderTarget->asTexture()->getTextureHandle())
; |
| 237 #endif |
| 238 } |
| 239 return true; |
| 240 |
| 171 } | 241 } |
| 172 | 242 |
| 173 bool Canvas2DLayerBridge::prepareMailbox(WebKit::WebExternalTextureMailbox* outM
ailbox) | 243 bool Canvas2DLayerBridge::prepareMailbox(WebKit::WebExternalTextureMailbox* outM
ailbox) |
| 174 { | 244 { |
| 175 #if ENABLE(CANVAS_USES_MAILBOX) | 245 #if ENABLE(CANVAS_USES_MAILBOX) |
| 246 if (!isValid()) |
| 247 return false; |
| 176 // Release to skia textures that were previouosly released by the | 248 // Release to skia textures that were previouosly released by the |
| 177 // compositor. We do this before acquiring the next snapshot in | 249 // compositor. We do this before acquiring the next snapshot in |
| 178 // order to cap maximum gpu memory consumption. | 250 // order to cap maximum gpu memory consumption. |
| 179 m_context->makeContextCurrent(); | 251 m_context->makeContextCurrent(); |
| 180 Vector<MailboxInfo>::iterator mailboxInfo; | 252 Vector<MailboxInfo>::iterator mailboxInfo; |
| 181 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai
lboxInfo++) { | 253 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai
lboxInfo++) { |
| 182 if (mailboxInfo->m_status == MailboxReleased) { | 254 if (mailboxInfo->m_status == MailboxReleased) { |
| 183 if (mailboxInfo->m_mailbox.syncPoint) { | 255 if (mailboxInfo->m_mailbox.syncPoint) { |
| 184 context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint); | 256 context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint); |
| 185 mailboxInfo->m_mailbox.syncPoint = 0; | 257 mailboxInfo->m_mailbox.syncPoint = 0; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 mailboxInfo->m_status = MailboxReleased; | 339 mailboxInfo->m_status = MailboxReleased; |
| 268 return; | 340 return; |
| 269 } | 341 } |
| 270 } | 342 } |
| 271 #endif | 343 #endif |
| 272 ASSERT_NOT_REACHED(); | 344 ASSERT_NOT_REACHED(); |
| 273 } | 345 } |
| 274 | 346 |
| 275 WebKit::WebLayer* Canvas2DLayerBridge::layer() | 347 WebKit::WebLayer* Canvas2DLayerBridge::layer() |
| 276 { | 348 { |
| 349 ASSERT(m_layer); |
| 277 return m_layer->layer(); | 350 return m_layer->layer(); |
| 278 } | 351 } |
| 279 | 352 |
| 280 void Canvas2DLayerBridge::contextAcquired() | 353 void Canvas2DLayerBridge::contextAcquired() |
| 281 { | 354 { |
| 282 Canvas2DLayerManager::get().layerDidDraw(this); | 355 Canvas2DLayerManager::get().layerDidDraw(this); |
| 283 m_didRecordDrawCommand = true; | 356 m_didRecordDrawCommand = true; |
| 284 } | 357 } |
| 285 | 358 |
| 286 unsigned Canvas2DLayerBridge::backBufferTexture() | 359 unsigned Canvas2DLayerBridge::backBufferTexture() |
| 287 { | 360 { |
| 361 if (!isValid()) |
| 362 return 0; |
| 288 contextAcquired(); | 363 contextAcquired(); |
| 289 m_canvas->flush(); | 364 m_canvas->flush(); |
| 290 m_context->flush(); | 365 m_context->flush(); |
| 291 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g
etDevice()->accessRenderTarget()); | 366 GrRenderTarget* renderTarget = reinterpret_cast<GrRenderTarget*>(m_canvas->g
etDevice()->accessRenderTarget()); |
| 292 if (renderTarget) { | 367 if (renderTarget) { |
| 293 return renderTarget->asTexture()->getTextureHandle(); | 368 return renderTarget->asTexture()->getTextureHandle(); |
| 294 } | 369 } |
| 295 return 0; | 370 return 0; |
| 296 } | 371 } |
| 297 | 372 |
| 298 #if ENABLE(CANVAS_USES_MAILBOX) | 373 #if ENABLE(CANVAS_USES_MAILBOX) |
| 299 Canvas2DLayerBridge::MailboxInfo::MailboxInfo(const MailboxInfo& other) { | 374 Canvas2DLayerBridge::MailboxInfo::MailboxInfo(const MailboxInfo& other) { |
| 300 // This copy constructor should only be used for Vector reallocation | 375 // This copy constructor should only be used for Vector reallocation |
| 301 // Assuming 'other' is to be destroyed, we swap m_image ownership | 376 // Assuming 'other' is to be destroyed, we swap m_image ownership |
| 302 // rather than do a refcount dance. | 377 // rather than do a refcount dance. |
| 303 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox)); | 378 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox)); |
| 304 m_image.swap(const_cast<SkAutoTUnref<SkImage>*>(&other.m_image)); | 379 m_image.swap(const_cast<SkAutoTUnref<SkImage>*>(&other.m_image)); |
| 305 m_status = other.m_status; | 380 m_status = other.m_status; |
| 306 } | 381 } |
| 307 #endif | 382 #endif |
| 308 | 383 |
| 309 } | 384 } |
| OLD | NEW |