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