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