Index: third_party/WebKit/Source/modules/csspaint/CSSPaintDefinition.cpp |
diff --git a/third_party/WebKit/Source/modules/csspaint/CSSPaintDefinition.cpp b/third_party/WebKit/Source/modules/csspaint/CSSPaintDefinition.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..380d72efb1f744a934957f6b975255f7e6b9a9c2 |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/csspaint/CSSPaintDefinition.cpp |
@@ -0,0 +1,98 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "modules/csspaint/CSSPaintDefinition.h" |
+ |
+#include "bindings/core/v8/ScriptState.h" |
+#include "bindings/core/v8/V8Binding.h" |
+#include "core/dom/ExecutionContext.h" |
+#include "core/frame/ConsoleTypes.h" |
+#include "core/inspector/ConsoleMessage.h" |
+#include "modules/csspaint/PaintRenderingContext2D.h" |
+#include "platform/graphics/ImageBuffer.h" |
+#include "platform/graphics/PaintGeneratedImage.h" |
+#include "platform/graphics/RecordingImageBufferSurface.h" |
+#include "platform/graphics/UnacceleratedImageBufferSurface.h" |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
+class UnacceleratedSurfaceFactory : public RecordingImageBufferFallbackSurfaceFactory { |
+public: |
+ virtual PassOwnPtr<ImageBufferSurface> createSurface(const IntSize& size, OpacityMode opacityMode) |
+ { |
+ // ASSERT_NOT_REACHED(); |
+ // return nullptr; |
+ return adoptPtr(new UnacceleratedImageBufferSurface(size, opacityMode)); |
+ } |
+ |
+ virtual ~UnacceleratedSurfaceFactory() { } |
+}; |
+ |
+} // namespace |
+ |
+PassRefPtrWillBeRawPtr<CSSPaintDefinition> CSSPaintDefinition::create(ScriptState* scriptState, v8::Local<v8::Function> constructor, v8::Local<v8::Function> paint) |
+{ |
+ return adoptRefWillBeNoop(new CSSPaintDefinition(scriptState, constructor, paint)); |
+} |
+ |
+CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::Function> constructor, v8::Local<v8::Function> paint) |
+ : m_scriptState(scriptState) |
+ , m_constructor(scriptState->isolate(), constructor) |
+ , m_paint(scriptState->isolate(), paint) |
+{ |
+} |
+ |
+CSSPaintDefinition::~CSSPaintDefinition() |
+{ |
+} |
+ |
+PassRefPtr<Image> CSSPaintDefinition::paint(const IntSize& size) |
+{ |
+ v8::Isolate* isolate = m_scriptState->isolate(); |
+ |
+ ScriptState::Scope scope(m_scriptState.get()); |
+ v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kRunMicrotasks); |
+ |
+ v8::Local<v8::Object> instance = paintInstance(); |
+ if (isUndefinedOrNull(instance)) |
+ return nullptr; |
+ |
+ OwnPtrWillBeRawPtr<PaintRenderingContext2D> renderingContext = PaintRenderingContext2D::create( |
+ ImageBuffer::create(adoptPtr(new RecordingImageBufferSurface(size, adoptPtr(new UnacceleratedSurfaceFactory()))))); |
+ |
+ v8::Local<v8::Value> argv[] = { |
+ toV8(renderingContext, m_scriptState->context()->Global(), isolate), |
+ toV8(size.width(), m_scriptState->context()->Global(), isolate), |
+ toV8(size.height(), m_scriptState->context()->Global(), isolate) |
+ }; |
+ |
+ v8::Local<v8::Function> paint = m_paint.newLocal(isolate); |
+ |
+ v8::TryCatch exceptionCatcher(isolate); |
+ paint->Call(instance, 3, argv); |
+ |
+ // TODO(ikilpatrick): error message if everything break, saaad trombone. |
+ return PaintGeneratedImage::create(renderingContext->imageBuffer()->getPicture(), size); |
+} |
+ |
+v8::Local<v8::Object> CSSPaintDefinition::paintInstance() |
+{ |
+ if (m_instance.isEmpty()) { |
+ v8::Local<v8::Function> constructor = m_constructor.newLocal(m_scriptState->isolate()); |
+ ASSERT(!isUndefinedOrNull(constructor)); |
+ |
+ v8::Local<v8::Object> paintInstance; |
+ if (!constructor->NewInstance(m_scriptState->context()).ToLocal(&paintInstance)) { |
+ m_scriptState->getExecutionContext()->addConsoleMessage(ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, "Failed to create instance of class.")); |
+ } |
+ |
+ return paintInstance; |
+ } |
+ |
+ return m_instance.newLocal(m_scriptState->isolate()); |
+} |
+ |
+} // namespace blink |