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 |
242 if (!m_commitPromiseResolver) { | |
243 m_commitPromiseResolver = ScriptPromiseResolver::create(scriptState); | |
244 m_commitPromiseResolver->keepAliveWhilePending(); | |
245 | |
246 m_overdrawFrame = nullptr; | |
Justin Novosad
2017/03/24 17:41:53
We don't to track m_overdrawFrame anymore.
| |
241 if (image) { | 247 if (image) { |
248 // We defer the submission of commit frames at the end of JS task | |
249 m_currentFrame = std::move(image); | |
250 m_currentFrameIsWebGLSoftwareRendering = isWebGLSoftwareRendering; | |
251 Platform::current()->currentThread()->addTaskObserver(m_context); | |
252 } | |
253 } else if (image) { | |
254 if (m_currentFrame) { | |
255 // An override of m_currentFrame can happen when there are multiple | |
256 // frames committed before JS task finishes. | |
257 m_currentFrame = std::move(image); | |
258 m_currentFrameIsWebGLSoftwareRendering = isWebGLSoftwareRendering; | |
259 } else { | |
260 // The current frame has been dispatched but the promise is not resolved | |
261 // yet. | |
242 m_overdrawFrame = std::move(image); | 262 m_overdrawFrame = std::move(image); |
243 m_overdrawFrameIsWebGLSoftwareRendering = isWebGLSoftwareRendering; | 263 m_overdrawFrameIsWebGLSoftwareRendering = isWebGLSoftwareRendering; |
244 } | 264 } |
245 } else { | |
246 m_overdrawFrame = nullptr; | |
247 m_commitPromiseResolver = ScriptPromiseResolver::create(scriptState); | |
248 m_commitPromiseResolver->keepAliveWhilePending(); | |
249 doCommit(std::move(image), isWebGLSoftwareRendering); | |
250 } | 265 } |
266 | |
251 return m_commitPromiseResolver->promise(); | 267 return m_commitPromiseResolver->promise(); |
252 } | 268 } |
253 | 269 |
270 void OffscreenCanvas::doCommitAtEndOfTask() { | |
Justin Novosad
2017/03/24 17:41:53
What happens if we reach this point and m_commitPr
xlai (Olivia)
2017/03/24 18:18:52
Actually, this situation was already handled in th
| |
271 doCommit(std::move(m_currentFrame), m_currentFrameIsWebGLSoftwareRendering); | |
272 Platform::current()->currentThread()->removeTaskObserver(m_context); | |
273 } | |
274 | |
254 void OffscreenCanvas::doCommit(RefPtr<StaticBitmapImage> image, | 275 void OffscreenCanvas::doCommit(RefPtr<StaticBitmapImage> image, |
255 bool isWebGLSoftwareRendering) { | 276 bool isWebGLSoftwareRendering) { |
256 double commitStartTime = WTF::monotonicallyIncreasingTime(); | 277 double commitStartTime = WTF::monotonicallyIncreasingTime(); |
257 getOrCreateFrameDispatcher()->dispatchFrame(std::move(image), commitStartTime, | 278 getOrCreateFrameDispatcher()->dispatchFrame(std::move(image), commitStartTime, |
258 isWebGLSoftwareRendering); | 279 isWebGLSoftwareRendering); |
259 } | 280 } |
260 | 281 |
261 void OffscreenCanvas::beginFrame() { | 282 void OffscreenCanvas::beginFrame() { |
262 if (m_overdrawFrame) { | 283 if (m_overdrawFrame) { |
263 // if we have an overdraw backlog, push the frame from the backlog | 284 // if we have an overdraw backlog, push the frame from the backlog |
264 // first and save the promise resolution for later. | 285 // first and save the promise resolution for later. |
265 doCommit(std::move(m_overdrawFrame), | 286 doCommit(std::move(m_overdrawFrame), |
266 m_overdrawFrameIsWebGLSoftwareRendering); | 287 m_overdrawFrameIsWebGLSoftwareRendering); |
267 } else if (m_commitPromiseResolver) { | 288 } else if (!m_currentFrame && m_commitPromiseResolver) { |
268 m_commitPromiseResolver->resolve(); | 289 m_commitPromiseResolver->resolve(); |
269 m_commitPromiseResolver.clear(); | 290 m_commitPromiseResolver.clear(); |
270 // We need to tell parent frame to stop sending signals on begin frame to | 291 // We need to tell parent frame to stop sending signals on begin frame to |
271 // avoid overhead once we resolve the promise. | 292 // avoid overhead once we resolve the promise. |
272 // In the case of overdraw frame (if block), we still need to wait for one | 293 // In the case of overdraw frame (if block), we still need to wait for one |
273 // more frame time to resolve the existing promise. | 294 // more frame time to resolve the existing promise. |
274 getOrCreateFrameDispatcher()->setNeedsBeginFrame(false); | 295 getOrCreateFrameDispatcher()->setNeedsBeginFrame(false); |
275 } | 296 } |
276 } | 297 } |
277 | 298 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 } | 348 } |
328 | 349 |
329 DEFINE_TRACE(OffscreenCanvas) { | 350 DEFINE_TRACE(OffscreenCanvas) { |
330 visitor->trace(m_context); | 351 visitor->trace(m_context); |
331 visitor->trace(m_executionContext); | 352 visitor->trace(m_executionContext); |
332 visitor->trace(m_commitPromiseResolver); | 353 visitor->trace(m_commitPromiseResolver); |
333 EventTargetWithInlineData::trace(visitor); | 354 EventTargetWithInlineData::trace(visitor); |
334 } | 355 } |
335 | 356 |
336 } // namespace blink | 357 } // namespace blink |
OLD | NEW |