Flutter Impeller
aiks_dl_atlas_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "display_list/dl_sampling_options.h"
7 
8 #include "flutter/display_list/dl_blend_mode.h"
9 #include "flutter/display_list/dl_builder.h"
10 #include "flutter/display_list/dl_color.h"
11 #include "flutter/display_list/dl_paint.h"
12 #include "flutter/testing/testing.h"
15 #include "include/core/SkRSXform.h"
16 #include "include/core/SkRefCnt.h"
17 
18 namespace impeller {
19 namespace testing {
20 
21 using namespace flutter;
22 
23 namespace {
24 SkRSXform MakeTranslation(Scalar tx, Scalar ty) {
25  return SkRSXform::Make(1, 0, tx, ty);
26 }
27 
28 std::tuple<std::vector<SkRect>, std::vector<SkRSXform>, sk_sp<DlImageImpeller>>
29 CreateTestData(const AiksTest* test) {
30  // Draws the image as four squares stiched together.
31  auto atlas =
32  DlImageImpeller::Make(test->CreateTextureForFixture("bay_bridge.jpg"));
33  auto size = atlas->impeller_texture()->GetSize();
34  // Divide image into four quadrants.
35  Scalar half_width = size.width / 2;
36  Scalar half_height = size.height / 2;
37  std::vector<SkRect> texture_coordinates = {
38  SkRect::MakeLTRB(0, 0, half_width, half_height),
39  SkRect::MakeLTRB(half_width, 0, size.width, half_height),
40  SkRect::MakeLTRB(0, half_height, half_width, size.height),
41  SkRect::MakeLTRB(half_width, half_height, size.width, size.height)};
42  // Position quadrants adjacent to eachother.
43  std::vector<SkRSXform> transforms = {
44  MakeTranslation(0, 0), MakeTranslation(half_width, 0),
45  MakeTranslation(0, half_height),
46  MakeTranslation(half_width, half_height)};
47  return std::make_tuple(texture_coordinates, transforms, atlas);
48 }
49 
50 } // namespace
51 
52 TEST_P(AiksTest, DrawAtlasNoColor) {
53  DisplayListBuilder builder;
54  auto [texture_coordinates, transforms, atlas] = CreateTestData(this);
55 
56  builder.Scale(GetContentScale().x, GetContentScale().y);
57  builder.DrawAtlas(atlas, transforms.data(), texture_coordinates.data(),
58  /*colors=*/nullptr, /*count=*/4, DlBlendMode::kSrcOver,
59  DlImageSampling::kNearestNeighbor, nullptr);
60 
61  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
62 }
63 
64 TEST_P(AiksTest, DrawAtlasWithColorAdvanced) {
65  DisplayListBuilder builder;
66  auto [texture_coordinates, transforms, atlas] = CreateTestData(this);
67 
68  std::vector<DlColor> colors = {DlColor::kRed(), DlColor::kGreen(),
69  DlColor::kBlue(), DlColor::kYellow()};
70 
71  builder.Scale(GetContentScale().x, GetContentScale().y);
72  builder.DrawAtlas(atlas, transforms.data(), texture_coordinates.data(),
73  colors.data(), /*count=*/4, DlBlendMode::kModulate,
74  DlImageSampling::kNearestNeighbor, /*cullRect=*/nullptr);
75 
76  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
77 }
78 
79 TEST_P(AiksTest, DrawAtlasWithColorSimple) {
80  DisplayListBuilder builder;
81  // Draws the image as four squares stiched together.
82  auto [texture_coordinates, transforms, atlas] = CreateTestData(this);
83 
84  std::vector<DlColor> colors = {DlColor::kRed(), DlColor::kGreen(),
85  DlColor::kBlue(), DlColor::kYellow()};
86 
87  builder.Scale(GetContentScale().x, GetContentScale().y);
88  builder.DrawAtlas(atlas, transforms.data(), texture_coordinates.data(),
89  colors.data(), /*count=*/4, DlBlendMode::kSrcATop,
90  DlImageSampling::kNearestNeighbor, /*cullRect=*/nullptr);
91 
92  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
93 }
94 
95 TEST_P(AiksTest, DrawAtlasWithOpacity) {
96  DisplayListBuilder builder;
97  // Draws the image as four squares stiched together slightly
98  // opaque
99  auto [texture_coordinates, transforms, atlas] = CreateTestData(this);
100 
101  DlPaint paint;
102  paint.setAlpha(128);
103  builder.Scale(GetContentScale().x, GetContentScale().y);
104  builder.DrawAtlas(atlas, transforms.data(), texture_coordinates.data(),
105  /*colors=*/nullptr, 4, DlBlendMode::kSrcOver,
106  DlImageSampling::kNearestNeighbor, /*cullRect=*/nullptr,
107  &paint);
108 
109  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
110 }
111 
112 TEST_P(AiksTest, DrawAtlasNoColorFullSize) {
113  auto atlas = DlImageImpeller::Make(CreateTextureForFixture("bay_bridge.jpg"));
114  auto size = atlas->impeller_texture()->GetSize();
115  std::vector<SkRect> texture_coordinates = {
116  SkRect::MakeLTRB(0, 0, size.width, size.height)};
117  std::vector<SkRSXform> transforms = {MakeTranslation(0, 0)};
118 
119  DisplayListBuilder builder;
120  builder.Scale(GetContentScale().x, GetContentScale().y);
121  builder.DrawAtlas(atlas, transforms.data(), texture_coordinates.data(),
122  /*colors=*/nullptr, /*count=*/1, DlBlendMode::kSrcOver,
123  DlImageSampling::kNearestNeighbor, /*cullRect=*/nullptr);
124 
125  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
126 }
127 
128 // Regression test for https://github.com/flutter/flutter/issues/127374.
129 TEST_P(AiksTest, DrawAtlasAdvancedAndTransform) {
130  DisplayListBuilder builder;
131  // Draws the image as four squares stiched together.
132  auto [texture_coordinates, transforms, atlas] = CreateTestData(this);
133 
134  builder.Scale(0.25, 0.25);
135  builder.DrawAtlas(atlas, transforms.data(), texture_coordinates.data(),
136  /*colors=*/nullptr, /*count=*/4, DlBlendMode::kModulate,
137  DlImageSampling::kNearestNeighbor, /*cullRect=*/nullptr);
138 
139  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
140 }
141 
142 // Regression test for https://github.com/flutter/flutter/issues/127374.
143 TEST_P(AiksTest, DrawAtlasWithColorAdvancedAndTransform) {
144  DisplayListBuilder builder;
145  // Draws the image as four squares stiched together.
146  auto [texture_coordinates, transforms, atlas] = CreateTestData(this);
147  std::vector<DlColor> colors = {DlColor::kRed(), DlColor::kGreen(),
148  DlColor::kBlue(), DlColor::kYellow()};
149 
150  builder.Scale(0.25, 0.25);
151  builder.DrawAtlas(atlas, transforms.data(), texture_coordinates.data(),
152  colors.data(), /*count=*/4, DlBlendMode::kModulate,
153  DlImageSampling::kNearestNeighbor, /*cullRect=*/nullptr);
154 
155  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
156 }
157 
158 TEST_P(AiksTest, DrawAtlasPlusWideGamut) {
159  DisplayListBuilder builder;
160  EXPECT_EQ(GetContext()->GetCapabilities()->GetDefaultColorFormat(),
162 
163  // Draws the image as four squares stiched together.
164  auto [texture_coordinates, transforms, atlas] = CreateTestData(this);
165  std::vector<DlColor> colors = {DlColor::kRed(), DlColor::kGreen(),
166  DlColor::kBlue(), DlColor::kYellow()};
167 
168  builder.DrawAtlas(atlas, transforms.data(), texture_coordinates.data(),
169  colors.data(), /*count=*/4, DlBlendMode::kPlus,
170  DlImageSampling::kNearestNeighbor, /*cullRect=*/nullptr);
171 
172  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
173 }
174 
175 } // namespace testing
176 } // namespace impeller
impeller::AiksPlayground
Definition: aiks_playground.h:17
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::PixelFormat::kB10G10R10A10XR
@ kB10G10R10A10XR
aiks_unittests.h
impeller::testing::AiksTest
AiksPlayground AiksTest
Definition: aiks_unittests.h:17
impeller::DlImageImpeller::Make
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
Definition: dl_image_impeller.cc:12
impeller::testing::TEST_P
TEST_P(AiksTest, CanRenderAdvancedBlendColorFilterWithSaveLayer)
Definition: aiks_blend_unittests.cc:21
flutter
Definition: dl_golden_blur_unittests.cc:14
scalar.h
paint
const Paint & paint
Definition: color_source.cc:38
impeller
Definition: aiks_blend_unittests.cc:18
dl_image_impeller.h