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_202404)56 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(KeyWithInvalidCodeSnippetIDTest, reporter, context,
57 CtsEnforcement::kApiLevel_202404) {
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_202404)85 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(KeyEqualityChecksSnippetID, reporter, context,
86 CtsEnforcement::kApiLevel_202404) {
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_202404)101 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ShaderInfoDetectsFixedFunctionBlend, reporter, context,
102 CtsEnforcement::kApiLevel_202404) {
103 ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
104
105 // For determining the DstReadStrategy, pass in reasonable render target texture properties.
106 const Caps* caps = context->priv().caps();
107
108 // Use reasonable render target texture information to query Caps for the DstReadStrategy to use
109 // should a dst read be required by any of the paints encountered.
110 // TODO(b/390458117): Once the TextureInfo argument is removed from getDstReadStrategy, this is
111 // no longer necessary.
112 DstReadStrategy dstReadStrategy = caps->getDstReadStrategy(
113 caps->getDefaultSampledTextureInfo(SkColorType::kRGBA_8888_SkColorType,
114 skgpu::Mipmapped::kNo,
115 skgpu::Protected::kNo,
116 skgpu::Renderable::kYes));
117
118 for (int bm = 0; bm <= (int) SkBlendMode::kLastCoeffMode; ++bm) {
119 PaintParamsKeyBuilder builder(dict);
120 // Use a solid color as the 1st root node; the 2nd root node represents the final blend.
121 add_block(&builder, (int) BuiltInCodeSnippetID::kSolidColorShader);
122 add_block(&builder, bm + kFixedBlendIDOffset);
123 UniquePaintParamsID paintID = dict->findOrCreate(&builder);
124
125 const RenderStep* renderStep = &context->priv().rendererProvider()->nonAABounds()->step(0);
126 bool dstReadRequired =
127 IsDstReadRequired(caps, static_cast<SkBlendMode>(bm), renderStep->coverage());
128
129 // ShaderInfo expects to receive a concrete determination of dstReadStrategy based upon
130 // whether a dst read is needed. Therefore, we need to decide whether to pass in the
131 // dstReadStrategy reported by caps OR DstReadStrategy::kNoneRequired.
132 std::unique_ptr<ShaderInfo> shaderInfo =
133 ShaderInfo::Make(caps,
134 dict,
135 /*rteDict=*/nullptr,
136 renderStep,
137 paintID,
138 /*useStorageBuffers=*/false,
139 skgpu::Swizzle::RGBA(),
140 dstReadRequired ? dstReadStrategy
141 : DstReadStrategy::kNoneRequired);
142
143 SkBlendMode expectedBM = static_cast<SkBlendMode>(bm);
144 if (expectedBM == SkBlendMode::kPlus) {
145 // The kPlus "coefficient" blend mode always triggers shader blending to add a clamping
146 // step that was originally elided in Porter-Duff due to automatic saturation to 8-bit
147 // color values. Shader-based blending always uses kSrc HW blending.
148 expectedBM = SkBlendMode::kSrc;
149 }
150 SkBlendModeCoeff expectedSrc, expectedDst;
151 REPORTER_ASSERT(reporter, SkBlendMode_AsCoeff(expectedBM, &expectedSrc, &expectedDst));
152 REPORTER_ASSERT(reporter, coeff_equal(expectedSrc, shaderInfo->blendInfo().fSrcBlend));
153 REPORTER_ASSERT(reporter, coeff_equal(expectedDst, shaderInfo->blendInfo().fDstBlend));
154
155 REPORTER_ASSERT(reporter, shaderInfo->blendInfo().fEquation == skgpu::BlendEquation::kAdd);
156 REPORTER_ASSERT(reporter,
157 shaderInfo->blendInfo().fBlendConstant == SK_PMColor4fTRANSPARENT);
158
159 bool expectedWriteColor = BlendModifiesDst(skgpu::BlendEquation::kAdd,
160 shaderInfo->blendInfo().fSrcBlend,
161 shaderInfo->blendInfo().fDstBlend);
162 REPORTER_ASSERT(reporter, shaderInfo->blendInfo().fWritesColor == expectedWriteColor);
163 }
164 }
165
166 // TODO: Add unit tests for converting a complex key to a ShaderInfo
167