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/html/canvas/OffscreenCanvas.h" | 5 #include "modules/offscreencanvas/OffscreenCanvas.h" |
6 | 6 |
| 7 #include "core/html/canvas/CanvasContextCreationAttributes.h" |
| 8 #include "modules/offscreencanvas/OffscreenCanvasRenderingContext.h" |
| 9 #include "modules/offscreencanvas/OffscreenCanvasRenderingContextFactory.h" |
| 10 #include "modules/offscreencanvas2d/OffscreenCanvasRenderingContext2D.h" |
7 #include "wtf/MathExtras.h" | 11 #include "wtf/MathExtras.h" |
8 | 12 |
9 namespace blink { | 13 namespace blink { |
10 | 14 |
11 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) | 15 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) |
12 { | 16 { |
13 return new OffscreenCanvas(IntSize(clampTo<int>(width), clampTo<int>(height)
)); | 17 return new OffscreenCanvas(IntSize(clampTo<int>(width), clampTo<int>(height)
)); |
14 } | 18 } |
15 | 19 |
16 void OffscreenCanvas::setWidth(unsigned width) | 20 void OffscreenCanvas::setWidth(unsigned width) |
17 { | 21 { |
18 m_size.setWidth(clampTo<int>(width)); | 22 m_size.setWidth(clampTo<int>(width)); |
19 } | 23 } |
20 | 24 |
21 void OffscreenCanvas::setHeight(unsigned height) | 25 void OffscreenCanvas::setHeight(unsigned height) |
22 { | 26 { |
23 m_size.setHeight(clampTo<int>(height)); | 27 m_size.setHeight(clampTo<int>(height)); |
24 } | 28 } |
25 | 29 |
26 OffscreenCanvas::OffscreenCanvas(const IntSize& size) | 30 OffscreenCanvas::OffscreenCanvas(const IntSize& size) |
27 : m_size(size) | 31 : m_size(size) |
28 { | 32 { |
29 } | 33 } |
30 | 34 |
| 35 OffscreenCanvasRenderingContext2D* OffscreenCanvas::getContext(const String& id,
const CanvasContextCreationAttributes& attributes) |
| 36 { |
| 37 OffscreenCanvasRenderingContext::ContextType contextType = OffscreenCanvasRe
nderingContext::contextTypeFromId(id); |
| 38 |
| 39 // Unknown type. |
| 40 if (contextType == OffscreenCanvasRenderingContext::ContextTypeCount) |
| 41 return nullptr; |
| 42 |
| 43 OffscreenCanvasRenderingContextFactory* factory = getRenderingContextFactory
(contextType); |
| 44 if (!factory) |
| 45 return nullptr; |
| 46 |
| 47 if (m_context) { |
| 48 if (m_context->contextType() != contextType) { |
| 49 factory->onError(this, "OffscreenCanvas has an existing context of a
different type"); |
| 50 return nullptr; |
| 51 } |
| 52 } else { |
| 53 m_context = factory->create(this, attributes); |
| 54 } |
| 55 |
| 56 // TODO: When there're more than one context type implemented in the future, |
| 57 // the return type here should be changed to base class of all Offscreen |
| 58 // context types. |
| 59 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); |
| 60 } |
| 61 |
| 62 OffscreenCanvasRenderingContext2D* OffscreenCanvas::renderingContext() const |
| 63 { |
| 64 // TODO: When there're more than one context type implemented in the future, |
| 65 // the return type here should be changed to base class of all Offscreen |
| 66 // context types. |
| 67 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); |
| 68 } |
| 69 |
| 70 OffscreenCanvas::ContextFactoryVector& OffscreenCanvas::renderingContextFactorie
s() |
| 71 { |
| 72 DEFINE_STATIC_LOCAL(ContextFactoryVector, s_contextFactories, (OffscreenCanv
asRenderingContext::ContextTypeCount)); |
| 73 return s_contextFactories; |
| 74 } |
| 75 |
| 76 OffscreenCanvasRenderingContextFactory* OffscreenCanvas::getRenderingContextFact
ory(int type) |
| 77 { |
| 78 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); |
| 79 return renderingContextFactories()[type].get(); |
| 80 } |
| 81 |
| 82 void OffscreenCanvas::registerRenderingContextFactory(PassOwnPtr<OffscreenCanvas
RenderingContextFactory> renderingContextFactory) |
| 83 { |
| 84 OffscreenCanvasRenderingContext::ContextType type = renderingContextFactory-
>contextType(); |
| 85 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); |
| 86 ASSERT(!renderingContextFactories()[type]); |
| 87 renderingContextFactories()[type] = renderingContextFactory; |
| 88 } |
| 89 |
31 DEFINE_TRACE(OffscreenCanvas) | 90 DEFINE_TRACE(OffscreenCanvas) |
32 { | 91 { |
| 92 visitor->trace(m_context); |
33 } | 93 } |
34 | 94 |
35 } // namespace blink | 95 } // namespace blink |
OLD | NEW |