提交 f7405a37 编写于 作者: A Adam Barth 提交者: GitHub

Remove more calls to Dart_GetField (#2747)

The only remaining calls to Dart_GetField are for Paint, which will be
handled in a later patch.
上级 d348a40e
......@@ -41,14 +41,7 @@ FlutterDartState* FlutterDartState::Current() {
void FlutterDartState::DidSetIsolate() {
Scope dart_scope(this);
x_handle_.Set(this, ToDart("x"));
y_handle_.Set(this, ToDart("y"));
dx_handle_.Set(this, ToDart("_dx"));
dy_handle_.Set(this, ToDart("_dy"));
value_handle_.Set(this, ToDart("_value"));
Dart_Handle library = Dart_LookupLibrary(ToDart("dart:ui"));
color_class_.Set(this, Dart_GetType(library, ToDart("Color"), 0, 0));
}
void FlutterDartState::set_mojo_services(
......
......@@ -37,12 +37,7 @@ class FlutterDartState : public DartState {
static FlutterDartState* Current();
// Cached handles to strings used in Dart/C++ conversions.
Dart_Handle x_handle() { return x_handle_.value(); }
Dart_Handle y_handle() { return y_handle_.value(); }
Dart_Handle dx_handle() { return dx_handle_.value(); }
Dart_Handle dy_handle() { return dy_handle_.value(); }
Dart_Handle value_handle() { return value_handle_.value(); }
Dart_Handle color_class() { return color_class_.value(); }
void set_mojo_services(std::unique_ptr<MojoServices> mojo_services);
MojoServices* mojo_services();
......@@ -58,12 +53,7 @@ class FlutterDartState : public DartState {
IsolateClient* isolate_client_;
std::string url_;
DartPersistentValue x_handle_;
DartPersistentValue y_handle_;
DartPersistentValue dx_handle_;
DartPersistentValue dy_handle_;
DartPersistentValue value_handle_;
DartPersistentValue color_class_;
std::unique_ptr<MojoServices> mojo_services_;
......
......@@ -20,8 +20,6 @@ sky_core_files = [
"editing/PositionWithAffinity.h",
"painting/Canvas.cpp",
"painting/Canvas.h",
"painting/CanvasColor.cpp",
"painting/CanvasColor.h",
"painting/CanvasGradient.cpp",
"painting/CanvasGradient.h",
"painting/CanvasImage.cpp",
......@@ -46,8 +44,6 @@ sky_core_files = [
"painting/Picture.h",
"painting/PictureRecorder.cpp",
"painting/PictureRecorder.h",
"painting/Point.cpp",
"painting/Point.h",
"painting/RRect.cpp",
"painting/RRect.h",
"painting/Shader.cpp",
......
......@@ -67,7 +67,8 @@ class SceneBuilder extends NativeFieldWrapperClass2 {
/// Rasterization outside the given rounded rectangle is discarded.
///
/// See [pop] for details about the operation stack.
void pushClipRRect(RRect rrect) native "SceneBuilder_pushClipRRect";
void pushClipRRect(RRect rrect) => _pushClipRRect(rrect._value);
void _pushClipRRect(Float32List rrect) native "SceneBuilder_pushClipRRect";
/// Pushes a path clip operation onto the operation stack.
///
......
......@@ -165,7 +165,8 @@ class Path extends NativeFieldWrapperClass2 {
/// Adds a new subpath that consists of the straight lines and
/// curves needed to form the rounded rectangle described by the
/// argument.
void addRRect(RRect rrect) native "Path_addRRect";
void addRRect(RRect rrect) => _addRRect(rrect._value);
void _addRRect(Float32List rrect) native "Path_addRRect";
/// Closes the last subpath, as if a straight line had been drawn
/// from the current point to the first point of the subpath.
......@@ -326,6 +327,27 @@ enum TileMode {
mirror
}
Int32List _encodeColorList(List<Color> colors) {
final int colorCount = colors.length;
final Int32List result = new Int32List(colorCount);
for (int i = 0; i < colorCount; ++i)
result[i] = colors[i].value;
return result;
}
Float32List _encodePointList(List<Point> points) {
final int pointCount = points.length;
final Float32List result = new Float32List(pointCount * 2);
for (int i = 0; i < pointCount; ++i) {
final int xIndex = i * 2;
final int yIndex = xIndex + 1;
final Point point = points[i];
result[xIndex] = point.x;
result[yIndex] = point.y;
}
return result;
}
/// A shader (as used by [Paint.shader]) that renders a color gradient.
///
/// There are two useful types of gradients, created by [new Gradient.linear]
......@@ -349,13 +371,16 @@ class Gradient extends Shader {
List<Color> colors,
[List<double> 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);
final Float32List endPointsBuffer = _encodePointList(endPoints);
final Int32List colorsBuffer = _encodeColorList(colors);
final Float32List colorStopsBuffer = colorStops == null ? null : new Float32List.fromList(colorStops);
_constructor();
_initLinear(endPointsBuffer, colorsBuffer, colorStopsBuffer, tileMode.index);
}
void _initLinear(List<Point> endPoints, List<Color> colors, List<double> colorStops, int tileMode) native "Gradient_initLinear";
void _initLinear(Float32List endPoints, Int32List colors, Float32List colorStops, int tileMode) native "Gradient_initLinear";
/// Creates a radial gradient centered at `center` that ends at `radius`
/// distance from the center. If `colorStops` is provided, `colorStops[i]` is
......@@ -368,14 +393,16 @@ class Gradient extends Shader {
List<Color> colors,
[List<double> colorStops = null,
TileMode tileMode = TileMode.clamp]) {
_constructor();
_validateColorStops(colors, colorStops);
_initRadial(center, radius, colors, colorStops, tileMode.index);
final Int32List colorsBuffer = _encodeColorList(colors);
final Float32List colorStopsBuffer = colorStops == null ? null : new Float32List.fromList(colorStops);
_constructor();
_initRadial(center.x, center.y, radius, colorsBuffer, colorStopsBuffer, tileMode.index);
}
void _initRadial(Point center, double radius, List<Color> colors, List<double> colorStops, int tileMode) native "Gradient_initRadial";
void _initRadial(double centerX, double centerY, double radius, Int32List colors, Float32List colorStops, int tileMode) native "Gradient_initRadial";
static void _validateColorStops(List<Color> colors, List<double> colorStops) {
if (colorStops != null && (colors == null || colors.length != colorStops.length))
if (colorStops != null && colors.length != colorStops.length)
throw new ArgumentError("[colors] and [colorStops] parameters must be equal length.");
}
}
......@@ -554,7 +581,8 @@ class Canvas extends NativeFieldWrapperClass2 {
/// Reduces the clip region to the intersection of the current clip and the
/// given rounded rectangle.
void clipRRect(RRect rrect) native "Canvas_clipRRect";
void clipRRect(RRect rrect) => _clipRRect(rrect._value);
void _clipRRect(Float32List rrect) native "Canvas_clipRRect";
/// Reduces the clip region to the intersection of the current clip and the
/// given [Path].
......@@ -594,14 +622,16 @@ class Canvas extends NativeFieldWrapperClass2 {
/// Draws a rounded rectangle with the given [Paint]. Whether the rectangle is
/// filled or stroked (or both) is controlled by [Paint.style].
void drawRRect(RRect rrect, Paint paint) native "Canvas_drawRRect";
void drawRRect(RRect rrect, Paint paint) => _drawRRect(rrect._value, paint);
void _drawRRect(Float32List rrect, Paint paint) native "Canvas_drawRRect";
/// Draws a shape consisting of the difference between two rounded rectangles
/// with the given [Paint]. Whether this shape is filled or stroked (or both)
/// is controlled by [Paint.style].
///
/// This shape is almost but not quite entirely unlike an annulus.
void drawDRRect(RRect outer, RRect inner, Paint paint) native "Canvas_drawDRRect";
void drawDRRect(RRect outer, RRect inner, Paint paint) => _drawDRRect(outer._value, inner._value, paint);
void _drawDRRect(Float32List outer, Float32List inner, Paint paint) native "Canvas_drawDRRect";
/// Draws an axis-aligned oval that fills the given axis-aligned rectangle
/// with the given [Paint]. Whether the oval is filled or stroked (or both) is
......@@ -726,34 +756,9 @@ class Canvas extends NativeFieldWrapperClass2 {
if (colors.isNotEmpty && colors.length != vertexCount)
throw new ArgumentError("[vertices] and [colors] lengths must match");
final Float32List vertexBuffer = new Float32List(vertexCount * 2);
for (int i = 0; i < vertexCount; ++i) {
final int xIndex = i * 2;
final int yIndex = xIndex + 1;
final Point vertex = vertices[i];
vertexBuffer[xIndex] = vertex.x;
vertexBuffer[yIndex] = vertex.y;
}
Float32List textureCoordinateBuffer;
if (textureCoordinates.isNotEmpty) {
textureCoordinateBuffer = new Float32List(vertexCount * 2);
for (int i = 0; i < vertexCount; ++i) {
final int xIndex = i * 2;
final int yIndex = xIndex + 1;
final Point textureCoordinate = textureCoordinates[i];
textureCoordinateBuffer[xIndex] = textureCoordinate.x;
textureCoordinateBuffer[yIndex] = textureCoordinate.y;
}
}
Int32List colorBuffer;
if (colors.isNotEmpty) {
colorBuffer = new Int32List(vertexCount);
for (int i = 0; i < vertexCount; ++i)
colorBuffer[i] = colors[i].value;
}
final Float32List vertexBuffer = _encodePointList(vertices);
final Float32List textureCoordinateBuffer = textureCoordinates.isEmpty ? null : _encodePointList(textureCoordinates);
final Int32List colorBuffer = colors.isEmpty ? null : _encodeColorList(colors);
final Int32List indexBuffer = new Int32List.fromList(indicies);
_drawVertices(
......@@ -804,13 +809,7 @@ class Canvas extends NativeFieldWrapperClass2 {
rectBuffer[index3] = rect.bottom;
}
Int32List colorBuffer;
if (colors.isNotEmpty) {
colorBuffer = new Int32List(rectCount);
for (int i = 0; i < rectCount; ++i)
colorBuffer[i] = colors[i].value;
}
final Int32List colorBuffer = colors.isEmpty ? null : _encodeColorList(colors);
final Float32List cullRectBuffer = cullRect?._value;
_drawAtlas(atlas, rstTransformBuffer, rectBuffer, colorBuffer, transferMode.index, cullRectBuffer, paint);
......
......@@ -349,6 +349,7 @@ void Canvas::drawVertices(
indices16.push_back(indices.data()[i]);
static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint doesn't use floats.");
static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor doesn't use int32_t.");
m_canvas->drawVertices(
vertexMode,
......
// 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/core/painting/CanvasColor.h"
#include "flutter/tonic/dart_error.h"
#include "sky/engine/core/script/ui_dart_state.h"
namespace blink {
CanvasColor DartConverter<CanvasColor>::FromDart(Dart_Handle dart_color) {
Dart_Handle value =
Dart_GetField(dart_color, UIDartState::Current()->value_handle());
uint64_t sk_color = 0;
Dart_Handle rv = Dart_IntegerToUint64(value, &sk_color);
DCHECK(!LogIfError(rv));
DCHECK(sk_color <= 0xffffffff);
return static_cast<SkColor>(sk_color);
}
CanvasColor DartConverter<CanvasColor>::FromArguments(
Dart_NativeArguments args,
int index,
Dart_Handle& exception) {
Dart_Handle dart_color = Dart_GetNativeArgument(args, index);
DCHECK(!LogIfError(dart_color));
return FromDart(dart_color);
}
void DartConverter<CanvasColor>::SetReturnValue(Dart_NativeArguments args,
CanvasColor val) {
Dart_Handle color_class = UIDartState::Current()->color_class();
Dart_Handle constructor_args[] = { ToDart(val.sk_color) };
Dart_SetReturnValue(args,
Dart_New(color_class, Dart_Null(), 1, constructor_args));
}
} // namespace blink
// 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 "flutter/tonic/dart_wrappable.h"
#include "third_party/skia/include/core/SkColor.h"
namespace blink {
struct CanvasColor {
SkColor sk_color;
CanvasColor(SkColor color) : sk_color(color) { }
CanvasColor() : sk_color() { }
operator SkColor() const { return sk_color; }
};
template <>
struct DartConverter<CanvasColor> {
static CanvasColor FromDart(Dart_Handle handle);
static CanvasColor FromArguments(Dart_NativeArguments args,
int index,
Dart_Handle& exception);
static void SetReturnValue(Dart_NativeArguments args, CanvasColor val);
};
} // namespace blink
#endif // SKY_ENGINE_CORE_PAINTING_CANVASCOLOR_H_
......@@ -36,41 +36,41 @@ scoped_refptr<CanvasGradient> CanvasGradient::create() {
return new CanvasGradient();
}
void CanvasGradient::initLinear(const std::vector<Point>& end_points,
const std::vector<CanvasColor>& colors,
const std::vector<float>& color_stops,
void CanvasGradient::initLinear(const Float32List& end_points,
const Int32List& colors,
const Float32List& color_stops,
SkShader::TileMode tile_mode) {
DCHECK(end_points.size() == 2);
DCHECK(colors.size() == color_stops.size() || color_stops.data() == nullptr);
SkPoint sk_end_points[2];
for (int i = 0; i < 2; ++i)
sk_end_points[i] = end_points[i].sk_point;
DCHECK(end_points.num_elements() == 2);
DCHECK(colors.num_elements() == color_stops.num_elements() || color_stops.data() == nullptr);
std::vector<SkColor> sk_colors;
sk_colors.reserve(colors.size());
for (const CanvasColor& color : colors)
sk_colors.push_back(color);
static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint doesn't use floats.");
static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor doesn't use int32_t.");
set_shader(SkGradientShader::MakeLinear(
sk_end_points, sk_colors.data(), color_stops.data(), sk_colors.size(),
reinterpret_cast<const SkPoint*>(end_points.data()),
reinterpret_cast<const SkColor*>(colors.data()),
color_stops.data(),
colors.num_elements(),
tile_mode));
}
void CanvasGradient::initRadial(const Point& center,
void CanvasGradient::initRadial(double centerX,
double centerY,
double radius,
const std::vector<CanvasColor>& colors,
const std::vector<float>& color_stops,
const Int32List& colors,
const Float32List& color_stops,
SkShader::TileMode tile_mode) {
DCHECK(colors.size() == color_stops.size() || color_stops.data() == nullptr);
DCHECK(colors.num_elements() == color_stops.num_elements() || color_stops.data() == nullptr);
std::vector<SkColor> sk_colors;
sk_colors.reserve(colors.size());
for (const CanvasColor& color : colors)
sk_colors.push_back(color);
static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor doesn't use int32_t.");
set_shader(SkGradientShader::MakeRadial(
center.sk_point, radius, sk_colors.data(), color_stops.data(),
sk_colors.size(), tile_mode));
SkPoint::Make(centerX, centerY),
radius,
reinterpret_cast<const SkColor*>(colors.data()),
color_stops.data(),
colors.num_elements(),
tile_mode));
}
CanvasGradient::CanvasGradient()
......
......@@ -6,8 +6,8 @@
#define SKY_ENGINE_CORE_PAINTING_CANVASGRADIENT_H_
#include "flutter/tonic/dart_wrappable.h"
#include "sky/engine/core/painting/CanvasColor.h"
#include "sky/engine/core/painting/Point.h"
#include "flutter/tonic/float32_list.h"
#include "flutter/tonic/int32_list.h"
#include "sky/engine/core/painting/Shader.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
......@@ -25,15 +25,16 @@ class CanvasGradient : public Shader {
~CanvasGradient() override;
static scoped_refptr<CanvasGradient> create();
void initLinear(const std::vector<Point>& end_points,
const std::vector<CanvasColor>& colors,
const std::vector<float>& color_stops,
void initLinear(const Float32List& end_points,
const Int32List& colors,
const Float32List& color_stops,
SkShader::TileMode tile_mode);
void initRadial(const Point& center,
void initRadial(double centerX,
double centerY,
double radius,
const std::vector<CanvasColor>& colors,
const std::vector<float>& color_stops,
const Int32List& colors,
const Float32List& color_stops,
SkShader::TileMode tile_mode);
static void RegisterNatives(DartLibraryNatives* natives);
......
// 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/core/painting/Point.h"
#include "base/logging.h"
#include "flutter/tonic/dart_error.h"
#include "sky/engine/core/script/ui_dart_state.h"
namespace blink {
// Convert handle.x,y ==> SkPoint.
Point DartConverter<Point>::FromDart(Dart_Handle handle) {
DCHECK(!LogIfError(handle));
Dart_Handle x_value =
Dart_GetField(handle, UIDartState::Current()->x_handle());
Dart_Handle y_value =
Dart_GetField(handle, UIDartState::Current()->y_handle());
double x = 0.0, y = 0.0;
Dart_Handle err = Dart_DoubleValue(x_value, &x);
DCHECK(!LogIfError(err));
err = Dart_DoubleValue(y_value, &y);
DCHECK(!LogIfError(err));
Point result;
result.sk_point.set(x, y);
result.is_null = false;
return result;
}
Point DartConverter<Point>::FromArguments(
Dart_NativeArguments args,
int index,
Dart_Handle& exception) {
return FromDart(Dart_GetNativeArgument(args, index));
}
} // namespace blink
// 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_POINT_H_
#define SKY_ENGINE_CORE_PAINTING_POINT_H_
#include "dart/runtime/include/dart_api.h"
#include "flutter/tonic/dart_converter.h"
#include "third_party/skia/include/core/SkPoint.h"
namespace blink {
// Very simple wrapper for SkPoint to add a null state.
class Point {
public:
SkPoint sk_point;
bool is_null;
};
template <>
struct DartConverter<Point> {
static Point FromDart(Dart_Handle handle);
static Point FromArguments(Dart_NativeArguments args,
int index,
Dart_Handle& exception);
};
} // namespace blink
#endif // SKY_ENGINE_CORE_PAINTING_POINT_H_
......@@ -5,37 +5,24 @@
#include "sky/engine/core/painting/RRect.h"
#include "flutter/tonic/dart_error.h"
#include "sky/engine/core/script/ui_dart_state.h"
#include "flutter/tonic/float32_list.h"
namespace blink {
// Construct an SkRRect from a Dart RRect object.
// The Dart RRect has a _value field which is a Float32List containing
// [left, top, right, bottom, xRad, yRad]
RRect DartConverter<RRect>::FromDart(Dart_Handle dart_rrect) {
// The Dart RRect is a Float32List containing
// [left, top, right, bottom, xRadius, yRadius]
RRect DartConverter<RRect>::FromDart(Dart_Handle value) {
Float32List buffer(value);
RRect result;
result.is_null = true;
if (Dart_IsNull(dart_rrect))
return result;
Dart_Handle value =
Dart_GetField(dart_rrect, UIDartState::Current()->value_handle());
if (Dart_IsNull(value))
if (buffer.data() == nullptr)
return result;
Dart_TypedData_Type type;
float* data = nullptr;
intptr_t num_elements = 0;
Dart_TypedDataAcquireData(
value, &type, reinterpret_cast<void**>(&data), &num_elements);
DCHECK(!LogIfError(value));
DCHECK(type == Dart_TypedData_kFloat32 && num_elements == 6);
result.sk_rrect.setRectXY(
SkRect::MakeLTRB(data[0], data[1], data[2], data[3]),
data[4], data[5]);
Dart_TypedDataReleaseData(value);
SkRect::MakeLTRB(buffer[0], buffer[1], buffer[2], buffer[3]),
buffer[4], buffer[5]);
result.is_null = false;
return result;
......@@ -44,9 +31,9 @@ RRect DartConverter<RRect>::FromDart(Dart_Handle dart_rrect) {
RRect DartConverter<RRect>::FromArguments(Dart_NativeArguments args,
int index,
Dart_Handle& exception) {
Dart_Handle dart_rrect = Dart_GetNativeArgument(args, index);
DCHECK(!LogIfError(dart_rrect));
return FromDart(dart_rrect);
Dart_Handle value = Dart_GetNativeArgument(args, index);
DCHECK(!LogIfError(value));
return FromDart(value);
}
} // namespace blink
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册