• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 "include/core/SkBitmap.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkBlender.h" // IWYU pragma: keep
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkShader.h"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTypes.h"
20 #include "include/gpu/GpuTypes.h"
21 #include "include/gpu/GrDirectContext.h"
22 #include "tests/CtsEnforcement.h"
23 #include "tests/Test.h"
24 #include "tools/RuntimeBlendUtils.h"
25 
26 #include <cmath>
27 #include <initializer_list>
28 #include <vector>
29 
30 struct GrContextOptions;
31 
nearly_equal(const SkColor & x,const SkColor & y)32 static bool nearly_equal(const SkColor& x, const SkColor& y) {
33     const int kTolerance = 1;
34     return abs((int)SkColorGetA(x) - (int)SkColorGetA(y)) <= kTolerance &&
35            abs((int)SkColorGetR(x) - (int)SkColorGetR(y)) <= kTolerance &&
36            abs((int)SkColorGetG(x) - (int)SkColorGetG(y)) <= kTolerance &&
37            abs((int)SkColorGetB(x) - (int)SkColorGetB(y)) <= kTolerance;
38 }
39 
test_blend(skiatest::Reporter * r,SkSurface * surface)40 static void test_blend(skiatest::Reporter* r, SkSurface* surface) {
41     SkBitmap bitmap;
42     REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
43 
44     for (int m = 0; m < kSkBlendModeCount; ++m) {
45         SkBlendMode mode = (SkBlendMode)m;
46         for (int alpha : {0x80, 0xFF}) {
47             for (bool useShader : {false, true}) {
48                 std::vector<SkColor> colors;
49                 for (bool useRuntimeBlend : {false, true}) {
50                     // Draw a solid red pixel.
51                     SkPaint paint;
52                     paint.setColor(SK_ColorRED);
53                     paint.setBlendMode(SkBlendMode::kSrc);
54                     surface->getCanvas()->drawRect(SkRect::MakeWH(1, 1), paint);
55 
56                     // Draw a blue pixel on top of it, using the passed-in blend mode.
57                     if (useShader) {
58                         // Install a different color in the paint, to ensure we're using the shader
59                         paint.setColor(SK_ColorGREEN);
60                         paint.setShader(SkShaders::Color(SkColorSetARGB(alpha, 0x00, 0x00, 0xFF)));
61                     } else {
62                         paint.setColor(SkColorSetARGB(alpha, 0x00, 0x00, 0xFF));
63                     }
64                     if (useRuntimeBlend) {
65                         paint.setBlender(GetRuntimeBlendForBlendMode(mode));
66                     } else {
67                         paint.setBlendMode(mode);
68                     }
69                     surface->getCanvas()->drawRect(SkRect::MakeWH(1, 1), paint);
70 
71                     // Read back the red/blue blended pixel.
72                     REPORTER_ASSERT(r,
73                                     surface->readPixels(bitmap.info(),
74                                                         bitmap.getPixels(),
75                                                         bitmap.rowBytes(),
76                                                         /*srcX=*/0,
77                                                         /*srcY=*/0));
78                     colors.push_back(bitmap.getColor(/*x=*/0, /*y=*/0));
79                 }
80 
81                 REPORTER_ASSERT(r,
82                                 nearly_equal(colors[0], colors[1]),
83                                 "Expected: %s %s %s blend matches. Actual: Built-in "
84                                 "A=%02X R=%02X G=%02X B=%02X, Runtime A=%02X R=%02X G=%02X B=%02X",
85                                 SkBlendMode_Name(mode),
86                                 (alpha == 0xFF) ? "solid" : "transparent",
87                                 useShader ? "shader" : "paint",
88                                 SkColorGetA(colors[0]),
89                                 SkColorGetR(colors[0]),
90                                 SkColorGetG(colors[0]),
91                                 SkColorGetB(colors[0]),
92                                 SkColorGetA(colors[1]),
93                                 SkColorGetR(colors[1]),
94                                 SkColorGetG(colors[1]),
95                                 SkColorGetB(colors[1]));
96             }
97         }
98     }
99 }
100 
DEF_TEST(SkRuntimeBlender_CPU,r)101 DEF_TEST(SkRuntimeBlender_CPU, r) {
102     const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
103     sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
104 
105     test_blend(r, surface.get());
106 }
107 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkRuntimeBlender_GPU,r,ctxInfo,CtsEnforcement::kApiLevel_T)108 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkRuntimeBlender_GPU,
109                                        r,
110                                        ctxInfo,
111                                        CtsEnforcement::kApiLevel_T) {
112     const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
113     sk_sp<SkSurface> surface(
114             SkSurface::MakeRenderTarget(ctxInfo.directContext(), skgpu::Budgeted::kNo, info));
115     test_blend(r, surface.get());
116 }
117