| 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/offscreencanvas2d/OffscreenCanvasRenderingContext2D.h" | 5 #include "modules/offscreencanvas2d/OffscreenCanvasRenderingContext2D.h" |
| 6 | 6 |
| 7 #include "bindings/modules/v8/UnionTypesModules.h" | 7 #include "bindings/modules/v8/UnionTypesModules.h" |
| 8 #include "core/frame/ImageBitmap.h" |
| 8 #include "platform/NotImplemented.h" | 9 #include "platform/NotImplemented.h" |
| 10 #include "platform/graphics/ImageBuffer.h" |
| 11 #include "platform/graphics/StaticBitmapImage.h" |
| 9 #include "wtf/Assertions.h" | 12 #include "wtf/Assertions.h" |
| 10 | 13 |
| 11 #define UNIMPLEMENTED ASSERT_NOT_REACHED | 14 #define UNIMPLEMENTED ASSERT_NOT_REACHED |
| 12 | 15 |
| 13 namespace blink { | 16 namespace blink { |
| 14 | 17 |
| 15 OffscreenCanvasRenderingContext2D::~OffscreenCanvasRenderingContext2D() | 18 OffscreenCanvasRenderingContext2D::~OffscreenCanvasRenderingContext2D() |
| 16 { | 19 { |
| 17 } | 20 } |
| 18 | 21 |
| 19 OffscreenCanvasRenderingContext2D::OffscreenCanvasRenderingContext2D(OffscreenCa
nvas* canvas, const CanvasContextCreationAttributes& attrs) | 22 OffscreenCanvasRenderingContext2D::OffscreenCanvasRenderingContext2D(OffscreenCa
nvas* canvas, const CanvasContextCreationAttributes& attrs) |
| 20 : OffscreenCanvasRenderingContext(canvas) | 23 : OffscreenCanvasRenderingContext(canvas) |
| 21 , m_hasAlpha(attrs.alpha()) | 24 , m_hasAlpha(attrs.alpha()) |
| 22 { | 25 { |
| 23 } | 26 } |
| 24 | 27 |
| 25 DEFINE_TRACE(OffscreenCanvasRenderingContext2D) | 28 DEFINE_TRACE(OffscreenCanvasRenderingContext2D) |
| 26 { | 29 { |
| 27 OffscreenCanvasRenderingContext::trace(visitor); | 30 OffscreenCanvasRenderingContext::trace(visitor); |
| 28 BaseRenderingContext2D::trace(visitor); | 31 BaseRenderingContext2D::trace(visitor); |
| 29 } | 32 } |
| 30 | 33 |
| 31 // BaseRenderingContext2D implementation | 34 // BaseRenderingContext2D implementation |
| 32 bool OffscreenCanvasRenderingContext2D::originClean() const | 35 bool OffscreenCanvasRenderingContext2D::originClean() const |
| 33 { | 36 { |
| 34 notImplemented(); | 37 return m_originClean; |
| 35 return true; | |
| 36 } | 38 } |
| 37 | 39 |
| 38 void OffscreenCanvasRenderingContext2D::setOriginTainted() | 40 void OffscreenCanvasRenderingContext2D::setOriginTainted() |
| 39 { | 41 { |
| 40 notImplemented(); | 42 m_originClean = false; |
| 41 } | 43 } |
| 42 | 44 |
| 43 bool OffscreenCanvasRenderingContext2D::wouldTaintOrigin(CanvasImageSource* sour
ce) | 45 bool OffscreenCanvasRenderingContext2D::wouldTaintOrigin(CanvasImageSource* sour
ce) |
| 44 { | 46 { |
| 45 notImplemented(); | 47 notImplemented(); |
| 46 return false; | 48 return false; |
| 47 } | 49 } |
| 48 | 50 |
| 49 int OffscreenCanvasRenderingContext2D::width() const | 51 int OffscreenCanvasRenderingContext2D::width() const |
| 50 { | 52 { |
| 53 return getOffscreenCanvas()->width(); |
| 54 } |
| 55 |
| 56 int OffscreenCanvasRenderingContext2D::height() const |
| 57 { |
| 51 return getOffscreenCanvas()->height(); | 58 return getOffscreenCanvas()->height(); |
| 52 } | 59 } |
| 53 | 60 |
| 54 int OffscreenCanvasRenderingContext2D::height() const | |
| 55 { | |
| 56 return getOffscreenCanvas()->width(); | |
| 57 } | |
| 58 | |
| 59 bool OffscreenCanvasRenderingContext2D::hasImageBuffer() const | 61 bool OffscreenCanvasRenderingContext2D::hasImageBuffer() const |
| 60 { | 62 { |
| 61 notImplemented(); | 63 return !!m_imageBuffer; |
| 62 return false; | |
| 63 } | 64 } |
| 64 | 65 |
| 65 ImageBuffer* OffscreenCanvasRenderingContext2D::imageBuffer() const | 66 ImageBuffer* OffscreenCanvasRenderingContext2D::imageBuffer() const |
| 66 { | 67 { |
| 67 notImplemented(); | 68 if (!m_imageBuffer) { |
| 68 return nullptr; | 69 // TODO: crbug.com/593514 Add support for GPU rendering |
| 70 OffscreenCanvasRenderingContext2D* nonConstThis = const_cast<OffscreenCa
nvasRenderingContext2D*>(this); |
| 71 nonConstThis->m_imageBuffer = ImageBuffer::create(IntSize(width(), heigh
t()), m_hasAlpha ? NonOpaque : Opaque, InitializeImagePixels); |
| 72 // TODO: crbug.com/593349 Restore matrix and clip state on the new Image
Buffer. |
| 73 } |
| 74 |
| 75 return m_imageBuffer.get(); |
| 69 } | 76 } |
| 70 | 77 |
| 71 bool OffscreenCanvasRenderingContext2D::parseColorOrCurrentColor(Color&, const S
tring& colorString) const | 78 PassRefPtrWillBeRawPtr<ImageBitmap> OffscreenCanvasRenderingContext2D::transferT
oImageBitmap(ExceptionState& exceptionState) |
| 72 { | 79 { |
| 73 notImplemented(); | 80 if (!imageBuffer()) |
| 74 return false; | 81 return nullptr; |
| 82 // TODO: crbug.com/593514 Add support for GPU rendering |
| 83 RefPtr<SkImage> skImage = m_imageBuffer->newSkImageSnapshot(PreferNoAccelera
tion, SnapshotReasonUnknown); |
| 84 RefPtr<StaticBitmapImage> image = StaticBitmapImage::create(skImage.release(
)); |
| 85 m_imageBuffer.clear(); // "Transfer" means no retained buffer |
| 86 return ImageBitmap::create(image.release()); |
| 87 } |
| 88 |
| 89 bool OffscreenCanvasRenderingContext2D::parseColorOrCurrentColor(Color& color, c
onst String& colorString) const |
| 90 { |
| 91 return ::blink::parseColorOrCurrentColor(color, colorString, nullptr); |
| 75 } | 92 } |
| 76 | 93 |
| 77 SkCanvas* OffscreenCanvasRenderingContext2D::drawingCanvas() const | 94 SkCanvas* OffscreenCanvasRenderingContext2D::drawingCanvas() const |
| 78 { | 95 { |
| 79 notImplemented(); | 96 ImageBuffer* buffer = imageBuffer(); |
| 80 return nullptr; | 97 if (!buffer) |
| 98 return nullptr; |
| 99 return imageBuffer()->canvas(); |
| 81 } | 100 } |
| 82 | 101 |
| 83 SkCanvas* OffscreenCanvasRenderingContext2D::existingDrawingCanvas() const | 102 SkCanvas* OffscreenCanvasRenderingContext2D::existingDrawingCanvas() const |
| 84 { | 103 { |
| 85 notImplemented(); | 104 if (!m_imageBuffer) |
| 86 return nullptr; | 105 return nullptr; |
| 106 return m_imageBuffer->canvas(); |
| 87 } | 107 } |
| 88 | 108 |
| 89 void OffscreenCanvasRenderingContext2D::disableDeferral(DisableDeferralReason) | 109 void OffscreenCanvasRenderingContext2D::disableDeferral(DisableDeferralReason) |
| 90 { | 110 { } |
| 91 notImplemented(); | |
| 92 } | |
| 93 | 111 |
| 94 AffineTransform OffscreenCanvasRenderingContext2D::baseTransform() const | 112 AffineTransform OffscreenCanvasRenderingContext2D::baseTransform() const |
| 95 { | 113 { |
| 96 notImplemented(); | 114 if (!m_imageBuffer) |
| 97 return 0; | 115 return AffineTransform(); // identity |
| 116 return m_imageBuffer->baseTransform(); |
| 98 } | 117 } |
| 99 | 118 |
| 100 void OffscreenCanvasRenderingContext2D::didDraw(const SkIRect& dirtyRect) | 119 void OffscreenCanvasRenderingContext2D::didDraw(const SkIRect& dirtyRect) |
| 101 { | 120 { } |
| 102 notImplemented(); | |
| 103 } | |
| 104 | 121 |
| 105 bool OffscreenCanvasRenderingContext2D::stateHasFilter() | 122 bool OffscreenCanvasRenderingContext2D::stateHasFilter() |
| 106 { | 123 { |
| 107 notImplemented(); | 124 // TODO: crbug.com/593838 make hasFilter accept nullptr |
| 125 // return state().hasFilter(nullptr, nullptr, IntSize(width(), height()), th
is); |
| 108 return false; | 126 return false; |
| 109 } | 127 } |
| 110 | 128 |
| 111 SkImageFilter* OffscreenCanvasRenderingContext2D::stateGetFilter() | 129 SkImageFilter* OffscreenCanvasRenderingContext2D::stateGetFilter() |
| 112 { | 130 { |
| 113 notImplemented(); | 131 // TODO: make getFilter accept nullptr |
| 132 // return state().getFilter(nullptr, nullptr, IntSize(width(), height()), th
is); |
| 114 return nullptr; | 133 return nullptr; |
| 115 } | 134 } |
| 116 | 135 |
| 117 void OffscreenCanvasRenderingContext2D::validateStateStack() | 136 void OffscreenCanvasRenderingContext2D::validateStateStack() |
| 118 { | 137 { |
| 119 notImplemented(); | 138 #if ENABLE(ASSERT) |
| 139 SkCanvas* skCanvas = existingDrawingCanvas(); |
| 140 if (skCanvas) { |
| 141 ASSERT(static_cast<size_t>(skCanvas->getSaveCount()) == m_stateStack.siz
e()); |
| 142 } |
| 143 #endif |
| 120 } | 144 } |
| 121 | 145 |
| 122 bool OffscreenCanvasRenderingContext2D::isContextLost() const | 146 bool OffscreenCanvasRenderingContext2D::isContextLost() const |
| 123 { | 147 { |
| 124 notImplemented(); | |
| 125 return false; | 148 return false; |
| 126 } | 149 } |
| 127 | 150 |
| 128 } | 151 } |
| OLD | NEW |