Chromium Code Reviews| 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 m_overdrawFrame = std::move(image); | |
|
xlai (Olivia)
2016/12/21 19:54:25
I'm wondering if you need to store the scriptState
Justin Novosad
2016/12/21 21:15:40
The overdraw frame does not generate a promise (th
| |
| 241 m_overdrawFrameIsWebGLSoftwareRendering = isWebGLSoftwareRendering; | |
| 242 } else { | |
| 243 m_overdrawFrame = nullptr; | |
| 244 m_commitPromiseResolver = ScriptPromiseResolver::create(scriptState); | |
| 245 m_commitPromiseResolver->keepAliveWhilePending(); | |
| 246 doCommit(std::move(image), isWebGLSoftwareRendering); | |
| 247 } | |
| 248 return m_commitPromiseResolver->promise(); | |
| 249 } | |
| 250 | |
| 251 void OffscreenCanvas::doCommit(RefPtr<StaticBitmapImage> image, | |
| 252 bool isWebGLSoftwareRendering) { | |
| 253 double commitStartTime = WTF::monotonicallyIncreasingTime(); | |
| 254 getOrCreateFrameDispatcher()->dispatchFrame(std::move(image), commitStartTime, | |
| 255 isWebGLSoftwareRendering); | |
| 256 } | |
| 257 | |
| 258 void OffscreenCanvas::beginFrame() { | |
| 259 if (m_overdrawFrame) { | |
| 260 // if we have an overdraw backlog, push the frame from the backlog | |
| 261 // first and save the promise resolution for later. | |
| 262 doCommit(std::move(m_overdrawFrame), | |
| 263 m_overdrawFrameIsWebGLSoftwareRendering); | |
|
xlai (Olivia)
2016/12/21 19:54:25
Hmmmm...After you doCommit on the overdrawFrame, d
Justin Novosad
2016/12/21 21:15:40
std::move already takes care of that.
| |
| 264 } else if (m_commitPromiseResolver) { | |
| 265 m_commitPromiseResolver->resolve(); | |
|
xlai (Olivia)
2016/12/21 19:54:25
So you only resolve the promiseResolver when there
Justin Novosad
2016/12/21 21:15:40
Exactly. As long as there is a backlog, we do not
| |
| 266 m_commitPromiseResolver.clear(); | |
| 267 } | |
| 268 } | |
| 269 | |
| 222 ScriptPromise OffscreenCanvas::convertToBlob(ScriptState* scriptState, | 270 ScriptPromise OffscreenCanvas::convertToBlob(ScriptState* scriptState, |
| 223 const ImageEncodeOptions& options, | 271 const ImageEncodeOptions& options, |
| 224 ExceptionState& exceptionState) { | 272 ExceptionState& exceptionState) { |
| 225 if (this->isNeutered()) { | 273 if (this->isNeutered()) { |
| 226 exceptionState.throwDOMException(InvalidStateError, | 274 exceptionState.throwDOMException(InvalidStateError, |
| 227 "OffscreenCanvas object is detached."); | 275 "OffscreenCanvas object is detached."); |
| 228 return exceptionState.reject(scriptState); | 276 return exceptionState.reject(scriptState); |
| 229 } | 277 } |
| 230 | 278 |
| 231 if (!this->originClean()) { | 279 if (!this->originClean()) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 document, resolver); | 312 document, resolver); |
| 265 | 313 |
| 266 asyncCreator->scheduleAsyncBlobCreation(options.quality()); | 314 asyncCreator->scheduleAsyncBlobCreation(options.quality()); |
| 267 | 315 |
| 268 return resolver->promise(); | 316 return resolver->promise(); |
| 269 } | 317 } |
| 270 | 318 |
| 271 DEFINE_TRACE(OffscreenCanvas) { | 319 DEFINE_TRACE(OffscreenCanvas) { |
| 272 visitor->trace(m_context); | 320 visitor->trace(m_context); |
| 273 visitor->trace(m_executionContext); | 321 visitor->trace(m_executionContext); |
| 322 visitor->trace(m_commitPromiseResolver); | |
| 274 EventTargetWithInlineData::trace(visitor); | 323 EventTargetWithInlineData::trace(visitor); |
| 275 } | 324 } |
| 276 | 325 |
| 277 } // namespace blink | 326 } // namespace blink |
| OLD | NEW |