Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RuntimeBlendTest.cpp
Go to the documentation of this file.
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
10#include "include/core/SkBlender.h" // IWYU pragma: keep
15#include "include/core/SkRect.h"
24#include "tests/Test.h"
26
27#include <cmath>
28#include <initializer_list>
29#include <vector>
30
31struct GrContextOptions;
32
33static bool nearly_equal(const SkColor& x, const SkColor& y) {
34 const int kTolerance = 1;
35 return abs((int)SkColorGetA(x) - (int)SkColorGetA(y)) <= kTolerance &&
36 abs((int)SkColorGetR(x) - (int)SkColorGetR(y)) <= kTolerance &&
37 abs((int)SkColorGetG(x) - (int)SkColorGetG(y)) <= kTolerance &&
38 abs((int)SkColorGetB(x) - (int)SkColorGetB(y)) <= kTolerance;
39}
40
43 REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
44
45 for (int m = 0; m < kSkBlendModeCount; ++m) {
46 SkBlendMode mode = (SkBlendMode)m;
47 for (int alpha : {0x80, 0xFF}) {
48 for (bool useShader : {false, true}) {
49 std::vector<SkColor> colors;
50 for (bool useRuntimeBlend : {false, true}) {
51 // Draw a solid red pixel.
53 paint.setColor(SK_ColorRED);
54 paint.setBlendMode(SkBlendMode::kSrc);
55 surface->getCanvas()->drawRect(SkRect::MakeWH(1, 1), paint);
56
57 // Draw a blue pixel on top of it, using the passed-in blend mode.
58 if (useShader) {
59 // Install a different color in the paint, to ensure we're using the shader
60 paint.setColor(SK_ColorGREEN);
61 paint.setShader(SkShaders::Color(SkColorSetARGB(alpha, 0x00, 0x00, 0xFF)));
62 } else {
63 paint.setColor(SkColorSetARGB(alpha, 0x00, 0x00, 0xFF));
64 }
65 if (useRuntimeBlend) {
66 paint.setBlender(GetRuntimeBlendForBlendMode(mode));
67 } else {
68 paint.setBlendMode(mode);
69 }
70 surface->getCanvas()->drawRect(SkRect::MakeWH(1, 1), paint);
71
72 // Read back the red/blue blended pixel.
74 surface->readPixels(bitmap.info(),
75 bitmap.getPixels(),
76 bitmap.rowBytes(),
77 /*srcX=*/0,
78 /*srcY=*/0));
79 colors.push_back(bitmap.getColor(/*x=*/0, /*y=*/0));
80 }
81
83 nearly_equal(colors[0], colors[1]),
84 "Expected: %s %s %s blend matches. Actual: Built-in "
85 "A=%02X R=%02X G=%02X B=%02X, Runtime A=%02X R=%02X G=%02X B=%02X",
86 SkBlendMode_Name(mode),
87 (alpha == 0xFF) ? "solid" : "transparent",
88 useShader ? "shader" : "paint",
89 SkColorGetA(colors[0]),
90 SkColorGetR(colors[0]),
91 SkColorGetG(colors[0]),
92 SkColorGetB(colors[0]),
93 SkColorGetA(colors[1]),
94 SkColorGetR(colors[1]),
95 SkColorGetG(colors[1]),
96 SkColorGetB(colors[1]));
97 }
98 }
99 }
100}
101
102DEF_TEST(SkRuntimeBlender_CPU, r) {
103 const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
105
106 test_blend(r, surface.get());
107}
108
110 r,
111 ctxInfo,
113 const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
115 SkSurfaces::RenderTarget(ctxInfo.directContext(), skgpu::Budgeted::kNo, info));
116 test_blend(r, surface.get());
117}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static constexpr float kTolerance
static void test_blend(skiatest::Reporter *r, SkSurface *surface)
static bool nearly_equal(const SkColor &x, const SkColor &y)
sk_sp< SkBlender > GetRuntimeBlendForBlendMode(SkBlendMode mode)
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
static constexpr int kSkBlendModeCount
Definition SkBlendMode.h:76
SkBlendMode
Definition SkBlendMode.h:38
#define SkColorGetR(color)
Definition SkColor.h:65
#define SkColorGetG(color)
Definition SkColor.h:69
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
#define SkColorGetA(color)
Definition SkColor.h:61
#define SkColorGetB(color)
Definition SkColor.h:73
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition Test.h:434
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
double y
double x
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609