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" |
| 8 #if !ENABLE(OILPAN) |
| 9 #include "core/frame/ImageBitmap.h" // So ~RefPtr can call unref() |
| 10 #endif |
7 #include "core/html/canvas/CanvasContextCreationAttributes.h" | 11 #include "core/html/canvas/CanvasContextCreationAttributes.h" |
8 #include "modules/offscreencanvas/OffscreenCanvasRenderingContext.h" | 12 #include "modules/offscreencanvas/OffscreenCanvasRenderingContext.h" |
9 #include "modules/offscreencanvas/OffscreenCanvasRenderingContextFactory.h" | 13 #include "modules/offscreencanvas/OffscreenCanvasRenderingContextFactory.h" |
10 #include "modules/offscreencanvas2d/OffscreenCanvasRenderingContext2D.h" | 14 #include "modules/offscreencanvas2d/OffscreenCanvasRenderingContext2D.h" |
11 #include "wtf/MathExtras.h" | 15 #include "wtf/MathExtras.h" |
12 | 16 |
13 namespace blink { | 17 namespace blink { |
14 | 18 |
| 19 OffscreenCanvas::OffscreenCanvas(const IntSize& size) |
| 20 : m_size(size) |
| 21 { } |
| 22 |
| 23 OffscreenCanvas::~OffscreenCanvas() |
| 24 { } |
| 25 |
15 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) | 26 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) |
16 { | 27 { |
17 return new OffscreenCanvas(IntSize(clampTo<int>(width), clampTo<int>(height)
)); | 28 return new OffscreenCanvas(IntSize(clampTo<int>(width), clampTo<int>(height)
)); |
18 } | 29 } |
19 | 30 |
20 void OffscreenCanvas::setWidth(unsigned width) | 31 void OffscreenCanvas::setWidth(unsigned width) |
21 { | 32 { |
22 m_size.setWidth(clampTo<int>(width)); | 33 m_size.setWidth(clampTo<int>(width)); |
23 } | 34 } |
24 | 35 |
25 void OffscreenCanvas::setHeight(unsigned height) | 36 void OffscreenCanvas::setHeight(unsigned height) |
26 { | 37 { |
27 m_size.setHeight(clampTo<int>(height)); | 38 m_size.setHeight(clampTo<int>(height)); |
28 } | 39 } |
29 | 40 |
30 OffscreenCanvas::OffscreenCanvas(const IntSize& size) | |
31 : m_size(size) | |
32 { | |
33 } | |
34 | |
35 OffscreenCanvasRenderingContext2D* OffscreenCanvas::getContext(const String& id,
const CanvasContextCreationAttributes& attributes) | 41 OffscreenCanvasRenderingContext2D* OffscreenCanvas::getContext(const String& id,
const CanvasContextCreationAttributes& attributes) |
36 { | 42 { |
37 OffscreenCanvasRenderingContext::ContextType contextType = OffscreenCanvasRe
nderingContext::contextTypeFromId(id); | 43 OffscreenCanvasRenderingContext::ContextType contextType = OffscreenCanvasRe
nderingContext::contextTypeFromId(id); |
38 | 44 |
39 // Unknown type. | 45 // Unknown type. |
40 if (contextType == OffscreenCanvasRenderingContext::ContextTypeCount) | 46 if (contextType == OffscreenCanvasRenderingContext::ContextTypeCount) |
41 return nullptr; | 47 return nullptr; |
42 | 48 |
43 OffscreenCanvasRenderingContextFactory* factory = getRenderingContextFactory
(contextType); | 49 OffscreenCanvasRenderingContextFactory* factory = getRenderingContextFactory
(contextType); |
44 if (!factory) | 50 if (!factory) |
45 return nullptr; | 51 return nullptr; |
46 | 52 |
47 if (m_context) { | 53 if (m_context) { |
48 if (m_context->getContextType() != contextType) { | 54 if (m_context->getContextType() != contextType) { |
49 factory->onError(this, "OffscreenCanvas has an existing context of a
different type"); | 55 factory->onError(this, "OffscreenCanvas has an existing context of a
different type"); |
50 return nullptr; | 56 return nullptr; |
51 } | 57 } |
52 } else { | 58 } else { |
53 m_context = factory->create(this, attributes); | 59 m_context = factory->create(this, attributes); |
54 } | 60 } |
55 | 61 |
56 // TODO: When there're more than one context type implemented in the future, | 62 // 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 | 63 // the return type here should be changed to base class of all Offscreen |
58 // context types. | 64 // context types. |
59 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); | 65 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); |
60 } | 66 } |
61 | 67 |
| 68 PassRefPtrWillBeRawPtr<ImageBitmap> OffscreenCanvas::transferToImageBitmap(Excep
tionState& exceptionState) |
| 69 { |
| 70 if (!m_context) { |
| 71 exceptionState.throwDOMException(InvalidStateError, "Cannot transfer an
ImageBitmap from an OffscreenCanvas with no context"); |
| 72 return nullptr; |
| 73 } |
| 74 RefPtrWillBeRawPtr<ImageBitmap> image = m_context->transferToImageBitmap(exc
eptionState); |
| 75 if (!image) { |
| 76 // Undocumented exception (not in spec) |
| 77 exceptionState.throwDOMException(V8GeneralError, "Out of memory"); |
| 78 } |
| 79 return image.release(); |
| 80 } |
| 81 |
62 OffscreenCanvasRenderingContext2D* OffscreenCanvas::renderingContext() const | 82 OffscreenCanvasRenderingContext2D* OffscreenCanvas::renderingContext() const |
63 { | 83 { |
64 // TODO: When there're more than one context type implemented in the future, | 84 // 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 | 85 // the return type here should be changed to base class of all Offscreen |
66 // context types. | 86 // context types. |
67 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); | 87 return static_cast<OffscreenCanvasRenderingContext2D*>(m_context.get()); |
68 } | 88 } |
69 | 89 |
70 OffscreenCanvas::ContextFactoryVector& OffscreenCanvas::renderingContextFactorie
s() | 90 OffscreenCanvas::ContextFactoryVector& OffscreenCanvas::renderingContextFactorie
s() |
71 { | 91 { |
(...skipping 14 matching lines...) Expand all Loading... |
86 ASSERT(!renderingContextFactories()[type]); | 106 ASSERT(!renderingContextFactories()[type]); |
87 renderingContextFactories()[type] = renderingContextFactory; | 107 renderingContextFactories()[type] = renderingContextFactory; |
88 } | 108 } |
89 | 109 |
90 DEFINE_TRACE(OffscreenCanvas) | 110 DEFINE_TRACE(OffscreenCanvas) |
91 { | 111 { |
92 visitor->trace(m_context); | 112 visitor->trace(m_context); |
93 } | 113 } |
94 | 114 |
95 } // namespace blink | 115 } // namespace blink |
OLD | NEW |