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 "core/dom/ExceptionCode.h" | 8 #include "core/dom/ExceptionCode.h" |
8 #include "core/fileapi/Blob.h" | 9 #include "core/fileapi/Blob.h" |
9 #include "core/frame/ImageBitmap.h" | 10 #include "core/frame/ImageBitmap.h" |
10 #include "core/html/ImageData.h" | 11 #include "core/html/ImageData.h" |
11 #include "core/html/canvas/CanvasAsyncBlobCreator.h" | 12 #include "core/html/canvas/CanvasAsyncBlobCreator.h" |
12 #include "core/html/canvas/CanvasContextCreationAttributes.h" | 13 #include "core/html/canvas/CanvasContextCreationAttributes.h" |
13 #include "core/html/canvas/CanvasRenderingContext.h" | 14 #include "core/html/canvas/CanvasRenderingContext.h" |
14 #include "core/html/canvas/CanvasRenderingContextFactory.h" | 15 #include "core/html/canvas/CanvasRenderingContextFactory.h" |
15 #include "platform/graphics/Image.h" | 16 #include "platform/graphics/Image.h" |
16 #include "platform/graphics/ImageBuffer.h" | 17 #include "platform/graphics/ImageBuffer.h" |
17 #include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h" | 18 #include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h" |
18 #include "platform/graphics/StaticBitmapImage.h" | 19 #include "platform/graphics/StaticBitmapImage.h" |
19 #include "platform/image-encoders/ImageEncoderUtils.h" | 20 #include "platform/image-encoders/ImageEncoderUtils.h" |
21 #include "public/platform/Platform.h" | |
20 #include "wtf/MathExtras.h" | 22 #include "wtf/MathExtras.h" |
21 #include <memory> | |
22 | 23 |
23 namespace blink { | 24 namespace blink { |
24 | 25 |
25 OffscreenCanvas::OffscreenCanvas(const IntSize& size) : m_size(size) {} | 26 OffscreenCanvas::OffscreenCanvas(const IntSize& size) : m_size(size) {} |
26 | 27 |
27 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) { | 28 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) { |
28 return new OffscreenCanvas( | 29 return new OffscreenCanvas( |
29 IntSize(clampTo<int>(width), clampTo<int>(height))); | 30 IntSize(clampTo<int>(width), clampTo<int>(height))); |
30 } | 31 } |
31 | 32 |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 this, m_clientId, m_sinkId, m_placeholderCanvasId, m_size.width(), | 231 this, m_clientId, m_sinkId, m_placeholderCanvasId, m_size.width(), |
231 m_size.height())); | 232 m_size.height())); |
232 } | 233 } |
233 return m_frameDispatcher.get(); | 234 return m_frameDispatcher.get(); |
234 } | 235 } |
235 | 236 |
236 ScriptPromise OffscreenCanvas::commit(RefPtr<StaticBitmapImage> image, | 237 ScriptPromise OffscreenCanvas::commit(RefPtr<StaticBitmapImage> image, |
237 bool isWebGLSoftwareRendering, | 238 bool isWebGLSoftwareRendering, |
238 ScriptState* scriptState) { | 239 ScriptState* scriptState) { |
239 getOrCreateFrameDispatcher()->setNeedsBeginFrame(true); | 240 getOrCreateFrameDispatcher()->setNeedsBeginFrame(true); |
240 if (m_commitPromiseResolver) { | 241 |
241 if (image) { | 242 if (!m_commitPromiseResolver) { |
242 m_overdrawFrame = std::move(image); | |
243 m_overdrawFrameIsWebGLSoftwareRendering = isWebGLSoftwareRendering; | |
244 } | |
245 } else { | |
246 m_overdrawFrame = nullptr; | |
247 m_commitPromiseResolver = ScriptPromiseResolver::create(scriptState); | 243 m_commitPromiseResolver = ScriptPromiseResolver::create(scriptState); |
248 m_commitPromiseResolver->keepAliveWhilePending(); | 244 m_commitPromiseResolver->keepAliveWhilePending(); |
249 doCommit(std::move(image), isWebGLSoftwareRendering); | 245 |
246 if (image) { | |
247 // We defer the submission of commit frames at the end of JS task | |
248 m_currentFrame = std::move(image); | |
249 m_currentFrameIsWebGLSoftwareRendering = isWebGLSoftwareRendering; | |
250 Platform::current()->currentThread()->addTaskObserver(m_context); | |
Justin Novosad
2017/03/24 18:40:31
I am concerned that the registration and de-regist
xlai (Olivia)
2017/03/24 19:50:17
Done.
| |
251 } | |
252 } else if (image) { | |
253 // Two possible scenarios: | |
254 // 1. An override of m_currentFrame can happen when there are multiple | |
255 // frames committed before JS task finishes. (m_currentFrame!=nullptr) | |
256 // 2. The current frame has been dispatched but the promise is not | |
257 // resolved yet. (m_currentFrame==nullptr) | |
258 m_currentFrame = std::move(image); | |
259 m_currentFrameIsWebGLSoftwareRendering = isWebGLSoftwareRendering; | |
250 } | 260 } |
261 | |
251 return m_commitPromiseResolver->promise(); | 262 return m_commitPromiseResolver->promise(); |
252 } | 263 } |
253 | 264 |
265 void OffscreenCanvas::doCommitAtEndOfTask() { | |
Justin Novosad
2017/03/24 18:40:31
I'd call this finalizeFrame
xlai (Olivia)
2017/03/24 19:50:17
Done.
| |
266 doCommit(std::move(m_currentFrame), m_currentFrameIsWebGLSoftwareRendering); | |
Justin Novosad
2017/03/24 18:40:31
Should put this inside "if (m_currentFrame)", just
xlai (Olivia)
2017/03/24 19:50:17
Done.
| |
267 Platform::current()->currentThread()->removeTaskObserver(m_context); | |
268 } | |
269 | |
254 void OffscreenCanvas::doCommit(RefPtr<StaticBitmapImage> image, | 270 void OffscreenCanvas::doCommit(RefPtr<StaticBitmapImage> image, |
255 bool isWebGLSoftwareRendering) { | 271 bool isWebGLSoftwareRendering) { |
256 double commitStartTime = WTF::monotonicallyIncreasingTime(); | 272 double commitStartTime = WTF::monotonicallyIncreasingTime(); |
257 getOrCreateFrameDispatcher()->dispatchFrame(std::move(image), commitStartTime, | 273 getOrCreateFrameDispatcher()->dispatchFrame(std::move(image), commitStartTime, |
258 isWebGLSoftwareRendering); | 274 isWebGLSoftwareRendering); |
259 } | 275 } |
260 | 276 |
261 void OffscreenCanvas::beginFrame() { | 277 void OffscreenCanvas::beginFrame() { |
262 if (m_overdrawFrame) { | 278 if (m_currentFrame) { |
263 // if we have an overdraw backlog, push the frame from the backlog | 279 // If we have an overdraw backlog, push the frame from the backlog |
264 // first and save the promise resolution for later. | 280 // first and save the promise resolution for later. |
265 doCommit(std::move(m_overdrawFrame), | 281 // Then we need to wait for one more frame time to resolve the existing |
266 m_overdrawFrameIsWebGLSoftwareRendering); | 282 // promise. |
283 doCommit(std::move(m_currentFrame), m_currentFrameIsWebGLSoftwareRendering); | |
267 } else if (m_commitPromiseResolver) { | 284 } else if (m_commitPromiseResolver) { |
268 m_commitPromiseResolver->resolve(); | 285 m_commitPromiseResolver->resolve(); |
269 m_commitPromiseResolver.clear(); | 286 m_commitPromiseResolver.clear(); |
270 // We need to tell parent frame to stop sending signals on begin frame to | 287 // We need to tell parent frame to stop sending signals on begin frame to |
271 // avoid overhead once we resolve the promise. | 288 // avoid overhead once we resolve the promise. |
272 // In the case of overdraw frame (if block), we still need to wait for one | |
273 // more frame time to resolve the existing promise. | |
274 getOrCreateFrameDispatcher()->setNeedsBeginFrame(false); | 289 getOrCreateFrameDispatcher()->setNeedsBeginFrame(false); |
275 } | 290 } |
276 } | 291 } |
277 | 292 |
278 ScriptPromise OffscreenCanvas::convertToBlob(ScriptState* scriptState, | 293 ScriptPromise OffscreenCanvas::convertToBlob(ScriptState* scriptState, |
279 const ImageEncodeOptions& options, | 294 const ImageEncodeOptions& options, |
280 ExceptionState& exceptionState) { | 295 ExceptionState& exceptionState) { |
281 if (this->isNeutered()) { | 296 if (this->isNeutered()) { |
282 exceptionState.throwDOMException(InvalidStateError, | 297 exceptionState.throwDOMException(InvalidStateError, |
283 "OffscreenCanvas object is detached."); | 298 "OffscreenCanvas object is detached."); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 } | 342 } |
328 | 343 |
329 DEFINE_TRACE(OffscreenCanvas) { | 344 DEFINE_TRACE(OffscreenCanvas) { |
330 visitor->trace(m_context); | 345 visitor->trace(m_context); |
331 visitor->trace(m_executionContext); | 346 visitor->trace(m_executionContext); |
332 visitor->trace(m_commitPromiseResolver); | 347 visitor->trace(m_commitPromiseResolver); |
333 EventTargetWithInlineData::trace(visitor); | 348 EventTargetWithInlineData::trace(visitor); |
334 } | 349 } |
335 | 350 |
336 } // namespace blink | 351 } // namespace blink |
OLD | NEW |