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 "bindings/core/v8/V8ObjectConstructor.h" |
| 11 #include "core/dom/ExecutionContext.h" |
| 12 #include "modules/csspaint/Geometry.h" |
| 13 #include "modules/csspaint/PaintRenderingContext2D.h" |
| 14 #include "platform/graphics/ImageBuffer.h" |
| 15 #include "platform/graphics/PaintGeneratedImage.h" |
| 16 #include "platform/graphics/RecordingImageBufferSurface.h" |
8 | 17 |
9 namespace blink { | 18 namespace blink { |
10 | 19 |
11 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc
al<v8::Function> constructor, v8::Local<v8::Function> paint) | 20 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc
al<v8::Function> constructor, v8::Local<v8::Function> paint) |
12 { | 21 { |
13 return new CSSPaintDefinition(scriptState, constructor, paint); | 22 return new CSSPaintDefinition(scriptState, constructor, paint); |
14 } | 23 } |
15 | 24 |
16 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F
unction> constructor, v8::Local<v8::Function> paint) | 25 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F
unction> constructor, v8::Local<v8::Function> paint) |
17 : m_scriptState(scriptState) | 26 : m_scriptState(scriptState) |
18 , m_constructor(scriptState->isolate(), constructor) | 27 , m_constructor(scriptState->isolate(), constructor) |
19 , m_paint(scriptState->isolate(), paint) | 28 , m_paint(scriptState->isolate(), paint) |
20 { | 29 { |
21 } | 30 } |
22 | 31 |
23 CSSPaintDefinition::~CSSPaintDefinition() | 32 CSSPaintDefinition::~CSSPaintDefinition() |
24 { | 33 { |
25 } | 34 } |
26 | 35 |
| 36 PassRefPtr<Image> CSSPaintDefinition::paint(const IntSize& size) |
| 37 { |
| 38 ScriptState::Scope scope(m_scriptState.get()); |
| 39 |
| 40 maybeCreatePaintInstance(); |
| 41 |
| 42 v8::Isolate* isolate = m_scriptState->isolate(); |
| 43 v8::Local<v8::Object> instance = m_instance.newLocal(isolate); |
| 44 |
| 45 // We may have failed to create an instance class, in which case produce an |
| 46 // invalid image. |
| 47 if (isUndefinedOrNull(instance)) |
| 48 return nullptr; |
| 49 |
| 50 PaintRenderingContext2D* renderingContext = PaintRenderingContext2D::create( |
| 51 ImageBuffer::create(adoptPtr(new RecordingImageBufferSurface(size)))); |
| 52 Geometry* geometry = Geometry::create(size); |
| 53 |
| 54 v8::Local<v8::Value> argv[] = { |
| 55 toV8(renderingContext, m_scriptState->context()->Global(), isolate), |
| 56 toV8(geometry, m_scriptState->context()->Global(), isolate) |
| 57 }; |
| 58 |
| 59 v8::Local<v8::Function> paint = m_paint.newLocal(isolate); |
| 60 |
| 61 v8::TryCatch block(isolate); |
| 62 block.SetVerbose(true); |
| 63 |
| 64 V8ScriptRunner::callFunction(paint, m_scriptState->getExecutionContext(), in
stance, 2, argv, isolate); |
| 65 |
| 66 // The paint function may have produced an error, in which case produce an |
| 67 // invalid image. |
| 68 if (block.HasCaught()) { |
| 69 return nullptr; |
| 70 } |
| 71 |
| 72 return PaintGeneratedImage::create(renderingContext->imageBuffer()->getPictu
re(), size); |
| 73 } |
| 74 |
| 75 void CSSPaintDefinition::maybeCreatePaintInstance() |
| 76 { |
| 77 if (m_didCallConstructor) |
| 78 return; |
| 79 |
| 80 DCHECK(m_instance.isEmpty()); |
| 81 |
| 82 v8::Isolate* isolate = m_scriptState->isolate(); |
| 83 v8::Local<v8::Function> constructor = m_constructor.newLocal(isolate); |
| 84 DCHECK(!isUndefinedOrNull(constructor)); |
| 85 |
| 86 v8::Local<v8::Object> paintInstance; |
| 87 if (V8ObjectConstructor::newInstance(isolate, constructor).ToLocal(&paintIns
tance)) { |
| 88 m_instance.set(isolate, paintInstance); |
| 89 } |
| 90 |
| 91 m_didCallConstructor = true; |
| 92 } |
| 93 |
27 } // namespace blink | 94 } // namespace blink |
OLD | NEW |