OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/csspaint/CSSPaintDefinition.h" |
| 6 |
| 7 #include "bindings/core/v8/ScriptState.h" |
| 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "core/dom/ExecutionContext.h" |
| 10 #include "core/frame/ConsoleTypes.h" |
| 11 #include "core/inspector/ConsoleMessage.h" |
| 12 #include "modules/csspaint/PaintRenderingContext2D.h" |
| 13 #include "platform/graphics/ImageBuffer.h" |
| 14 #include "platform/graphics/PaintGeneratedImage.h" |
| 15 #include "platform/graphics/RecordingImageBufferSurface.h" |
| 16 #include "platform/graphics/UnacceleratedImageBufferSurface.h" |
| 17 |
| 18 namespace blink { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class UnacceleratedSurfaceFactory : public RecordingImageBufferFallbackSurfaceFa
ctory { |
| 23 public: |
| 24 virtual PassOwnPtr<ImageBufferSurface> createSurface(const IntSize& size, Op
acityMode opacityMode) |
| 25 { |
| 26 // ASSERT_NOT_REACHED(); |
| 27 // return nullptr; |
| 28 return adoptPtr(new UnacceleratedImageBufferSurface(size, opacityMode)); |
| 29 } |
| 30 |
| 31 virtual ~UnacceleratedSurfaceFactory() { } |
| 32 }; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 PassRefPtrWillBeRawPtr<CSSPaintDefinition> CSSPaintDefinition::create(ScriptStat
e* scriptState, v8::Local<v8::Function> constructor, v8::Local<v8::Function> pai
nt) |
| 37 { |
| 38 return adoptRefWillBeNoop(new CSSPaintDefinition(scriptState, constructor, p
aint)); |
| 39 } |
| 40 |
| 41 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F
unction> constructor, v8::Local<v8::Function> paint) |
| 42 : m_scriptState(scriptState) |
| 43 , m_constructor(scriptState->isolate(), constructor) |
| 44 , m_paint(scriptState->isolate(), paint) |
| 45 { |
| 46 } |
| 47 |
| 48 CSSPaintDefinition::~CSSPaintDefinition() |
| 49 { |
| 50 } |
| 51 |
| 52 PassRefPtr<Image> CSSPaintDefinition::paint(const IntSize& size) |
| 53 { |
| 54 v8::Isolate* isolate = m_scriptState->isolate(); |
| 55 |
| 56 ScriptState::Scope scope(m_scriptState.get()); |
| 57 v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kRunMicrot
asks); |
| 58 |
| 59 v8::Local<v8::Object> instance = paintInstance(); |
| 60 if (isUndefinedOrNull(instance)) |
| 61 return nullptr; |
| 62 |
| 63 OwnPtrWillBeRawPtr<PaintRenderingContext2D> renderingContext = PaintRenderin
gContext2D::create( |
| 64 ImageBuffer::create(adoptPtr(new RecordingImageBufferSurface(size, adopt
Ptr(new UnacceleratedSurfaceFactory()))))); |
| 65 |
| 66 v8::Local<v8::Value> argv[] = { |
| 67 toV8(renderingContext, m_scriptState->context()->Global(), isolate), |
| 68 toV8(size.width(), m_scriptState->context()->Global(), isolate), |
| 69 toV8(size.height(), m_scriptState->context()->Global(), isolate) |
| 70 }; |
| 71 |
| 72 v8::Local<v8::Function> paint = m_paint.newLocal(isolate); |
| 73 |
| 74 v8::TryCatch exceptionCatcher(isolate); |
| 75 paint->Call(instance, 3, argv); |
| 76 |
| 77 // TODO(ikilpatrick): error message if everything break, saaad trombone. |
| 78 return PaintGeneratedImage::create(renderingContext->imageBuffer()->getPictu
re(), size); |
| 79 } |
| 80 |
| 81 v8::Local<v8::Object> CSSPaintDefinition::paintInstance() |
| 82 { |
| 83 if (m_instance.isEmpty()) { |
| 84 v8::Local<v8::Function> constructor = m_constructor.newLocal(m_scriptSta
te->isolate()); |
| 85 ASSERT(!isUndefinedOrNull(constructor)); |
| 86 |
| 87 v8::Local<v8::Object> paintInstance; |
| 88 if (!constructor->NewInstance(m_scriptState->context()).ToLocal(&paintIn
stance)) { |
| 89 m_scriptState->getExecutionContext()->addConsoleMessage(ConsoleMessa
ge::create(JSMessageSource, ErrorMessageLevel, "Failed to create instance of cla
ss.")); |
| 90 } |
| 91 |
| 92 return paintInstance; |
| 93 } |
| 94 |
| 95 return m_instance.newLocal(m_scriptState->isolate()); |
| 96 } |
| 97 |
| 98 } // namespace blink |
OLD | NEW |