| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/imagebitmap/ImageBitmapRenderingContext.h" | 5 #include "modules/imagebitmap/ImageBitmapRenderingContext.h" |
| 6 | 6 |
| 7 #include "bindings/modules/v8/RenderingContext.h" | 7 #include "bindings/modules/v8/RenderingContext.h" |
| 8 #include "core/frame/ImageBitmap.h" | 8 #include "core/frame/ImageBitmap.h" |
| 9 #include "platform/graphics/GraphicsContext.h" | 9 #include "platform/graphics/GraphicsContext.h" |
| 10 #include "platform/graphics/StaticBitmapImage.h" | 10 #include "platform/graphics/StaticBitmapImage.h" |
| 11 #include "platform/graphics/gpu/ImageLayerBridge.h" |
| 11 #include "third_party/skia/include/core/SkImage.h" | 12 #include "third_party/skia/include/core/SkImage.h" |
| 12 #include "third_party/skia/include/core/SkSurface.h" | 13 #include "third_party/skia/include/core/SkSurface.h" |
| 13 | 14 |
| 14 namespace blink { | 15 namespace blink { |
| 15 | 16 |
| 16 ImageBitmapRenderingContext::ImageBitmapRenderingContext( | 17 ImageBitmapRenderingContext::ImageBitmapRenderingContext( |
| 17 HTMLCanvasElement* canvas, | 18 HTMLCanvasElement* canvas, |
| 18 const CanvasContextCreationAttributes& attrs, | 19 const CanvasContextCreationAttributes& attrs, |
| 19 Document& document) | 20 Document& document) |
| 20 : CanvasRenderingContext(canvas, nullptr, attrs) {} | 21 : CanvasRenderingContext(canvas, nullptr, attrs), |
| 22 m_imageLayerBridge( |
| 23 new ImageLayerBridge(attrs.alpha() ? NonOpaque : Opaque)) {} |
| 21 | 24 |
| 22 ImageBitmapRenderingContext::~ImageBitmapRenderingContext() {} | 25 ImageBitmapRenderingContext::~ImageBitmapRenderingContext() {} |
| 23 | 26 |
| 24 void ImageBitmapRenderingContext::setCanvasGetContextResult( | 27 void ImageBitmapRenderingContext::setCanvasGetContextResult( |
| 25 RenderingContext& result) { | 28 RenderingContext& result) { |
| 26 result.setImageBitmapRenderingContext(this); | 29 result.setImageBitmapRenderingContext(this); |
| 27 } | 30 } |
| 28 | 31 |
| 29 void ImageBitmapRenderingContext::transferFromImageBitmap( | 32 void ImageBitmapRenderingContext::transferFromImageBitmap( |
| 30 ImageBitmap* imageBitmap, | 33 ImageBitmap* imageBitmap, |
| 31 ExceptionState& exceptionState) { | 34 ExceptionState& exceptionState) { |
| 32 if (!imageBitmap) { | 35 if (imageBitmap && imageBitmap->isNeutered()) { |
| 33 m_image.release(); | |
| 34 return; | |
| 35 } | |
| 36 | |
| 37 if (imageBitmap->isNeutered()) { | |
| 38 exceptionState.throwDOMException(InvalidStateError, | 36 exceptionState.throwDOMException(InvalidStateError, |
| 39 "The input ImageBitmap has been detached"); | 37 "The input ImageBitmap has been detached"); |
| 40 return; | 38 return; |
| 41 } | 39 } |
| 42 | 40 |
| 43 m_image = imageBitmap->bitmapImage(); | 41 m_imageLayerBridge->setImage(imageBitmap ? imageBitmap->bitmapImage() |
| 44 if (!m_image) | 42 : nullptr); |
| 45 return; | |
| 46 | 43 |
| 47 sk_sp<SkImage> skImage = m_image->imageForCurrentFrame(); | 44 didDraw(); |
| 48 if (skImage->isTextureBacked()) { | |
| 49 // TODO(junov): crbug.com/585607 Eliminate this readback and use an | |
| 50 // ExternalTextureLayer | |
| 51 sk_sp<SkSurface> surface = | |
| 52 SkSurface::MakeRasterN32Premul(skImage->width(), skImage->height()); | |
| 53 if (!surface) { | |
| 54 // silent failure | |
| 55 m_image.clear(); | |
| 56 return; | |
| 57 } | |
| 58 surface->getCanvas()->drawImage(skImage, 0, 0); | |
| 59 m_image = StaticBitmapImage::create(surface->makeImageSnapshot()); | |
| 60 } | |
| 61 didDraw(skImage->bounds()); | |
| 62 imageBitmap->close(); | |
| 63 } | |
| 64 | 45 |
| 65 bool ImageBitmapRenderingContext::paint(GraphicsContext& gc, const IntRect& r) { | 46 if (imageBitmap) |
| 66 if (!m_image) | 47 imageBitmap->close(); |
| 67 return true; | |
| 68 | |
| 69 // With impl-side painting, it is unsafe to use a gpu-backed SkImage | |
| 70 DCHECK(!m_image->imageForCurrentFrame()->isTextureBacked()); | |
| 71 gc.drawImage(m_image.get(), r, nullptr, creationAttributes().alpha() | |
| 72 ? SkBlendMode::kSrcOver | |
| 73 : SkBlendMode::kSrc); | |
| 74 | |
| 75 return true; | |
| 76 } | 48 } |
| 77 | 49 |
| 78 CanvasRenderingContext* ImageBitmapRenderingContext::Factory::create( | 50 CanvasRenderingContext* ImageBitmapRenderingContext::Factory::create( |
| 79 HTMLCanvasElement* canvas, | 51 HTMLCanvasElement* canvas, |
| 80 const CanvasContextCreationAttributes& attrs, | 52 const CanvasContextCreationAttributes& attrs, |
| 81 Document& document) { | 53 Document& document) { |
| 82 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled()) | 54 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled()) |
| 83 return nullptr; | 55 return nullptr; |
| 84 return new ImageBitmapRenderingContext(canvas, attrs, document); | 56 return new ImageBitmapRenderingContext(canvas, attrs, document); |
| 85 } | 57 } |
| 86 | 58 |
| 87 void ImageBitmapRenderingContext::stop() { | 59 void ImageBitmapRenderingContext::stop() { |
| 88 m_image.clear(); | 60 m_imageLayerBridge->dispose(); |
| 61 } |
| 62 |
| 63 PassRefPtr<Image> ImageBitmapRenderingContext::getImage(AccelerationHint, |
| 64 SnapshotReason) const { |
| 65 return m_imageLayerBridge->image(); |
| 66 } |
| 67 |
| 68 WebLayer* ImageBitmapRenderingContext::platformLayer() const { |
| 69 return m_imageLayerBridge->platformLayer(); |
| 70 } |
| 71 |
| 72 bool ImageBitmapRenderingContext::isPaintable() const { |
| 73 return !!m_imageLayerBridge->image(); |
| 74 } |
| 75 |
| 76 DEFINE_TRACE(ImageBitmapRenderingContext) { |
| 77 visitor->trace(m_imageLayerBridge); |
| 78 CanvasRenderingContext::trace(visitor); |
| 79 } |
| 80 |
| 81 bool ImageBitmapRenderingContext::isAccelerated() const { |
| 82 return m_imageLayerBridge->isAccelerated(); |
| 89 } | 83 } |
| 90 | 84 |
| 91 } // blink | 85 } // blink |
| OLD | NEW |