xref: /aosp_15_r20/external/skia/tests/graphite/ImageShaderTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "tests/Test.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkTileMode.h"
12 #include "include/gpu/graphite/Context.h"
13 #include "include/gpu/graphite/Surface.h"
14 #include "include/gpu/graphite/Image.h"
15 #include "include/gpu/graphite/Recorder.h"
16 #include "src/gpu/graphite/Surface_Graphite.h"
17 #include "src/shaders/SkImageShader.h"
18 #include "tools/ToolUtils.h"
19 
20 namespace skgpu::graphite {
21 
22 namespace {
23 
24 constexpr SkColor4f kRectColor = SkColors::kRed;
25 constexpr SkColor4f kBgColor = SkColors::kTransparent;
26 
27 struct Expectation {
28     SkPoint pos;
29     SkColor4f color;
30 };
31 
test_draw(skiatest::Reporter * reporter,Context * context,SkISize canvasSize,SkISize imageSize,SkRect srcRect,SkRect dstRect,SkTileMode tileMode,SkSamplingOptions samplingOptions,std::vector<Expectation> expectations)32 void test_draw(skiatest::Reporter* reporter,
33                Context* context,
34                SkISize canvasSize,
35                SkISize imageSize,
36                SkRect srcRect,
37                SkRect dstRect,
38                SkTileMode tileMode,
39                SkSamplingOptions samplingOptions,
40                std::vector<Expectation> expectations) {
41     std::unique_ptr<Recorder> recorder = context->makeRecorder();
42     REPORTER_ASSERT(reporter, recorder);
43     sk_sp<SkSurface> surface = SkSurfaces::RenderTarget(
44             recorder.get(),
45             SkImageInfo::Make(canvasSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
46     SkCanvas* canvas = surface->getCanvas();
47 
48     SkBitmap bitmap;
49     bitmap.allocPixels(SkImageInfo::Make(imageSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
50                        0);
51     bitmap.eraseColor(kRectColor);
52     bitmap.setImmutable();
53     sk_sp<SkImage> image = SkImages::TextureFromImage(recorder.get(), bitmap.asImage(),
54                                                       {/*fMipmapped=*/false});
55 
56     SkPaint p;
57     SkMatrix srcToDst = SkMatrix::RectToRect(srcRect, dstRect);
58     p.setShader(SkImageShader::MakeSubset(
59             std::move(image), srcRect, tileMode, tileMode, samplingOptions, &srcToDst));
60     canvas->drawRect(dstRect, p);
61 
62     SkPixmap pm;
63 
64     SkBitmap result;
65     result.allocPixels(SkImageInfo::Make(canvasSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
66     bool peekPixelsSuccess = result.peekPixels(&pm);
67     REPORTER_ASSERT(reporter, peekPixelsSuccess);
68 
69     bool readPixelsSuccess = surface->readPixels(pm, 0, 0);
70     REPORTER_ASSERT(reporter, readPixelsSuccess);
71 
72     for (const Expectation& e : expectations) {
73         SkColor4f a = e.color;
74         SkColor4f b = pm.getColor4f(e.pos.fX, e.pos.fY);
75         REPORTER_ASSERT(reporter,
76                         a == b,
77                         "At position {%.1f, %.1f}, "
78                         "expected {%.1f, %.1f, %.1f, %.1f}, "
79                         "found {%.1f, %.1f, %.1f, %.1f}",
80                         e.pos.fX, e.pos.fY,
81                         a.fR, a.fG, a.fB, a.fA,
82                         b.fR, b.fG, b.fB, b.fA);
83     }
84 }
85 
86 }  // anonymous namespace
87 
DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(ImageShaderTest,reporter,context,CtsEnforcement::kApiLevel_V)88 DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(ImageShaderTest, reporter, context,
89                                          CtsEnforcement::kApiLevel_V) {
90     // Test that a subset bound covering less than half of a pixel causes that pixel not to be
91     // drawn when using decal tiling and nearest-neighbor filtering. In this case we have a subset
92     // that covers 3/4 the pixel column at y=1, all of the y=2 column, and 1/4 the y=3 column.
93     test_draw(reporter,
94               context,
95               /*canvasSize=*/SkISize::Make(100, 100),
96               /*imageSize=*/SkISize::Make(4, 4),
97               /*srcRect=*/SkRect::MakeLTRB(1.25, 0.0f, 3.25f, 2.0f),
98               /*dstRect=*/SkRect::MakeLTRB(0, 0, 80, 80),
99               SkTileMode::kDecal,
100               SkSamplingOptions(),
101 
102               // Pixel that should sample the image at y=1, since that's where the subset starts.
103               {{{0, 40}, kRectColor},
104                // Pixel that would sample the image at y=3, but the subset bound at y=3.25 prevents
105                // us from sampling the image.
106                {{75, 40}, kBgColor}});
107 }
108 
109 }  // namespace skgpu::graphite
110