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 "modules/offscreencanvas/OffscreenCanvas.h" | 5 #include "modules/offscreencanvas/OffscreenCanvas.h" |
| 6 | 6 |
| 7 #include "core/dom/ExceptionCode.h" | 7 #include "core/dom/ExceptionCode.h" |
| 8 #include "core/html/canvas/CanvasContextCreationAttributes.h" | 8 #include "core/html/canvas/CanvasContextCreationAttributes.h" |
| 9 #include "modules/offscreencanvas/OffscreenCanvasRenderingContext.h" | 9 #include "modules/offscreencanvas/OffscreenCanvasRenderingContext.h" |
| 10 #include "modules/offscreencanvas/OffscreenCanvasRenderingContextFactory.h" | 10 #include "modules/offscreencanvas/OffscreenCanvasRenderingContextFactory.h" |
| 11 #include "modules/offscreencanvas2d/OffscreenCanvasRenderingContext2D.h" | 11 #include "modules/offscreencanvas2d/OffscreenCanvasRenderingContext2D.h" |
| 12 #include "wtf/MathExtras.h" | 12 #include "wtf/MathExtras.h" |
| 13 | 13 |
| 14 namespace blink { | 14 namespace blink { |
| 15 | 15 |
| 16 OffscreenCanvas::OffscreenCanvas(const IntSize& size) | 16 OffscreenCanvas::OffscreenCanvas(const IntSize& size) |
| 17 : m_size(size) | 17 : m_canvasId(-1) // DOMNodeIds starts from 0, using -1 to indicate no associ ated canvas element. |
| 18 , m_size(size) | |
| 18 { } | 19 { } |
| 19 | 20 |
| 20 OffscreenCanvas::~OffscreenCanvas() | 21 OffscreenCanvas::~OffscreenCanvas() |
| 21 { } | 22 { } |
| 22 | 23 |
| 23 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) | 24 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) |
| 24 { | 25 { |
| 25 return new OffscreenCanvas(IntSize(clampTo<int>(width), clampTo<int>(height) )); | 26 return new OffscreenCanvas(IntSize(clampTo<int>(width), clampTo<int>(height) )); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void OffscreenCanvas::setWidth(unsigned width) | 29 void OffscreenCanvas::setWidth(unsigned width) |
| 29 { | 30 { |
| 30 m_size.setWidth(clampTo<int>(width)); | 31 m_size.setWidth(clampTo<int>(width)); |
| 31 } | 32 } |
| 32 | 33 |
| 33 void OffscreenCanvas::setHeight(unsigned height) | 34 void OffscreenCanvas::setHeight(unsigned height) |
| 34 { | 35 { |
| 35 m_size.setHeight(clampTo<int>(height)); | 36 m_size.setHeight(clampTo<int>(height)); |
| 36 } | 37 } |
| 37 | 38 |
| 39 void OffscreenCanvas::setNeutered() | |
| 40 { | |
| 41 m_isNeutered = true; | |
|
Justin Novosad
2016/04/12 18:10:23
Add "ASSERT(!m_context);"
xidachen
2016/04/12 19:35:14
Done.
| |
| 42 m_size.setWidth(0); | |
|
Justin Novosad
2016/04/12 18:10:23
Why do we need to reset m_size? This has observabl
xidachen
2016/04/12 19:35:14
When an offscreenCanvas gets transferred to worker
| |
| 43 m_size.setHeight(0); | |
| 44 } | |
| 45 | |
| 38 OffscreenCanvasRenderingContext2D* OffscreenCanvas::getContext(const String& id, const CanvasContextCreationAttributes& attributes) | 46 OffscreenCanvasRenderingContext2D* OffscreenCanvas::getContext(const String& id, const CanvasContextCreationAttributes& attributes) |
|
Justin Novosad
2016/04/12 18:10:23
This method need to throw an InvalidStateError exc
xidachen
2016/04/12 19:35:14
Done.
| |
| 39 { | 47 { |
| 40 OffscreenCanvasRenderingContext::ContextType contextType = OffscreenCanvasRe nderingContext::contextTypeFromId(id); | 48 OffscreenCanvasRenderingContext::ContextType contextType = OffscreenCanvasRe nderingContext::contextTypeFromId(id); |
| 41 | 49 |
| 42 // Unknown type. | 50 // Unknown type. |
| 43 if (contextType == OffscreenCanvasRenderingContext::ContextTypeCount) | 51 if (contextType == OffscreenCanvasRenderingContext::ContextTypeCount) |
| 44 return nullptr; | 52 return nullptr; |
| 45 | 53 |
| 46 OffscreenCanvasRenderingContextFactory* factory = getRenderingContextFactory (contextType); | 54 OffscreenCanvasRenderingContextFactory* factory = getRenderingContextFactory (contextType); |
| 47 if (!factory) | 55 if (!factory) |
| 48 return nullptr; | 56 return nullptr; |
| 49 | 57 |
| 50 if (m_context) { | 58 if (m_context) { |
| 51 if (m_context->getContextType() != contextType) { | 59 if (m_context->getContextType() != contextType) { |
| 52 factory->onError(this, "OffscreenCanvas has an existing context of a different type"); | 60 factory->onError(this, "OffscreenCanvas has an existing context of a different type"); |
| 53 return nullptr; | 61 return nullptr; |
| 54 } | 62 } |
| 55 } else { | 63 } else { |
| 56 m_context = factory->create(this, attributes); | 64 m_context = factory->create(this, attributes); |
| 57 } | 65 } |
| 58 | 66 |
| 59 // TODO: When there're more than one context type implemented in the future, | 67 // TODO: When there're more than one context type implemented in the future, |
| 60 // the return type here should be changed to base class of all Offscreen | 68 // the return type here should be changed to base class of all Offscreen |
| 61 // context types. | 69 // context types. |
| 62 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); | 70 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); |
| 63 } | 71 } |
| 64 | 72 |
| 65 ImageBitmap* OffscreenCanvas::transferToImageBitmap(ExceptionState& exceptionSta te) | 73 ImageBitmap* OffscreenCanvas::transferToImageBitmap(ExceptionState& exceptionSta te) |
|
Justin Novosad
2016/04/12 18:10:23
Same here: throw if neutered + test.
xidachen
2016/04/12 19:35:14
Done.
| |
| 66 { | 74 { |
| 67 if (!m_context) { | 75 if (!m_context) { |
| 68 exceptionState.throwDOMException(InvalidStateError, "Cannot transfer an ImageBitmap from an OffscreenCanvas with no context"); | 76 exceptionState.throwDOMException(InvalidStateError, "Cannot transfer an ImageBitmap from an OffscreenCanvas with no context"); |
| 69 return nullptr; | 77 return nullptr; |
| 70 } | 78 } |
| 71 ImageBitmap* image = m_context->transferToImageBitmap(exceptionState); | 79 ImageBitmap* image = m_context->transferToImageBitmap(exceptionState); |
| 72 if (!image) { | 80 if (!image) { |
| 73 // Undocumented exception (not in spec) | 81 // Undocumented exception (not in spec) |
| 74 exceptionState.throwDOMException(V8GeneralError, "Out of memory"); | 82 exceptionState.throwDOMException(V8GeneralError, "Out of memory"); |
| 75 } | 83 } |
| 76 return image; | 84 return image; |
| 77 } | 85 } |
| 78 | 86 |
| 79 OffscreenCanvasRenderingContext2D* OffscreenCanvas::renderingContext() const | 87 OffscreenCanvasRenderingContext2D* OffscreenCanvas::renderingContext() const |
|
Justin Novosad
2016/04/12 18:10:23
ASSERT(!m_isNeutered);
xidachen
2016/04/12 19:35:14
Done.
| |
| 80 { | 88 { |
| 81 // TODO: When there're more than one context type implemented in the future, | 89 // TODO: When there're more than one context type implemented in the future, |
| 82 // the return type here should be changed to base class of all Offscreen | 90 // the return type here should be changed to base class of all Offscreen |
| 83 // context types. | 91 // context types. |
| 84 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); | 92 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); |
| 85 } | 93 } |
| 86 | 94 |
| 87 OffscreenCanvas::ContextFactoryVector& OffscreenCanvas::renderingContextFactorie s() | 95 OffscreenCanvas::ContextFactoryVector& OffscreenCanvas::renderingContextFactorie s() |
| 88 { | 96 { |
| 89 DEFINE_STATIC_LOCAL(ContextFactoryVector, s_contextFactories, (OffscreenCanv asRenderingContext::ContextTypeCount)); | 97 DEFINE_STATIC_LOCAL(ContextFactoryVector, s_contextFactories, (OffscreenCanv asRenderingContext::ContextTypeCount)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 100 { | 108 { |
| 101 OffscreenCanvasRenderingContext::ContextType type = renderingContextFactory- >getContextType(); | 109 OffscreenCanvasRenderingContext::ContextType type = renderingContextFactory- >getContextType(); |
| 102 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); | 110 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); |
| 103 ASSERT(!renderingContextFactories()[type]); | 111 ASSERT(!renderingContextFactories()[type]); |
| 104 renderingContextFactories()[type] = renderingContextFactory; | 112 renderingContextFactories()[type] = renderingContextFactory; |
| 105 } | 113 } |
| 106 | 114 |
| 107 DEFINE_TRACE(OffscreenCanvas) | 115 DEFINE_TRACE(OffscreenCanvas) |
| 108 { | 116 { |
| 109 visitor->trace(m_context); | 117 visitor->trace(m_context); |
| 110 visitor->trace(m_canvas); | |
| 111 } | 118 } |
| 112 | 119 |
| 113 } // namespace blink | 120 } // namespace blink |
| OLD | NEW |