blob: 8a523b16ec51f5bb0edfb17676ffc718cbb35649 [file] [log] [blame]
Brian Salomon764e5462018-08-21 12:07:00 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContext.h"
9#include "src/gpu/GrContextPriv.h"
10#include "src/gpu/GrMemoryPool.h"
11#include "src/gpu/GrOpFlushState.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040012#include "src/gpu/GrOpsTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/ops/GrOp.h"
14#include "tests/Test.h"
Brian Salomon764e5462018-08-21 12:07:00 -040015
Brian Salomona7682c82018-10-24 10:04:37 -040016// We create Ops that write a value into a range of a buffer. We create ranges from
17// kNumOpPositions starting positions x kRanges canonical ranges. We repeat each range kNumRepeats
18// times (with a different value written by each of the repeats).
19namespace {
20struct Range {
21 unsigned fOffset;
22 unsigned fLength;
23};
Brian Salomon764e5462018-08-21 12:07:00 -040024
Brian Salomona7682c82018-10-24 10:04:37 -040025static constexpr int kNumOpPositions = 4;
26static constexpr Range kRanges[] = {{0, 4,}, {1, 2}};
27static constexpr int kNumRanges = (int)SK_ARRAY_COUNT(kRanges);
28static constexpr int kNumRepeats = 2;
29static constexpr int kNumOps = kNumRepeats * kNumOpPositions * kNumRanges;
30
31static constexpr uint64_t fact(int n) {
Brian Salomon764e5462018-08-21 12:07:00 -040032 assert(n > 0);
33 return n > 1 ? n * fact(n - 1) : 1;
34}
35
Brian Salomona7682c82018-10-24 10:04:37 -040036// How wide should our result buffer be to hold values written by the ranges of the ops.
37static constexpr unsigned result_width() {
38 unsigned maxLength = 0;
39 for (size_t i = 0; i < kNumRanges; ++i) {
40 maxLength = maxLength > kRanges[i].fLength ? maxLength : kRanges[i].fLength;
41 }
42 return kNumOpPositions + maxLength - 1;
Brian Salomon764e5462018-08-21 12:07:00 -040043}
44
Brian Salomona7682c82018-10-24 10:04:37 -040045// Number of possible allowable binary chainings among the kNumOps ops.
46static constexpr int kNumCombinableValues = fact(kNumOps) / fact(kNumOps - 2);
47using Combinable = std::array<GrOp::CombineResult, kNumCombinableValues>;
48
Brian Salomon588cec72018-11-14 13:56:37 -050049/**
50 * The index in Combinable for the result for combining op 'b' into op 'a', i.e. the result of
51 * op[a]->combineIfPossible(op[b]).
52 */
53int64_t combinable_index(int a, int b) {
Brian Salomona7682c82018-10-24 10:04:37 -040054 SkASSERT(b != a);
55 // Each index gets kNumOps - 1 contiguous bools
Brian Salomon588cec72018-11-14 13:56:37 -050056 int64_t aOffset = a * (kNumOps - 1);
Brian Salomona7682c82018-10-24 10:04:37 -040057 // Within a's range we have one value each other op, but not one for a itself.
58 int64_t bIdxInA = b < a ? b : b - 1;
Brian Salomon588cec72018-11-14 13:56:37 -050059 return aOffset + bIdxInA;
60}
61
62/**
63 * Creates a legal set of combinability results for the ops. The likelihood that any two ops
64 * in a group can merge is randomly chosen.
65 */
66static void init_combinable(int numGroups, Combinable* combinable, SkRandom* random) {
67 SkScalar mergeProbability = random->nextUScalar1();
68 std::fill_n(combinable->begin(), kNumCombinableValues, GrOp::CombineResult::kCannotCombine);
69 SkTDArray<int> groups[kNumOps];
70 for (int i = 0; i < kNumOps; ++i) {
71 auto& group = groups[random->nextULessThan(numGroups)];
72 for (int g = 0; g < group.count(); ++g) {
73 int j = group[g];
74 if (random->nextUScalar1() < mergeProbability) {
75 (*combinable)[combinable_index(i, j)] = GrOp::CombineResult::kMerged;
76 } else {
77 (*combinable)[combinable_index(i, j)] = GrOp::CombineResult::kMayChain;
78 }
79 if (random->nextUScalar1() < mergeProbability) {
80 (*combinable)[combinable_index(j, i)] = GrOp::CombineResult::kMerged;
81 } else {
82 (*combinable)[combinable_index(j, i)] = GrOp::CombineResult::kMayChain;
83 }
84 }
85 group.push_back(i);
86 }
Brian Salomona7682c82018-10-24 10:04:37 -040087}
88
Brian Salomon764e5462018-08-21 12:07:00 -040089/**
90 * A simple test op. It has an integer position, p. When it executes it writes p into an array
91 * of ints at index p and p+1. It takes a bitfield that indicates allowed pair-wise chainings.
92 */
93class TestOp : public GrOp {
94public:
95 DEFINE_OP_CLASS_ID
96
Brian Salomona7682c82018-10-24 10:04:37 -040097 static std::unique_ptr<TestOp> Make(GrContext* context, int value, const Range& range,
98 int result[], const Combinable* combinable) {
Robert Phillips9da87e02019-02-04 13:26:26 -050099 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomona7682c82018-10-24 10:04:37 -0400100 return pool->allocate<TestOp>(value, range, result, combinable);
Brian Salomon764e5462018-08-21 12:07:00 -0400101 }
102
103 const char* name() const override { return "TestOp"; }
104
Brian Salomona7682c82018-10-24 10:04:37 -0400105 void writeResult(int result[]) const {
106 for (const auto& op : ChainRange<TestOp>(this)) {
107 for (const auto& vr : op.fValueRanges) {
108 for (unsigned i = 0; i < vr.fRange.fLength; ++i) {
109 result[vr.fRange.fOffset + i] = vr.fValue;
110 }
111 }
112 }
113 }
Brian Salomon764e5462018-08-21 12:07:00 -0400114
115private:
116 friend class ::GrOpMemoryPool; // for ctor
117
Brian Salomona7682c82018-10-24 10:04:37 -0400118 TestOp(int value, const Range& range, int result[], const Combinable* combinable)
119 : INHERITED(ClassID()), fResult(result), fCombinable(combinable) {
120 fValueRanges.push_back({value, range});
121 this->setBounds(SkRect::MakeXYWH(range.fOffset, 0, range.fOffset + range.fLength, 1),
Greg Daniel5faf4742019-10-01 15:14:44 -0400122 HasAABloat::kNo, IsHairline::kNo);
Brian Salomon764e5462018-08-21 12:07:00 -0400123 }
124
125 void onPrepare(GrOpFlushState*) override {}
126
Brian Salomon588cec72018-11-14 13:56:37 -0500127 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override {
Brian Salomon764e5462018-08-21 12:07:00 -0400128 for (auto& op : ChainRange<TestOp>(this)) {
129 op.writeResult(fResult);
130 }
131 }
132
Brian Salomona7682c82018-10-24 10:04:37 -0400133 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override {
134 auto that = t->cast<TestOp>();
Brian Salomon588cec72018-11-14 13:56:37 -0500135 int v0 = fValueRanges[0].fValue;
136 int v1 = that->fValueRanges[0].fValue;
137 auto result = (*fCombinable)[combinable_index(v0, v1)];
Brian Salomona7682c82018-10-24 10:04:37 -0400138 if (result == GrOp::CombineResult::kMerged) {
139 std::move(that->fValueRanges.begin(), that->fValueRanges.end(),
140 std::back_inserter(fValueRanges));
Brian Salomona7682c82018-10-24 10:04:37 -0400141 }
142 return result;
Brian Salomon764e5462018-08-21 12:07:00 -0400143 }
144
Brian Salomona7682c82018-10-24 10:04:37 -0400145 struct ValueRange {
146 int fValue;
147 Range fRange;
148 };
149 std::vector<ValueRange> fValueRanges;
Brian Salomon764e5462018-08-21 12:07:00 -0400150 int* fResult;
Brian Salomona7682c82018-10-24 10:04:37 -0400151 const Combinable* fCombinable;
Brian Salomon764e5462018-08-21 12:07:00 -0400152
153 typedef GrOp INHERITED;
154};
155} // namespace
156
157/**
158 * Tests adding kNumOps to an op list with all possible allowed chaining configurations. Tests
159 * adding the ops in all possible orders and verifies that the chained executions don't violate
160 * painter's order.
161 */
162DEF_GPUTEST(OpChainTest, reporter, /*ctxInfo*/) {
163 auto context = GrContext::MakeMock(nullptr);
164 SkASSERT(context);
165 GrSurfaceDesc desc;
166 desc.fConfig = kRGBA_8888_GrPixelConfig;
167 desc.fWidth = kNumOps + 1;
168 desc.fHeight = 1;
Brian Salomon764e5462018-08-21 12:07:00 -0400169
Greg Daniel4065d452018-11-16 15:43:41 -0500170 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400171 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
172 GrRenderable::kYes);
Greg Daniel4065d452018-11-16 15:43:41 -0500173
Robert Phillips9da87e02019-02-04 13:26:26 -0500174 auto proxy = context->priv().proxyProvider()->createProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400175 format, desc, GrRenderable::kYes, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400176 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Brian Salomon764e5462018-08-21 12:07:00 -0400177 SkASSERT(proxy);
Robert Phillips9da87e02019-02-04 13:26:26 -0500178 proxy->instantiate(context->priv().resourceProvider());
Brian Salomona7682c82018-10-24 10:04:37 -0400179 int result[result_width()];
180 int validResult[result_width()];
Brian Salomon764e5462018-08-21 12:07:00 -0400181
182 int permutation[kNumOps];
183 for (int i = 0; i < kNumOps; ++i) {
184 permutation[i] = i;
185 }
Brian Salomon588cec72018-11-14 13:56:37 -0500186 // Op order permutations.
Brian Salomona7682c82018-10-24 10:04:37 -0400187 static constexpr int kNumPermutations = 100;
Brian Salomon588cec72018-11-14 13:56:37 -0500188 // For a given number of chainability groups, this is the number of random combinability reuslts
189 // we will test.
190 static constexpr int kNumCombinabilitiesPerGrouping = 20;
Brian Salomona7682c82018-10-24 10:04:37 -0400191 SkRandom random;
192 bool repeat = false;
193 Combinable combinable;
194 for (int p = 0; p < kNumPermutations; ++p) {
195 for (int i = 0; i < kNumOps - 2 && !repeat; ++i) {
196 // The current implementation of nextULessThan() is biased. :(
197 unsigned j = i + random.nextULessThan(kNumOps - i);
198 std::swap(permutation[i], permutation[j]);
199 }
Brian Salomon588cec72018-11-14 13:56:37 -0500200 // g is the number of chainable groups that we partition the ops into.
201 for (int g = 1; g < kNumOps; ++g) {
202 for (int c = 0; c < kNumCombinabilitiesPerGrouping; ++c) {
203 init_combinable(g, &combinable, &random);
204 GrTokenTracker tracker;
Robert Phillips9da87e02019-02-04 13:26:26 -0500205 GrOpFlushState flushState(context->priv().getGpu(),
Brian Salomon2c791fc2019-04-02 11:52:03 -0400206 context->priv().resourceProvider(),
Brian Salomon2c791fc2019-04-02 11:52:03 -0400207 &tracker);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400208 GrOpsTask opsTask(sk_ref_sp(context->priv().opMemoryPool()),
Robert Phillips831a2932019-04-12 17:18:39 -0400209 sk_ref_sp(proxy->asRenderTargetProxy()),
Robert Phillipsd6841482019-02-08 10:29:20 -0500210 context->priv().auditTrail());
Brian Salomon588cec72018-11-14 13:56:37 -0500211 // This assumes the particular values of kRanges.
212 std::fill_n(result, result_width(), -1);
213 std::fill_n(validResult, result_width(), -1);
214 for (int i = 0; i < kNumOps; ++i) {
215 int value = permutation[i];
216 // factor out the repeats and then use the canonical starting position and range
217 // to determine an actual range.
218 int j = value % (kNumRanges * kNumOpPositions);
219 int pos = j % kNumOpPositions;
220 Range range = kRanges[j / kNumOpPositions];
221 range.fOffset += pos;
222 auto op = TestOp::Make(context.get(), value, range, result, &combinable);
223 op->writeResult(validResult);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400224 opsTask.addOp(std::move(op),
225 GrTextureResolveManager(context->priv().drawingManager()),
226 *context->priv().caps());
Brian Salomon588cec72018-11-14 13:56:37 -0500227 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400228 opsTask.makeClosed(*context->priv().caps());
229 opsTask.prepare(&flushState);
230 opsTask.execute(&flushState);
231 opsTask.endFlush();
Brian Salomona7682c82018-10-24 10:04:37 -0400232#if 0 // Useful to repeat a random configuration that fails the test while debugger attached.
Brian Salomon588cec72018-11-14 13:56:37 -0500233 if (!std::equal(result, result + result_width(), validResult)) {
234 repeat = true;
235 }
Brian Salomona7682c82018-10-24 10:04:37 -0400236#endif
Brian Salomon588cec72018-11-14 13:56:37 -0500237 (void)repeat;
238 REPORTER_ASSERT(reporter, std::equal(result, result + result_width(), validResult));
239 }
Brian Salomon764e5462018-08-21 12:07:00 -0400240 }
Brian Salomona7682c82018-10-24 10:04:37 -0400241 }
Brian Salomon764e5462018-08-21 12:07:00 -0400242}