From 2e693b1750e0bcd692ae8e22bde4e6e409786d86 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 28 Dec 2015 23:02:22 -0800 Subject: [PATCH] Remove IDL from Shader subclasses - Gradient - ImageShader --- sky/engine/bindings/dart_ui.cc | 8 ++- sky/engine/core/core.gni | 5 -- sky/engine/core/dart/painting.dart | 73 +++++++++++++++++++++ sky/engine/core/painting/CanvasColor.h | 2 +- sky/engine/core/painting/CanvasGradient.cpp | 26 ++++++++ sky/engine/core/painting/CanvasGradient.h | 12 ++-- sky/engine/core/painting/Gradient.dart | 54 --------------- sky/engine/core/painting/Gradient.idl | 14 ---- sky/engine/core/painting/ImageShader.cpp | 32 +++++++++ sky/engine/core/painting/ImageShader.dart | 20 ------ sky/engine/core/painting/ImageShader.h | 3 + sky/engine/core/painting/ImageShader.idl | 10 --- sky/engine/core/painting/Point.cpp | 2 +- sky/engine/core/painting/Point.h | 7 +- sky/engine/core/painting/Shader.cpp | 2 + sky/engine/core/painting/Shader.idl | 6 -- 16 files changed, 157 insertions(+), 119 deletions(-) delete mode 100644 sky/engine/core/painting/Gradient.dart delete mode 100644 sky/engine/core/painting/Gradient.idl delete mode 100644 sky/engine/core/painting/ImageShader.dart delete mode 100644 sky/engine/core/painting/ImageShader.idl delete mode 100644 sky/engine/core/painting/Shader.idl diff --git a/sky/engine/bindings/dart_ui.cc b/sky/engine/bindings/dart_ui.cc index 52ea49372..487c7e6fd 100644 --- a/sky/engine/bindings/dart_ui.cc +++ b/sky/engine/bindings/dart_ui.cc @@ -8,10 +8,12 @@ #include "sky/engine/bindings/dart_runtime_hooks.h" #include "sky/engine/core/compositing/Scene.h" #include "sky/engine/core/compositing/SceneBuilder.h" +#include "sky/engine/core/painting/CanvasGradient.h" #include "sky/engine/core/painting/ColorFilter.h" -#include "sky/engine/core/painting/MaskFilter.h" #include "sky/engine/core/painting/DrawLooperLayerInfo.h" +#include "sky/engine/core/painting/ImageShader.h" #include "sky/engine/core/painting/LayerDrawLooperBuilder.h" +#include "sky/engine/core/painting/MaskFilter.h" #include "sky/engine/core/painting/painting.h" #include "sky/engine/core/text/Paragraph.h" #include "sky/engine/core/text/ParagraphBuilder.h" @@ -45,10 +47,12 @@ const uint8_t* GetSymbol(Dart_NativeFunction native_function) { void DartUI::InitForIsolate() { if (!g_natives) { g_natives = new DartLibraryNatives(); + CanvasGradient::RegisterNatives(g_natives); + ColorFilter::RegisterNatives(g_natives); DartRuntimeHooks::RegisterNatives(g_natives); DrawLooperLayerInfo::RegisterNatives(g_natives); + ImageShader::RegisterNatives(g_natives); LayerDrawLooperBuilder::RegisterNatives(g_natives); - ColorFilter::RegisterNatives(g_natives); MaskFilter::RegisterNatives(g_natives); Painting::RegisterNatives(g_natives); Paragraph::RegisterNatives(g_natives); diff --git a/sky/engine/core/core.gni b/sky/engine/core/core.gni index f8b6c3409..432045a4d 100644 --- a/sky/engine/core/core.gni +++ b/sky/engine/core/core.gni @@ -235,13 +235,10 @@ sky_core_files = [ core_idl_files = get_path_info([ "painting/Canvas.idl", - "painting/Gradient.idl", "painting/Image.idl", - "painting/ImageShader.idl", "painting/Path.idl", "painting/Picture.idl", "painting/PictureRecorder.idl", - "painting/Shader.idl", ], "abspath") @@ -256,8 +253,6 @@ core_dart_files = get_path_info([ "dart/window.dart", "painting/Color.dart", "painting/FilterQuality.dart", - "painting/Gradient.dart", - "painting/ImageShader.dart", "painting/Offset.dart", "painting/OffsetBase.dart", "painting/Paint.dart", diff --git a/sky/engine/core/dart/painting.dart b/sky/engine/core/dart/painting.dart index 12e4c53f1..04163b659 100644 --- a/sky/engine/core/dart/painting.dart +++ b/sky/engine/core/dart/painting.dart @@ -83,3 +83,76 @@ class ColorFilter extends NativeFieldWrapperClass2 { _constructor(color, transferMode); } } + +abstract class Shader extends NativeFieldWrapperClass2 { +} + +/// Defines what happens at the edge of the gradient. +enum TileMode { + /// Edge is clamped to the final color. + clamp, + /// Edge is repeated from first color to last. + repeated, + /// Edge is mirrored from last color to first. + mirror +} + +void _validateColorStops(List colors, List colorStops) { + if (colorStops != null && (colors == null || colors.length != colorStops.length)) { + throw new ArgumentError( + "[colors] and [colorStops] parameters must be equal length."); + } +} + +class Gradient extends Shader { + void _constructor() native "Gradient_constructor"; + void _initLinear(List endPoints, List colors, List colorStops, int tileMode) native "Gradient_initLinear"; + void _initRadial(Point center, double radius, List colors, List colorStops, int tileMode) native "Gradient_initRadial"; + + /// Creates a linear gradient from [endPoint[0]] to [endPoint[1]]. If + /// [colorStops] is provided, [colorStops[i]] is a number from 0 to 1 that + /// specifies where [color[i]] begins in the gradient. + // TODO(mpcomplete): Maybe pass a list of (color, colorStop) pairs instead? + Gradient.linear(List endPoints, + List colors, + [List colorStops = null, + TileMode tileMode = TileMode.clamp]) { + _constructor(); + if (endPoints == null || endPoints.length != 2) + throw new ArgumentError("Expected exactly 2 [endPoints]."); + _validateColorStops(colors, colorStops); + _initLinear(endPoints, colors, colorStops, tileMode.index); + } + + /// Creates a radial gradient centered at [center] that ends at [radius] + /// distance from the center. If [colorStops] is provided, [colorStops[i]] is + /// a number from 0 to 1 that specifies where [color[i]] begins in the + /// gradient. + Gradient.radial(Point center, + double radius, + List colors, + [List colorStops = null, + TileMode tileMode = TileMode.clamp]) { + _constructor(); + _validateColorStops(colors, colorStops); + _initRadial(center, radius, colors, colorStops, tileMode.index); + } +} + +class ImageShader extends Shader { + void _constructor() native "ImageShader_constructor"; + void _initWithImage(Image image, int tmx, int tmy, Float64List matrix4) native "ImageShader_initWithImage"; + + ImageShader(Image image, TileMode tmx, TileMode tmy, Float64List matrix4) { + if (image == null) + throw new ArgumentError("[image] argument cannot be null"); + if (tmx == null) + throw new ArgumentError("[tmx] argument cannot be null"); + if (tmy == null) + throw new ArgumentError("[tmy] argument cannot be null"); + if (matrix4 == null) + throw new ArgumentError("[matrix4] argument cannot be null"); + _initWithImage(image, tmx.index, tmy.index, matrix4); + } +} + diff --git a/sky/engine/core/painting/CanvasColor.h b/sky/engine/core/painting/CanvasColor.h index 245c2cd43..81cac2416 100644 --- a/sky/engine/core/painting/CanvasColor.h +++ b/sky/engine/core/painting/CanvasColor.h @@ -18,7 +18,7 @@ struct CanvasColor { CanvasColor(SkColor color) : color(color) { } CanvasColor() : color() { } - operator SkColor() { return color; } + operator SkColor() const { return color; } }; template <> diff --git a/sky/engine/core/painting/CanvasGradient.cpp b/sky/engine/core/painting/CanvasGradient.cpp index 359e0fcd0..d3277a04c 100644 --- a/sky/engine/core/painting/CanvasGradient.cpp +++ b/sky/engine/core/painting/CanvasGradient.cpp @@ -4,8 +4,34 @@ #include "sky/engine/core/painting/CanvasGradient.h" +#include "sky/engine/tonic/dart_args.h" +#include "sky/engine/tonic/dart_binding_macros.h" +#include "sky/engine/tonic/dart_converter.h" +#include "sky/engine/tonic/dart_library_natives.h" + namespace blink { +typedef CanvasGradient Gradient; // Because the C++ name doesn't match the Dart name. + +static void Gradient_constructor(Dart_NativeArguments args) { + DartCallConstructor(&CanvasGradient::create, args); +} + +IMPLEMENT_WRAPPERTYPEINFO(Gradient); + +#define FOR_EACH_BINDING(V) \ + V(Gradient, initLinear) \ + V(Gradient, initRadial) + +FOR_EACH_BINDING(DART_NATIVE_CALLBACK) + +void CanvasGradient::RegisterNatives(DartLibraryNatives* natives) { + natives->Register({ + { "Gradient_constructor", Gradient_constructor, 1, true }, +FOR_EACH_BINDING(DART_REGISTER_NATIVE) + }); +} + PassRefPtr CanvasGradient::create() { return adoptRef(new CanvasGradient()); } diff --git a/sky/engine/core/painting/CanvasGradient.h b/sky/engine/core/painting/CanvasGradient.h index 93aea75c9..181f68fbf 100644 --- a/sky/engine/core/painting/CanvasGradient.h +++ b/sky/engine/core/painting/CanvasGradient.h @@ -12,16 +12,15 @@ #include "third_party/skia/include/effects/SkGradientShader.h" namespace blink { - -class TileMode {}; +class DartLibraryNatives; template <> -struct DartConverter : public DartConverterEnum {}; +struct DartConverter : public DartConverterInteger {}; -COMPILE_ASSERT(SkShader::kTileModeCount == 3, Need_to_update_Gradient_dart); +COMPILE_ASSERT(SkShader::kTileModeCount == 3, Need_to_update_TileMode_enum); class CanvasGradient : public Shader { - DEFINE_WRAPPERTYPEINFO(); + DEFINE_WRAPPERTYPEINFO(); public: ~CanvasGradient() override; static PassRefPtr create(); @@ -30,12 +29,15 @@ class CanvasGradient : public Shader { const Vector& colors, const Vector& color_stops, SkShader::TileMode tile_mode); + void initRadial(const Point& center, double radius, const Vector& colors, const Vector& color_stops, SkShader::TileMode tile_mode); + static void RegisterNatives(DartLibraryNatives* natives); + private: CanvasGradient(); }; diff --git a/sky/engine/core/painting/Gradient.dart b/sky/engine/core/painting/Gradient.dart deleted file mode 100644 index 7eb56e662..000000000 --- a/sky/engine/core/painting/Gradient.dart +++ /dev/null @@ -1,54 +0,0 @@ -// 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_ui; - -/// Defines what happens at the edge of the gradient. -enum TileMode { - /// Edge is clamped to the final color. - clamp, - /// Edge is repeated from first color to last. - repeated, - /// Edge is mirrored from last color to first. - mirror -} - -/// Extends the generated _Gradient interface via the PrivateDart attribute. -class Gradient extends _Gradient { - /// Creates a linear gradient from [endPoint[0]] to [endPoint[1]]. If - /// [colorStops] is provided, [colorStops[i]] is a number from 0 to 1 that - /// specifies where [color[i]] begins in the gradient. - // TODO(mpcomplete): Maybe pass a list of (color, colorStop) pairs instead? - Gradient.linear(List endPoints, - List colors, - [List colorStops = null, - TileMode tileMode = TileMode.clamp]) - : super() { - if (endPoints == null || endPoints.length != 2) - throw new ArgumentError("Expected exactly 2 [endPoints]."); - validateColorStops(colors, colorStops); - this._initLinear(endPoints, colors, colorStops, tileMode); - } - - /// Creates a radial gradient centered at [center] that ends at [radius] - /// distance from the center. If [colorStops] is provided, [colorStops[i]] is - /// a number from 0 to 1 that specifies where [color[i]] begins in the - /// gradient. - Gradient.radial(Point center, - double radius, - List colors, - [List colorStops = null, - TileMode tileMode = TileMode.clamp]) - : super() { - validateColorStops(colors, colorStops); - this._initRadial(center, radius, colors, colorStops, tileMode); - } - - void validateColorStops(List colors, List colorStops) { - if (colorStops != null && (colors == null || colors.length != colorStops.length)) { - throw new ArgumentError( - "[colors] and [colorStops] parameters must be equal length."); - } - } -} diff --git a/sky/engine/core/painting/Gradient.idl b/sky/engine/core/painting/Gradient.idl deleted file mode 100644 index 532e3a04e..000000000 --- a/sky/engine/core/painting/Gradient.idl +++ /dev/null @@ -1,14 +0,0 @@ -// 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. - -[ - ImplementedAs=CanvasGradient, - PrivateDart, - Constructor() -] interface Gradient : Shader { - void initLinear(Point[] endPoints, Color[] colors, float[] colorStops, - TileMode tileMode); - void initRadial(Point center, double radius, Color[] colors, - float[] colorStops, TileMode tileMode); -}; diff --git a/sky/engine/core/painting/ImageShader.cpp b/sky/engine/core/painting/ImageShader.cpp index a7b8cf1a9..8a53ca134 100644 --- a/sky/engine/core/painting/ImageShader.cpp +++ b/sky/engine/core/painting/ImageShader.cpp @@ -4,8 +4,40 @@ #include "sky/engine/core/painting/ImageShader.h" +#include "sky/engine/tonic/dart_args.h" +#include "sky/engine/tonic/dart_binding_macros.h" +#include "sky/engine/tonic/dart_converter.h" +#include "sky/engine/tonic/dart_library_natives.h" + namespace blink { +static void ImageShader_constructor(Dart_NativeArguments args) { + DartCallConstructor(&ImageShader::create, args); +} + +static void ImageShader_initWithImage(Dart_NativeArguments args) { + DartArgIterator it(args); + CanvasImage* image = it.GetNext(); + SkShader::TileMode tmx = it.GetNext(); + SkShader::TileMode tmy = it.GetNext(); + Float64List matrix4 = it.GetNext(); + if (it.had_exception()) + return; + ExceptionState es; + GetReceiver(args)->initWithImage(image, tmx, tmy, matrix4, es); + if (es.had_exception()) + Dart_ThrowException(es.GetDartException(args, true)); +} + +IMPLEMENT_WRAPPERTYPEINFO(ImageShader); + +void ImageShader::RegisterNatives(DartLibraryNatives* natives) { + natives->Register({ + { "ImageShader_constructor", ImageShader_constructor, 1, true }, + { "ImageShader_initWithImage", ImageShader_initWithImage, 5, true }, + }); +} + PassRefPtr ImageShader::create() { return adoptRef(new ImageShader()); } diff --git a/sky/engine/core/painting/ImageShader.dart b/sky/engine/core/painting/ImageShader.dart deleted file mode 100644 index 0c541614e..000000000 --- a/sky/engine/core/painting/ImageShader.dart +++ /dev/null @@ -1,20 +0,0 @@ -// 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_ui; - -class ImageShader extends _ImageShader { - ImageShader(Image image, TileMode tmx, TileMode tmy, Float64List matrix4) { - if (image == null) - throw new ArgumentError("[image] argument cannot be null"); - if (tmx == null) - throw new ArgumentError("[tmx] argument cannot be null"); - if (tmy == null) - throw new ArgumentError("[tmy] argument cannot be null"); - if (matrix4 == null) - throw new ArgumentError("[matrix4] argument cannot be null"); - - this._initWithImage(image, tmx, tmy, matrix4); - } -} diff --git a/sky/engine/core/painting/ImageShader.h b/sky/engine/core/painting/ImageShader.h index 2ca4d0d29..7eefda7f9 100644 --- a/sky/engine/core/painting/ImageShader.h +++ b/sky/engine/core/painting/ImageShader.h @@ -16,6 +16,7 @@ #include "third_party/skia/include/core/SkMatrix.h" namespace blink { +class DartLibraryNatives; class ImageShader : public Shader { DEFINE_WRAPPERTYPEINFO(); @@ -29,6 +30,8 @@ class ImageShader : public Shader { const Float64List& matrix4, ExceptionState& es); + static void RegisterNatives(DartLibraryNatives* natives); + private: ImageShader(); }; diff --git a/sky/engine/core/painting/ImageShader.idl b/sky/engine/core/painting/ImageShader.idl deleted file mode 100644 index ce88e7ce4..000000000 --- a/sky/engine/core/painting/ImageShader.idl +++ /dev/null @@ -1,10 +0,0 @@ -// 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. - -[ - PrivateDart, - Constructor() -] interface ImageShader : Shader { - [RaisesException] void initWithImage(Image image, TileMode tmx, TileMode tmy, Float64List matrix4); -}; diff --git a/sky/engine/core/painting/Point.cpp b/sky/engine/core/painting/Point.cpp index 0c9a882b6..fd430977b 100644 --- a/sky/engine/core/painting/Point.cpp +++ b/sky/engine/core/painting/Point.cpp @@ -29,7 +29,7 @@ Point DartConverter::FromDart(Dart_Handle handle) { return result; } -Point DartConverter::FromArgumentsWithNullCheck( +Point DartConverter::FromArguments( Dart_NativeArguments args, int index, Dart_Handle& exception) { diff --git a/sky/engine/core/painting/Point.h b/sky/engine/core/painting/Point.h index c08c48e72..c8c767d87 100644 --- a/sky/engine/core/painting/Point.h +++ b/sky/engine/core/painting/Point.h @@ -20,9 +20,14 @@ class Point { template <> struct DartConverter { static Point FromDart(Dart_Handle handle); + static Point FromArguments(Dart_NativeArguments args, + int index, + Dart_Handle& exception); static Point FromArgumentsWithNullCheck(Dart_NativeArguments args, int index, - Dart_Handle& exception); + Dart_Handle& exception) { + return FromArguments(args, index, exception); + } }; } // namespace blink diff --git a/sky/engine/core/painting/Shader.cpp b/sky/engine/core/painting/Shader.cpp index cf03e0b55..b9baf7033 100644 --- a/sky/engine/core/painting/Shader.cpp +++ b/sky/engine/core/painting/Shader.cpp @@ -6,6 +6,8 @@ namespace blink { +IMPLEMENT_WRAPPERTYPEINFO(Shader); + Shader::Shader(PassRefPtr shader) : shader_(shader) { } diff --git a/sky/engine/core/painting/Shader.idl b/sky/engine/core/painting/Shader.idl deleted file mode 100644 index d29440d5f..000000000 --- a/sky/engine/core/painting/Shader.idl +++ /dev/null @@ -1,6 +0,0 @@ -// 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. - -interface Shader { -}; -- GitLab