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 "core/dom/ExceptionCode.h" | 7 #include "core/dom/ExceptionCode.h" |
8 #include "core/fileapi/Blob.h" | 8 #include "core/fileapi/Blob.h" |
9 #include "core/frame/ImageBitmap.h" | 9 #include "core/frame/ImageBitmap.h" |
10 #include "core/html/ImageData.h" | 10 #include "core/html/ImageData.h" |
11 #include "core/html/canvas/CanvasAsyncBlobCreator.h" | 11 #include "core/html/canvas/CanvasAsyncBlobCreator.h" |
12 #include "core/html/canvas/CanvasContextCreationAttributes.h" | 12 #include "core/html/canvas/CanvasContextCreationAttributes.h" |
13 #include "core/html/canvas/CanvasRenderingContext.h" | 13 #include "core/html/canvas/CanvasRenderingContext.h" |
14 #include "core/html/canvas/CanvasRenderingContextFactory.h" | 14 #include "core/html/canvas/CanvasRenderingContextFactory.h" |
15 #include "platform/graphics/Image.h" | 15 #include "platform/graphics/Image.h" |
16 #include "platform/graphics/ImageBuffer.h" | 16 #include "platform/graphics/ImageBuffer.h" |
17 #include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h" | 17 #include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h" |
18 #include "platform/graphics/StaticBitmapImage.h" | 18 #include "platform/graphics/StaticBitmapImage.h" |
19 #include "platform/image-encoders/ImageEncoderUtils.h" | 19 #include "platform/image-encoders/ImageEncoderUtils.h" |
20 #include "wtf/MathExtras.h" | 20 #include "wtf/MathExtras.h" |
21 #include <memory> | 21 #include <memory> |
22 | 22 |
23 namespace blink { | 23 namespace blink { |
24 | 24 |
25 OffscreenCanvas::OffscreenCanvas(const IntSize& size) | 25 OffscreenCanvas::OffscreenCanvas(const IntSize& size) : m_size(size) {} |
26 : m_size(size), m_originClean(true) {} | |
27 | 26 |
28 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) { | 27 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) { |
29 return new OffscreenCanvas( | 28 return new OffscreenCanvas( |
30 IntSize(clampTo<int>(width), clampTo<int>(height))); | 29 IntSize(clampTo<int>(width), clampTo<int>(height))); |
31 } | 30 } |
32 | 31 |
| 32 OffscreenCanvas::~OffscreenCanvas() {} |
| 33 |
| 34 void OffscreenCanvas::dispose() { |
| 35 if (m_context) { |
| 36 m_context->detachOffscreenCanvas(); |
| 37 m_context = nullptr; |
| 38 } |
| 39 if (m_commitPromiseResolver) { |
| 40 // keepAliveWhilePending() guarantees the promise resolver is never |
| 41 // GC-ed before the OffscreenCanvas |
| 42 m_commitPromiseResolver->reject(); |
| 43 m_commitPromiseResolver.clear(); |
| 44 } |
| 45 } |
| 46 |
33 void OffscreenCanvas::setWidth(unsigned width) { | 47 void OffscreenCanvas::setWidth(unsigned width) { |
34 IntSize newSize = m_size; | 48 IntSize newSize = m_size; |
35 newSize.setWidth(clampTo<int>(width)); | 49 newSize.setWidth(clampTo<int>(width)); |
36 setSize(newSize); | 50 setSize(newSize); |
37 } | 51 } |
38 | 52 |
39 void OffscreenCanvas::setHeight(unsigned height) { | 53 void OffscreenCanvas::setHeight(unsigned height) { |
40 IntSize newSize = m_size; | 54 IntSize newSize = m_size; |
41 newSize.setHeight(clampTo<int>(height)); | 55 newSize.setHeight(clampTo<int>(height)); |
42 setSize(newSize); | 56 setSize(newSize); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 bool OffscreenCanvas::isAccelerated() const { | 220 bool OffscreenCanvas::isAccelerated() const { |
207 return m_context && m_context->isAccelerated(); | 221 return m_context && m_context->isAccelerated(); |
208 } | 222 } |
209 | 223 |
210 OffscreenCanvasFrameDispatcher* OffscreenCanvas::getOrCreateFrameDispatcher() { | 224 OffscreenCanvasFrameDispatcher* OffscreenCanvas::getOrCreateFrameDispatcher() { |
211 if (!m_frameDispatcher) { | 225 if (!m_frameDispatcher) { |
212 // The frame dispatcher connects the current thread of OffscreenCanvas | 226 // The frame dispatcher connects the current thread of OffscreenCanvas |
213 // (either main or worker) to the browser process and remains unchanged | 227 // (either main or worker) to the browser process and remains unchanged |
214 // throughout the lifetime of this OffscreenCanvas. | 228 // throughout the lifetime of this OffscreenCanvas. |
215 m_frameDispatcher = WTF::wrapUnique(new OffscreenCanvasFrameDispatcherImpl( | 229 m_frameDispatcher = WTF::wrapUnique(new OffscreenCanvasFrameDispatcherImpl( |
216 m_clientId, m_sinkId, m_placeholderCanvasId, m_size.width(), | 230 this, m_clientId, m_sinkId, m_placeholderCanvasId, m_size.width(), |
217 m_size.height())); | 231 m_size.height())); |
218 } | 232 } |
219 return m_frameDispatcher.get(); | 233 return m_frameDispatcher.get(); |
220 } | 234 } |
221 | 235 |
| 236 ScriptPromise OffscreenCanvas::commit(RefPtr<StaticBitmapImage> image, |
| 237 bool isWebGLSoftwareRendering, |
| 238 ScriptState* scriptState) { |
| 239 if (m_commitPromiseResolver) { |
| 240 if (image) { |
| 241 m_overdrawFrame = std::move(image); |
| 242 m_overdrawFrameIsWebGLSoftwareRendering = isWebGLSoftwareRendering; |
| 243 } |
| 244 } else { |
| 245 m_overdrawFrame = nullptr; |
| 246 m_commitPromiseResolver = ScriptPromiseResolver::create(scriptState); |
| 247 m_commitPromiseResolver->keepAliveWhilePending(); |
| 248 doCommit(std::move(image), isWebGLSoftwareRendering); |
| 249 } |
| 250 return m_commitPromiseResolver->promise(); |
| 251 } |
| 252 |
| 253 void OffscreenCanvas::doCommit(RefPtr<StaticBitmapImage> image, |
| 254 bool isWebGLSoftwareRendering) { |
| 255 double commitStartTime = WTF::monotonicallyIncreasingTime(); |
| 256 getOrCreateFrameDispatcher()->dispatchFrame(std::move(image), commitStartTime, |
| 257 isWebGLSoftwareRendering); |
| 258 } |
| 259 |
| 260 void OffscreenCanvas::beginFrame() { |
| 261 if (m_overdrawFrame) { |
| 262 // if we have an overdraw backlog, push the frame from the backlog |
| 263 // first and save the promise resolution for later. |
| 264 doCommit(std::move(m_overdrawFrame), |
| 265 m_overdrawFrameIsWebGLSoftwareRendering); |
| 266 } else if (m_commitPromiseResolver) { |
| 267 m_commitPromiseResolver->resolve(); |
| 268 m_commitPromiseResolver.clear(); |
| 269 } |
| 270 } |
| 271 |
222 ScriptPromise OffscreenCanvas::convertToBlob(ScriptState* scriptState, | 272 ScriptPromise OffscreenCanvas::convertToBlob(ScriptState* scriptState, |
223 const ImageEncodeOptions& options, | 273 const ImageEncodeOptions& options, |
224 ExceptionState& exceptionState) { | 274 ExceptionState& exceptionState) { |
225 if (this->isNeutered()) { | 275 if (this->isNeutered()) { |
226 exceptionState.throwDOMException(InvalidStateError, | 276 exceptionState.throwDOMException(InvalidStateError, |
227 "OffscreenCanvas object is detached."); | 277 "OffscreenCanvas object is detached."); |
228 return exceptionState.reject(scriptState); | 278 return exceptionState.reject(scriptState); |
229 } | 279 } |
230 | 280 |
231 if (!this->originClean()) { | 281 if (!this->originClean()) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 document, resolver); | 314 document, resolver); |
265 | 315 |
266 asyncCreator->scheduleAsyncBlobCreation(options.quality()); | 316 asyncCreator->scheduleAsyncBlobCreation(options.quality()); |
267 | 317 |
268 return resolver->promise(); | 318 return resolver->promise(); |
269 } | 319 } |
270 | 320 |
271 DEFINE_TRACE(OffscreenCanvas) { | 321 DEFINE_TRACE(OffscreenCanvas) { |
272 visitor->trace(m_context); | 322 visitor->trace(m_context); |
273 visitor->trace(m_executionContext); | 323 visitor->trace(m_executionContext); |
| 324 visitor->trace(m_commitPromiseResolver); |
274 EventTargetWithInlineData::trace(visitor); | 325 EventTargetWithInlineData::trace(visitor); |
275 } | 326 } |
276 | 327 |
277 } // namespace blink | 328 } // namespace blink |
OLD | NEW |