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" | 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "bindings/core/v8/V8BindingMacros.h" | 9 #include "bindings/core/v8/V8BindingMacros.h" |
| 10 #include "bindings/core/v8/V8ObjectConstructor.h" | 10 #include "bindings/core/v8/V8ObjectConstructor.h" |
| 11 #include "core/css/CSSComputedStyleDeclaration.h" | 11 #include "core/css/CSSComputedStyleDeclaration.h" |
| 12 #include "core/css/cssom/FilteredComputedStylePropertyMap.h" | 12 #include "core/css/cssom/FilteredComputedStylePropertyMap.h" |
| 13 #include "core/dom/ExecutionContext.h" | 13 #include "core/dom/ExecutionContext.h" |
| 14 #include "core/layout/LayoutObject.h" | 14 #include "core/layout/LayoutObject.h" |
| 15 #include "modules/csspaint/PaintRenderingContext2D.h" | 15 #include "modules/csspaint/PaintRenderingContext2D.h" |
| 16 #include "modules/csspaint/PaintSize.h" | 16 #include "modules/csspaint/PaintSize.h" |
| 17 #include "platform/graphics/ImageBuffer.h" | 17 #include "platform/graphics/ImageBuffer.h" |
| 18 #include "platform/graphics/PaintGeneratedImage.h" | 18 #include "platform/graphics/PaintGeneratedImage.h" |
| 19 #include "platform/graphics/RecordingImageBufferSurface.h" | 19 #include "platform/graphics/RecordingImageBufferSurface.h" |
| 20 #include "wtf/PtrUtil.h" | 20 #include "wtf/PtrUtil.h" |
| 21 | 21 |
| 22 namespace blink { | 22 namespace blink { |
| 23 | 23 |
| 24 namespace { | |
| 25 | |
| 26 IntSize adjustToCssSize(const IntSize& size, float zoom) | |
|
Justin Novosad
2016/07/04 15:45:59
This name is confusing IMHO. My understanding is t
Gleb Lanbin
2016/07/06 20:36:18
done, renamed to getSpecifiedSize, according to Ob
| |
| 27 { | |
| 28 IntSize newSize(size); | |
| 29 newSize.scale(1 / zoom); | |
|
Justin Novosad
2016/07/04 15:45:59
Due to floating-point precision issues, this arith
Gleb Lanbin
2016/07/06 20:36:18
Done.
| |
| 30 return newSize; | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 24 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc al<v8::Function> constructor, v8::Local<v8::Function> paint, Vector<CSSPropertyI D>& nativeInvalidationProperties, Vector<AtomicString>& customInvalidationProper ties) | 35 CSSPaintDefinition* CSSPaintDefinition::create(ScriptState* scriptState, v8::Loc al<v8::Function> constructor, v8::Local<v8::Function> paint, Vector<CSSPropertyI D>& nativeInvalidationProperties, Vector<AtomicString>& customInvalidationProper ties) |
| 25 { | 36 { |
| 26 return new CSSPaintDefinition(scriptState, constructor, paint, nativeInvalid ationProperties, customInvalidationProperties); | 37 return new CSSPaintDefinition(scriptState, constructor, paint, nativeInvalid ationProperties, customInvalidationProperties); |
| 27 } | 38 } |
| 28 | 39 |
| 29 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F unction> constructor, v8::Local<v8::Function> paint, Vector<CSSPropertyID>& nati veInvalidationProperties, Vector<AtomicString>& customInvalidationProperties) | 40 CSSPaintDefinition::CSSPaintDefinition(ScriptState* scriptState, v8::Local<v8::F unction> constructor, v8::Local<v8::Function> paint, Vector<CSSPropertyID>& nati veInvalidationProperties, Vector<AtomicString>& customInvalidationProperties) |
| 30 : m_scriptState(scriptState) | 41 : m_scriptState(scriptState) |
| 31 , m_constructor(scriptState->isolate(), constructor) | 42 , m_constructor(scriptState->isolate(), constructor) |
| 32 , m_paint(scriptState->isolate(), paint) | 43 , m_paint(scriptState->isolate(), paint) |
| 33 { | 44 { |
| 34 m_nativeInvalidationProperties.swap(nativeInvalidationProperties); | 45 m_nativeInvalidationProperties.swap(nativeInvalidationProperties); |
| 35 m_customInvalidationProperties.swap(customInvalidationProperties); | 46 m_customInvalidationProperties.swap(customInvalidationProperties); |
| 36 } | 47 } |
| 37 | 48 |
| 38 CSSPaintDefinition::~CSSPaintDefinition() | 49 CSSPaintDefinition::~CSSPaintDefinition() |
| 39 { | 50 { |
| 40 } | 51 } |
| 41 | 52 |
| 42 PassRefPtr<Image> CSSPaintDefinition::paint(const LayoutObject& layoutObject, co nst IntSize& size) | 53 PassRefPtr<Image> CSSPaintDefinition::paint(const LayoutObject& layoutObject, co nst IntSize& size, float zoom) |
| 43 { | 54 { |
| 44 ScriptState::Scope scope(m_scriptState.get()); | 55 ScriptState::Scope scope(m_scriptState.get()); |
| 45 | 56 |
| 46 maybeCreatePaintInstance(); | 57 maybeCreatePaintInstance(); |
| 47 | 58 |
| 48 v8::Isolate* isolate = m_scriptState->isolate(); | 59 v8::Isolate* isolate = m_scriptState->isolate(); |
| 49 v8::Local<v8::Object> instance = m_instance.newLocal(isolate); | 60 v8::Local<v8::Object> instance = m_instance.newLocal(isolate); |
| 50 | 61 |
| 51 // We may have failed to create an instance class, in which case produce an | 62 // We may have failed to create an instance class, in which case produce an |
| 52 // invalid image. | 63 // invalid image. |
| 53 if (isUndefinedOrNull(instance)) | 64 if (isUndefinedOrNull(instance)) |
| 54 return nullptr; | 65 return nullptr; |
| 55 | 66 |
| 56 DCHECK(layoutObject.node()); | 67 DCHECK(layoutObject.node()); |
| 57 | 68 |
| 69 const auto cssSize = adjustToCssSize(size, zoom); | |
|
Justin Novosad
2016/07/04 15:45:58
abuse of "auto".
Gleb Lanbin
2016/07/06 20:36:18
Done.
| |
| 58 PaintRenderingContext2D* renderingContext = PaintRenderingContext2D::create( | 70 PaintRenderingContext2D* renderingContext = PaintRenderingContext2D::create( |
| 59 ImageBuffer::create(wrapUnique(new RecordingImageBufferSurface(size)))); | 71 ImageBuffer::create(wrapUnique(new RecordingImageBufferSurface(size)))); |
| 60 PaintSize* paintSize = PaintSize::create(size); | 72 PaintSize* paintSize = PaintSize::create(cssSize); |
| 61 StylePropertyMap* styleMap = FilteredComputedStylePropertyMap::create( | 73 StylePropertyMap* styleMap = FilteredComputedStylePropertyMap::create( |
| 62 CSSComputedStyleDeclaration::create(layoutObject.node()), | 74 CSSComputedStyleDeclaration::create(layoutObject.node()), |
| 63 m_nativeInvalidationProperties, m_customInvalidationProperties); | 75 m_nativeInvalidationProperties, m_customInvalidationProperties); |
| 64 | 76 |
| 65 v8::Local<v8::Value> argv[] = { | 77 v8::Local<v8::Value> argv[] = { |
| 66 toV8(renderingContext, m_scriptState->context()->Global(), isolate), | 78 toV8(renderingContext, m_scriptState->context()->Global(), isolate), |
| 67 toV8(paintSize, m_scriptState->context()->Global(), isolate), | 79 toV8(paintSize, m_scriptState->context()->Global(), isolate), |
| 68 toV8(styleMap, m_scriptState->context()->Global(), isolate) | 80 toV8(styleMap, m_scriptState->context()->Global(), isolate) |
| 69 }; | 81 }; |
| 70 | 82 |
| 71 v8::Local<v8::Function> paint = m_paint.newLocal(isolate); | 83 v8::Local<v8::Function> paint = m_paint.newLocal(isolate); |
| 72 | 84 |
| 73 v8::TryCatch block(isolate); | 85 v8::TryCatch block(isolate); |
| 74 block.SetVerbose(true); | 86 block.SetVerbose(true); |
| 75 | 87 |
| 76 V8ScriptRunner::callFunction(paint, m_scriptState->getExecutionContext(), in stance, 3, argv, isolate); | 88 V8ScriptRunner::callFunction(paint, m_scriptState->getExecutionContext(), in stance, 3, argv, isolate); |
| 77 | 89 |
| 78 // The paint function may have produced an error, in which case produce an | 90 // The paint function may have produced an error, in which case produce an |
| 79 // invalid image. | 91 // invalid image. |
| 80 if (block.HasCaught()) { | 92 if (block.HasCaught()) { |
| 81 return nullptr; | 93 return nullptr; |
| 82 } | 94 } |
| 83 | 95 |
| 84 return PaintGeneratedImage::create(renderingContext->imageBuffer()->getPictu re(), size); | 96 return PaintGeneratedImage::create(renderingContext->imageBuffer()->getPictu re(), cssSize); |
| 85 } | 97 } |
| 86 | 98 |
| 87 void CSSPaintDefinition::maybeCreatePaintInstance() | 99 void CSSPaintDefinition::maybeCreatePaintInstance() |
| 88 { | 100 { |
| 89 if (m_didCallConstructor) | 101 if (m_didCallConstructor) |
| 90 return; | 102 return; |
| 91 | 103 |
| 92 DCHECK(m_instance.isEmpty()); | 104 DCHECK(m_instance.isEmpty()); |
| 93 | 105 |
| 94 v8::Isolate* isolate = m_scriptState->isolate(); | 106 v8::Isolate* isolate = m_scriptState->isolate(); |
| 95 v8::Local<v8::Function> constructor = m_constructor.newLocal(isolate); | 107 v8::Local<v8::Function> constructor = m_constructor.newLocal(isolate); |
| 96 DCHECK(!isUndefinedOrNull(constructor)); | 108 DCHECK(!isUndefinedOrNull(constructor)); |
| 97 | 109 |
| 98 v8::Local<v8::Object> paintInstance; | 110 v8::Local<v8::Object> paintInstance; |
| 99 if (V8ObjectConstructor::newInstance(isolate, constructor).ToLocal(&paintIns tance)) { | 111 if (V8ObjectConstructor::newInstance(isolate, constructor).ToLocal(&paintIns tance)) { |
| 100 m_instance.set(isolate, paintInstance); | 112 m_instance.set(isolate, paintInstance); |
| 101 } | 113 } |
| 102 | 114 |
| 103 m_didCallConstructor = true; | 115 m_didCallConstructor = true; |
| 104 } | 116 } |
| 105 | 117 |
| 106 } // namespace blink | 118 } // namespace blink |
| OLD | NEW |