Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/gpu/GrContext.h" |
| 9 | #include "src/gpu/GrContextPriv.h" |
| 10 | #include "src/gpu/GrMemoryPool.h" |
| 11 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrOpsTask.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/ops/GrOp.h" |
| 14 | #include "tests/Test.h" |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 15 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 16 | // 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). |
| 19 | namespace { |
| 20 | struct Range { |
| 21 | unsigned fOffset; |
| 22 | unsigned fLength; |
| 23 | }; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 24 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 25 | static constexpr int kNumOpPositions = 4; |
| 26 | static constexpr Range kRanges[] = {{0, 4,}, {1, 2}}; |
| 27 | static constexpr int kNumRanges = (int)SK_ARRAY_COUNT(kRanges); |
| 28 | static constexpr int kNumRepeats = 2; |
| 29 | static constexpr int kNumOps = kNumRepeats * kNumOpPositions * kNumRanges; |
| 30 | |
| 31 | static constexpr uint64_t fact(int n) { |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 32 | assert(n > 0); |
| 33 | return n > 1 ? n * fact(n - 1) : 1; |
| 34 | } |
| 35 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 36 | // How wide should our result buffer be to hold values written by the ranges of the ops. |
| 37 | static 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 Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 45 | // Number of possible allowable binary chainings among the kNumOps ops. |
| 46 | static constexpr int kNumCombinableValues = fact(kNumOps) / fact(kNumOps - 2); |
| 47 | using Combinable = std::array<GrOp::CombineResult, kNumCombinableValues>; |
| 48 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 49 | /** |
| 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 | */ |
| 53 | int64_t combinable_index(int a, int b) { |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 54 | SkASSERT(b != a); |
| 55 | // Each index gets kNumOps - 1 contiguous bools |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 56 | int64_t aOffset = a * (kNumOps - 1); |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 57 | // 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 Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 59 | 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 | */ |
| 66 | static 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 Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 89 | /** |
| 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 | */ |
| 93 | class TestOp : public GrOp { |
| 94 | public: |
| 95 | DEFINE_OP_CLASS_ID |
| 96 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 97 | static std::unique_ptr<TestOp> Make(GrContext* context, int value, const Range& range, |
| 98 | int result[], const Combinable* combinable) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 99 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 100 | return pool->allocate<TestOp>(value, range, result, combinable); |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | const char* name() const override { return "TestOp"; } |
| 104 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 105 | 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 Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | friend class ::GrOpMemoryPool; // for ctor |
| 117 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 118 | 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 Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 122 | HasAABloat::kNo, IsHairline::kNo); |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void onPrepare(GrOpFlushState*) override {} |
| 126 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 127 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override { |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 128 | for (auto& op : ChainRange<TestOp>(this)) { |
| 129 | op.writeResult(fResult); |
| 130 | } |
| 131 | } |
| 132 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 133 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas* arenas, |
| 134 | const GrCaps&) override { |
| 135 | // This op doesn't use the arenas, but make sure the GrOpsTask is sending it |
| 136 | SkASSERT(arenas); |
| 137 | (void) arenas; |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 138 | auto that = t->cast<TestOp>(); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 139 | int v0 = fValueRanges[0].fValue; |
| 140 | int v1 = that->fValueRanges[0].fValue; |
| 141 | auto result = (*fCombinable)[combinable_index(v0, v1)]; |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 142 | if (result == GrOp::CombineResult::kMerged) { |
| 143 | std::move(that->fValueRanges.begin(), that->fValueRanges.end(), |
| 144 | std::back_inserter(fValueRanges)); |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 145 | } |
| 146 | return result; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 149 | struct ValueRange { |
| 150 | int fValue; |
| 151 | Range fRange; |
| 152 | }; |
| 153 | std::vector<ValueRange> fValueRanges; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 154 | int* fResult; |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 155 | const Combinable* fCombinable; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 156 | |
| 157 | typedef GrOp INHERITED; |
| 158 | }; |
| 159 | } // namespace |
| 160 | |
| 161 | /** |
| 162 | * Tests adding kNumOps to an op list with all possible allowed chaining configurations. Tests |
| 163 | * adding the ops in all possible orders and verifies that the chained executions don't violate |
| 164 | * painter's order. |
| 165 | */ |
| 166 | DEF_GPUTEST(OpChainTest, reporter, /*ctxInfo*/) { |
| 167 | auto context = GrContext::MakeMock(nullptr); |
| 168 | SkASSERT(context); |
| 169 | GrSurfaceDesc desc; |
| 170 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 171 | desc.fWidth = kNumOps + 1; |
| 172 | desc.fHeight = 1; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 173 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 174 | const GrBackendFormat format = |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 175 | context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 176 | GrRenderable::kYes); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 177 | |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 178 | static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 179 | auto proxy = context->priv().proxyProvider()->createProxy( |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 180 | format, desc, GrRenderable::kYes, 1, kOrigin, GrMipMapped::kNo, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 181 | SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone); |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 182 | SkASSERT(proxy); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 183 | proxy->instantiate(context->priv().resourceProvider()); |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 184 | |
| 185 | GrSwizzle outSwizzle = context->priv().caps()->getOutputSwizzle(format, |
| 186 | GrColorType::kRGBA_8888); |
| 187 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 188 | int result[result_width()]; |
| 189 | int validResult[result_width()]; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 190 | |
| 191 | int permutation[kNumOps]; |
| 192 | for (int i = 0; i < kNumOps; ++i) { |
| 193 | permutation[i] = i; |
| 194 | } |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 195 | // Op order permutations. |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 196 | static constexpr int kNumPermutations = 100; |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 197 | // For a given number of chainability groups, this is the number of random combinability reuslts |
| 198 | // we will test. |
| 199 | static constexpr int kNumCombinabilitiesPerGrouping = 20; |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 200 | SkRandom random; |
| 201 | bool repeat = false; |
| 202 | Combinable combinable; |
| 203 | for (int p = 0; p < kNumPermutations; ++p) { |
| 204 | for (int i = 0; i < kNumOps - 2 && !repeat; ++i) { |
| 205 | // The current implementation of nextULessThan() is biased. :( |
| 206 | unsigned j = i + random.nextULessThan(kNumOps - i); |
| 207 | std::swap(permutation[i], permutation[j]); |
| 208 | } |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 209 | // g is the number of chainable groups that we partition the ops into. |
| 210 | for (int g = 1; g < kNumOps; ++g) { |
| 211 | for (int c = 0; c < kNumCombinabilitiesPerGrouping; ++c) { |
| 212 | init_combinable(g, &combinable, &random); |
| 213 | GrTokenTracker tracker; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 214 | GrOpFlushState flushState(context->priv().getGpu(), |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 215 | context->priv().resourceProvider(), |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 216 | &tracker); |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 217 | GrOpsTask opsTask(context->priv().arenas(), |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 218 | GrSurfaceProxyView(proxy, kOrigin, outSwizzle), |
Robert Phillips | 61fc799 | 2019-10-22 11:58:17 -0400 | [diff] [blame] | 219 | context->priv().auditTrail()); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 220 | // This assumes the particular values of kRanges. |
| 221 | std::fill_n(result, result_width(), -1); |
| 222 | std::fill_n(validResult, result_width(), -1); |
| 223 | for (int i = 0; i < kNumOps; ++i) { |
| 224 | int value = permutation[i]; |
| 225 | // factor out the repeats and then use the canonical starting position and range |
| 226 | // to determine an actual range. |
| 227 | int j = value % (kNumRanges * kNumOpPositions); |
| 228 | int pos = j % kNumOpPositions; |
| 229 | Range range = kRanges[j / kNumOpPositions]; |
| 230 | range.fOffset += pos; |
| 231 | auto op = TestOp::Make(context.get(), value, range, result, &combinable); |
| 232 | op->writeResult(validResult); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 233 | opsTask.addOp(std::move(op), |
| 234 | GrTextureResolveManager(context->priv().drawingManager()), |
| 235 | *context->priv().caps()); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 236 | } |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 237 | opsTask.makeClosed(*context->priv().caps()); |
| 238 | opsTask.prepare(&flushState); |
| 239 | opsTask.execute(&flushState); |
| 240 | opsTask.endFlush(); |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 241 | #if 0 // Useful to repeat a random configuration that fails the test while debugger attached. |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 242 | if (!std::equal(result, result + result_width(), validResult)) { |
| 243 | repeat = true; |
| 244 | } |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 245 | #endif |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 246 | (void)repeat; |
| 247 | REPORTER_ASSERT(reporter, std::equal(result, result + result_width(), validResult)); |
| 248 | } |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 249 | } |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 250 | } |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 251 | } |