| 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 "third_party/skia/include/core/SkImage.h" | 11 #include "third_party/skia/include/core/SkImage.h" |
| 12 #include "third_party/skia/include/core/SkSurface.h" | 12 #include "third_party/skia/include/core/SkSurface.h" |
| 13 | 13 |
| 14 namespace blink { | 14 namespace blink { |
| 15 | 15 |
| 16 ImageBitmapRenderingContext::ImageBitmapRenderingContext( | 16 ImageBitmapRenderingContext::ImageBitmapRenderingContext( |
| 17 HTMLCanvasElement* canvas, | 17 HTMLCanvasElement* canvas, |
| 18 const CanvasContextCreationAttributes& attrs, | 18 const CanvasContextCreationAttributes& attrs, |
| 19 Document& document) | 19 Document& document) |
| 20 : CanvasRenderingContext(canvas, nullptr, attrs) {} | 20 : CanvasRenderingContext(canvas, nullptr, attrs) {} |
| 21 | 21 |
| 22 ImageBitmapRenderingContext::~ImageBitmapRenderingContext() {} | 22 ImageBitmapRenderingContext::~ImageBitmapRenderingContext() {} |
| 23 | 23 |
| 24 void ImageBitmapRenderingContext::setCanvasGetContextResult( | 24 void ImageBitmapRenderingContext::setCanvasGetContextResult( |
| 25 RenderingContext& result) { | 25 RenderingContext& result) { |
| 26 result.setImageBitmapRenderingContext(this); | 26 result.setImageBitmapRenderingContext(this); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void ImageBitmapRenderingContext::transferFromImageBitmap( | 29 void ImageBitmapRenderingContext::transferFromImageBitmap( |
| 30 ImageBitmap* imageBitmap, | 30 ImageBitmap* imageBitmap) { |
| 31 ExceptionState& exceptionState) { | |
| 32 if (!imageBitmap) { | 31 if (!imageBitmap) { |
| 33 m_image.release(); | 32 m_image.release(); |
| 34 return; | 33 return; |
| 35 } | 34 } |
| 36 | 35 |
| 37 if (imageBitmap->isNeutered()) { | |
| 38 exceptionState.throwDOMException(InvalidStateError, | |
| 39 "The input ImageBitmap has been detached"); | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 m_image = imageBitmap->bitmapImage(); | 36 m_image = imageBitmap->bitmapImage(); |
| 44 if (!m_image) | 37 if (!m_image) |
| 45 return; | 38 return; |
| 46 | 39 |
| 47 sk_sp<SkImage> skImage = m_image->imageForCurrentFrame(); | 40 sk_sp<SkImage> skImage = m_image->imageForCurrentFrame(); |
| 48 if (skImage->isTextureBacked()) { | 41 if (skImage->isTextureBacked()) { |
| 49 // TODO(junov): crbug.com/585607 Eliminate this readback and use an | 42 // TODO(junov): crbug.com/585607 Eliminate this readback and use an |
| 50 // ExternalTextureLayer | 43 // ExternalTextureLayer |
| 51 sk_sp<SkSurface> surface = | 44 sk_sp<SkSurface> surface = |
| 52 SkSurface::MakeRasterN32Premul(skImage->width(), skImage->height()); | 45 SkSurface::MakeRasterN32Premul(skImage->width(), skImage->height()); |
| 53 if (!surface) { | 46 if (!surface) { |
| 54 // silent failure | 47 // silent failure |
| 55 m_image.clear(); | 48 m_image.clear(); |
| 56 return; | 49 return; |
| 57 } | 50 } |
| 58 surface->getCanvas()->drawImage(skImage, 0, 0); | 51 surface->getCanvas()->drawImage(skImage, 0, 0); |
| 59 m_image = StaticBitmapImage::create(surface->makeImageSnapshot()); | 52 m_image = StaticBitmapImage::create(surface->makeImageSnapshot()); |
| 60 } | 53 } |
| 61 canvas()->didDraw( | 54 canvas()->didDraw( |
| 62 FloatRect(FloatPoint(), FloatSize(m_image->width(), m_image->height()))); | 55 FloatRect(FloatPoint(), FloatSize(m_image->width(), m_image->height()))); |
| 63 imageBitmap->close(); | |
| 64 } | 56 } |
| 65 | 57 |
| 66 bool ImageBitmapRenderingContext::paint(GraphicsContext& gc, const IntRect& r) { | 58 bool ImageBitmapRenderingContext::paint(GraphicsContext& gc, const IntRect& r) { |
| 67 if (!m_image) | 59 if (!m_image) |
| 68 return true; | 60 return true; |
| 69 | 61 |
| 70 // With impl-side painting, it is unsafe to use a gpu-backed SkImage | 62 // With impl-side painting, it is unsafe to use a gpu-backed SkImage |
| 71 ASSERT(!m_image->imageForCurrentFrame()->isTextureBacked()); | 63 ASSERT(!m_image->imageForCurrentFrame()->isTextureBacked()); |
| 72 gc.drawImage(m_image.get(), r, nullptr, creationAttributes().alpha() | 64 gc.drawImage(m_image.get(), r, nullptr, creationAttributes().alpha() |
| 73 ? SkXfermode::kSrcOver_Mode | 65 ? SkXfermode::kSrcOver_Mode |
| 74 : SkXfermode::kSrc_Mode); | 66 : SkXfermode::kSrc_Mode); |
| 75 | 67 |
| 76 return true; | 68 return true; |
| 77 } | 69 } |
| 78 | 70 |
| 79 CanvasRenderingContext* ImageBitmapRenderingContext::Factory::create( | 71 CanvasRenderingContext* ImageBitmapRenderingContext::Factory::create( |
| 80 HTMLCanvasElement* canvas, | 72 HTMLCanvasElement* canvas, |
| 81 const CanvasContextCreationAttributes& attrs, | 73 const CanvasContextCreationAttributes& attrs, |
| 82 Document& document) { | 74 Document& document) { |
| 83 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled()) | 75 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled()) |
| 84 return nullptr; | 76 return nullptr; |
| 85 return new ImageBitmapRenderingContext(canvas, attrs, document); | 77 return new ImageBitmapRenderingContext(canvas, attrs, document); |
| 86 } | 78 } |
| 87 | 79 |
| 88 void ImageBitmapRenderingContext::stop() { | 80 void ImageBitmapRenderingContext::stop() { |
| 89 m_image.clear(); | 81 m_image.clear(); |
| 90 } | 82 } |
| 91 | 83 |
| 92 } // blink | 84 } // blink |
| OLD | NEW |