Flutter Engine
The Flutter Engine
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) {
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) {
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
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",
87 (alpha == 0xFF) ? "solid" : "transparent",
88 useShader ? "shader" : "paint",
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
Definition: GrQuadUtils.cpp:29
DEF_TEST(SkRuntimeBlender_CPU, r)
static void test_blend(skiatest::Reporter *r, SkSurface *surface)
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkRuntimeBlender_GPU, r, ctxInfo, CtsEnforcement::kApiLevel_T)
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 REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
double y
double x
PODArray< SkColor > colors
Definition: SkRecords.h:276
SK_API sk_sp< SkShader > Color(SkColor)
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)
Definition: bitmap.py:1
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
SIN Vec< N, float > abs(const Vec< N, float > &x)
Definition: SkVx.h:707
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609