| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "core/offscreencanvas/OffscreenCanvas.h" | 5 #include "core/offscreencanvas/OffscreenCanvas.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include "core/dom/ExceptionCode.h" | 8 #include "core/dom/ExceptionCode.h" |
| 9 #include "core/dom/ExecutionContext.h" | 9 #include "core/dom/ExecutionContext.h" |
| 10 #include "core/fileapi/Blob.h" | 10 #include "core/fileapi/Blob.h" |
| 11 #include "core/frame/ImageBitmap.h" | 11 #include "core/frame/ImageBitmap.h" |
| 12 #include "core/html/ImageData.h" | 12 #include "core/html/ImageData.h" |
| 13 #include "core/html/canvas/CanvasAsyncBlobCreator.h" | 13 #include "core/html/canvas/CanvasAsyncBlobCreator.h" |
| 14 #include "core/html/canvas/CanvasContextCreationAttributes.h" | 14 #include "core/html/canvas/CanvasContextCreationAttributes.h" |
| 15 #include "core/html/canvas/CanvasRenderingContext.h" | 15 #include "core/html/canvas/CanvasRenderingContext.h" |
| 16 #include "core/html/canvas/CanvasRenderingContextFactory.h" | 16 #include "core/html/canvas/CanvasRenderingContextFactory.h" |
| 17 #include "platform/graphics/Image.h" | 17 #include "platform/graphics/Image.h" |
| 18 #include "platform/graphics/ImageBuffer.h" | 18 #include "platform/graphics/ImageBuffer.h" |
| 19 #include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h" | 19 #include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h" |
| 20 #include "platform/graphics/StaticBitmapImage.h" | 20 #include "platform/graphics/StaticBitmapImage.h" |
| 21 #include "platform/graphics/UnacceleratedImageBufferSurface.h" |
| 22 #include "platform/graphics/gpu/AcceleratedImageBufferSurface.h" |
| 21 #include "platform/image-encoders/ImageEncoderUtils.h" | 23 #include "platform/image-encoders/ImageEncoderUtils.h" |
| 22 #include "platform/instrumentation/tracing/TraceEvent.h" | 24 #include "platform/instrumentation/tracing/TraceEvent.h" |
| 23 #include "platform/wtf/MathExtras.h" | 25 #include "platform/wtf/MathExtras.h" |
| 24 #include "public/platform/Platform.h" | 26 #include "public/platform/Platform.h" |
| 25 #include "third_party/skia/include/core/SkSurface.h" | 27 #include "third_party/skia/include/core/SkSurface.h" |
| 26 | 28 |
| 27 namespace blink { | 29 namespace blink { |
| 28 | 30 |
| 29 OffscreenCanvas::OffscreenCanvas(const IntSize& size) : size_(size) {} | 31 OffscreenCanvas::OffscreenCanvas(const IntSize& size) : size_(size) {} |
| 30 | 32 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // Undocumented exception (not in spec) | 104 // Undocumented exception (not in spec) |
| 103 exception_state.ThrowDOMException(kV8Error, "Out of memory"); | 105 exception_state.ThrowDOMException(kV8Error, "Out of memory"); |
| 104 } | 106 } |
| 105 return image; | 107 return image; |
| 106 } | 108 } |
| 107 | 109 |
| 108 PassRefPtr<Image> OffscreenCanvas::GetSourceImageForCanvas( | 110 PassRefPtr<Image> OffscreenCanvas::GetSourceImageForCanvas( |
| 109 SourceImageStatus* status, | 111 SourceImageStatus* status, |
| 110 AccelerationHint hint, | 112 AccelerationHint hint, |
| 111 SnapshotReason reason, | 113 SnapshotReason reason, |
| 112 const FloatSize& size) const { | 114 const FloatSize& size) { |
| 113 if (!context_) { | 115 if (!context_) { |
| 114 *status = kInvalidSourceImageStatus; | 116 *status = kInvalidSourceImageStatus; |
| 115 sk_sp<SkSurface> surface = | 117 sk_sp<SkSurface> surface = |
| 116 SkSurface::MakeRasterN32Premul(size_.Width(), size_.Height()); | 118 SkSurface::MakeRasterN32Premul(size_.Width(), size_.Height()); |
| 117 return surface ? StaticBitmapImage::Create(surface->makeImageSnapshot()) | 119 return surface ? StaticBitmapImage::Create(surface->makeImageSnapshot()) |
| 118 : nullptr; | 120 : nullptr; |
| 119 } | 121 } |
| 120 if (!size.Width() || !size.Height()) { | 122 if (!size.Width() || !size.Height()) { |
| 121 *status = kZeroSizeCanvasSourceImageStatus; | 123 *status = kZeroSizeCanvasSourceImageStatus; |
| 122 return nullptr; | 124 return nullptr; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 // The frame dispatcher connects the current thread of OffscreenCanvas | 232 // The frame dispatcher connects the current thread of OffscreenCanvas |
| 231 // (either main or worker) to the browser process and remains unchanged | 233 // (either main or worker) to the browser process and remains unchanged |
| 232 // throughout the lifetime of this OffscreenCanvas. | 234 // throughout the lifetime of this OffscreenCanvas. |
| 233 frame_dispatcher_ = WTF::WrapUnique(new OffscreenCanvasFrameDispatcherImpl( | 235 frame_dispatcher_ = WTF::WrapUnique(new OffscreenCanvasFrameDispatcherImpl( |
| 234 this, client_id_, sink_id_, placeholder_canvas_id_, size_.Width(), | 236 this, client_id_, sink_id_, placeholder_canvas_id_, size_.Width(), |
| 235 size_.Height())); | 237 size_.Height())); |
| 236 } | 238 } |
| 237 return frame_dispatcher_.get(); | 239 return frame_dispatcher_.get(); |
| 238 } | 240 } |
| 239 | 241 |
| 242 void OffscreenCanvas::DiscardImageBuffer() { |
| 243 image_buffer_.reset(); |
| 244 needs_matrix_clip_restore_ = true; |
| 245 } |
| 246 |
| 247 ImageBuffer* OffscreenCanvas::GetOrCreateImageBuffer() { |
| 248 if (!image_buffer_) { |
| 249 IntSize surface_size(width(), height()); |
| 250 OpacityMode opacity_mode = |
| 251 context_->CreationAttributes().hasAlpha() ? kNonOpaque : kOpaque; |
| 252 std::unique_ptr<ImageBufferSurface> surface; |
| 253 if (RuntimeEnabledFeatures::accelerated2dCanvasEnabled()) { |
| 254 surface.reset( |
| 255 new AcceleratedImageBufferSurface(surface_size, opacity_mode)); |
| 256 } |
| 257 |
| 258 if (!surface || !surface->IsValid()) { |
| 259 surface.reset(new UnacceleratedImageBufferSurface( |
| 260 surface_size, opacity_mode, kInitializeImagePixels)); |
| 261 } |
| 262 |
| 263 image_buffer_ = ImageBuffer::Create(std::move(surface)); |
| 264 |
| 265 if (needs_matrix_clip_restore_) { |
| 266 needs_matrix_clip_restore_ = false; |
| 267 context_->RestoreCanvasMatrixClipStack(image_buffer_->Canvas()); |
| 268 } |
| 269 } |
| 270 |
| 271 return image_buffer_.get(); |
| 272 } |
| 273 |
| 240 ScriptPromise OffscreenCanvas::Commit(RefPtr<StaticBitmapImage> image, | 274 ScriptPromise OffscreenCanvas::Commit(RefPtr<StaticBitmapImage> image, |
| 241 bool is_web_gl_software_rendering, | 275 bool is_web_gl_software_rendering, |
| 242 ScriptState* script_state, | 276 ScriptState* script_state, |
| 243 ExceptionState& exception_state) { | 277 ExceptionState& exception_state) { |
| 244 TRACE_EVENT0("blink", "OffscreenCanvas::Commit"); | 278 TRACE_EVENT0("blink", "OffscreenCanvas::Commit"); |
| 245 | 279 |
| 246 if (!HasPlaceholderCanvas()) { | 280 if (!HasPlaceholderCanvas()) { |
| 247 exception_state.ThrowDOMException( | 281 exception_state.ThrowDOMException( |
| 248 kInvalidStateError, | 282 kInvalidStateError, |
| 249 "Commit() was called on a context whose " | 283 "Commit() was called on a context whose " |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 } | 402 } |
| 369 | 403 |
| 370 DEFINE_TRACE(OffscreenCanvas) { | 404 DEFINE_TRACE(OffscreenCanvas) { |
| 371 visitor->Trace(context_); | 405 visitor->Trace(context_); |
| 372 visitor->Trace(execution_context_); | 406 visitor->Trace(execution_context_); |
| 373 visitor->Trace(commit_promise_resolver_); | 407 visitor->Trace(commit_promise_resolver_); |
| 374 EventTargetWithInlineData::Trace(visitor); | 408 EventTargetWithInlineData::Trace(visitor); |
| 375 } | 409 } |
| 376 | 410 |
| 377 } // namespace blink | 411 } // namespace blink |
| OLD | NEW |