2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "platform/graphics/Canvas2DLayerBridge.h"
30 #include "GrContext.h"
32 #include "SkSurface.h"
33 #include "platform/TraceEvent.h"
34 #include "platform/graphics/Canvas2DLayerManager.h"
35 #include "platform/graphics/GraphicsLayer.h"
36 #include "public/platform/Platform.h"
37 #include "public/platform/WebCompositorSupport.h"
38 #include "public/platform/WebGraphicsContext3D.h"
39 #include "public/platform/WebGraphicsContext3DProvider.h"
40 #include "wtf/RefCountedLeakCounter.h"
44 InvalidMailboxIndex = -1,
47 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, canvas2DLayerBridgeInstanceCounter, ("Canvas2DLayerBridge"));
52 static PassRefPtr<SkSurface> createSkSurface(GrContext* gr, const IntSize& size, int msaaSampleCount = 0)
57 SkImageInfo info = SkImageInfo::MakeN32Premul(size.width(), size.height());
58 return adoptRef(SkSurface::NewRenderTarget(gr, info, msaaSampleCount));
61 PassRefPtr<Canvas2DLayerBridge> Canvas2DLayerBridge::create(const IntSize& size, OpacityMode opacityMode, int msaaSampleCount)
63 TRACE_EVENT_INSTANT0("test_gpu", "Canvas2DLayerBridgeCreation");
64 OwnPtr<WebGraphicsContext3DProvider> contextProvider = adoptPtr(Platform::current()->createSharedOffscreenGraphicsContext3DProvider());
67 RefPtr<SkSurface> surface(createSkSurface(contextProvider->grContext(), size, msaaSampleCount));
70 RefPtr<Canvas2DLayerBridge> layerBridge;
71 OwnPtr<SkDeferredCanvas> canvas = adoptPtr(SkDeferredCanvas::Create(surface.get()));
72 layerBridge = adoptRef(new Canvas2DLayerBridge(contextProvider.release(), canvas.release(), surface.release(), msaaSampleCount, opacityMode));
73 return layerBridge.release();
76 Canvas2DLayerBridge::Canvas2DLayerBridge(PassOwnPtr<WebGraphicsContext3DProvider> contextProvider, PassOwnPtr<SkDeferredCanvas> canvas, PassRefPtr<SkSurface> surface, int msaaSampleCount, OpacityMode opacityMode)
79 , m_contextProvider(contextProvider)
81 , m_msaaSampleCount(msaaSampleCount)
83 , m_didRecordDrawCommand(false)
84 , m_isSurfaceValid(true)
86 , m_framesSinceMailboxRelease(0)
87 , m_destructionInProgress(false)
88 , m_rateLimitingEnabled(false)
93 , m_releasedMailboxInfoIndex(InvalidMailboxIndex)
97 ASSERT(m_contextProvider);
98 // Used by browser tests to detect the use of a Canvas2DLayerBridge.
99 TRACE_EVENT_INSTANT0("test_gpu", "Canvas2DLayerBridgeCreation");
100 m_layer = adoptPtr(Platform::current()->compositorSupport()->createExternalTextureLayer(this));
101 m_layer->setOpaque(opacityMode == Opaque);
102 m_layer->setBlendBackgroundColor(opacityMode != Opaque);
103 GraphicsLayer::registerContentsLayer(m_layer->layer());
104 m_layer->setRateLimitContext(m_rateLimitingEnabled);
105 m_canvas->setNotificationClient(this);
107 canvas2DLayerBridgeInstanceCounter.increment();
111 Canvas2DLayerBridge::~Canvas2DLayerBridge()
113 ASSERT(m_destructionInProgress);
114 ASSERT(!Canvas2DLayerManager::get().isInList(this));
116 freeReleasedMailbox();
118 Vector<MailboxInfo>::iterator mailboxInfo;
119 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); ++mailboxInfo) {
120 ASSERT(mailboxInfo->m_status != MailboxInUse);
121 ASSERT(mailboxInfo->m_status != MailboxReleased || m_contextProvider->context3d()->isContextLost() || !m_isSurfaceValid);
126 canvas2DLayerBridgeInstanceCounter.decrement();
130 void Canvas2DLayerBridge::beginDestruction()
132 ASSERT(!m_destructionInProgress);
133 setRateLimitingEnabled(false);
134 m_canvas->silentFlush();
136 freeTransientResources();
138 m_destructionInProgress = true;
139 GraphicsLayer::unregisterContentsLayer(m_layer->layer());
140 m_canvas->setNotificationClient(0);
143 m_layer->clearTexture();
144 // Orphaning the layer is required to trigger the recration of a new layer
145 // in the case where destruction is caused by a canvas resize. Test:
146 // virtual/gpu/fast/canvas/canvas-resize-after-paint-without-layout.html
147 m_layer->layer()->removeFromParent();
148 // To anyone who ever hits this assert: Please update crbug.com/344666
150 ASSERT(!m_bytesAllocated);
153 void Canvas2DLayerBridge::setIsHidden(bool hidden)
155 ASSERT(!m_destructionInProgress);
156 bool newHiddenValue = hidden || m_destructionInProgress;
157 if (m_isHidden == newHiddenValue)
160 m_isHidden = newHiddenValue;
162 freeTransientResources();
166 void Canvas2DLayerBridge::willAccessPixels()
168 // A readback operation may alter the texture parameters, which may affect
169 // the compositor's behavior. Therefore, we must trigger copy-on-write
170 // even though we are not technically writing to the texture, only to its
172 m_surface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
175 void Canvas2DLayerBridge::freeTransientResources()
177 ASSERT(!m_destructionInProgress);
178 if (!m_isSurfaceValid)
180 freeReleasedMailbox();
182 freeMemoryIfPossible(bytesAllocated());
183 ASSERT(!hasTransientResources());
186 bool Canvas2DLayerBridge::hasTransientResources() const
188 return !m_destructionInProgress && (hasReleasedMailbox() || bytesAllocated());
191 void Canvas2DLayerBridge::limitPendingFrames()
193 ASSERT(!m_destructionInProgress);
195 freeTransientResources();
198 if (m_didRecordDrawCommand) {
200 m_didRecordDrawCommand = false;
201 if (m_framesPending > 1) {
202 // Turn on the rate limiter if this layer tends to accumulate a
203 // non-discardable multi-frame backlog of draw commands.
204 setRateLimitingEnabled(true);
206 if (m_rateLimitingEnabled) {
210 ++m_framesSinceMailboxRelease;
211 if (releasedMailboxHasExpired()) {
212 freeReleasedMailbox();
216 void Canvas2DLayerBridge::prepareForDraw()
218 ASSERT(!m_destructionInProgress);
220 if (!checkSurfaceValid()) {
222 // drop pending commands because there is no surface to draw to
223 m_canvas->silentFlush();
229 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAllocated)
231 ASSERT(!m_destructionInProgress);
232 intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated;
233 m_bytesAllocated = bytesAllocated;
234 Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this, delta);
237 size_t Canvas2DLayerBridge::storageAllocatedForRecording()
239 return m_canvas->storageAllocatedForRecording();
242 void Canvas2DLayerBridge::flushedDrawCommands()
244 ASSERT(!m_destructionInProgress);
245 storageAllocatedForRecordingChanged(storageAllocatedForRecording());
249 void Canvas2DLayerBridge::skippedPendingDrawCommands()
251 ASSERT(!m_destructionInProgress);
252 // Stop triggering the rate limiter if SkDeferredCanvas is detecting
253 // and optimizing overdraw.
254 setRateLimitingEnabled(false);
255 flushedDrawCommands();
258 void Canvas2DLayerBridge::setRateLimitingEnabled(bool enabled)
260 ASSERT(!m_destructionInProgress);
261 if (m_rateLimitingEnabled != enabled) {
262 m_rateLimitingEnabled = enabled;
263 m_layer->setRateLimitContext(m_rateLimitingEnabled);
267 size_t Canvas2DLayerBridge::freeMemoryIfPossible(size_t bytesToFree)
269 ASSERT(!m_destructionInProgress);
270 size_t bytesFreed = m_canvas->freeMemoryIfPossible(bytesToFree);
271 m_bytesAllocated -= bytesFreed;
273 Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this, -((intptr_t)bytesFreed));
277 void Canvas2DLayerBridge::flush()
279 ASSERT(!m_destructionInProgress);
280 if (m_canvas->hasPendingCommands()) {
281 TRACE_EVENT0("cc", "Canvas2DLayerBridge::flush");
282 freeReleasedMailbox(); // To avoid unnecessary triple-buffering
287 bool Canvas2DLayerBridge::releasedMailboxHasExpired()
289 // This heuristic indicates that the canvas is not being
290 // actively presented by the compositor (3 frames rendered since
291 // last mailbox release), suggesting that double buffering is not required.
292 return hasReleasedMailbox() && m_framesSinceMailboxRelease > 2;
295 Canvas2DLayerBridge::MailboxInfo* Canvas2DLayerBridge::releasedMailboxInfo()
297 return hasReleasedMailbox() ? &m_mailboxes[m_releasedMailboxInfoIndex] : 0;
300 bool Canvas2DLayerBridge::hasReleasedMailbox() const
302 return m_releasedMailboxInfoIndex != InvalidMailboxIndex;
305 void Canvas2DLayerBridge::freeReleasedMailbox()
307 if (!m_isSurfaceValid || m_contextProvider->context3d()->isContextLost())
309 MailboxInfo* mailboxInfo = releasedMailboxInfo();
313 ASSERT(mailboxInfo->m_status == MailboxReleased);
314 if (mailboxInfo->m_mailbox.syncPoint) {
315 context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint);
316 mailboxInfo->m_mailbox.syncPoint = 0;
318 // Invalidate texture state in case the compositor altered it since the copy-on-write.
319 if (mailboxInfo->m_image) {
320 if (isHidden() || releasedMailboxHasExpired())
321 mailboxInfo->m_image->getTexture()->resetFlag(static_cast<GrTextureFlags>(GrTexture::kReturnToCache_FlagBit));
322 mailboxInfo->m_image->getTexture()->textureParamsModified();
323 mailboxInfo->m_image.clear();
325 mailboxInfo->m_status = MailboxAvailable;
326 m_releasedMailboxInfoIndex = InvalidMailboxIndex;
327 Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this);
330 WebGraphicsContext3D* Canvas2DLayerBridge::context()
332 // Check on m_layer is necessary because context() may be called during
333 // the destruction of m_layer
334 if (m_layer && !m_destructionInProgress)
335 checkSurfaceValid(); // To ensure rate limiter is disabled if context is lost.
336 return m_contextProvider ? m_contextProvider->context3d() : 0;
339 bool Canvas2DLayerBridge::checkSurfaceValid()
341 ASSERT(!m_destructionInProgress);
342 if (m_destructionInProgress || !m_isSurfaceValid)
344 if (m_contextProvider->context3d()->isContextLost()) {
345 m_isSurfaceValid = false;
348 m_imageBuffer->notifySurfaceInvalid();
349 setRateLimitingEnabled(false);
351 return m_isSurfaceValid;
354 bool Canvas2DLayerBridge::restoreSurface()
356 ASSERT(!m_destructionInProgress);
357 if (m_destructionInProgress)
359 ASSERT(m_layer && !m_isSurfaceValid);
361 WebGraphicsContext3D* sharedContext = 0;
362 // We must clear the mailboxes before calling m_layer->clearTexture() to prevent
363 // re-entry via mailboxReleased from operating on defunct GrContext objects.
365 m_releasedMailboxInfoIndex = InvalidMailboxIndex;
366 m_layer->clearTexture();
367 m_contextProvider = adoptPtr(Platform::current()->createSharedOffscreenGraphicsContext3DProvider());
368 if (m_contextProvider)
369 sharedContext = m_contextProvider->context3d();
371 if (sharedContext && !sharedContext->isContextLost()) {
372 IntSize size(m_canvas->getTopDevice()->width(), m_canvas->getTopDevice()->height());
373 RefPtr<SkSurface> surface(createSkSurface(m_contextProvider->grContext(), size, m_msaaSampleCount));
375 m_surface = surface.release();
376 m_canvas->setSurface(m_surface.get());
377 m_isSurfaceValid = true;
378 // FIXME: draw sad canvas picture into new buffer crbug.com/243842
382 return m_isSurfaceValid;
385 bool Canvas2DLayerBridge::prepareMailbox(WebExternalTextureMailbox* outMailbox, WebExternalBitmap* bitmap)
387 if (m_destructionInProgress) {
388 // It can be hit in the following sequence.
389 // 1. Canvas draws something.
390 // 2. The compositor begins the frame.
391 // 3. Javascript makes a context be lost.
396 // Using accelerated 2d canvas with software renderer, which
397 // should only happen in tests that use fake graphics contexts
398 // or in Android WebView in software mode. In this case, we do
399 // not care about producing any results for this canvas.
400 m_canvas->silentFlush();
404 if (!checkSurfaceValid())
407 WebGraphicsContext3D* webContext = context();
409 // Release to skia textures that were previouosly released by the
410 // compositor. We do this before acquiring the next snapshot in
411 // order to cap maximum gpu memory consumption.
414 RefPtr<SkImage> image = adoptRef(m_canvas->newImageSnapshot());
416 // Early exit if canvas was not drawn to since last prepareMailbox
417 if (image->uniqueID() == m_lastImageId)
419 m_lastImageId = image->uniqueID();
421 MailboxInfo* mailboxInfo = createMailboxInfo();
422 mailboxInfo->m_status = MailboxInUse;
423 mailboxInfo->m_image = image;
425 ASSERT(mailboxInfo->m_mailbox.syncPoint == 0);
426 ASSERT(mailboxInfo->m_image.get());
428 // set m_parentLayerBridge to make sure 'this' stays alive as long as it has
430 ASSERT(!mailboxInfo->m_parentLayerBridge);
431 mailboxInfo->m_parentLayerBridge = this;
432 *outMailbox = mailboxInfo->m_mailbox;
434 GrContext* grContext = m_contextProvider->grContext();
436 return true; // for testing: skip gl stuff when using a mock graphics context.
438 ASSERT(mailboxInfo->m_image->getTexture());
440 // Because of texture sharing with the compositor, we must invalidate
441 // the state cached in skia so that the deferred copy on write
442 // in SkSurface_Gpu does not make any false assumptions.
443 mailboxInfo->m_image->getTexture()->textureParamsModified();
445 webContext->bindTexture(GL_TEXTURE_2D, mailboxInfo->m_image->getTexture()->getTextureHandle());
446 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
447 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
448 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
449 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
450 webContext->produceTextureCHROMIUM(GL_TEXTURE_2D, mailboxInfo->m_mailbox.name);
452 // With hidden canvases, we release the SkImage immediately because
453 // there is no need for animations to be double buffered.
454 mailboxInfo->m_image.clear();
457 mailboxInfo->m_mailbox.syncPoint = webContext->insertSyncPoint();
459 webContext->bindTexture(GL_TEXTURE_2D, 0);
460 // Because we are changing the texture binding without going through skia,
461 // we must dirty the context.
462 grContext->resetContext(kTextureBinding_GrGLBackendState);
467 Canvas2DLayerBridge::MailboxInfo* Canvas2DLayerBridge::createMailboxInfo() {
468 ASSERT(!m_destructionInProgress);
469 MailboxInfo* mailboxInfo;
470 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mailboxInfo++) {
471 if (mailboxInfo->m_status == MailboxAvailable) {
476 // No available mailbox: create one.
477 m_mailboxes.grow(m_mailboxes.size() + 1);
478 mailboxInfo = &m_mailboxes.last();
479 context()->genMailboxCHROMIUM(mailboxInfo->m_mailbox.name);
480 // Worst case, canvas is triple buffered. More than 3 active mailboxes
481 // means there is a problem.
482 // For the single-threaded case, this value needs to be at least
483 // kMaxSwapBuffersPending+1 (in render_widget.h).
484 // Because of crbug.com/247874, it needs to be kMaxSwapBuffersPending+2.
485 // TODO(piman): fix this.
486 ASSERT(m_mailboxes.size() <= 4);
487 ASSERT(mailboxInfo < m_mailboxes.end());
491 void Canvas2DLayerBridge::mailboxReleased(const WebExternalTextureMailbox& mailbox, bool lostResource)
493 freeReleasedMailbox(); // Never have more than one mailbox in the released state.
494 bool contextLost = !m_isSurfaceValid || m_contextProvider->context3d()->isContextLost();
495 Vector<MailboxInfo>::iterator mailboxInfo;
496 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); ++mailboxInfo) {
497 if (nameEquals(mailboxInfo->m_mailbox, mailbox)) {
498 mailboxInfo->m_mailbox.syncPoint = mailbox.syncPoint;
499 ASSERT(mailboxInfo->m_status == MailboxInUse);
500 ASSERT(mailboxInfo->m_parentLayerBridge.get() == this);
503 // No need to clean up the mailbox resource, but make sure the
504 // mailbox can also be reusable once the context is restored.
505 mailboxInfo->m_status = MailboxAvailable;
506 m_releasedMailboxInfoIndex = InvalidMailboxIndex;
507 Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this);
508 } else if (lostResource) {
509 // In case of the resource is lost, we need to delete the backing
510 // texture and remove the mailbox from list to avoid reusing it
512 if (mailboxInfo->m_image) {
513 GrTexture* texture = mailboxInfo->m_image->getTexture();
515 texture->resetFlag(static_cast<GrTextureFlags>(GrTexture::kReturnToCache_FlagBit));
516 texture->textureParamsModified();
518 mailboxInfo->m_image.clear();
520 if (m_destructionInProgress) {
521 mailboxInfo->m_status = MailboxAvailable; // To satisfy assert in destructor
523 // The following line may trigger self destruction. We do not care about
524 // not cleaning up m_mailboxes during destruction sequence because
525 // mailboxes will not be recycled after this point. Calling remove()
526 // could trigger a memory use after free, so we just clear the self
527 // reference to be safe, and we let the Canvas2DLayerBridge destructor
528 // take care of freeing m_mailboxes.
529 mailboxInfo->m_parentLayerBridge.clear();
531 size_t i = mailboxInfo - m_mailboxes.begin();
532 m_mailboxes.remove(i); // indirectly clears mailboxInfo->m_parentLayerBridge
533 Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this);
535 // mailboxInfo is not valid from this point, so we return immediately.
538 mailboxInfo->m_status = MailboxReleased;
539 m_releasedMailboxInfoIndex = mailboxInfo - m_mailboxes.begin();
540 m_framesSinceMailboxRelease = 0;
542 freeReleasedMailbox();
544 ASSERT(!m_destructionInProgress);
545 Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this);
548 // Trigger Canvas2DLayerBridge self-destruction if this is the
549 // last live mailbox and the layer bridge is not externally
551 mailboxInfo->m_parentLayerBridge.clear();
557 WebLayer* Canvas2DLayerBridge::layer() const
559 ASSERT(!m_destructionInProgress);
561 return m_layer->layer();
564 void Canvas2DLayerBridge::finalizeFrame(const FloatRect &dirtyRect)
566 ASSERT(!m_destructionInProgress);
567 Canvas2DLayerManager::get().layerDidDraw(this);
568 m_layer->layer()->invalidateRect(dirtyRect);
569 m_didRecordDrawCommand = true;
572 Platform3DObject Canvas2DLayerBridge::getBackingTexture()
574 ASSERT(!m_destructionInProgress);
575 if (!checkSurfaceValid())
579 GrRenderTarget* renderTarget = m_canvas->getTopDevice()->accessRenderTarget();
581 return renderTarget->asTexture()->getTextureHandle();
586 Canvas2DLayerBridge::MailboxInfo::MailboxInfo(const MailboxInfo& other) {
587 // This copy constructor should only be used for Vector reallocation
588 // Assuming 'other' is to be destroyed, we transfer m_image and
589 // m_parentLayerBridge ownership rather than do a refcount dance.
590 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox));
591 m_image = const_cast<MailboxInfo*>(&other)->m_image.release();
592 m_parentLayerBridge = const_cast<MailboxInfo*>(&other)->m_parentLayerBridge.release();
593 m_status = other.m_status;