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/SkBlendMode.h"
11 #include "src/base/SkArenaAlloc.h"
12 #include "src/gpu/Swizzle.h"
13 #include "src/gpu/graphite/ContextPriv.h"
14 #include "src/gpu/graphite/ContextUtils.h"
15 #include "src/gpu/graphite/PaintParamsKey.h"
16 #include "src/gpu/graphite/RendererProvider.h"
17 #include "src/gpu/graphite/ShaderCodeDictionary.h"
18 #include "src/gpu/graphite/ShaderInfo.h"
19
20 using namespace skgpu::graphite;
21
22 namespace {
23
add_block(PaintParamsKeyBuilder * builder,int snippetID)24 void add_block(PaintParamsKeyBuilder* builder, int snippetID) {
25 builder->beginBlock(snippetID);
26 builder->endBlock();
27 }
28
create_key(const ShaderCodeDictionary * dict,int snippetID,SkArenaAlloc * arena)29 PaintParamsKey create_key(const ShaderCodeDictionary* dict, int snippetID, SkArenaAlloc* arena) {
30 PaintParamsKeyBuilder builder{dict};
31 add_block(&builder, snippetID);
32
33 AutoLockBuilderAsKey keyView{&builder};
34 return keyView->clone(arena);
35 }
36
coeff_equal(SkBlendModeCoeff skCoeff,skgpu::BlendCoeff gpuCoeff)37 bool coeff_equal(SkBlendModeCoeff skCoeff, skgpu::BlendCoeff gpuCoeff) {
38 switch(skCoeff) {
39 case SkBlendModeCoeff::kZero: return skgpu::BlendCoeff::kZero == gpuCoeff;
40 case SkBlendModeCoeff::kOne: return skgpu::BlendCoeff::kOne == gpuCoeff;
41 case SkBlendModeCoeff::kSC: return skgpu::BlendCoeff::kSC == gpuCoeff;
42 case SkBlendModeCoeff::kISC: return skgpu::BlendCoeff::kISC == gpuCoeff;
43 case SkBlendModeCoeff::kDC: return skgpu::BlendCoeff::kDC == gpuCoeff;
44 case SkBlendModeCoeff::kIDC: return skgpu::BlendCoeff::kIDC == gpuCoeff;
45 case SkBlendModeCoeff::kSA: return skgpu::BlendCoeff::kSA == gpuCoeff;
46 case SkBlendModeCoeff::kISA: return skgpu::BlendCoeff::kISA == gpuCoeff;
47 case SkBlendModeCoeff::kDA: return skgpu::BlendCoeff::kDA == gpuCoeff;
48 case SkBlendModeCoeff::kIDA: return skgpu::BlendCoeff::kIDA == gpuCoeff;
49 default: return false;
50 }
51 }
52
53 } // anonymous namespace
54
55 // These are intended to be unit tests of the PaintParamsKeyBuilder and PaintParamsKey.
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(KeyWithInvalidCodeSnippetIDTest,reporter,context,CtsEnforcement::kApiLevel_V)56 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(KeyWithInvalidCodeSnippetIDTest, reporter, context,
57 CtsEnforcement::kApiLevel_V) {
58 SkArenaAlloc arena{256};
59 ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
60
61 // A builder without any data is invalid. The Builder and the PaintParamKeys can include
62 // invalid IDs without themselves becoming invalid. Normally adding an invalid ID triggers an
63 // assert in debug builds, since the properly functioning key system should never encounter an
64 // invalid ID.
65 PaintParamsKeyBuilder builder(dict);
66 AutoLockBuilderAsKey keyView{&builder};
67 REPORTER_ASSERT(reporter, !keyView->isValid());
68 REPORTER_ASSERT(reporter, !PaintParamsKey::Invalid().isValid());
69
70 // However, if the program gets in a malformed state on release builds, the key
71 // could contain an invalid ID. In that case the invalid snippet IDs are detected when
72 // reconstructing the key into an effect tree for SkSL generation. To test this, we manually
73 // construct an invalid span and test that it returns a null shader node tree when treated as
74 // a PaintParamsKey.
75 // NOTE: This is intentionally abusing memory to create a corrupt scenario and is dependent on
76 // the structure of PaintParamsKey (just SkSpan<const int32_t>).
77 int32_t invalidKeyData[3] = {(int32_t) BuiltInCodeSnippetID::kSolidColorShader,
78 SkKnownRuntimeEffects::kSkiaBuiltInReservedCnt - 1,
79 (int32_t) BuiltInCodeSnippetID::kFixedBlend_Src};
80 SkSpan<const int32_t> invalidKeySpan{invalidKeyData, std::size(invalidKeyData)*sizeof(int32_t)};
81 const PaintParamsKey* fakeKey = reinterpret_cast<const PaintParamsKey*>(&invalidKeySpan);
82 REPORTER_ASSERT(reporter, fakeKey->getRootNodes(dict, &arena).empty());
83 }
84
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(KeyEqualityChecksSnippetID,reporter,context,CtsEnforcement::kApiLevel_V)85 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(KeyEqualityChecksSnippetID, reporter, context,
86 CtsEnforcement::kApiLevel_V) {
87 SkArenaAlloc arena{256};
88 ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
89
90 PaintParamsKey keyA = create_key(dict, (int) BuiltInCodeSnippetID::kSolidColorShader, &arena);
91 PaintParamsKey keyB = create_key(dict, (int) BuiltInCodeSnippetID::kSolidColorShader, &arena);
92 PaintParamsKey keyC = create_key(dict, (int) BuiltInCodeSnippetID::kRGBPaintColor, &arena);
93
94 // Verify that keyA matches keyB, and that it does not match keyC.
95 REPORTER_ASSERT(reporter, keyA == keyB);
96 REPORTER_ASSERT(reporter, keyA != keyC);
97 REPORTER_ASSERT(reporter, !(keyA == keyC));
98 REPORTER_ASSERT(reporter, !(keyA != keyB));
99 }
100
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ShaderInfoDetectsFixedFunctionBlend,reporter,context,CtsEnforcement::kApiLevel_V)101 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ShaderInfoDetectsFixedFunctionBlend, reporter, context,
102 CtsEnforcement::kApiLevel_V) {
103 ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
104
105 for (int bm = 0; bm <= (int) SkBlendMode::kLastCoeffMode; ++bm) {
106 PaintParamsKeyBuilder builder(dict);
107 // Use a solid color as the 1st root node; the 2nd root node represents the final blend.
108 add_block(&builder, (int) BuiltInCodeSnippetID::kSolidColorShader);
109 add_block(&builder, bm + kFixedBlendIDOffset);
110 UniquePaintParamsID paintID = dict->findOrCreate(&builder);
111
112 const RenderStep* renderStep = &context->priv().rendererProvider()->nonAABounds()->step(0);
113 std::unique_ptr<ShaderInfo> shaderInfo = ShaderInfo::Make(context->priv().caps(),
114 dict,
115 /*rteDict=*/nullptr,
116 renderStep,
117 paintID,
118 /*useStorageBuffers=*/false,
119 skgpu::Swizzle::RGBA());
120
121 SkBlendMode expectedBM = static_cast<SkBlendMode>(bm);
122 if (expectedBM == SkBlendMode::kPlus) {
123 // The kPlus "coefficient" blend mode always triggers shader blending to add a clamping
124 // step that was originally elided in Porter-Duff due to automatic saturation to 8-bit
125 // color values. Shader-based blending always uses kSrc HW blending.
126 expectedBM = SkBlendMode::kSrc;
127 }
128 SkBlendModeCoeff expectedSrc, expectedDst;
129 REPORTER_ASSERT(reporter, SkBlendMode_AsCoeff(expectedBM, &expectedSrc, &expectedDst));
130 REPORTER_ASSERT(reporter, coeff_equal(expectedSrc, shaderInfo->blendInfo().fSrcBlend));
131 REPORTER_ASSERT(reporter, coeff_equal(expectedDst, shaderInfo->blendInfo().fDstBlend));
132
133 REPORTER_ASSERT(reporter, shaderInfo->blendInfo().fEquation == skgpu::BlendEquation::kAdd);
134 REPORTER_ASSERT(reporter,
135 shaderInfo->blendInfo().fBlendConstant == SK_PMColor4fTRANSPARENT);
136
137 bool expectedWriteColor = BlendModifiesDst(skgpu::BlendEquation::kAdd,
138 shaderInfo->blendInfo().fSrcBlend,
139 shaderInfo->blendInfo().fDstBlend);
140 REPORTER_ASSERT(reporter, shaderInfo->blendInfo().fWritesColor == expectedWriteColor);
141 }
142 }
143
144 // TODO: Add unit tests for converting a complex key to a ShaderInfo
145