OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/csspaint/CSSPaintDefinition.h" | 5 #include "modules/csspaint/CSSPaintDefinition.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
| 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "bindings/core/v8/V8BindingMacros.h" |
| 10 #include "core/dom/ExecutionContext.h" |
| 11 #include "core/frame/ConsoleTypes.h" |
| 12 #include "core/inspector/ConsoleMessage.h" |
| 13 #include "modules/csspaint/Geometry.h" |
| 14 #include "modules/csspaint/PaintRenderingContext2D.h" |
| 15 #include "platform/ScriptForbiddenScope.h" |
| 16 #include "platform/graphics/ImageBuffer.h" |
| 17 #include "platform/graphics/PaintGeneratedImage.h" |
| 18 #include "platform/graphics/RecordingImageBufferSurface.h" |
| 19 #include "platform/graphics/UnacceleratedImageBufferSurface.h" |
8 | 20 |
9 namespace blink { | 21 namespace blink { |
10 | 22 |
| 23 namespace { |
| 24 |
| 25 // TODO(ikilpatrick): Modify RecordingImageBufferSurface so that it cannot |
| 26 // fallback to a UnacceleratedImageBufferSurface. |
| 27 class UnacceleratedSurfaceFactory : public RecordingImageBufferFallbackSurfaceFa
ctory { |
| 28 public: |
| 29 virtual PassOwnPtr<ImageBufferSurface> createSurface(const IntSize& size, Op
acityMode opacityMode) |
| 30 { |
| 31 return adoptPtr(new UnacceleratedImageBufferSurface(size, opacityMode)); |
| 32 } |
| 33 |
| 34 virtual ~UnacceleratedSurfaceFactory() { } |
| 35 }; |
| 36 |
| 37 static void clearHandle(const v8::WeakCallbackInfo<ScopedPersistent<v8::Object>>
& data) |
| 38 { |
| 39 data.GetParameter()->clear(); |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
11 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc
al<v8::Function> constructor, v8::Local<v8::Function> paint) | 44 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc
al<v8::Function> constructor, v8::Local<v8::Function> paint) |
12 { | 45 { |
13 return new CSSPaintDefinition(scriptState, constructor, paint); | 46 return new CSSPaintDefinition(scriptState, constructor, paint); |
14 } | 47 } |
15 | 48 |
16 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F
unction> constructor, v8::Local<v8::Function> paint) | 49 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F
unction> constructor, v8::Local<v8::Function> paint) |
17 : m_scriptState(scriptState) | 50 : m_scriptState(scriptState) |
18 , m_constructor(scriptState->isolate(), constructor) | 51 , m_constructor(scriptState->isolate(), constructor) |
19 , m_paint(scriptState->isolate(), paint) | 52 , m_paint(scriptState->isolate(), paint) |
20 { | 53 { |
21 } | 54 } |
22 | 55 |
23 CSSPaintDefinition::~CSSPaintDefinition() | 56 CSSPaintDefinition::~CSSPaintDefinition() |
24 { | 57 { |
25 } | 58 } |
26 | 59 |
| 60 PassRefPtr<Image> CSSPaintDefinition::paint(const IntSize& size) |
| 61 { |
| 62 v8::Isolate* isolate = m_scriptState->isolate(); |
| 63 |
| 64 ScriptState::Scope scope(m_scriptState.get()); |
| 65 v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kRunMicrot
asks); |
| 66 |
| 67 v8::Local<v8::Object> instance = m_instance.newLocal(m_scriptState->isolate(
)); |
| 68 |
| 69 // We may have failed to create an instance class, in which case produce an |
| 70 // invalid image. |
| 71 if (isUndefinedOrNull(instance)) |
| 72 return nullptr; |
| 73 |
| 74 PaintRenderingContext2D* renderingContext = PaintRenderingContext2D::create( |
| 75 ImageBuffer::create(adoptPtr(new RecordingImageBufferSurface(size, adopt
Ptr(new UnacceleratedSurfaceFactory()))))); |
| 76 Geometry* geom = Geometry::create(size); |
| 77 |
| 78 v8::Local<v8::Value> argv[] = { |
| 79 toV8(renderingContext, m_scriptState->context()->Global(), isolate), |
| 80 toV8(geom, m_scriptState->context()->Global(), isolate) |
| 81 }; |
| 82 |
| 83 v8::Local<v8::Function> paint = m_paint.newLocal(isolate); |
| 84 |
| 85 ScriptForbiddenScope::AllowUserAgentScript allowScripting; |
| 86 v8::TryCatch block(isolate); |
| 87 |
| 88 V8ScriptRunner::callFunction(paint, m_scriptState->getExecutionContext(), in
stance, 2, argv, isolate); |
| 89 |
| 90 // The paint function may have produced an error, in which case produce an |
| 91 // invalid image. |
| 92 if (block.HasCaught()) { |
| 93 // TODO(ikilpatrick): messageHandlerInMainThread doesn't currently know |
| 94 // about main-thread worklets. |
| 95 m_scriptState->getExecutionContext()->addConsoleMessage(ConsoleMessage::
create(JSMessageSource, ErrorMessageLevel, "Failed to invoke the paint method.")
); |
| 96 return nullptr; |
| 97 } |
| 98 |
| 99 return PaintGeneratedImage::create(renderingContext->imageBuffer()->getPictu
re(), size); |
| 100 } |
| 101 |
| 102 v8::Local<v8::Object> CSSPaintDefinition::paintInstance() |
| 103 { |
| 104 v8::Isolate* isolate = m_scriptState->isolate(); |
| 105 |
| 106 ScriptState::Scope scope(m_scriptState.get()); |
| 107 v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kDoNotRunM
icrotasks); |
| 108 |
| 109 if (m_instance.isEmpty()) { |
| 110 v8::Local<v8::Function> constructor = m_constructor.newLocal(isolate); |
| 111 ASSERT(!isUndefinedOrNull(constructor)); |
| 112 |
| 113 v8::Local<v8::Object> paintInstance; |
| 114 v8::TryCatch block(isolate); |
| 115 if (!v8Call(constructor->NewInstance(m_scriptState->context()), paintIns
tance, block)) { |
| 116 // TODO(ikilpatrick): messageHandlerInMainThread doesn't currently |
| 117 // know about main-thread worklets. |
| 118 m_scriptState->getExecutionContext()->addConsoleMessage(ConsoleMessa
ge::create(JSMessageSource, ErrorMessageLevel, "Failed to create instance of cla
ss.")); |
| 119 } else { |
| 120 m_instance.set(isolate, paintInstance); |
| 121 m_instance.setWeak(&m_instance, clearHandle); |
| 122 } |
| 123 |
| 124 return paintInstance; |
| 125 } |
| 126 |
| 127 return m_instance.newLocal(isolate); |
| 128 } |
| 129 |
27 } // namespace blink | 130 } // namespace blink |
OLD | NEW |