| 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/OffscreenCanvasRenderingContext2DOrWebGLRenderingC
ontextOrWebGL2RenderingContext.h" | 7 #include "bindings/modules/v8/OffscreenCanvasRenderingContext2DOrWebGLRenderingC
ontextOrWebGL2RenderingContext.h" |
| 8 #include "bindings/modules/v8/V8WebGLTexture.h" |
| 8 #include "core/dom/ExecutionContext.h" | 9 #include "core/dom/ExecutionContext.h" |
| 9 #include "core/frame/ImageBitmap.h" | 10 #include "core/frame/ImageBitmap.h" |
| 10 #include "core/frame/Settings.h" | 11 #include "core/frame/Settings.h" |
| 11 #include "core/workers/WorkerGlobalScope.h" | 12 #include "core/workers/WorkerGlobalScope.h" |
| 12 #include "core/workers/WorkerSettings.h" | 13 #include "core/workers/WorkerSettings.h" |
| 14 #include "modules/webgl/WebGLContextGroup.h" |
| 13 #include "platform/graphics/ImageBuffer.h" | 15 #include "platform/graphics/ImageBuffer.h" |
| 14 #include "platform/graphics/StaticBitmapImage.h" | 16 #include "platform/graphics/StaticBitmapImage.h" |
| 17 #include "platform/graphics/gpu/SharedGpuContext.h" |
| 18 #include "platform/graphics/gpu/TextureImageBufferSurface.h" |
| 15 #include "platform/graphics/paint/PaintCanvas.h" | 19 #include "platform/graphics/paint/PaintCanvas.h" |
| 16 #include "platform/wtf/Assertions.h" | 20 #include "platform/wtf/Assertions.h" |
| 17 #include "platform/wtf/CurrentTime.h" | 21 #include "platform/wtf/CurrentTime.h" |
| 22 #include "third_party/skia/include/gpu/GrContext.h" |
| 18 | 23 |
| 19 namespace blink { | 24 namespace blink { |
| 20 | 25 |
| 21 OffscreenCanvasRenderingContext2D::~OffscreenCanvasRenderingContext2D() {} | 26 OffscreenCanvasRenderingContext2D::~OffscreenCanvasRenderingContext2D() {} |
| 22 | 27 |
| 23 OffscreenCanvasRenderingContext2D::OffscreenCanvasRenderingContext2D( | 28 OffscreenCanvasRenderingContext2D::OffscreenCanvasRenderingContext2D( |
| 24 ScriptState* script_state, | 29 ScriptState* script_state, |
| 25 OffscreenCanvas* canvas, | 30 OffscreenCanvas* canvas, |
| 26 const CanvasContextCreationAttributes& attrs) | 31 const CanvasContextCreationAttributes& attrs) |
| 27 : CanvasRenderingContext(canvas, attrs) { | 32 : CanvasRenderingContext(canvas, attrs) { |
| 28 ExecutionContext* execution_context = ExecutionContext::From(script_state); | 33 ExecutionContext* execution_context = ExecutionContext::From(script_state); |
| 34 |
| 29 if (execution_context->IsDocument()) { | 35 if (execution_context->IsDocument()) { |
| 30 if (ToDocument(execution_context) | 36 if (ToDocument(execution_context) |
| 31 ->GetSettings() | 37 ->GetSettings() |
| 32 ->GetDisableReadingFromCanvas()) | 38 ->GetDisableReadingFromCanvas()) |
| 33 canvas->SetDisableReadingFromCanvasTrue(); | 39 canvas->SetDisableReadingFromCanvasTrue(); |
| 34 return; | 40 } else { |
| 41 WorkerSettings* worker_settings = |
| 42 ToWorkerGlobalScope(execution_context)->GetWorkerSettings(); |
| 43 if (worker_settings && worker_settings->DisableReadingFromCanvas()) |
| 44 canvas->SetDisableReadingFromCanvasTrue(); |
| 35 } | 45 } |
| 36 | 46 |
| 37 WorkerSettings* worker_settings = | 47 if (attrs.hasTexture()) { |
| 38 ToWorkerGlobalScope(execution_context)->GetWorkerSettings(); | 48 webgl_texture_ = V8WebGLTexture::toImplWithTypeCheck( |
| 39 if (worker_settings && worker_settings->DisableReadingFromCanvas()) | 49 attrs.texture().GetIsolate(), attrs.texture().V8Value()); |
| 40 canvas->SetDisableReadingFromCanvasTrue(); | 50 if (!webgl_texture_) { |
| 51 // TODO(fserb): exception_state.ThrowTypeError("member texture is not of |
| 52 // type WebGLTexture."); |
| 53 DVLOG(1) << "member texture is not of type WebGLTexture."; |
| 54 return; |
| 55 } |
| 56 DVLOG(1) << "OCRC2D with texture!"; |
| 57 GetImageBuffer(); |
| 58 } |
| 41 } | 59 } |
| 42 | 60 |
| 43 DEFINE_TRACE(OffscreenCanvasRenderingContext2D) { | 61 DEFINE_TRACE(OffscreenCanvasRenderingContext2D) { |
| 44 CanvasRenderingContext::Trace(visitor); | 62 CanvasRenderingContext::Trace(visitor); |
| 45 BaseRenderingContext2D::Trace(visitor); | 63 BaseRenderingContext2D::Trace(visitor); |
| 64 visitor->Trace(webgl_texture_); |
| 46 } | 65 } |
| 47 | 66 |
| 48 ScriptPromise OffscreenCanvasRenderingContext2D::commit( | 67 ScriptPromise OffscreenCanvasRenderingContext2D::commit( |
| 49 ScriptState* script_state, | 68 ScriptState* script_state, |
| 50 ExceptionState& exception_state) { | 69 ExceptionState& exception_state) { |
| 51 UseCounter::Feature feature = UseCounter::kOffscreenCanvasCommit2D; | 70 UseCounter::Feature feature = UseCounter::kOffscreenCanvasCommit2D; |
| 52 UseCounter::Count(ExecutionContext::From(script_state), feature); | 71 UseCounter::Count(ExecutionContext::From(script_state), feature); |
| 53 bool is_web_gl_software_rendering = false; | 72 bool is_web_gl_software_rendering = false; |
| 54 return host()->Commit(TransferToStaticBitmapImage(), | 73 return host()->Commit(TransferToStaticBitmapImage(), |
| 55 is_web_gl_software_rendering, script_state, | 74 is_web_gl_software_rendering, script_state, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 81 | 100 |
| 82 int OffscreenCanvasRenderingContext2D::Width() const { | 101 int OffscreenCanvasRenderingContext2D::Width() const { |
| 83 return host()->Size().Width(); | 102 return host()->Size().Width(); |
| 84 } | 103 } |
| 85 | 104 |
| 86 int OffscreenCanvasRenderingContext2D::Height() const { | 105 int OffscreenCanvasRenderingContext2D::Height() const { |
| 87 return host()->Size().Height(); | 106 return host()->Size().Height(); |
| 88 } | 107 } |
| 89 | 108 |
| 90 bool OffscreenCanvasRenderingContext2D::HasImageBuffer() const { | 109 bool OffscreenCanvasRenderingContext2D::HasImageBuffer() const { |
| 110 if (webgl_texture_) { |
| 111 return !!texture_image_buffer_; |
| 112 } |
| 91 return host()->GetImageBuffer(); | 113 return host()->GetImageBuffer(); |
| 92 } | 114 } |
| 93 | 115 |
| 94 void OffscreenCanvasRenderingContext2D::Reset() { | 116 void OffscreenCanvasRenderingContext2D::Reset() { |
| 95 host()->DiscardImageBuffer(); | 117 host()->DiscardImageBuffer(); |
| 96 BaseRenderingContext2D::Reset(); | 118 BaseRenderingContext2D::Reset(); |
| 97 } | 119 } |
| 98 | 120 |
| 99 ColorBehavior OffscreenCanvasRenderingContext2D::DrawImageColorBehavior() | 121 ColorBehavior OffscreenCanvasRenderingContext2D::DrawImageColorBehavior() |
| 100 const { | 122 const { |
| 101 return CanvasRenderingContext::ColorBehaviorForMediaDrawnToCanvas(); | 123 return CanvasRenderingContext::ColorBehaviorForMediaDrawnToCanvas(); |
| 102 } | 124 } |
| 103 | 125 |
| 104 ImageBuffer* OffscreenCanvasRenderingContext2D::GetImageBuffer() const { | 126 ImageBuffer* OffscreenCanvasRenderingContext2D::GetImageBuffer() const { |
| 127 if (webgl_texture_) { |
| 128 return const_cast<OffscreenCanvasRenderingContext2D*>(this) |
| 129 ->CreateTextureImageBuffer(); |
| 130 } |
| 105 return const_cast<CanvasRenderingContextHost*>(host()) | 131 return const_cast<CanvasRenderingContextHost*>(host()) |
| 106 ->GetOrCreateImageBuffer(); | 132 ->GetOrCreateImageBuffer(); |
| 107 } | 133 } |
| 108 | 134 |
| 135 ImageBuffer* OffscreenCanvasRenderingContext2D::CreateTextureImageBuffer() { |
| 136 DCHECK(webgl_texture_); |
| 137 if (!texture_image_buffer_) { |
| 138 OpacityMode opacity_mode = |
| 139 CreationAttributes().hasAlpha() ? kNonOpaque : kOpaque; |
| 140 |
| 141 std::unique_ptr<ImageBufferSurface> surface; |
| 142 surface.reset(new TextureImageBufferSurface( |
| 143 webgl_texture_->ContextGroup()->GetAGLInterface(), |
| 144 webgl_texture_->GetTarget(), webgl_texture_->Object(), host()->Size(), |
| 145 opacity_mode)); |
| 146 texture_image_buffer_ = ImageBuffer::Create(std::move(surface)); |
| 147 } |
| 148 |
| 149 return texture_image_buffer_.get(); |
| 150 } |
| 151 |
| 109 RefPtr<StaticBitmapImage> | 152 RefPtr<StaticBitmapImage> |
| 110 OffscreenCanvasRenderingContext2D::TransferToStaticBitmapImage() { | 153 OffscreenCanvasRenderingContext2D::TransferToStaticBitmapImage() { |
| 111 if (!GetImageBuffer()) | 154 if (!GetImageBuffer()) |
| 112 return nullptr; | 155 return nullptr; |
| 113 sk_sp<SkImage> sk_image = GetImageBuffer()->NewSkImageSnapshot( | 156 sk_sp<SkImage> sk_image = GetImageBuffer()->NewSkImageSnapshot( |
| 114 kPreferAcceleration, kSnapshotReasonTransferToImageBitmap); | 157 kPreferAcceleration, kSnapshotReasonTransferToImageBitmap); |
| 115 RefPtr<StaticBitmapImage> image = | 158 RefPtr<StaticBitmapImage> image = |
| 116 StaticBitmapImage::Create(std::move(sk_image)); | 159 StaticBitmapImage::Create(std::move(sk_image)); |
| 117 image->SetOriginClean(this->OriginClean()); | 160 image->SetOriginClean(this->OriginClean()); |
| 118 return image; | 161 return image; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 228 |
| 186 void OffscreenCanvasRenderingContext2D::DisableDeferral(DisableDeferralReason) { | 229 void OffscreenCanvasRenderingContext2D::DisableDeferral(DisableDeferralReason) { |
| 187 } | 230 } |
| 188 | 231 |
| 189 AffineTransform OffscreenCanvasRenderingContext2D::BaseTransform() const { | 232 AffineTransform OffscreenCanvasRenderingContext2D::BaseTransform() const { |
| 190 if (!HasImageBuffer()) | 233 if (!HasImageBuffer()) |
| 191 return AffineTransform(); // identity | 234 return AffineTransform(); // identity |
| 192 return GetImageBuffer()->BaseTransform(); | 235 return GetImageBuffer()->BaseTransform(); |
| 193 } | 236 } |
| 194 | 237 |
| 195 void OffscreenCanvasRenderingContext2D::DidDraw(const SkIRect& dirty_rect) {} | 238 void OffscreenCanvasRenderingContext2D::DidDraw(const SkIRect& dirty_rect) { |
| 239 GrContext* gr_context = SharedGpuContext::Gr(); |
| 240 if (texture_image_buffer_ && gr_context) { |
| 241 DVLOG(1) << "Flush"; |
| 242 gr_context->resetContext(); |
| 243 texture_image_buffer_->FlushGpu(kFlushReasonDrawImageOfWebGL); |
| 244 DVLOG(1) << "EndFlush"; |
| 245 } |
| 246 } |
| 196 | 247 |
| 197 bool OffscreenCanvasRenderingContext2D::StateHasFilter() { | 248 bool OffscreenCanvasRenderingContext2D::StateHasFilter() { |
| 198 return GetState().HasFilterForOffscreenCanvas(host()->Size()); | 249 return GetState().HasFilterForOffscreenCanvas(host()->Size()); |
| 199 } | 250 } |
| 200 | 251 |
| 201 sk_sp<SkImageFilter> OffscreenCanvasRenderingContext2D::StateGetFilter() { | 252 sk_sp<SkImageFilter> OffscreenCanvasRenderingContext2D::StateGetFilter() { |
| 202 return GetState().GetFilterForOffscreenCanvas(host()->Size()); | 253 return GetState().GetFilterForOffscreenCanvas(host()->Size()); |
| 203 } | 254 } |
| 204 | 255 |
| 205 void OffscreenCanvasRenderingContext2D::ValidateStateStack() const { | 256 void OffscreenCanvasRenderingContext2D::ValidateStateStack() const { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 228 } | 279 } |
| 229 | 280 |
| 230 CanvasPixelFormat OffscreenCanvasRenderingContext2D::PixelFormat() const { | 281 CanvasPixelFormat OffscreenCanvasRenderingContext2D::PixelFormat() const { |
| 231 return color_params().pixel_format(); | 282 return color_params().pixel_format(); |
| 232 } | 283 } |
| 233 | 284 |
| 234 bool OffscreenCanvasRenderingContext2D::IsAccelerated() const { | 285 bool OffscreenCanvasRenderingContext2D::IsAccelerated() const { |
| 235 return HasImageBuffer() && GetImageBuffer()->IsAccelerated(); | 286 return HasImageBuffer() && GetImageBuffer()->IsAccelerated(); |
| 236 } | 287 } |
| 237 } | 288 } |
| OLD | NEW |