Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / Canvas2DLayerBridge.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
13  *
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.
24  */
25
26 #include "config.h"
27
28 #include "platform/graphics/Canvas2DLayerBridge.h"
29
30 #include "GrContext.h"
31 #include "SkDevice.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"
41
42 namespace {
43 enum {
44     InvalidMailboxIndex = -1,
45 };
46
47 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, canvas2DLayerBridgeInstanceCounter, ("Canvas2DLayerBridge"));
48 }
49
50 namespace blink {
51
52 static PassRefPtr<SkSurface> createSkSurface(GrContext* gr, const IntSize& size, int msaaSampleCount = 0)
53 {
54     if (!gr)
55         return nullptr;
56     gr->resetContext();
57     SkImageInfo info = SkImageInfo::MakeN32Premul(size.width(), size.height());
58     return adoptRef(SkSurface::NewRenderTarget(gr, info,  msaaSampleCount));
59 }
60
61 PassRefPtr<Canvas2DLayerBridge> Canvas2DLayerBridge::create(const IntSize& size, OpacityMode opacityMode, int msaaSampleCount)
62 {
63     TRACE_EVENT_INSTANT0("test_gpu", "Canvas2DLayerBridgeCreation");
64     OwnPtr<WebGraphicsContext3DProvider> contextProvider = adoptPtr(Platform::current()->createSharedOffscreenGraphicsContext3DProvider());
65     if (!contextProvider)
66         return nullptr;
67     RefPtr<SkSurface> surface(createSkSurface(contextProvider->grContext(), size, msaaSampleCount));
68     if (!surface)
69         return nullptr;
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();
74 }
75
76 Canvas2DLayerBridge::Canvas2DLayerBridge(PassOwnPtr<WebGraphicsContext3DProvider> contextProvider, PassOwnPtr<SkDeferredCanvas> canvas, PassRefPtr<SkSurface> surface, int msaaSampleCount, OpacityMode opacityMode)
77     : m_canvas(canvas)
78     , m_surface(surface)
79     , m_contextProvider(contextProvider)
80     , m_imageBuffer(0)
81     , m_msaaSampleCount(msaaSampleCount)
82     , m_bytesAllocated(0)
83     , m_didRecordDrawCommand(false)
84     , m_isSurfaceValid(true)
85     , m_framesPending(0)
86     , m_framesSinceMailboxRelease(0)
87     , m_destructionInProgress(false)
88     , m_rateLimitingEnabled(false)
89     , m_isHidden(false)
90     , m_next(0)
91     , m_prev(0)
92     , m_lastImageId(0)
93     , m_releasedMailboxInfoIndex(InvalidMailboxIndex)
94 {
95     ASSERT(m_canvas);
96     ASSERT(m_surface);
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);
106 #ifndef NDEBUG
107     canvas2DLayerBridgeInstanceCounter.increment();
108 #endif
109 }
110
111 Canvas2DLayerBridge::~Canvas2DLayerBridge()
112 {
113     ASSERT(m_destructionInProgress);
114     ASSERT(!Canvas2DLayerManager::get().isInList(this));
115     m_layer.clear();
116     freeReleasedMailbox();
117 #if ENABLE(ASSERT)
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);
122     }
123 #endif
124     m_mailboxes.clear();
125 #ifndef NDEBUG
126     canvas2DLayerBridgeInstanceCounter.decrement();
127 #endif
128 }
129
130 void Canvas2DLayerBridge::beginDestruction()
131 {
132     ASSERT(!m_destructionInProgress);
133     setRateLimitingEnabled(false);
134     m_canvas->silentFlush();
135     m_imageBuffer = 0;
136     freeTransientResources();
137     setIsHidden(true);
138     m_destructionInProgress = true;
139     GraphicsLayer::unregisterContentsLayer(m_layer->layer());
140     m_canvas->setNotificationClient(0);
141     m_surface.clear();
142     m_canvas.clear();
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
149     // with repro steps.
150     ASSERT(!m_bytesAllocated);
151 }
152
153 void Canvas2DLayerBridge::setIsHidden(bool hidden)
154 {
155     ASSERT(!m_destructionInProgress);
156     bool newHiddenValue = hidden || m_destructionInProgress;
157     if (m_isHidden == newHiddenValue)
158         return;
159
160     m_isHidden = newHiddenValue;
161     if (isHidden()) {
162         freeTransientResources();
163     }
164 }
165
166 void Canvas2DLayerBridge::willAccessPixels()
167 {
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
171     // parameters.
172     m_surface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
173 }
174
175 void Canvas2DLayerBridge::freeTransientResources()
176 {
177     ASSERT(!m_destructionInProgress);
178     freeReleasedMailbox();
179     flush();
180     freeMemoryIfPossible(bytesAllocated());
181     ASSERT(!hasTransientResources());
182 }
183
184 bool Canvas2DLayerBridge::hasTransientResources() const
185 {
186     return !m_destructionInProgress && (hasReleasedMailbox() || bytesAllocated());
187 }
188
189 void Canvas2DLayerBridge::limitPendingFrames()
190 {
191     ASSERT(!m_destructionInProgress);
192     if (isHidden()) {
193         freeTransientResources();
194         return;
195     }
196     if (m_didRecordDrawCommand) {
197         m_framesPending++;
198         m_didRecordDrawCommand = false;
199         if (m_framesPending > 1) {
200             // Turn on the rate limiter if this layer tends to accumulate a
201             // non-discardable multi-frame backlog of draw commands.
202             setRateLimitingEnabled(true);
203         }
204         if (m_rateLimitingEnabled) {
205             flush();
206         }
207     }
208     ++m_framesSinceMailboxRelease;
209     if (releasedMailboxHasExpired()) {
210         freeReleasedMailbox();
211     }
212 }
213
214 void Canvas2DLayerBridge::prepareForDraw()
215 {
216     ASSERT(!m_destructionInProgress);
217     ASSERT(m_layer);
218     if (!checkSurfaceValid()) {
219         if (m_canvas) {
220             // drop pending commands because there is no surface to draw to
221             m_canvas->silentFlush();
222         }
223         return;
224     }
225     context()->makeContextCurrent();
226 }
227
228 void Canvas2DLayerBridge::storageAllocatedForRecordingChanged(size_t bytesAllocated)
229 {
230     ASSERT(!m_destructionInProgress);
231     intptr_t delta = (intptr_t)bytesAllocated - (intptr_t)m_bytesAllocated;
232     m_bytesAllocated = bytesAllocated;
233     Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this, delta);
234 }
235
236 size_t Canvas2DLayerBridge::storageAllocatedForRecording()
237 {
238     return m_canvas->storageAllocatedForRecording();
239 }
240
241 void Canvas2DLayerBridge::flushedDrawCommands()
242 {
243     ASSERT(!m_destructionInProgress);
244     storageAllocatedForRecordingChanged(storageAllocatedForRecording());
245     m_framesPending = 0;
246 }
247
248 void Canvas2DLayerBridge::skippedPendingDrawCommands()
249 {
250     ASSERT(!m_destructionInProgress);
251     // Stop triggering the rate limiter if SkDeferredCanvas is detecting
252     // and optimizing overdraw.
253     setRateLimitingEnabled(false);
254     flushedDrawCommands();
255 }
256
257 void Canvas2DLayerBridge::setRateLimitingEnabled(bool enabled)
258 {
259     ASSERT(!m_destructionInProgress);
260     if (m_rateLimitingEnabled != enabled) {
261         m_rateLimitingEnabled = enabled;
262         m_layer->setRateLimitContext(m_rateLimitingEnabled);
263     }
264 }
265
266 size_t Canvas2DLayerBridge::freeMemoryIfPossible(size_t bytesToFree)
267 {
268     ASSERT(!m_destructionInProgress);
269     size_t bytesFreed = m_canvas->freeMemoryIfPossible(bytesToFree);
270     m_bytesAllocated -= bytesFreed;
271     if (bytesFreed)
272         Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this, -((intptr_t)bytesFreed));
273     return bytesFreed;
274 }
275
276 void Canvas2DLayerBridge::flush()
277 {
278     ASSERT(!m_destructionInProgress);
279     if (m_canvas->hasPendingCommands()) {
280         TRACE_EVENT0("cc", "Canvas2DLayerBridge::flush");
281         freeReleasedMailbox(); // To avoid unnecessary triple-buffering
282         m_canvas->flush();
283     }
284 }
285
286 bool Canvas2DLayerBridge::releasedMailboxHasExpired()
287 {
288     // This heuristic indicates that the canvas is not being
289     // actively presented by the compositor (3 frames rendered since
290     // last mailbox release), suggesting that double buffering is not required.
291     return hasReleasedMailbox() && m_framesSinceMailboxRelease > 2;
292 }
293
294 Canvas2DLayerBridge::MailboxInfo* Canvas2DLayerBridge::releasedMailboxInfo()
295 {
296     return hasReleasedMailbox() ? &m_mailboxes[m_releasedMailboxInfoIndex] : 0;
297 }
298
299 bool Canvas2DLayerBridge::hasReleasedMailbox() const
300 {
301     return m_releasedMailboxInfoIndex != InvalidMailboxIndex;
302 }
303
304 void Canvas2DLayerBridge::freeReleasedMailbox()
305 {
306     if (!m_isSurfaceValid || m_contextProvider->context3d()->isContextLost())
307         return;
308     MailboxInfo* mailboxInfo = releasedMailboxInfo();
309     if (!mailboxInfo)
310         return;
311
312     ASSERT(mailboxInfo->m_status == MailboxReleased);
313     if (mailboxInfo->m_mailbox.syncPoint) {
314         context()->waitSyncPoint(mailboxInfo->m_mailbox.syncPoint);
315         mailboxInfo->m_mailbox.syncPoint = 0;
316     }
317     // Invalidate texture state in case the compositor altered it since the copy-on-write.
318     if (mailboxInfo->m_image) {
319         if (isHidden() || releasedMailboxHasExpired())
320             mailboxInfo->m_image->getTexture()->resetFlag(static_cast<GrTextureFlags>(GrTexture::kReturnToCache_FlagBit));
321         mailboxInfo->m_image->getTexture()->textureParamsModified();
322         mailboxInfo->m_image.clear();
323     }
324     mailboxInfo->m_status = MailboxAvailable;
325     m_releasedMailboxInfoIndex = InvalidMailboxIndex;
326     Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this);
327 }
328
329 WebGraphicsContext3D* Canvas2DLayerBridge::context()
330 {
331     // Check on m_layer is necessary because context() may be called during
332     // the destruction of m_layer
333     if (m_layer && !m_destructionInProgress)
334         checkSurfaceValid(); // To ensure rate limiter is disabled if context is lost.
335     return m_contextProvider ? m_contextProvider->context3d() : 0;
336 }
337
338 bool Canvas2DLayerBridge::checkSurfaceValid()
339 {
340     ASSERT(!m_destructionInProgress);
341     if (m_destructionInProgress || !m_isSurfaceValid)
342         return false;
343     if (m_contextProvider->context3d()->isContextLost()) {
344         m_isSurfaceValid = false;
345         m_surface.clear();
346         if (m_imageBuffer)
347             m_imageBuffer->notifySurfaceInvalid();
348         setRateLimitingEnabled(false);
349     }
350     return m_isSurfaceValid;
351 }
352
353 bool Canvas2DLayerBridge::restoreSurface()
354 {
355     ASSERT(!m_destructionInProgress);
356     if (m_destructionInProgress)
357         return false;
358     ASSERT(m_layer && !m_isSurfaceValid);
359
360     WebGraphicsContext3D* sharedContext = 0;
361     // We must clear the mailboxes before calling m_layer->clearTexture() to prevent
362     // re-entry via mailboxReleased from operating on defunct GrContext objects.
363     m_mailboxes.clear();
364     m_releasedMailboxInfoIndex = InvalidMailboxIndex;
365     m_layer->clearTexture();
366     m_contextProvider = adoptPtr(Platform::current()->createSharedOffscreenGraphicsContext3DProvider());
367     if (m_contextProvider)
368         sharedContext = m_contextProvider->context3d();
369
370     if (sharedContext && !sharedContext->isContextLost()) {
371         IntSize size(m_canvas->getTopDevice()->width(), m_canvas->getTopDevice()->height());
372         RefPtr<SkSurface> surface(createSkSurface(m_contextProvider->grContext(), size, m_msaaSampleCount));
373         if (surface.get()) {
374             m_surface = surface.release();
375             m_canvas->setSurface(m_surface.get());
376             m_isSurfaceValid = true;
377             // FIXME: draw sad canvas picture into new buffer crbug.com/243842
378         }
379     }
380
381     return m_isSurfaceValid;
382 }
383
384 bool Canvas2DLayerBridge::prepareMailbox(WebExternalTextureMailbox* outMailbox, WebExternalBitmap* bitmap)
385 {
386     if (m_destructionInProgress) {
387         // It can be hit in the following sequence.
388         // 1. Canvas draws something.
389         // 2. The compositor begins the frame.
390         // 3. Javascript makes a context be lost.
391         // 4. Here.
392         return false;
393     }
394     if (bitmap) {
395         // Using accelerated 2d canvas with software renderer, which
396         // should only happen in tests that use fake graphics contexts
397         // or in Android WebView in software mode. In this case, we do
398         // not care about producing any results for this canvas.
399         m_canvas->silentFlush();
400         m_lastImageId = 0;
401         return false;
402     }
403     if (!checkSurfaceValid())
404         return false;
405
406     WebGraphicsContext3D* webContext = context();
407
408     // Release to skia textures that were previouosly released by the
409     // compositor. We do this before acquiring the next snapshot in
410     // order to cap maximum gpu memory consumption.
411     webContext->makeContextCurrent();
412     flush();
413
414     RefPtr<SkImage> image = adoptRef(m_canvas->newImageSnapshot());
415
416     // Early exit if canvas was not drawn to since last prepareMailbox
417     if (image->uniqueID() == m_lastImageId)
418         return false;
419     m_lastImageId = image->uniqueID();
420
421     MailboxInfo* mailboxInfo = createMailboxInfo();
422     mailboxInfo->m_status = MailboxInUse;
423     mailboxInfo->m_image = image;
424
425     ASSERT(mailboxInfo->m_mailbox.syncPoint == 0);
426     ASSERT(mailboxInfo->m_image.get());
427     ASSERT(mailboxInfo->m_image->getTexture());
428
429     // Because of texture sharing with the compositor, we must invalidate
430     // the state cached in skia so that the deferred copy on write
431     // in SkSurface_Gpu does not make any false assumptions.
432     mailboxInfo->m_image->getTexture()->textureParamsModified();
433
434     webContext->bindTexture(GL_TEXTURE_2D, mailboxInfo->m_image->getTexture()->getTextureHandle());
435     webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
436     webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
437     webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
438     webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
439     webContext->produceTextureCHROMIUM(GL_TEXTURE_2D, mailboxInfo->m_mailbox.name);
440     if (isHidden()) {
441         // With hidden canvases, we release the SkImage immediately because
442         // there is no need for animations to be double buffered.
443         mailboxInfo->m_image.clear();
444     } else {
445         webContext->flush();
446         mailboxInfo->m_mailbox.syncPoint = webContext->insertSyncPoint();
447     }
448     webContext->bindTexture(GL_TEXTURE_2D, 0);
449     // Because we are changing the texture binding without going through skia,
450     // we must dirty the context.
451     m_contextProvider->grContext()->resetContext(kTextureBinding_GrGLBackendState);
452
453     // set m_parentLayerBridge to make sure 'this' stays alive as long as it has
454     // live mailboxes
455     ASSERT(!mailboxInfo->m_parentLayerBridge);
456     mailboxInfo->m_parentLayerBridge = this;
457     *outMailbox = mailboxInfo->m_mailbox;
458
459     return true;
460 }
461
462 Canvas2DLayerBridge::MailboxInfo* Canvas2DLayerBridge::createMailboxInfo() {
463     ASSERT(!m_destructionInProgress);
464     MailboxInfo* mailboxInfo;
465     for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mailboxInfo++) {
466         if (mailboxInfo->m_status == MailboxAvailable) {
467             return mailboxInfo;
468         }
469     }
470
471     // No available mailbox: create one.
472     m_mailboxes.grow(m_mailboxes.size() + 1);
473     mailboxInfo = &m_mailboxes.last();
474     context()->genMailboxCHROMIUM(mailboxInfo->m_mailbox.name);
475     // Worst case, canvas is triple buffered.  More than 3 active mailboxes
476     // means there is a problem.
477     // For the single-threaded case, this value needs to be at least
478     // kMaxSwapBuffersPending+1 (in render_widget.h).
479     // Because of crbug.com/247874, it needs to be kMaxSwapBuffersPending+2.
480     // TODO(piman): fix this.
481     ASSERT(m_mailboxes.size() <= 4);
482     ASSERT(mailboxInfo < m_mailboxes.end());
483     return mailboxInfo;
484 }
485
486 void Canvas2DLayerBridge::mailboxReleased(const WebExternalTextureMailbox& mailbox, bool lostResource)
487 {
488     freeReleasedMailbox(); // Never have more than one mailbox in the released state.
489     bool contextLost = !m_isSurfaceValid || m_contextProvider->context3d()->isContextLost();
490     Vector<MailboxInfo>::iterator mailboxInfo;
491     for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); ++mailboxInfo) {
492         if (nameEquals(mailboxInfo->m_mailbox, mailbox)) {
493             mailboxInfo->m_mailbox.syncPoint = mailbox.syncPoint;
494             ASSERT(mailboxInfo->m_status == MailboxInUse);
495             ASSERT(mailboxInfo->m_parentLayerBridge.get() == this);
496
497             if (contextLost) {
498                 // No need to clean up the mailbox resource, but make sure the
499                 // mailbox can also be reusable once the context is restored.
500                 mailboxInfo->m_status = MailboxAvailable;
501                 m_releasedMailboxInfoIndex = InvalidMailboxIndex;
502                 Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this);
503             } else if (lostResource) {
504                 // In case of the resource is lost, we need to delete the backing
505                 // texture and remove the mailbox from list to avoid reusing it
506                 // in future.
507                 if (mailboxInfo->m_image) {
508                     mailboxInfo->m_image->getTexture()->resetFlag(
509                         static_cast<GrTextureFlags>(GrTexture::kReturnToCache_FlagBit));
510                     mailboxInfo->m_image->getTexture()->textureParamsModified();
511                     mailboxInfo->m_image.clear();
512                 }
513                 size_t i = mailboxInfo - m_mailboxes.begin();
514                 m_mailboxes.remove(i);
515                 Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this);
516                 // Here we need to return early since mailboxInfo removal would
517                 // also clear m_parentLayerBridge reference.
518                 return;
519             } else {
520                 mailboxInfo->m_status = MailboxReleased;
521                 m_releasedMailboxInfoIndex = mailboxInfo - m_mailboxes.begin();
522                 m_framesSinceMailboxRelease = 0;
523                 if (isHidden()) {
524                     freeReleasedMailbox();
525                 } else {
526                     ASSERT(!m_destructionInProgress);
527                     Canvas2DLayerManager::get().layerTransientResourceAllocationChanged(this);
528                 }
529             }
530             // Trigger Canvas2DLayerBridge self-destruction if this is the
531             // last live mailbox and the layer bridge is not externally
532             // referenced.
533             mailboxInfo->m_parentLayerBridge.clear();
534             return;
535         }
536     }
537 }
538
539 WebLayer* Canvas2DLayerBridge::layer() const
540 {
541     ASSERT(!m_destructionInProgress);
542     ASSERT(m_layer);
543     return m_layer->layer();
544 }
545
546 void Canvas2DLayerBridge::finalizeFrame()
547 {
548     ASSERT(!m_destructionInProgress);
549     Canvas2DLayerManager::get().layerDidDraw(this);
550     m_didRecordDrawCommand = true;
551 }
552
553 Platform3DObject Canvas2DLayerBridge::getBackingTexture()
554 {
555     ASSERT(!m_destructionInProgress);
556     if (!checkSurfaceValid())
557         return 0;
558     m_canvas->flush();
559     context()->flush();
560     GrRenderTarget* renderTarget = m_canvas->getTopDevice()->accessRenderTarget();
561     if (renderTarget) {
562         return renderTarget->asTexture()->getTextureHandle();
563     }
564     return 0;
565 }
566
567 Canvas2DLayerBridge::MailboxInfo::MailboxInfo(const MailboxInfo& other) {
568     // This copy constructor should only be used for Vector reallocation
569     // Assuming 'other' is to be destroyed, we transfer m_image ownership
570     // rather than do a refcount dance.
571     memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox));
572     m_image = const_cast<MailboxInfo*>(&other)->m_image.release();
573     m_status = other.m_status;
574 }
575
576 } // namespace blink