Convert a dart:ui Paint to a DisplayList DlPaint (flutter/engine#36629)
This commit is contained in:
@@ -1680,6 +1680,7 @@ FILE: ../../../flutter/lib/ui/painting/multi_frame_codec.cc
|
||||
FILE: ../../../flutter/lib/ui/painting/multi_frame_codec.h
|
||||
FILE: ../../../flutter/lib/ui/painting/paint.cc
|
||||
FILE: ../../../flutter/lib/ui/painting/paint.h
|
||||
FILE: ../../../flutter/lib/ui/painting/paint_unittests.cc
|
||||
FILE: ../../../flutter/lib/ui/painting/path.cc
|
||||
FILE: ../../../flutter/lib/ui/painting/path.h
|
||||
FILE: ../../../flutter/lib/ui/painting/path_measure.cc
|
||||
|
||||
@@ -166,7 +166,7 @@ class DlPaint {
|
||||
return colorFilter_;
|
||||
}
|
||||
const DlColorFilter* getColorFilterPtr() const { return colorFilter_.get(); }
|
||||
DlPaint& setColorFilter(std::shared_ptr<DlColorFilter> filter) {
|
||||
DlPaint& setColorFilter(std::shared_ptr<const DlColorFilter> filter) {
|
||||
colorFilter_ = filter ? filter->shared() : nullptr;
|
||||
return *this;
|
||||
}
|
||||
@@ -179,7 +179,7 @@ class DlPaint {
|
||||
return imageFilter_;
|
||||
}
|
||||
const DlImageFilter* getImageFilterPtr() const { return imageFilter_.get(); }
|
||||
DlPaint& setImageFilter(std::shared_ptr<DlImageFilter> filter) {
|
||||
DlPaint& setImageFilter(std::shared_ptr<const DlImageFilter> filter) {
|
||||
imageFilter_ = filter;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -231,6 +231,7 @@ if (enable_unittests) {
|
||||
"painting/image_dispose_unittests.cc",
|
||||
"painting/image_encoding_unittests.cc",
|
||||
"painting/image_generator_registry_unittests.cc",
|
||||
"painting/paint_unittests.cc",
|
||||
"painting/path_unittests.cc",
|
||||
"painting/single_frame_codec_unittests.cc",
|
||||
"semantics/semantics_update_builder_unittests.cc",
|
||||
|
||||
@@ -360,6 +360,18 @@ Future<void> pumpImage() async {
|
||||
}
|
||||
void _captureImageAndPicture(Image image, Picture picture) native 'CaptureImageAndPicture';
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void convertPaintToDlPaint() {
|
||||
Paint paint = Paint();
|
||||
paint.blendMode = BlendMode.modulate;
|
||||
paint.color = Color.fromARGB(0x11, 0x22, 0x33, 0x44);
|
||||
paint.colorFilter = ColorFilter.mode(Color.fromARGB(0x55, 0x66, 0x77, 0x88), BlendMode.xor);
|
||||
paint.maskFilter = MaskFilter.blur(BlurStyle.inner, .75);
|
||||
paint.style = PaintingStyle.stroke;
|
||||
_convertPaintToDlPaint(paint);
|
||||
}
|
||||
void _convertPaintToDlPaint(Paint paint) native 'ConvertPaintToDlPaint';
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void hooksTests() {
|
||||
void test(String name, VoidCallback testFunction) {
|
||||
|
||||
@@ -1112,6 +1112,8 @@ class Paint {
|
||||
//
|
||||
// The binary format must match the deserialization code in paint.cc.
|
||||
|
||||
// C++ unit tests access this.
|
||||
@pragma('vm:entry-point')
|
||||
final ByteData _data = ByteData(_kDataByteCount);
|
||||
|
||||
static const int _kIsAntiAliasIndex = 0;
|
||||
@@ -1147,6 +1149,8 @@ class Paint {
|
||||
static const int _kDataByteCount = 56;
|
||||
|
||||
// Binary format must match the deserialization code in paint.cc.
|
||||
// C++ unit tests access this.
|
||||
@pragma('vm:entry-point')
|
||||
List<Object?>? _objects;
|
||||
|
||||
List<Object?> _ensureObjectsInitialized() {
|
||||
|
||||
@@ -331,6 +331,98 @@ bool Paint::sync_to(DisplayListBuilder* builder,
|
||||
return true;
|
||||
}
|
||||
|
||||
void Paint::toDlPaint(DlPaint& paint) const {
|
||||
if (isNull()) {
|
||||
return;
|
||||
}
|
||||
FML_DCHECK(paint == DlPaint());
|
||||
|
||||
tonic::DartByteData byte_data(paint_data_);
|
||||
FML_CHECK(byte_data.length_in_bytes() == kDataByteCount);
|
||||
|
||||
const uint32_t* uint_data = static_cast<const uint32_t*>(byte_data.data());
|
||||
const float* float_data = static_cast<const float*>(byte_data.data());
|
||||
|
||||
Dart_Handle values[kObjectCount];
|
||||
if (!Dart_IsNull(paint_objects_)) {
|
||||
FML_DCHECK(Dart_IsList(paint_objects_));
|
||||
intptr_t length = 0;
|
||||
Dart_ListLength(paint_objects_, &length);
|
||||
|
||||
FML_CHECK(length == kObjectCount);
|
||||
if (Dart_IsError(
|
||||
Dart_ListGetRange(paint_objects_, 0, kObjectCount, values))) {
|
||||
return;
|
||||
}
|
||||
|
||||
Dart_Handle shader = values[kShaderIndex];
|
||||
if (!Dart_IsNull(shader)) {
|
||||
if (Shader* decoded = tonic::DartConverter<Shader*>::FromDart(shader)) {
|
||||
auto sampling =
|
||||
ImageFilter::SamplingFromIndex(uint_data[kFilterQualityIndex]);
|
||||
paint.setColorSource(decoded->shader(sampling));
|
||||
}
|
||||
}
|
||||
|
||||
Dart_Handle color_filter = values[kColorFilterIndex];
|
||||
if (!Dart_IsNull(color_filter)) {
|
||||
ColorFilter* decoded =
|
||||
tonic::DartConverter<ColorFilter*>::FromDart(color_filter);
|
||||
paint.setColorFilter(decoded->filter());
|
||||
}
|
||||
|
||||
Dart_Handle image_filter = values[kImageFilterIndex];
|
||||
if (!Dart_IsNull(image_filter)) {
|
||||
ImageFilter* decoded =
|
||||
tonic::DartConverter<ImageFilter*>::FromDart(image_filter);
|
||||
paint.setImageFilter(decoded->filter());
|
||||
}
|
||||
}
|
||||
|
||||
paint.setAntiAlias(uint_data[kIsAntiAliasIndex] == 0);
|
||||
|
||||
uint32_t encoded_color = uint_data[kColorIndex];
|
||||
paint.setColor(encoded_color ^ kColorDefault);
|
||||
|
||||
uint32_t encoded_blend_mode = uint_data[kBlendModeIndex];
|
||||
uint32_t blend_mode = encoded_blend_mode ^ kBlendModeDefault;
|
||||
paint.setBlendMode(static_cast<DlBlendMode>(blend_mode));
|
||||
|
||||
uint32_t style = uint_data[kStyleIndex];
|
||||
paint.setDrawStyle(static_cast<DlDrawStyle>(style));
|
||||
|
||||
float stroke_width = float_data[kStrokeWidthIndex];
|
||||
paint.setStrokeWidth(stroke_width);
|
||||
|
||||
float stroke_miter_limit = float_data[kStrokeMiterLimitIndex];
|
||||
paint.setStrokeMiter(stroke_miter_limit + kStrokeMiterLimitDefault);
|
||||
|
||||
uint32_t stroke_cap = uint_data[kStrokeCapIndex];
|
||||
paint.setStrokeCap(static_cast<DlStrokeCap>(stroke_cap));
|
||||
|
||||
uint32_t stroke_join = uint_data[kStrokeJoinIndex];
|
||||
paint.setStrokeJoin(static_cast<DlStrokeJoin>(stroke_join));
|
||||
|
||||
paint.setInvertColors(uint_data[kInvertColorIndex] != 0);
|
||||
|
||||
paint.setDither(uint_data[kDitherIndex] != 0);
|
||||
|
||||
switch (uint_data[kMaskFilterIndex]) {
|
||||
case kNull:
|
||||
break;
|
||||
case kBlur:
|
||||
SkBlurStyle blur_style =
|
||||
static_cast<SkBlurStyle>(uint_data[kMaskFilterBlurStyleIndex]);
|
||||
double sigma = float_data[kMaskFilterSigmaIndex];
|
||||
std::shared_ptr<DlBlurMaskFilter> dl_filter =
|
||||
std::make_shared<DlBlurMaskFilter>(blur_style, sigma);
|
||||
if (dl_filter->skia_object()) {
|
||||
paint.setMaskFilter(dl_filter);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
namespace tonic {
|
||||
|
||||
@@ -19,6 +19,8 @@ class Paint {
|
||||
|
||||
const SkPaint* paint(SkPaint& paint) const;
|
||||
|
||||
void toDlPaint(DlPaint& paint) const;
|
||||
|
||||
/// Synchronize the Dart properties to the display list according
|
||||
/// to the attribute flags that indicate which properties are needed.
|
||||
/// The return value indicates if the paint was non-null and can
|
||||
|
||||
62
engine/src/flutter/lib/ui/painting/paint_unittests.cc
Normal file
62
engine/src/flutter/lib/ui/painting/paint_unittests.cc
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright 2013 The Flutter 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 "flutter/lib/ui/painting/paint.h"
|
||||
#include "flutter/shell/common/shell_test.h"
|
||||
#include "flutter/shell/common/thread_host.h"
|
||||
|
||||
#include "flutter/testing/testing.h"
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
TEST_F(ShellTest, ConvertPaintToDlPaint) {
|
||||
auto message_latch = std::make_shared<fml::AutoResetWaitableEvent>();
|
||||
DlPaint dl_paint;
|
||||
|
||||
auto nativeToDlPaint = [message_latch, &dl_paint](Dart_NativeArguments args) {
|
||||
Dart_Handle dart_paint = Dart_GetNativeArgument(args, 0);
|
||||
Dart_Handle paint_objects =
|
||||
Dart_GetField(dart_paint, tonic::ToDart("_objects"));
|
||||
Dart_Handle paint_data = Dart_GetField(dart_paint, tonic::ToDart("_data"));
|
||||
Paint ui_paint(paint_objects, paint_data);
|
||||
ui_paint.toDlPaint(dl_paint);
|
||||
message_latch->Signal();
|
||||
};
|
||||
|
||||
Settings settings = CreateSettingsForFixture();
|
||||
TaskRunners task_runners("test", // label
|
||||
GetCurrentTaskRunner(), // platform
|
||||
CreateNewThread(), // raster
|
||||
CreateNewThread(), // ui
|
||||
CreateNewThread() // io
|
||||
);
|
||||
|
||||
AddNativeCallback("ConvertPaintToDlPaint",
|
||||
CREATE_NATIVE_ENTRY(nativeToDlPaint));
|
||||
|
||||
std::unique_ptr<Shell> shell = CreateShell(settings, task_runners);
|
||||
|
||||
ASSERT_TRUE(shell->IsSetup());
|
||||
auto configuration = RunConfiguration::InferFromSettings(settings);
|
||||
configuration.SetEntrypoint("convertPaintToDlPaint");
|
||||
|
||||
shell->RunEngine(std::move(configuration), [](auto result) {
|
||||
ASSERT_EQ(result, Engine::RunStatus::Success);
|
||||
});
|
||||
|
||||
message_latch->Wait();
|
||||
DestroyShell(std::move(shell), task_runners);
|
||||
|
||||
ASSERT_EQ(dl_paint.getBlendMode(), DlBlendMode::kModulate);
|
||||
ASSERT_EQ(static_cast<uint32_t>(dl_paint.getColor()), 0x11223344u);
|
||||
ASSERT_EQ(*dl_paint.getColorFilter(),
|
||||
DlBlendColorFilter(0x55667788, DlBlendMode::kXor));
|
||||
ASSERT_EQ(*dl_paint.getMaskFilter(),
|
||||
DlBlurMaskFilter(SkBlurStyle::kInner_SkBlurStyle, 0.75));
|
||||
ASSERT_EQ(dl_paint.getDrawStyle(), DlDrawStyle::kStroke);
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
Reference in New Issue
Block a user