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 | |
chrishtr
2016/04/06 18:55:20
Cannot fall back, or has a required un-accelerated
ikilpatrick
2016/04/07 23:03:00
At the moment when you invoke RecordingImageBuffer
| |
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 RawPtr<CSSPaintDefinition> CSSPaintDefinition::create(ScriptState* scriptState, v8::Local<v8::Function> constructor, v8::Local<v8::Function> paint) | 44 RawPtr<CSSPaintDefinition> CSSPaintDefinition::create(ScriptState* scriptState, v8::Local<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 if (isUndefinedOrNull(instance)) | |
69 return nullptr; | |
70 | |
71 PaintRenderingContext2D* renderingContext = PaintRenderingContext2D::create( | |
72 ImageBuffer::create(adoptPtr(new RecordingImageBufferSurface(size, adopt Ptr(new UnacceleratedSurfaceFactory()))))); | |
73 Geometry* geom = Geometry::create(size); | |
74 | |
75 v8::Local<v8::Value> argv[] = { | |
76 toV8(renderingContext, m_scriptState->context()->Global(), isolate), | |
77 toV8(geom, m_scriptState->context()->Global(), isolate) | |
78 }; | |
79 | |
80 v8::Local<v8::Function> paint = m_paint.newLocal(isolate); | |
81 | |
82 ScriptForbiddenScope::AllowUserAgentScript allowScripting; | |
83 v8::TryCatch block(isolate); | |
84 | |
85 V8ScriptRunner::callFunction(paint, m_scriptState->getExecutionContext(), in stance, 2, argv, isolate); | |
86 | |
87 if (block.HasCaught()) { | |
88 block.ReThrow(); | |
89 return nullptr; | |
90 } | |
91 | |
92 return PaintGeneratedImage::create(renderingContext->imageBuffer()->getPictu re(), size); | |
93 } | |
94 | |
95 v8::Local<v8::Object> CSSPaintDefinition::paintInstance() | |
96 { | |
97 v8::Isolate* isolate = m_scriptState->isolate(); | |
98 | |
99 ScriptState::Scope scope(m_scriptState.get()); | |
100 v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kDoNotRunM icrotasks); | |
101 | |
102 if (m_instance.isEmpty()) { | |
103 v8::Local<v8::Function> constructor = m_constructor.newLocal(isolate); | |
104 ASSERT(!isUndefinedOrNull(constructor)); | |
105 | |
106 v8::Local<v8::Object> paintInstance; | |
107 v8::TryCatch block(isolate); | |
108 if (!v8Call(constructor->NewInstance(m_scriptState->context()), paintIns tance, block)) { | |
109 m_scriptState->getExecutionContext()->addConsoleMessage(ConsoleMessa ge::create(JSMessageSource, ErrorMessageLevel, "Failed to create instance of cla ss.")); | |
110 } else { | |
111 m_instance.set(isolate, paintInstance); | |
112 m_instance.setWeak(&m_instance, clearHandle); | |
113 } | |
114 | |
115 return paintInstance; | |
116 } | |
117 | |
118 return m_instance.newLocal(isolate); | |
119 } | |
120 | |
27 } // namespace blink | 121 } // namespace blink |
OLD | NEW |