OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/offscreencanvas/OffscreenCanvas.h" |
| 6 |
| 7 #include "core/html/canvas/CanvasContextCreationAttributes.h" |
| 8 #include "modules/offscreencanvas/OffscreenCanvasRenderingContext.h" |
| 9 #include "modules/offscreencanvas/OffscreenCanvasRenderingContextFactory.h" |
| 10 #include "wtf/MathExtras.h" |
| 11 |
| 12 namespace blink { |
| 13 |
| 14 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) |
| 15 { |
| 16 return new OffscreenCanvas(IntSize(clampTo<int>(width), clampTo<int>(height)
)); |
| 17 } |
| 18 |
| 19 void OffscreenCanvas::setWidth(unsigned width) |
| 20 { |
| 21 m_size.setWidth(clampTo<int>(width)); |
| 22 } |
| 23 |
| 24 void OffscreenCanvas::setHeight(unsigned height) |
| 25 { |
| 26 m_size.setHeight(clampTo<int>(height)); |
| 27 } |
| 28 |
| 29 OffscreenCanvas::OffscreenCanvas(const IntSize& size) |
| 30 : m_size(size) |
| 31 { |
| 32 } |
| 33 |
| 34 OffscreenCanvasRenderingContext* OffscreenCanvas::getContext(const String& id, c
onst CanvasContextCreationAttributes& attributes) |
| 35 { |
| 36 OffscreenCanvasRenderingContext::ContextType contextType = OffscreenCanvasRe
nderingContext::contextTypeFromId(id); |
| 37 |
| 38 // Unknown type. |
| 39 if (contextType == OffscreenCanvasRenderingContext::ContextTypeCount) |
| 40 return nullptr; |
| 41 |
| 42 OffscreenCanvasRenderingContextFactory* factory = getRenderingContextFactory
(contextType); |
| 43 if (!factory) |
| 44 return nullptr; |
| 45 |
| 46 if (m_context) { |
| 47 if (m_context->contextType() == contextType) |
| 48 return m_context.get(); |
| 49 |
| 50 factory->onError(this, "OffscreenCanvas has an existing context of a dif
ferent type"); |
| 51 return nullptr; |
| 52 } |
| 53 |
| 54 m_context = factory->create(this, attributes); |
| 55 return m_context.get(); |
| 56 } |
| 57 |
| 58 OffscreenCanvas::ContextFactoryVector& OffscreenCanvas::renderingContextFactorie
s() |
| 59 { |
| 60 DEFINE_STATIC_LOCAL(ContextFactoryVector, s_contextFactories, (OffscreenCanv
asRenderingContext::ContextTypeCount)); |
| 61 return s_contextFactories; |
| 62 } |
| 63 |
| 64 OffscreenCanvasRenderingContextFactory* OffscreenCanvas::getRenderingContextFact
ory(int type) |
| 65 { |
| 66 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); |
| 67 return renderingContextFactories()[type].get(); |
| 68 } |
| 69 |
| 70 void OffscreenCanvas::registerRenderingContextFactory(PassOwnPtr<OffscreenCanvas
RenderingContextFactory> renderingContextFactory) |
| 71 { |
| 72 OffscreenCanvasRenderingContext::ContextType type = renderingContextFactory-
>contextType(); |
| 73 ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); |
| 74 ASSERT(!renderingContextFactories()[type]); |
| 75 renderingContextFactories()[type] = renderingContextFactory; |
| 76 } |
| 77 |
| 78 DEFINE_TRACE(OffscreenCanvas) |
| 79 { |
| 80 visitor->trace(m_context); |
| 81 } |
| 82 |
| 83 } // namespace blink |
OLD | NEW |