Chromium Code Reviews| Index: third_party/WebKit/Source/modules/offscreencanvas/OffscreenCanvas.cpp |
| diff --git a/third_party/WebKit/Source/modules/offscreencanvas/OffscreenCanvas.cpp b/third_party/WebKit/Source/modules/offscreencanvas/OffscreenCanvas.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6b55fa142e40cd859d850afa1794b9443a66a531 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/offscreencanvas/OffscreenCanvas.cpp |
| @@ -0,0 +1,83 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
|
Justin Novosad
2016/03/07 15:30:26
Not sure why the diff is not tracking this file as
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "modules/offscreencanvas/OffscreenCanvas.h" |
| + |
| +#include "core/html/canvas/CanvasContextCreationAttributes.h" |
| +#include "modules/offscreencanvas/OffscreenCanvasRenderingContext.h" |
| +#include "modules/offscreencanvas/OffscreenCanvasRenderingContextFactory.h" |
| +#include "wtf/MathExtras.h" |
| + |
| +namespace blink { |
| + |
| +OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) |
| +{ |
| + return new OffscreenCanvas(IntSize(clampTo<int>(width), clampTo<int>(height))); |
| +} |
| + |
| +void OffscreenCanvas::setWidth(unsigned width) |
| +{ |
| + m_size.setWidth(clampTo<int>(width)); |
| +} |
| + |
| +void OffscreenCanvas::setHeight(unsigned height) |
| +{ |
| + m_size.setHeight(clampTo<int>(height)); |
| +} |
| + |
| +OffscreenCanvas::OffscreenCanvas(const IntSize& size) |
| + : m_size(size) |
| +{ |
| +} |
| + |
| +OffscreenCanvasRenderingContext* OffscreenCanvas::getContext(const String& id, const CanvasContextCreationAttributes& attributes) |
| +{ |
| + OffscreenCanvasRenderingContext::ContextType contextType = OffscreenCanvasRenderingContext::contextTypeFromId(id); |
| + |
| + // Unknown type. |
| + if (contextType == OffscreenCanvasRenderingContext::ContextTypeCount) |
| + return nullptr; |
| + |
| + OffscreenCanvasRenderingContextFactory* factory = getRenderingContextFactory(contextType); |
| + if (!factory) |
| + return nullptr; |
| + |
| + if (m_context) { |
| + if (m_context->contextType() == contextType) |
| + return m_context.get(); |
| + |
| + factory->onError(this, "OffscreenCanvas has an existing context of a different type"); |
| + return nullptr; |
| + } |
| + |
| + m_context = factory->create(this, attributes); |
| + return m_context.get(); |
| +} |
| + |
| +OffscreenCanvas::ContextFactoryVector& OffscreenCanvas::renderingContextFactories() |
| +{ |
| + DEFINE_STATIC_LOCAL(ContextFactoryVector, s_contextFactories, (OffscreenCanvasRenderingContext::ContextTypeCount)); |
| + return s_contextFactories; |
| +} |
| + |
| +OffscreenCanvasRenderingContextFactory* OffscreenCanvas::getRenderingContextFactory(int type) |
| +{ |
| + ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); |
| + return renderingContextFactories()[type].get(); |
| +} |
| + |
| +void OffscreenCanvas::registerRenderingContextFactory(PassOwnPtr<OffscreenCanvasRenderingContextFactory> renderingContextFactory) |
| +{ |
| + OffscreenCanvasRenderingContext::ContextType type = renderingContextFactory->contextType(); |
| + ASSERT(type < OffscreenCanvasRenderingContext::ContextTypeCount); |
| + ASSERT(!renderingContextFactories()[type]); |
| + renderingContextFactories()[type] = renderingContextFactory; |
| +} |
| + |
| +DEFINE_TRACE(OffscreenCanvas) |
| +{ |
| + visitor->trace(m_context); |
| +} |
| + |
| +} // namespace blink |