Chromium Code Reviews| 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 "core/frame/ConsoleTypes.h" | |
| 13 #include "core/inspector/ConsoleMessage.h" | |
| 14 #include "modules/csspaint/Geometry.h" | |
| 15 #include "modules/csspaint/PaintRenderingContext2D.h" | |
| 16 #include "platform/ScriptForbiddenScope.h" | |
| 17 #include "platform/graphics/ImageBuffer.h" | |
| 18 #include "platform/graphics/PaintGeneratedImage.h" | |
| 19 #include "platform/graphics/RecordingImageBufferSurface.h" | |
| 20 #include "platform/graphics/UnacceleratedImageBufferSurface.h" | |
| 8 | 21 |
| 9 namespace blink { | 22 namespace blink { |
| 10 | 23 |
| 24 namespace { | |
| 25 | |
| 26 // TODO(ikilpatrick): Modify RecordingImageBufferSurface so that it cannot | |
| 27 // fallback to a UnacceleratedImageBufferSurface. | |
| 28 class UnacceleratedSurfaceFactory : public RecordingImageBufferFallbackSurfaceFa ctory { | |
| 29 public: | |
| 30 virtual PassOwnPtr<ImageBufferSurface> createSurface(const IntSize& size, Op acityMode opacityMode) | |
| 31 { | |
| 32 return adoptPtr(new UnacceleratedImageBufferSurface(size, opacityMode)); | |
| 33 } | |
| 34 | |
| 35 virtual ~UnacceleratedSurfaceFactory() { } | |
| 36 }; | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 11 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc al<v8::Function> constructor, v8::Local<v8::Function> paint) | 40 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc al<v8::Function> constructor, v8::Local<v8::Function> paint) |
| 12 { | 41 { |
| 13 return new CSSPaintDefinition(scriptState, constructor, paint); | 42 return new CSSPaintDefinition(scriptState, constructor, paint); |
| 14 } | 43 } |
| 15 | 44 |
| 16 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F unction> constructor, v8::Local<v8::Function> paint) | 45 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F unction> constructor, v8::Local<v8::Function> paint) |
| 17 : m_scriptState(scriptState) | 46 : m_scriptState(scriptState) |
| 18 , m_constructor(scriptState->isolate(), constructor) | 47 , m_constructor(scriptState->isolate(), constructor) |
| 19 , m_paint(scriptState->isolate(), paint) | 48 , m_paint(scriptState->isolate(), paint) |
| 20 { | 49 { |
| 21 } | 50 } |
| 22 | 51 |
| 23 CSSPaintDefinition::~CSSPaintDefinition() | 52 CSSPaintDefinition::~CSSPaintDefinition() |
| 24 { | 53 { |
| 25 } | 54 } |
| 26 | 55 |
| 56 PassRefPtr<Image> CSSPaintDefinition::paint(const IntSize& size) | |
| 57 { | |
| 58 ScriptState::Scope scope(m_scriptState.get()); | |
| 59 | |
| 60 maybeCreatePaintInstance(); | |
| 61 | |
| 62 v8::Isolate* isolate = m_scriptState->isolate(); | |
| 63 v8::Local<v8::Object> instance = m_instance.newLocal(isolate); | |
| 64 | |
| 65 // We may have failed to create an instance class, in which case produce an | |
| 66 // invalid image. | |
| 67 if (isUndefinedOrNull(instance)) | |
| 68 return nullptr; | |
| 69 | |
| 70 PaintRenderingContext2D* renderingContext = PaintRenderingContext2D::create( | |
| 71 ImageBuffer::create(adoptPtr(new RecordingImageBufferSurface(size, adopt Ptr(new UnacceleratedSurfaceFactory()))))); | |
| 72 Geometry* geometry = Geometry::create(size); | |
| 73 | |
| 74 v8::Local<v8::Value> argv[] = { | |
| 75 toV8(renderingContext, m_scriptState->context()->Global(), isolate), | |
| 76 toV8(geometry, m_scriptState->context()->Global(), isolate) | |
| 77 }; | |
| 78 | |
| 79 v8::Local<v8::Function> paint = m_paint.newLocal(isolate); | |
| 80 | |
| 81 v8::TryCatch block(isolate); | |
| 82 V8ScriptRunner::callFunction(paint, m_scriptState->getExecutionContext(), in stance, 2, argv, isolate); | |
| 83 | |
| 84 // The paint function may have produced an error, in which case produce an | |
| 85 // invalid image. | |
| 86 if (block.HasCaught()) { | |
| 87 return nullptr; | |
| 88 } | |
| 89 | |
| 90 return PaintGeneratedImage::create(renderingContext->imageBuffer()->getPictu re(), size); | |
| 91 } | |
| 92 | |
| 93 void CSSPaintDefinition::maybeCreatePaintInstance() | |
| 94 { | |
| 95 DCHECK(!m_invalidConstructor || !m_instance.isEmpty()); | |
| 96 | |
| 97 if (m_invalidConstructor) | |
|
haraken
2016/04/12 00:44:20
I'd replace m_invalidConstructor with m_didCallCon
ikilpatrick
2016/04/12 03:08:09
Yup we just call the constructor once. In the spec
| |
| 98 return; | |
| 99 | |
| 100 if (!m_instance.isEmpty()) | |
| 101 return; | |
| 102 | |
| 103 v8::Isolate* isolate = m_scriptState->isolate(); | |
| 104 v8::Local<v8::Function> constructor = m_constructor.newLocal(isolate); | |
| 105 DCHECK(!isUndefinedOrNull(constructor)); | |
| 106 | |
| 107 v8::Local<v8::Object> paintInstance; | |
| 108 if (!V8ObjectConstructor::newInstance(isolate, constructor).ToLocal(&paintIn stance)) { | |
| 109 m_invalidConstructor = true; | |
| 110 } else { | |
| 111 m_instance.set(isolate, paintInstance); | |
| 112 } | |
| 113 } | |
| 114 | |
| 27 } // namespace blink | 115 } // namespace blink |
| OLD | NEW |