Make 'DlImageColorSource' work with 'DlImage's (flutter/engine#35258)
This commit is contained in:
@@ -2200,7 +2200,7 @@ BoundsTolerance CanvasCompareTester::DefaultTolerance =
|
||||
|
||||
const sk_sp<SkImage> CanvasCompareTester::kTestImage = makeTestImage();
|
||||
const DlImageColorSource CanvasCompareTester::kTestImageColorSource(
|
||||
kTestImage,
|
||||
DlImage::Make(kTestImage),
|
||||
DlTileMode::kRepeat,
|
||||
DlTileMode::kRepeat,
|
||||
DlImageSampling::kLinear);
|
||||
|
||||
@@ -17,8 +17,8 @@ std::shared_ptr<DlColorSource> DlColorSource::From(SkShader* sk_shader) {
|
||||
SkImage* image = sk_shader->isAImage(&local_matrix, xy);
|
||||
if (image) {
|
||||
return std::make_shared<DlImageColorSource>(
|
||||
sk_ref_sp(image), ToDl(xy[0]), ToDl(xy[1]), DlImageSampling::kLinear,
|
||||
&local_matrix);
|
||||
DlImage::Make(image), ToDl(xy[0]), ToDl(xy[1]),
|
||||
DlImageSampling::kLinear, &local_matrix);
|
||||
}
|
||||
}
|
||||
// Skia provides |SkShader->asAGradient(&info)| method to access the
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "flutter/display_list/display_list.h"
|
||||
#include "flutter/display_list/display_list_attributes.h"
|
||||
#include "flutter/display_list/display_list_color.h"
|
||||
#include "flutter/display_list/display_list_image.h"
|
||||
#include "flutter/display_list/display_list_sampling_options.h"
|
||||
#include "flutter/display_list/display_list_tile_mode.h"
|
||||
#include "flutter/display_list/types.h"
|
||||
@@ -201,14 +202,13 @@ class DlMatrixColorSourceBase : public DlColorSource {
|
||||
class DlImageColorSource final : public SkRefCnt,
|
||||
public DlMatrixColorSourceBase {
|
||||
public:
|
||||
// TODO(100984): Color sources must be DlImages instead of SkImages.
|
||||
DlImageColorSource(sk_sp<const SkImage> image,
|
||||
DlImageColorSource(sk_sp<const DlImage> image,
|
||||
DlTileMode horizontal_tile_mode,
|
||||
DlTileMode vertical_tile_mode,
|
||||
DlImageSampling sampling = DlImageSampling::kLinear,
|
||||
const SkMatrix* matrix = nullptr)
|
||||
: DlMatrixColorSourceBase(matrix),
|
||||
sk_image_(image),
|
||||
image_(image),
|
||||
horizontal_tile_mode_(horizontal_tile_mode),
|
||||
vertical_tile_mode_(vertical_tile_mode),
|
||||
sampling_(sampling) {}
|
||||
@@ -221,39 +221,48 @@ class DlImageColorSource final : public SkRefCnt,
|
||||
|
||||
std::shared_ptr<DlColorSource> with_sampling(
|
||||
DlImageSampling sampling) const override {
|
||||
return std::make_shared<DlImageColorSource>(
|
||||
sk_image_, horizontal_tile_mode_, vertical_tile_mode_, sampling,
|
||||
matrix_ptr());
|
||||
return std::make_shared<DlImageColorSource>(image_, horizontal_tile_mode_,
|
||||
vertical_tile_mode_, sampling,
|
||||
matrix_ptr());
|
||||
}
|
||||
|
||||
DlColorSourceType type() const override { return DlColorSourceType::kImage; }
|
||||
size_t size() const override { return sizeof(*this); }
|
||||
|
||||
bool is_opaque() const override { return sk_image_->isOpaque(); }
|
||||
bool is_opaque() const override {
|
||||
// TODO(109286): Consider implementing 'isOpaque' on 'DlImage'.
|
||||
if (!image_->skia_image()) {
|
||||
return false;
|
||||
}
|
||||
return image_->skia_image()->isOpaque();
|
||||
}
|
||||
|
||||
sk_sp<const SkImage> image() const { return sk_image_; }
|
||||
sk_sp<const DlImage> image() const { return image_; }
|
||||
DlTileMode horizontal_tile_mode() const { return horizontal_tile_mode_; }
|
||||
DlTileMode vertical_tile_mode() const { return vertical_tile_mode_; }
|
||||
DlImageSampling sampling() const { return sampling_; }
|
||||
|
||||
virtual sk_sp<SkShader> skia_object() const override {
|
||||
return sk_image_->makeShader(ToSk(horizontal_tile_mode_),
|
||||
ToSk(vertical_tile_mode_), ToSk(sampling_),
|
||||
matrix_ptr());
|
||||
if (!image_->skia_image()) {
|
||||
return nullptr;
|
||||
}
|
||||
return image_->skia_image()->makeShader(ToSk(horizontal_tile_mode_),
|
||||
ToSk(vertical_tile_mode_),
|
||||
ToSk(sampling_), matrix_ptr());
|
||||
}
|
||||
|
||||
protected:
|
||||
bool equals_(DlColorSource const& other) const override {
|
||||
FML_DCHECK(other.type() == DlColorSourceType::kImage);
|
||||
auto that = static_cast<DlImageColorSource const*>(&other);
|
||||
return (sk_image_ == that->sk_image_ && matrix() == that->matrix() &&
|
||||
return (image_->Equals(that->image_) && matrix() == that->matrix() &&
|
||||
horizontal_tile_mode_ == that->horizontal_tile_mode_ &&
|
||||
vertical_tile_mode_ == that->vertical_tile_mode_ &&
|
||||
sampling_ == that->sampling_);
|
||||
}
|
||||
|
||||
private:
|
||||
sk_sp<const SkImage> sk_image_;
|
||||
sk_sp<const DlImage> image_;
|
||||
DlTileMode horizontal_tile_mode_;
|
||||
DlTileMode vertical_tile_mode_;
|
||||
DlImageSampling sampling_;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "flutter/display_list/display_list_attributes_testing.h"
|
||||
#include "flutter/display_list/display_list_builder.h"
|
||||
#include "flutter/display_list/display_list_color_source.h"
|
||||
#include "flutter/display_list/display_list_image.h"
|
||||
#include "flutter/display_list/display_list_sampling_options.h"
|
||||
#include "flutter/display_list/types.h"
|
||||
#include "third_party/skia/include/core/SkSurface.h"
|
||||
@@ -12,7 +13,7 @@
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
static sk_sp<SkImage> MakeTestImage(int w, int h, SkColor color) {
|
||||
static sk_sp<DlImage> MakeTestImage(int w, int h, SkColor color) {
|
||||
sk_sp<SkSurface> surface;
|
||||
if (SkColorGetA(color) < 255) {
|
||||
surface = SkSurface::MakeRasterN32Premul(w, h);
|
||||
@@ -23,11 +24,11 @@ static sk_sp<SkImage> MakeTestImage(int w, int h, SkColor color) {
|
||||
}
|
||||
SkCanvas* canvas = surface->getCanvas();
|
||||
canvas->drawColor(color);
|
||||
return surface->makeImageSnapshot();
|
||||
return DlImage::Make(surface->makeImageSnapshot());
|
||||
}
|
||||
|
||||
static const sk_sp<SkImage> kTestImage1 = MakeTestImage(10, 10, SK_ColorGREEN);
|
||||
static const sk_sp<SkImage> kTestAlphaImage1 =
|
||||
static const sk_sp<DlImage> kTestImage1 = MakeTestImage(10, 10, SK_ColorGREEN);
|
||||
static const sk_sp<DlImage> kTestAlphaImage1 =
|
||||
MakeTestImage(10, 10, SK_ColorTRANSPARENT);
|
||||
// clang-format off
|
||||
static const SkMatrix kTestMatrix1 =
|
||||
@@ -117,15 +118,16 @@ TEST(DisplayListColorSource, FromSkiaColorShader) {
|
||||
}
|
||||
|
||||
TEST(DisplayListColorSource, FromSkiaImageShader) {
|
||||
sk_sp<SkShader> shader =
|
||||
kTestImage1->makeShader(ToSk(DlImageSampling::kLinear), &kTestMatrix1);
|
||||
sk_sp<SkShader> shader = kTestImage1->skia_image()->makeShader(
|
||||
ToSk(DlImageSampling::kLinear), &kTestMatrix1);
|
||||
std::shared_ptr<DlColorSource> source = DlColorSource::From(shader);
|
||||
DlImageColorSource dl_source(kTestImage1, DlTileMode::kClamp,
|
||||
DlTileMode::kClamp, DlImageSampling::kLinear,
|
||||
&kTestMatrix1);
|
||||
ASSERT_EQ(source->type(), DlColorSourceType::kImage);
|
||||
ASSERT_EQ(*source->asImage(), dl_source);
|
||||
ASSERT_EQ(source->asImage()->image(), kTestImage1);
|
||||
ASSERT_TRUE(source->asImage()->image()->Equals(kTestImage1));
|
||||
ASSERT_TRUE(kTestImage1->Equals(source->asImage()->image()));
|
||||
ASSERT_EQ(source->asImage()->matrix(), kTestMatrix1);
|
||||
ASSERT_EQ(source->asImage()->horizontal_tile_mode(), DlTileMode::kClamp);
|
||||
ASSERT_EQ(source->asImage()->vertical_tile_mode(), DlTileMode::kClamp);
|
||||
|
||||
@@ -97,6 +97,21 @@ class DlImage : public SkRefCnt {
|
||||
/// image.
|
||||
virtual std::optional<std::string> get_error() const;
|
||||
|
||||
bool Equals(const DlImage* other) const {
|
||||
if (!other) {
|
||||
return false;
|
||||
}
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
return skia_image() == other->skia_image() &&
|
||||
impeller_texture() == other->impeller_texture();
|
||||
}
|
||||
|
||||
bool Equals(const DlImage& other) const { return Equals(&other); }
|
||||
|
||||
bool Equals(sk_sp<const DlImage> other) const { return Equals(other.get()); }
|
||||
|
||||
protected:
|
||||
DlImage();
|
||||
};
|
||||
|
||||
@@ -99,7 +99,7 @@ static const sk_sp<SkBlender> kTestBlender2 =
|
||||
SkBlenders::Arithmetic(0.2, 0.2, 0.2, 0.2, true);
|
||||
static const sk_sp<SkBlender> kTestBlender3 =
|
||||
SkBlenders::Arithmetic(0.3, 0.3, 0.3, 0.3, true);
|
||||
static const DlImageColorSource kTestSource1(TestImage1->skia_image(),
|
||||
static const DlImageColorSource kTestSource1(TestImage1,
|
||||
DlTileMode::kClamp,
|
||||
DlTileMode::kMirror,
|
||||
kLinearSampling);
|
||||
|
||||
@@ -38,11 +38,7 @@ Dart_Handle ImageShader::initWithImage(CanvasImage* image,
|
||||
return ToDart("ImageShader constructor with GPU image is not supported.");
|
||||
}
|
||||
|
||||
auto raw_sk_image = image->image()->skia_image();
|
||||
if (!raw_sk_image) {
|
||||
return ToDart("ImageShader constructor with Impeller is not supported.");
|
||||
}
|
||||
sk_image_ = UIDartState::CreateGPUObject(std::move(raw_sk_image));
|
||||
image_ = image->image();
|
||||
tonic::Float64List matrix4(matrix_handle);
|
||||
SkMatrix local_matrix = ToSkMatrix(matrix4);
|
||||
matrix4.Release();
|
||||
@@ -51,7 +47,7 @@ Dart_Handle ImageShader::initWithImage(CanvasImage* image,
|
||||
sampling_is_locked_ ? ImageFilter::SamplingFromIndex(filter_quality_index)
|
||||
: DlImageSampling::kLinear;
|
||||
cached_shader_ = UIDartState::CreateGPUObject(sk_make_sp<DlImageColorSource>(
|
||||
sk_image_.skia_object(), ToDl(tmx), ToDl(tmy), sampling, &local_matrix));
|
||||
image_, ToDl(tmx), ToDl(tmy), sampling, &local_matrix));
|
||||
return Dart_Null();
|
||||
}
|
||||
|
||||
@@ -72,11 +68,11 @@ std::shared_ptr<DlColorSource> ImageShader::shader(DlImageSampling sampling) {
|
||||
}
|
||||
|
||||
int ImageShader::width() {
|
||||
return sk_image_.skia_object()->width();
|
||||
return image_->width();
|
||||
}
|
||||
|
||||
int ImageShader::height() {
|
||||
return sk_image_.skia_object()->height();
|
||||
return image_->height();
|
||||
}
|
||||
|
||||
ImageShader::ImageShader() = default;
|
||||
|
||||
@@ -38,7 +38,7 @@ class ImageShader : public Shader {
|
||||
private:
|
||||
ImageShader();
|
||||
|
||||
flutter::SkiaGPUObject<SkImage> sk_image_;
|
||||
sk_sp<const DlImage> image_;
|
||||
bool sampling_is_locked_;
|
||||
|
||||
flutter::SkiaGPUObject<DlImageColorSource> cached_shader_;
|
||||
|
||||
Reference in New Issue
Block a user