[impeller] Convert drawImageNine into drawImageRect (flutter/engine#35173)
This commit is contained in:
@@ -510,6 +510,8 @@ FILE: ../../../flutter/impeller/display_list/display_list_image_impeller.h
|
||||
FILE: ../../../flutter/impeller/display_list/display_list_playground.cc
|
||||
FILE: ../../../flutter/impeller/display_list/display_list_playground.h
|
||||
FILE: ../../../flutter/impeller/display_list/display_list_unittests.cc
|
||||
FILE: ../../../flutter/impeller/display_list/nine_patch_converter.cc
|
||||
FILE: ../../../flutter/impeller/display_list/nine_patch_converter.h
|
||||
FILE: ../../../flutter/impeller/docs/assets/read_frame_captures/image1.png
|
||||
FILE: ../../../flutter/impeller/docs/assets/read_frame_captures/image10.png
|
||||
FILE: ../../../flutter/impeller/docs/assets/read_frame_captures/image11.png
|
||||
|
||||
@@ -10,6 +10,8 @@ impeller_component("display_list") {
|
||||
"display_list_dispatcher.h",
|
||||
"display_list_image_impeller.cc",
|
||||
"display_list_image_impeller.h",
|
||||
"nine_patch_converter.cc",
|
||||
"nine_patch_converter.h",
|
||||
]
|
||||
|
||||
public_deps = [
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "impeller/display_list/display_list_image_impeller.h"
|
||||
#include "impeller/display_list/nine_patch_converter.h"
|
||||
#include "impeller/entity/contents/filters/filter_contents.h"
|
||||
#include "impeller/entity/contents/linear_gradient_contents.h"
|
||||
#include "impeller/entity/contents/radial_gradient_contents.h"
|
||||
@@ -827,6 +828,24 @@ static impeller::SamplerDescriptor ToSamplerDescriptor(
|
||||
return desc;
|
||||
}
|
||||
|
||||
static impeller::SamplerDescriptor ToSamplerDescriptor(
|
||||
const flutter::DlFilterMode options) {
|
||||
impeller::SamplerDescriptor desc;
|
||||
switch (options) {
|
||||
case flutter::DlFilterMode::kNearest:
|
||||
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
|
||||
desc.label = "Nearest Sampler";
|
||||
break;
|
||||
case flutter::DlFilterMode::kLinear:
|
||||
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
|
||||
desc.label = "Linear Sampler";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return desc;
|
||||
}
|
||||
|
||||
// |flutter::Dispatcher|
|
||||
void DisplayListDispatcher::drawImageRect(
|
||||
const sk_sp<flutter::DlImage> image,
|
||||
@@ -837,7 +856,7 @@ void DisplayListDispatcher::drawImageRect(
|
||||
SkCanvas::SrcRectConstraint constraint) {
|
||||
canvas_.DrawImageRect(
|
||||
std::make_shared<Image>(image->impeller_texture()), // image
|
||||
ToRect(src), // source rect
|
||||
ToRect(src), // source rect
|
||||
ToRect(dst), // destination rect
|
||||
paint_, // paint
|
||||
ToSamplerDescriptor(sampling) // sampling
|
||||
@@ -850,8 +869,11 @@ void DisplayListDispatcher::drawImageNine(const sk_sp<flutter::DlImage> image,
|
||||
const SkRect& dst,
|
||||
flutter::DlFilterMode filter,
|
||||
bool render_with_attributes) {
|
||||
// Needs https://github.com/flutter/flutter/issues/95434
|
||||
UNIMPLEMENTED;
|
||||
NinePatchConverter converter = {};
|
||||
converter.DrawNinePatch(
|
||||
std::make_shared<Image>(image->impeller_texture()),
|
||||
Rect::MakeLTRB(center.fLeft, center.fTop, center.fRight, center.fBottom),
|
||||
ToRect(dst), ToSamplerDescriptor(filter), &canvas_, &paint_);
|
||||
}
|
||||
|
||||
// |flutter::Dispatcher|
|
||||
@@ -862,6 +884,9 @@ void DisplayListDispatcher::drawImageLattice(
|
||||
flutter::DlFilterMode filter,
|
||||
bool render_with_attributes) {
|
||||
// Needs https://github.com/flutter/flutter/issues/95434
|
||||
// Don't implement this one since it is not exposed by flutter,
|
||||
// Skia internally converts calls to drawImageNine into this method,
|
||||
// which is then converted back to drawImageNine by display list.
|
||||
UNIMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
@@ -357,5 +357,81 @@ TEST_P(DisplayListTest, CanDrawBackdropFilter) {
|
||||
ASSERT_TRUE(OpenPlaygroundHere(callback));
|
||||
}
|
||||
|
||||
TEST_P(DisplayListTest, CanDrawNinePatchImage) {
|
||||
// Image is drawn with corners to scale and center pieces stretched to fit.
|
||||
auto texture = CreateTextureForFixture("embarcadero.jpg");
|
||||
flutter::DisplayListBuilder builder;
|
||||
auto size = texture->GetSize();
|
||||
builder.drawImageNine(
|
||||
DlImageImpeller::Make(texture),
|
||||
SkIRect::MakeLTRB(size.width / 4, size.height / 4, size.width * 3 / 4,
|
||||
size.height * 3 / 4),
|
||||
SkRect::MakeLTRB(0, 0, size.width * 2, size.height * 2),
|
||||
flutter::DlFilterMode::kNearest, true);
|
||||
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
|
||||
}
|
||||
|
||||
TEST_P(DisplayListTest, CanDrawNinePatchImageCenterWidthBiggerThanDest) {
|
||||
// Edge case, the width of the corners does not leave any room for the
|
||||
// center slice. The center (across the vertical axis) is folded out of the
|
||||
// resulting image.
|
||||
auto texture = CreateTextureForFixture("embarcadero.jpg");
|
||||
flutter::DisplayListBuilder builder;
|
||||
auto size = texture->GetSize();
|
||||
builder.drawImageNine(
|
||||
DlImageImpeller::Make(texture),
|
||||
SkIRect::MakeLTRB(size.width / 4, size.height / 4, size.width * 3 / 4,
|
||||
size.height * 3 / 4),
|
||||
SkRect::MakeLTRB(0, 0, size.width / 2, size.height),
|
||||
flutter::DlFilterMode::kNearest, true);
|
||||
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
|
||||
}
|
||||
|
||||
TEST_P(DisplayListTest, CanDrawNinePatchImageCenterHeightBiggerThanDest) {
|
||||
// Edge case, the height of the corners does not leave any room for the
|
||||
// center slice. The center (across the horizontal axis) is folded out of the
|
||||
// resulting image.
|
||||
auto texture = CreateTextureForFixture("embarcadero.jpg");
|
||||
flutter::DisplayListBuilder builder;
|
||||
auto size = texture->GetSize();
|
||||
builder.drawImageNine(
|
||||
DlImageImpeller::Make(texture),
|
||||
SkIRect::MakeLTRB(size.width / 4, size.height / 4, size.width * 3 / 4,
|
||||
size.height * 3 / 4),
|
||||
SkRect::MakeLTRB(0, 0, size.width, size.height / 2),
|
||||
flutter::DlFilterMode::kNearest, true);
|
||||
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
|
||||
}
|
||||
|
||||
TEST_P(DisplayListTest, CanDrawNinePatchImageCenterBiggerThanDest) {
|
||||
// Edge case, the width and height of the corners does not leave any
|
||||
// room for the center slices. Only the corners are displayed.
|
||||
auto texture = CreateTextureForFixture("embarcadero.jpg");
|
||||
flutter::DisplayListBuilder builder;
|
||||
auto size = texture->GetSize();
|
||||
builder.drawImageNine(
|
||||
DlImageImpeller::Make(texture),
|
||||
SkIRect::MakeLTRB(size.width / 4, size.height / 4, size.width * 3 / 4,
|
||||
size.height * 3 / 4),
|
||||
SkRect::MakeLTRB(0, 0, size.width / 2, size.height / 2),
|
||||
flutter::DlFilterMode::kNearest, true);
|
||||
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
|
||||
}
|
||||
|
||||
TEST_P(DisplayListTest, CanDrawNinePatchImageCornersScaledDown) {
|
||||
// Edge case, there is not enough room for the corners to be drawn
|
||||
// without scaling them down.
|
||||
auto texture = CreateTextureForFixture("embarcadero.jpg");
|
||||
flutter::DisplayListBuilder builder;
|
||||
auto size = texture->GetSize();
|
||||
builder.drawImageNine(
|
||||
DlImageImpeller::Make(texture),
|
||||
SkIRect::MakeLTRB(size.width / 4, size.height / 4, size.width * 3 / 4,
|
||||
size.height * 3 / 4),
|
||||
SkRect::MakeLTRB(0, 0, size.width / 4, size.height / 4),
|
||||
flutter::DlFilterMode::kNearest, true);
|
||||
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace impeller
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
// 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 <vector>
|
||||
|
||||
#include "impeller/display_list/nine_patch_converter.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
NinePatchConverter::NinePatchConverter() = default;
|
||||
|
||||
NinePatchConverter::~NinePatchConverter() = default;
|
||||
|
||||
std::vector<double> NinePatchConverter::InitSlices(double img0,
|
||||
double imgC0,
|
||||
double imgC1,
|
||||
double img1,
|
||||
double dst0,
|
||||
double dst1) {
|
||||
auto imageDim = img1 - img0;
|
||||
auto destDim = dst1 - dst0;
|
||||
|
||||
if (imageDim == destDim) {
|
||||
// If the src and dest are the same size then we do not need scaling
|
||||
// We return 4 values for a single slice
|
||||
return {img0, dst0, img1, dst1};
|
||||
}
|
||||
|
||||
auto edge0Dim = imgC0 - img0;
|
||||
auto edge1Dim = img1 - imgC1;
|
||||
auto edgesDim = edge0Dim + edge1Dim;
|
||||
|
||||
if (edgesDim >= destDim) {
|
||||
// the center portion has disappeared, leaving only the edges to scale to a
|
||||
// common center position in the destination this produces only 2 slices
|
||||
// which is 8 values
|
||||
auto dstC = dst0 + destDim * edge0Dim / edgesDim;
|
||||
// clang-format off
|
||||
return {
|
||||
img0, dst0, imgC0, dstC,
|
||||
imgC1, dstC, img1, dst1,
|
||||
};
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// center portion is nonEmpty and only that part is scaled
|
||||
// we need 3 slices which is 12 values
|
||||
auto dstC0 = dst0 + edge0Dim;
|
||||
auto dstC1 = dst1 - edge1Dim;
|
||||
// clang-format off
|
||||
return {
|
||||
img0, dst0, imgC0, dstC0,
|
||||
imgC0, dstC0, imgC1, dstC1,
|
||||
imgC1, dstC1, img1, dst1,
|
||||
};
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
void NinePatchConverter::DrawNinePatch(std::shared_ptr<Image> image,
|
||||
Rect center,
|
||||
Rect dst,
|
||||
SamplerDescriptor sampler,
|
||||
Canvas* canvas,
|
||||
Paint* paint) {
|
||||
if (dst.IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
auto image_size = image->GetSize();
|
||||
auto hSlices = InitSlices(0, center.GetLeft(), center.GetRight(),
|
||||
image_size.width, dst.GetLeft(), dst.GetRight());
|
||||
auto vSlices = InitSlices(0, center.GetTop(), center.GetBottom(),
|
||||
image_size.height, dst.GetTop(), dst.GetBottom());
|
||||
|
||||
for (size_t yi = 0; yi < vSlices.size(); yi += 4) {
|
||||
auto srcY0 = vSlices[yi];
|
||||
auto dstY0 = vSlices[yi + 1];
|
||||
auto srcY1 = vSlices[yi + 2];
|
||||
auto dstY1 = vSlices[yi + 3];
|
||||
for (size_t xi = 0; xi < hSlices.size(); xi += 4) {
|
||||
auto srcX0 = hSlices[xi];
|
||||
auto dstX0 = hSlices[xi + 1];
|
||||
auto srcX1 = hSlices[xi + 2];
|
||||
auto dstX1 = hSlices[xi + 3];
|
||||
// TODO(jonahwilliams): consider converting this into a single call to
|
||||
// DrawImageAtlas.
|
||||
canvas->DrawImageRect(image, Rect::MakeLTRB(srcX0, srcY0, srcX1, srcY1),
|
||||
Rect::MakeLTRB(dstX0, dstY0, dstX1, dstY1), *paint,
|
||||
sampler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
@@ -0,0 +1,47 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/aiks/canvas.h"
|
||||
#include "impeller/aiks/image.h"
|
||||
#include "impeller/aiks/paint.h"
|
||||
#include "impeller/geometry/path.h"
|
||||
#include "impeller/renderer/sampler_descriptor.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
// Converts a call to draw a nine patch image into a draw atlas call.
|
||||
class NinePatchConverter {
|
||||
public:
|
||||
NinePatchConverter();
|
||||
|
||||
~NinePatchConverter();
|
||||
|
||||
void DrawNinePatch(std::shared_ptr<Image> image,
|
||||
Rect center,
|
||||
Rect dst,
|
||||
SamplerDescriptor sampler,
|
||||
Canvas* canvas,
|
||||
Paint* paint);
|
||||
|
||||
private:
|
||||
// Return a list of slice coordinates based on the size of the nine-slice
|
||||
// parameters in one dimension. Each set of slice coordinates contains a
|
||||
// begin/end pair for each of the source (image) and dest (screen) in the
|
||||
// order (src0, dst0, src1, dst1). The area from src0 => src1 of the image is
|
||||
// painted on the screen from dst0 => dst1 The slices for each dimension are
|
||||
// generated independently.
|
||||
std::vector<double> InitSlices(double img0,
|
||||
double imgC0,
|
||||
double imgC1,
|
||||
double img1,
|
||||
double dst0,
|
||||
double dst1);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
Reference in New Issue
Block a user