xref: /aosp_15_r20/external/skia/tests/OpChainTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2018 Google Inc.
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/SkRect.h"
9 #include "include/core/SkRefCnt.h"
10 #include "include/core/SkScalar.h"
11 #include "include/core/SkSize.h"
12 #include "include/core/SkTypes.h"
13 #include "include/gpu/GpuTypes.h"
14 #include "include/gpu/ganesh/GrBackendSurface.h"
15 #include "include/gpu/ganesh/GrDirectContext.h"
16 #include "include/gpu/ganesh/GrTypes.h"
17 #include "include/private/base/SkTDArray.h"
18 #include "include/private/gpu/ganesh/GrTypesPriv.h"
19 #include "src/base/SkRandom.h"
20 #include "src/gpu/AtlasTypes.h"
21 #include "src/gpu/SkBackingFit.h"
22 #include "src/gpu/Swizzle.h"
23 #include "src/gpu/ganesh/GrCaps.h"
24 #include "src/gpu/ganesh/GrDirectContextPriv.h"
25 #include "src/gpu/ganesh/GrOpFlushState.h"
26 #include "src/gpu/ganesh/GrProxyProvider.h"
27 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
28 #include "src/gpu/ganesh/GrSurfaceProxy.h"
29 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
30 #include "src/gpu/ganesh/GrTextureProxy.h"
31 #include "src/gpu/ganesh/GrTextureResolveManager.h"
32 #include "src/gpu/ganesh/ops/GrOp.h"
33 #include "src/gpu/ganesh/ops/OpsTask.h"
34 #include "tests/CtsEnforcement.h"
35 #include "tests/Test.h"
36 
37 #include <algorithm>
38 #include <array>
39 #include <cassert>
40 #include <cstddef>
41 #include <cstdint>
42 #include <iterator>
43 #include <utility>
44 #include <vector>
45 
46 class GrAppliedClip;
47 class GrDrawingManager;
48 class GrDstProxyView;
49 class GrRecordingContext;
50 class SkArenaAlloc;
51 enum class GrXferBarrierFlags;
52 struct GrContextOptions;
53 
54 // We create Ops that write a value into a range of a buffer. We create ranges from
55 // kNumOpPositions starting positions x kRanges canonical ranges. We repeat each range kNumRepeats
56 // times (with a different value written by each of the repeats).
57 namespace {
58 struct Range {
59     unsigned fOffset;
60     unsigned fLength;
61 };
62 
63 static constexpr int kNumOpPositions = 4;
64 static constexpr Range kRanges[] = {{0, 4,}, {1, 2}};
65 static constexpr int kNumRanges = (int)std::size(kRanges);
66 static constexpr int kNumRepeats = 2;
67 static constexpr int kNumOps = kNumRepeats * kNumOpPositions * kNumRanges;
68 
fact(int n)69 static constexpr uint64_t fact(int n) {
70     assert(n > 0);
71     return n > 1 ? n * fact(n - 1) : 1;
72 }
73 
74 // How wide should our result buffer be to hold values written by the ranges of the ops.
result_width()75 static constexpr unsigned result_width() {
76     unsigned maxLength = 0;
77     for (size_t i = 0; i < kNumRanges; ++i) {
78         maxLength = maxLength > kRanges[i].fLength ? maxLength : kRanges[i].fLength;
79     }
80     return kNumOpPositions + maxLength - 1;
81 }
82 
83 // Number of possible allowable binary chainings among the kNumOps ops.
84 static constexpr int kNumCombinableValues = fact(kNumOps) / fact(kNumOps - 2);
85 using Combinable = std::array<GrOp::CombineResult, kNumCombinableValues>;
86 
87 /**
88  * The index in Combinable for the result for combining op 'b' into op 'a', i.e. the result of
89  * op[a]->combineIfPossible(op[b]).
90  */
combinable_index(int a,int b)91 int64_t combinable_index(int a, int b) {
92     SkASSERT(b != a);
93     // Each index gets kNumOps - 1 contiguous bools
94     int64_t aOffset = a * (kNumOps - 1);
95     // Within a's range we have one value each other op, but not one for a itself.
96     int64_t bIdxInA = b < a ? b : b - 1;
97     return aOffset + bIdxInA;
98 }
99 
100 /**
101  * Creates a legal set of combinability results for the ops. The likelihood that any two ops
102  * in a group can merge is randomly chosen.
103  */
init_combinable(int numGroups,Combinable * combinable,SkRandom * random)104 static void init_combinable(int numGroups, Combinable* combinable, SkRandom* random) {
105     SkScalar mergeProbability = random->nextUScalar1();
106     std::fill_n(combinable->begin(), kNumCombinableValues, GrOp::CombineResult::kCannotCombine);
107     SkTDArray<int> groups[kNumOps];
108     for (int i = 0; i < kNumOps; ++i) {
109         auto& group = groups[random->nextULessThan(numGroups)];
110         for (int g = 0; g < group.size(); ++g) {
111             int j = group[g];
112             if (random->nextUScalar1() < mergeProbability) {
113                 (*combinable)[combinable_index(i, j)] = GrOp::CombineResult::kMerged;
114             } else {
115                 (*combinable)[combinable_index(i, j)] = GrOp::CombineResult::kMayChain;
116             }
117             if (random->nextUScalar1() < mergeProbability) {
118                 (*combinable)[combinable_index(j, i)] = GrOp::CombineResult::kMerged;
119             } else {
120                 (*combinable)[combinable_index(j, i)] = GrOp::CombineResult::kMayChain;
121             }
122         }
123         group.push_back(i);
124     }
125 }
126 
127 /**
128  * A simple test op. It has an integer position, p. When it executes it writes p into an array
129  * of ints at index p and p+1. It takes a bitfield that indicates allowed pair-wise chainings.
130  */
131 class TestOp : public GrOp {
132 public:
133     DEFINE_OP_CLASS_ID
134 
Make(GrRecordingContext * context,int value,const Range & range,int result[],const Combinable * combinable)135     static GrOp::Owner Make(GrRecordingContext* context, int value, const Range& range,
136                             int result[], const Combinable* combinable) {
137         return GrOp::Make<TestOp>(context, value, range, result, combinable);
138     }
139 
name() const140     const char* name() const override { return "TestOp"; }
141 
writeResult(int result[]) const142     void writeResult(int result[]) const {
143         for (const auto& op : ChainRange<TestOp>(this)) {
144             for (const auto& vr : op.fValueRanges) {
145                 for (unsigned i = 0; i < vr.fRange.fLength; ++i) {
146                     result[vr.fRange.fOffset + i] = vr.fValue;
147                 }
148             }
149         }
150     }
151 
152 private:
153     friend class ::GrOp;  // for ctor
154 
TestOp(int value,const Range & range,int result[],const Combinable * combinable)155     TestOp(int value, const Range& range, int result[], const Combinable* combinable)
156             : INHERITED(ClassID()), fResult(result), fCombinable(combinable) {
157         fValueRanges.push_back({value, range});
158         this->setBounds(SkRect::MakeXYWH(range.fOffset, 0, range.fOffset + range.fLength, 1),
159                         HasAABloat::kNo, IsHairline::kNo);
160     }
161 
onPrePrepare(GrRecordingContext *,const GrSurfaceProxyView & writeView,GrAppliedClip *,const GrDstProxyView &,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)162     void onPrePrepare(GrRecordingContext*,
163                       const GrSurfaceProxyView& writeView,
164                       GrAppliedClip*,
165                       const GrDstProxyView&,
166                       GrXferBarrierFlags renderPassXferBarriers,
167                       GrLoadOp colorLoadOp) override {}
168 
onPrepare(GrOpFlushState *)169     void onPrepare(GrOpFlushState*) override {}
170 
onExecute(GrOpFlushState *,const SkRect & chainBounds)171     void onExecute(GrOpFlushState*, const SkRect& chainBounds) override {
172         for (auto& op : ChainRange<TestOp>(this)) {
173             op.writeResult(fResult);
174         }
175     }
176 
onCombineIfPossible(GrOp * t,SkArenaAlloc * arenas,const GrCaps &)177     CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc* arenas, const GrCaps&) override {
178         // This op doesn't use the arenas, but make sure the OpsTask is sending it
179         SkASSERT(arenas);
180         (void) arenas;
181         auto that = t->cast<TestOp>();
182         int v0 = fValueRanges[0].fValue;
183         int v1 = that->fValueRanges[0].fValue;
184         auto result = (*fCombinable)[combinable_index(v0, v1)];
185         if (result == GrOp::CombineResult::kMerged) {
186             std::move(that->fValueRanges.begin(), that->fValueRanges.end(),
187                       std::back_inserter(fValueRanges));
188         }
189         return result;
190     }
191 
192     struct ValueRange {
193         int fValue;
194         Range fRange;
195     };
196     std::vector<ValueRange> fValueRanges;
197     int* fResult;
198     const Combinable* fCombinable;
199 
200     using INHERITED = GrOp;
201 };
202 }  // namespace
203 
204 /**
205  * Tests adding kNumOps to an op list with all possible allowed chaining configurations. Tests
206  * adding the ops in all possible orders and verifies that the chained executions don't violate
207  * painter's order.
208  */
209 DEF_GANESH_TEST(OpChainTest, reporter, /*ctxInfo*/, CtsEnforcement::kApiLevel_T) {
210     sk_sp<GrDirectContext> dContext = GrDirectContext::MakeMock(nullptr);
211     SkASSERT(dContext);
212     const GrCaps* caps = dContext->priv().caps();
213     static constexpr SkISize kDims = {kNumOps + 1, 1};
214 
215     const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
216                                                                  GrRenderable::kYes);
217 
218     static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
219     auto proxy = dContext->priv().proxyProvider()->createProxy(format,
220                                                                kDims,
221                                                                GrRenderable::kYes,
222                                                                1,
223                                                                skgpu::Mipmapped::kNo,
224                                                                SkBackingFit::kExact,
225                                                                skgpu::Budgeted::kNo,
226                                                                GrProtected::kNo,
227                                                                /*label=*/"OpChainTest",
228                                                                GrInternalSurfaceFlags::kNone);
229     SkASSERT(proxy);
230     proxy->instantiate(dContext->priv().resourceProvider());
231 
232     skgpu::Swizzle writeSwizzle = caps->getWriteSwizzle(format, GrColorType::kRGBA_8888);
233 
234     int result[result_width()];
235     int validResult[result_width()];
236 
237     int permutation[kNumOps];
238     for (int i = 0; i < kNumOps; ++i) {
239         permutation[i] = i;
240     }
241     // Op order permutations.
242     static constexpr int kNumPermutations = 100;
243     // For a given number of chainability groups, this is the number of random combinability reuslts
244     // we will test.
245     static constexpr int kNumCombinabilitiesPerGrouping = 20;
246     SkRandom random;
247     bool repeat = false;
248     Combinable combinable;
249     GrDrawingManager* drawingMgr = dContext->priv().drawingManager();
250     sk_sp<GrArenas> arenas = sk_make_sp<GrArenas>();
251     for (int p = 0; p < kNumPermutations; ++p) {
252         for (int i = 0; i < kNumOps - 2 && !repeat; ++i) {
253             // The current implementation of nextULessThan() is biased. :(
254             unsigned j = i + random.nextULessThan(kNumOps - i);
255             std::swap(permutation[i], permutation[j]);
256         }
257         // g is the number of chainable groups that we partition the ops into.
258         for (int g = 1; g < kNumOps; ++g) {
259             for (int c = 0; c < kNumCombinabilitiesPerGrouping; ++c) {
260                 init_combinable(g, &combinable, &random);
261                 skgpu::TokenTracker tracker;
262                 GrOpFlushState flushState(dContext->priv().getGpu(),
263                                           dContext->priv().resourceProvider(),
264                                           &tracker);
265                 skgpu::ganesh::OpsTask opsTask(drawingMgr,
266                                                GrSurfaceProxyView(proxy, kOrigin, writeSwizzle),
267                                                dContext->priv().auditTrail(),
268                                                arenas);
269                 // This assumes the particular values of kRanges.
270                 std::fill_n(result, result_width(), -1);
271                 std::fill_n(validResult, result_width(), -1);
272                 for (int i = 0; i < kNumOps; ++i) {
273                     int value = permutation[i];
274                     // factor out the repeats and then use the canonical starting position and range
275                     // to determine an actual range.
276                     int j = value % (kNumRanges * kNumOpPositions);
277                     int pos = j % kNumOpPositions;
278                     Range range = kRanges[j / kNumOpPositions];
279                     range.fOffset += pos;
280                     auto op = TestOp::Make(dContext.get(), value, range, result, &combinable);
281                     TestOp* testOp = (TestOp*)op.get();
282                     testOp->writeResult(validResult);
283                     opsTask.addOp(drawingMgr, std::move(op),
284                                   GrTextureResolveManager(dContext->priv().drawingManager()),
285                                   *caps);
286                 }
287                 opsTask.makeClosed(dContext.get());
288                 opsTask.prepare(&flushState);
289                 opsTask.execute(&flushState);
290                 opsTask.endFlush(drawingMgr);
291                 opsTask.disown(drawingMgr);
292 #if 0  // Useful to repeat a random configuration that fails the test while debugger attached.
293                 if (!std::equal(result, result + result_width(), validResult)) {
294                     repeat = true;
295                 }
296 #endif
297                 (void)repeat;
298                 REPORTER_ASSERT(reporter, std::equal(result, result + result_width(), validResult));
299             }
300         }
301     }
302 }
303