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 // TODO(ccameron): Determine the correct color behavior here. | 44 didDraw(); |
xlai (Olivia)
2017/03/09 21:57:27
I think you accidentally took away this TODO comme
Justin Novosad
2017/03/10 21:09:59
Don't worry, ccameron removed this concurently in
| |
48 // ImageBitmapRenderingContext. | |
49 // https://crbug.com/672306 | |
50 sk_sp<SkImage> skImage = | |
51 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()); | |
52 if (skImage->isTextureBacked()) { | |
53 // TODO(junov): crbug.com/585607 Eliminate this readback and use an | |
54 // ExternalTextureLayer | |
55 sk_sp<SkSurface> surface = | |
56 SkSurface::MakeRasterN32Premul(skImage->width(), skImage->height()); | |
57 if (!surface) { | |
58 // silent failure | |
59 m_image.clear(); | |
60 return; | |
61 } | |
62 surface->getCanvas()->drawImage(skImage, 0, 0); | |
63 m_image = StaticBitmapImage::create(surface->makeImageSnapshot()); | |
64 } | |
65 didDraw(skImage->bounds()); | |
66 imageBitmap->close(); | |
67 } | |
68 | 45 |
69 bool ImageBitmapRenderingContext::paint(GraphicsContext& gc, const IntRect& r) { | 46 if (imageBitmap) |
70 if (!m_image) | 47 imageBitmap->close(); |
71 return true; | |
72 | |
73 // With impl-side painting, it is unsafe to use a gpu-backed SkImage | |
74 DCHECK( | |
75 !m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()) | |
76 ->isTextureBacked()); | |
77 gc.drawImage(m_image.get(), r, nullptr, creationAttributes().alpha() | |
78 ? SkBlendMode::kSrcOver | |
79 : SkBlendMode::kSrc); | |
80 | |
81 return true; | |
82 } | 48 } |
83 | 49 |
84 CanvasRenderingContext* ImageBitmapRenderingContext::Factory::create( | 50 CanvasRenderingContext* ImageBitmapRenderingContext::Factory::create( |
85 HTMLCanvasElement* canvas, | 51 HTMLCanvasElement* canvas, |
86 const CanvasContextCreationAttributes& attrs, | 52 const CanvasContextCreationAttributes& attrs, |
87 Document& document) { | 53 Document& document) { |
88 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled()) | 54 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled()) |
89 return nullptr; | 55 return nullptr; |
90 return new ImageBitmapRenderingContext(canvas, attrs, document); | 56 return new ImageBitmapRenderingContext(canvas, attrs, document); |
91 } | 57 } |
92 | 58 |
93 void ImageBitmapRenderingContext::stop() { | 59 void ImageBitmapRenderingContext::stop() { |
94 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(); | |
95 } | 83 } |
96 | 84 |
97 } // blink | 85 } // blink |
OLD | NEW |