| 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" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 void OffscreenCanvas::setWidth(unsigned width) | 28 void OffscreenCanvas::setWidth(unsigned width) |
| 29 { | 29 { |
| 30 m_size.setWidth(clampTo<int>(width)); | 30 m_size.setWidth(clampTo<int>(width)); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void OffscreenCanvas::setHeight(unsigned height) | 33 void OffscreenCanvas::setHeight(unsigned height) |
| 34 { | 34 { |
| 35 m_size.setHeight(clampTo<int>(height)); | 35 m_size.setHeight(clampTo<int>(height)); |
| 36 } | 36 } |
| 37 | 37 |
| 38 OffscreenCanvasRenderingContext2D* OffscreenCanvas::getContext(const String& id,
const CanvasContextCreationAttributes& attributes) | 38 void OffscreenCanvas::getContext(ScriptState* scriptState, const String& id, con
st CanvasContextCreationAttributes& attributes, OffscreenRenderingContext& resul
t) |
| 39 { | 39 { |
| 40 OffscreenCanvasRenderingContext::ContextType contextType = OffscreenCanvasRe
nderingContext::contextTypeFromId(id); | 40 OffscreenCanvasRenderingContext::ContextType contextType = OffscreenCanvasRe
nderingContext::contextTypeFromId(id); |
| 41 | 41 |
| 42 // Unknown type. | 42 // Unknown type. |
| 43 if (contextType == OffscreenCanvasRenderingContext::ContextTypeCount) | 43 if (contextType == OffscreenCanvasRenderingContext::ContextTypeCount) |
| 44 return nullptr; | 44 return; |
| 45 | 45 |
| 46 OffscreenCanvasRenderingContextFactory* factory = getRenderingContextFactory
(contextType); | 46 OffscreenCanvasRenderingContextFactory* factory = getRenderingContextFactory
(contextType); |
| 47 if (!factory) | 47 if (!factory) |
| 48 return nullptr; | 48 return; |
| 49 | 49 |
| 50 if (m_context) { | 50 if (m_context) { |
| 51 if (m_context->getContextType() != contextType) { | 51 if (m_context->getOffscreenCanvasRenderingContextType() != contextType)
{ |
| 52 factory->onError(this, "OffscreenCanvas has an existing context of a
different type"); | 52 factory->onError(this, "OffscreenCanvas has an existing context of a
different type"); |
| 53 return nullptr; | 53 return; |
| 54 } | 54 } |
| 55 } else { | 55 } else { |
| 56 m_context = factory->create(this, attributes); | 56 m_context = factory->create(scriptState, this, attributes); |
| 57 } | 57 } |
| 58 | 58 |
| 59 // TODO: When there're more than one context type implemented in the future, | 59 // 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 | 60 // the return type here should be changed to base class of all Offscreen |
| 61 // context types. | 61 // context types. |
| 62 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); | 62 m_context->setOffscreenCanvasGetContextResult(result); |
| 63 } | 63 } |
| 64 | 64 |
| 65 ImageBitmap* OffscreenCanvas::transferToImageBitmap(ExceptionState& exceptionSta
te) | 65 ImageBitmap* OffscreenCanvas::transferToImageBitmap(ExceptionState& exceptionSta
te) |
| 66 { | 66 { |
| 67 if (!m_context) { | 67 if (!m_context) { |
| 68 exceptionState.throwDOMException(InvalidStateError, "Cannot transfer an
ImageBitmap from an OffscreenCanvas with no context"); | 68 exceptionState.throwDOMException(InvalidStateError, "Cannot transfer an
ImageBitmap from an OffscreenCanvas with no context"); |
| 69 return nullptr; | 69 return nullptr; |
| 70 } | 70 } |
| 71 ImageBitmap* image = m_context->transferToImageBitmap(exceptionState); | 71 ImageBitmap* image = m_context->transferToImageBitmap(exceptionState); |
| 72 if (!image) { | 72 if (!image) { |
| 73 // Undocumented exception (not in spec) | 73 // Undocumented exception (not in spec) |
| 74 exceptionState.throwDOMException(V8GeneralError, "Out of memory"); | 74 exceptionState.throwDOMException(V8GeneralError, "Out of memory"); |
| 75 } | 75 } |
| 76 return image; | 76 return image; |
| 77 } | 77 } |
| 78 | 78 |
| 79 OffscreenCanvasRenderingContext2D* OffscreenCanvas::renderingContext() const | 79 OffscreenCanvasRenderingContext* OffscreenCanvas::renderingContext() const |
| 80 { | 80 { |
| 81 // TODO: When there're more than one context type implemented in the future, | 81 // 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 | 82 // the return type here should be changed to base class of all Offscreen |
| 83 // context types. | 83 // context types. |
| 84 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); | 84 return m_context.get(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 OffscreenCanvas::ContextFactoryVector& OffscreenCanvas::renderingContextFactorie
s() | 87 OffscreenCanvas::ContextFactoryVector& OffscreenCanvas::renderingContextFactorie
s() |
| 88 { | 88 { |
| 89 DEFINE_STATIC_LOCAL(ContextFactoryVector, s_contextFactories, (OffscreenCanv
asRenderingContext::ContextTypeCount)); | 89 DEFINE_STATIC_LOCAL(ContextFactoryVector, s_contextFactories, (OffscreenCanv
asRenderingContext::ContextTypeCount)); |
| 90 return s_contextFactories; | 90 return s_contextFactories; |
| 91 } | 91 } |
| 92 | 92 |
| 93 OffscreenCanvasRenderingContextFactory* OffscreenCanvas::getRenderingContextFact
ory(int type) | 93 OffscreenCanvasRenderingContextFactory* OffscreenCanvas::getRenderingContextFact
ory(int type) |
| 94 { | 94 { |
| 95 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); | 95 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); |
| 96 return renderingContextFactories()[type].get(); | 96 return renderingContextFactories()[type].get(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void OffscreenCanvas::registerRenderingContextFactory(PassOwnPtr<OffscreenCanvas
RenderingContextFactory> renderingContextFactory) | 99 void OffscreenCanvas::registerRenderingContextFactory(PassOwnPtr<OffscreenCanvas
RenderingContextFactory> renderingContextFactory) |
| 100 { | 100 { |
| 101 OffscreenCanvasRenderingContext::ContextType type = renderingContextFactory-
>getContextType(); | 101 OffscreenCanvasRenderingContext::ContextType type = renderingContextFactory-
>getContextType(); |
| 102 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); | 102 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); |
| 103 ASSERT(!renderingContextFactories()[type]); | 103 ASSERT(!renderingContextFactories()[type]); |
| 104 renderingContextFactories()[type] = renderingContextFactory; | 104 renderingContextFactories()[type] = renderingContextFactory; |
| 105 } | 105 } |
| 106 | 106 |
| 107 DEFINE_TRACE(OffscreenCanvas) | 107 DEFINE_TRACE(OffscreenCanvas) |
| 108 { | 108 { |
| 109 visitor->trace(m_context); | 109 visitor->trace(m_context); |
| 110 visitor->trace(m_canvas); | 110 visitor->trace(m_canvas); |
| 111 } | 111 } |
| 112 | 112 |
| 113 } // namespace blink | 113 } // namespace blink |
| OLD | NEW |