diff --git a/engine/bindings/scripts/dart_types.py b/engine/bindings/scripts/dart_types.py index 2a063b87a59..8563c222e30 100644 --- a/engine/bindings/scripts/dart_types.py +++ b/engine/bindings/scripts/dart_types.py @@ -114,7 +114,9 @@ CPP_SPECIAL_CONVERSION_RULES = { 'boolean': 'bool', 'unrestricted double': 'double', 'unrestricted float': 'float', - 'Rect': 'Rect', # Pass Rect by value, not pointer. + # Pass these by value, not pointer. + 'Rect': 'Rect', + 'Color': 'CanvasColor', } @@ -354,6 +356,7 @@ DART_TO_CPP_VALUE = { # We should handle it automatically, but map to a String for now. 'StorageType': 'DartUtilities::dartToString(args, {index}, exception, {auto_scope})', 'Rect': 'DartConverter<{implemented_as}>::FromArguments{null_check}(args, {index}, exception)', + 'Color': 'DartConverter::FromArguments{null_check}(args, {index}, exception)', } @@ -618,6 +621,7 @@ DART_SET_RETURN_VALUE = { # Typed arrays don't have special Dart* classes for Dart. 'ArrayBuffer': 'Dart_SetReturnValue(args, DartUtilities::arrayBufferToDart({cpp_value}))', 'TypedList': 'Dart_SetReturnValue(args, DartUtilities::arrayBufferViewToDart({cpp_value}))', + 'Color': 'DartConverter::SetReturnValue(args, {cpp_value})', } diff --git a/engine/core/core.gni b/engine/core/core.gni index 536494015d2..aee3bab65c0 100644 --- a/engine/core/core.gni +++ b/engine/core/core.gni @@ -837,6 +837,8 @@ sky_core_files = [ "page/SpellCheckerClient.h", "painting/Canvas.cpp", "painting/Canvas.h", + "painting/CanvasColor.cpp", + "painting/CanvasColor.h", "painting/CanvasImage.cpp", "painting/CanvasImage.h", "painting/CanvasPath.cpp", @@ -1148,6 +1150,7 @@ core_idl_files = get_path_info([ "abspath") core_dart_files = get_path_info([ + "painting/Color.dart", "painting/Point.dart", "painting/Rect.dart", "painting/Size.dart", diff --git a/engine/core/painting/CanvasColor.cpp b/engine/core/painting/CanvasColor.cpp new file mode 100644 index 00000000000..9788da811c8 --- /dev/null +++ b/engine/core/painting/CanvasColor.cpp @@ -0,0 +1,45 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/engine/config.h" +#include "sky/engine/core/painting/CanvasColor.h" + +#include "sky/engine/core/script/dom_dart_state.h" +#include "sky/engine/tonic/dart_builtin.h" +#include "sky/engine/tonic/dart_error.h" +#include "sky/engine/tonic/dart_value.h" + +namespace blink { + +// Convert dart_color => SkColor. +CanvasColor DartConverter::FromArgumentsWithNullCheck( + Dart_NativeArguments args, + int index, + Dart_Handle& exception) { + CanvasColor result; + + Dart_Handle dart_color = Dart_GetNativeArgument(args, index); + DCHECK(!LogIfError(dart_color)); + + Dart_Handle value = + Dart_GetField(dart_color, DOMDartState::Current()->value_handle()); + + uint64_t color = 0; + Dart_Handle rv = Dart_IntegerToUint64(value, &color); + DCHECK(!LogIfError(rv)); + DCHECK(color <= 0xffffffff); + + result.sk_color = static_cast(color); + return result; +} + +void DartConverter::SetReturnValue( + Dart_NativeArguments args, unsigned val) { + Dart_Handle color_class = DOMDartState::Current()->color_class(); + Dart_Handle constructor_args[] = { ToDart(val) }; + Dart_SetReturnValue(args, + Dart_New(color_class, Dart_Null(), 1, constructor_args)); +} + +} // namespace blink diff --git a/engine/core/painting/CanvasColor.h b/engine/core/painting/CanvasColor.h new file mode 100644 index 00000000000..43e719c214b --- /dev/null +++ b/engine/core/painting/CanvasColor.h @@ -0,0 +1,31 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_ENGINE_CORE_PAINTING_CANVASCOLOR_H_ +#define SKY_ENGINE_CORE_PAINTING_CANVASCOLOR_H_ + +#include "sky/engine/core/painting/Rect.h" +#include "sky/engine/tonic/dart_wrappable.h" +#include "sky/engine/wtf/PassRefPtr.h" +#include "sky/engine/wtf/RefCounted.h" +#include "third_party/skia/include/core/SkColor.h" + +namespace blink { + +class CanvasColor { + public: + SkColor sk_color; +}; + +template<> +struct DartConverter { + static CanvasColor FromArgumentsWithNullCheck(Dart_NativeArguments args, + int index, + Dart_Handle& exception); + static void SetReturnValue(Dart_NativeArguments args, unsigned val); +}; + +} // namespace blink + +#endif // SKY_ENGINE_CORE_PAINTING_CANVASCOLOR_H_ diff --git a/engine/core/painting/Color.dart b/engine/core/painting/Color.dart new file mode 100644 index 00000000000..b88e3b45619 --- /dev/null +++ b/engine/core/painting/Color.dart @@ -0,0 +1,18 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of dart.sky; + +class Color { + final int _value; + int get value => _value; + + const Color(this._value); + const Color.fromARGB(int a, int r, int g, int b) : + _value = (((a & 0xff) << 24) | + ((r & 0xff) << 16) | + ((g & 0xff) << 8) | + ((b & 0xff) << 0)); + +} diff --git a/engine/core/painting/Paint.h b/engine/core/painting/Paint.h index ed94297790d..9826515a8cc 100644 --- a/engine/core/painting/Paint.h +++ b/engine/core/painting/Paint.h @@ -5,6 +5,7 @@ #ifndef SKY_ENGINE_CORE_PAINTING_PAINT_H_ #define SKY_ENGINE_CORE_PAINTING_PAINT_H_ +#include "sky/engine/core/painting/CanvasColor.h" #include "sky/engine/tonic/dart_wrappable.h" #include "sky/engine/wtf/PassRefPtr.h" #include "sky/engine/wtf/RefCounted.h" @@ -29,6 +30,7 @@ public: unsigned color() const { return m_paint.getColor(); } void setColor(unsigned color) { m_paint.setColor(color); } + void setColor(CanvasColor color) { m_paint.setColor(color.sk_color); } void setARGB(unsigned a, unsigned r, unsigned g, unsigned b) { diff --git a/engine/core/painting/Paint.idl b/engine/core/painting/Paint.idl index d2ef2ddb327..8d1b4594029 100644 --- a/engine/core/painting/Paint.idl +++ b/engine/core/painting/Paint.idl @@ -6,8 +6,9 @@ Constructor(), ] interface Paint { attribute boolean isAntiAlias; - attribute unsigned long color; + attribute Color color; + // TODO(mpcomplete): remove this in favor of assigning to |color|. void setARGB(unsigned long a, unsigned long r, unsigned long g, unsigned long b); void setDrawLooper(DrawLooper looper); void setColorFilter(ColorFilter filter); diff --git a/engine/core/script/dom_dart_state.cc b/engine/core/script/dom_dart_state.cc index 121bde04415..86bebaf806e 100644 --- a/engine/core/script/dom_dart_state.cc +++ b/engine/core/script/dom_dart_state.cc @@ -7,6 +7,7 @@ #include "sky/engine/core/dom/Document.h" #include "sky/engine/core/script/dart_loader.h" +#include "sky/engine/tonic/dart_builtin.h" namespace blink { @@ -22,6 +23,9 @@ void DOMDartState::DidSetIsolate() { x_handle_.Set(this, Dart_NewStringFromCString("x")); y_handle_.Set(this, Dart_NewStringFromCString("y")); value_handle_.Set(this, Dart_NewStringFromCString("_value")); + + Dart_Handle sky_library = DartBuiltin::LookupLibrary("dart:sky"); + color_class_.Set(this, Dart_GetType(sky_library, ToDart("Color"), 0, 0)); } DOMDartState* DOMDartState::Current() { diff --git a/engine/core/script/dom_dart_state.h b/engine/core/script/dom_dart_state.h index 776b1f250a6..11e7bebf1ab 100644 --- a/engine/core/script/dom_dart_state.h +++ b/engine/core/script/dom_dart_state.h @@ -35,6 +35,7 @@ class DOMDartState : public DartState { Dart_Handle x_handle() { return x_handle_.value(); } Dart_Handle y_handle() { return y_handle_.value(); } Dart_Handle value_handle() { return value_handle_.value(); } + Dart_Handle color_class() { return color_class_.value(); } private: RefPtr document_; @@ -43,6 +44,7 @@ class DOMDartState : public DartState { DartPersistentValue x_handle_; DartPersistentValue y_handle_; DartPersistentValue value_handle_; + DartPersistentValue color_class_; }; } // namespace blink diff --git a/examples/raw/painting.sky b/examples/raw/painting.sky index fcfa3d56b36..195cdf0c702 100644 --- a/examples/raw/painting.sky +++ b/examples/raw/painting.sky @@ -22,7 +22,7 @@ void main() { context.clipRect(new Rect.fromLTRB(0.0, 0.0, context.width, radius)); context.translate(mid.x, mid.y); - paint.setARGB(128, 255, 0, 255); + paint.color = const Color.fromARGB(128, 255, 0, 255); context.rotateDegrees(45.0); context.drawRect(new Rect.fromLTRB(-radius, -radius, radius, radius), @@ -35,7 +35,7 @@ void main() { 0.0, 0.0, 1.0 ]; context.concat(scaleMatrix); - paint.setARGB(128, 0, 255, 0); + paint.color = const Color.fromARGB(128, 0, 255, 0); context.drawCircle(0.0, 0.0, radius, paint); context.restore(); @@ -46,19 +46,19 @@ void main() { new DrawLooperLayerInfo() ..setOffset(150.0, 0.0)..setPaintBits(-1)..setColorMode(1), (Paint layerPaint) { - layerPaint.setARGB(128, 255, 255, 0); + layerPaint.color = const Color.fromARGB(128, 255, 255, 0); layerPaint.setColorFilter(new ColorFilter(0x770000ff, 5)); }) ..addLayerOnTop( new DrawLooperLayerInfo()..setOffset(75.0, 75.0)..setColorMode(1), (Paint layerPaint) { - layerPaint.setARGB(128, 255, 0, 0); + layerPaint.color = const Color.fromARGB(128, 255, 0, 0); }) ..addLayerOnTop( new DrawLooperLayerInfo()..setOffset(225.0, 75.0), (Paint layerPaint) { // Since this layer uses a DST color mode, this has no effect. - layerPaint.setARGB(128, 255, 0, 0); + layerPaint.color = const Color.fromARGB(128, 255, 0, 0); }); paint.setDrawLooper(builder.build()); context.drawCircle(0.0, 0.0, radius, paint);