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 | |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 8 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrProxyProvider.h" |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/ops/GrOp.h" |
| 16 | #include "tests/Test.h" |
Mike Klein | 0d6f815 | 2020-08-06 07:58:25 -0500 | [diff] [blame] | 17 | #include <iterator> |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 18 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 19 | // We create Ops that write a value into a range of a buffer. We create ranges from |
| 20 | // kNumOpPositions starting positions x kRanges canonical ranges. We repeat each range kNumRepeats |
| 21 | // times (with a different value written by each of the repeats). |
| 22 | namespace { |
| 23 | struct Range { |
| 24 | unsigned fOffset; |
| 25 | unsigned fLength; |
| 26 | }; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 27 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 28 | static constexpr int kNumOpPositions = 4; |
| 29 | static constexpr Range kRanges[] = {{0, 4,}, {1, 2}}; |
| 30 | static constexpr int kNumRanges = (int)SK_ARRAY_COUNT(kRanges); |
| 31 | static constexpr int kNumRepeats = 2; |
| 32 | static constexpr int kNumOps = kNumRepeats * kNumOpPositions * kNumRanges; |
| 33 | |
| 34 | static constexpr uint64_t fact(int n) { |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 35 | assert(n > 0); |
| 36 | return n > 1 ? n * fact(n - 1) : 1; |
| 37 | } |
| 38 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 39 | // How wide should our result buffer be to hold values written by the ranges of the ops. |
| 40 | static constexpr unsigned result_width() { |
| 41 | unsigned maxLength = 0; |
| 42 | for (size_t i = 0; i < kNumRanges; ++i) { |
| 43 | maxLength = maxLength > kRanges[i].fLength ? maxLength : kRanges[i].fLength; |
| 44 | } |
| 45 | return kNumOpPositions + maxLength - 1; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 48 | // Number of possible allowable binary chainings among the kNumOps ops. |
| 49 | static constexpr int kNumCombinableValues = fact(kNumOps) / fact(kNumOps - 2); |
| 50 | using Combinable = std::array<GrOp::CombineResult, kNumCombinableValues>; |
| 51 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 52 | /** |
| 53 | * The index in Combinable for the result for combining op 'b' into op 'a', i.e. the result of |
| 54 | * op[a]->combineIfPossible(op[b]). |
| 55 | */ |
| 56 | int64_t combinable_index(int a, int b) { |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 57 | SkASSERT(b != a); |
| 58 | // Each index gets kNumOps - 1 contiguous bools |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 59 | int64_t aOffset = a * (kNumOps - 1); |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 60 | // Within a's range we have one value each other op, but not one for a itself. |
| 61 | int64_t bIdxInA = b < a ? b : b - 1; |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 62 | return aOffset + bIdxInA; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Creates a legal set of combinability results for the ops. The likelihood that any two ops |
| 67 | * in a group can merge is randomly chosen. |
| 68 | */ |
| 69 | static void init_combinable(int numGroups, Combinable* combinable, SkRandom* random) { |
| 70 | SkScalar mergeProbability = random->nextUScalar1(); |
| 71 | std::fill_n(combinable->begin(), kNumCombinableValues, GrOp::CombineResult::kCannotCombine); |
| 72 | SkTDArray<int> groups[kNumOps]; |
| 73 | for (int i = 0; i < kNumOps; ++i) { |
| 74 | auto& group = groups[random->nextULessThan(numGroups)]; |
| 75 | for (int g = 0; g < group.count(); ++g) { |
| 76 | int j = group[g]; |
| 77 | if (random->nextUScalar1() < mergeProbability) { |
| 78 | (*combinable)[combinable_index(i, j)] = GrOp::CombineResult::kMerged; |
| 79 | } else { |
| 80 | (*combinable)[combinable_index(i, j)] = GrOp::CombineResult::kMayChain; |
| 81 | } |
| 82 | if (random->nextUScalar1() < mergeProbability) { |
| 83 | (*combinable)[combinable_index(j, i)] = GrOp::CombineResult::kMerged; |
| 84 | } else { |
| 85 | (*combinable)[combinable_index(j, i)] = GrOp::CombineResult::kMayChain; |
| 86 | } |
| 87 | } |
| 88 | group.push_back(i); |
| 89 | } |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 92 | /** |
| 93 | * A simple test op. It has an integer position, p. When it executes it writes p into an array |
| 94 | * of ints at index p and p+1. It takes a bitfield that indicates allowed pair-wise chainings. |
| 95 | */ |
| 96 | class TestOp : public GrOp { |
| 97 | public: |
| 98 | DEFINE_OP_CLASS_ID |
| 99 | |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 100 | static std::unique_ptr<TestOp> Make(GrRecordingContext* context, int value, const Range& range, |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 101 | int result[], const Combinable* combinable) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 102 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 103 | return pool->allocate<TestOp>(value, range, result, combinable); |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | const char* name() const override { return "TestOp"; } |
| 107 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 108 | void writeResult(int result[]) const { |
| 109 | for (const auto& op : ChainRange<TestOp>(this)) { |
| 110 | for (const auto& vr : op.fValueRanges) { |
| 111 | for (unsigned i = 0; i < vr.fRange.fLength; ++i) { |
| 112 | result[vr.fRange.fOffset + i] = vr.fValue; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | friend class ::GrOpMemoryPool; // for ctor |
| 120 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 121 | TestOp(int value, const Range& range, int result[], const Combinable* combinable) |
| 122 | : INHERITED(ClassID()), fResult(result), fCombinable(combinable) { |
| 123 | fValueRanges.push_back({value, range}); |
| 124 | this->setBounds(SkRect::MakeXYWH(range.fOffset, 0, range.fOffset + range.fLength, 1), |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 125 | HasAABloat::kNo, IsHairline::kNo); |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Robert Phillips | c655c3a | 2020-03-18 13:23:45 -0400 | [diff] [blame] | 128 | void onPrePrepare(GrRecordingContext*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 129 | const GrSurfaceProxyView* writeView, |
Robert Phillips | c655c3a | 2020-03-18 13:23:45 -0400 | [diff] [blame] | 130 | GrAppliedClip*, |
| 131 | const GrXferProcessor::DstProxyView&) override {} |
| 132 | |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 133 | void onPrepare(GrOpFlushState*) override {} |
| 134 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 135 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override { |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 136 | for (auto& op : ChainRange<TestOp>(this)) { |
| 137 | op.writeResult(fResult); |
| 138 | } |
| 139 | } |
| 140 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 141 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas* arenas, |
| 142 | const GrCaps&) override { |
| 143 | // This op doesn't use the arenas, but make sure the GrOpsTask is sending it |
| 144 | SkASSERT(arenas); |
| 145 | (void) arenas; |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 146 | auto that = t->cast<TestOp>(); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 147 | int v0 = fValueRanges[0].fValue; |
| 148 | int v1 = that->fValueRanges[0].fValue; |
| 149 | auto result = (*fCombinable)[combinable_index(v0, v1)]; |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 150 | if (result == GrOp::CombineResult::kMerged) { |
| 151 | std::move(that->fValueRanges.begin(), that->fValueRanges.end(), |
| 152 | std::back_inserter(fValueRanges)); |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 153 | } |
| 154 | return result; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 157 | struct ValueRange { |
| 158 | int fValue; |
| 159 | Range fRange; |
| 160 | }; |
| 161 | std::vector<ValueRange> fValueRanges; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 162 | int* fResult; |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 163 | const Combinable* fCombinable; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 164 | |
| 165 | typedef GrOp INHERITED; |
| 166 | }; |
| 167 | } // namespace |
| 168 | |
| 169 | /** |
| 170 | * Tests adding kNumOps to an op list with all possible allowed chaining configurations. Tests |
| 171 | * adding the ops in all possible orders and verifies that the chained executions don't violate |
| 172 | * painter's order. |
| 173 | */ |
| 174 | DEF_GPUTEST(OpChainTest, reporter, /*ctxInfo*/) { |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 175 | sk_sp<GrDirectContext> dContext = GrDirectContext::MakeMock(nullptr); |
| 176 | SkASSERT(dContext); |
| 177 | const GrCaps* caps = dContext->priv().caps(); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 178 | static constexpr SkISize kDims = {kNumOps + 1, 1}; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 179 | |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 180 | const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 181 | GrRenderable::kYes); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 182 | |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 183 | static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin; |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 184 | auto proxy = dContext->priv().proxyProvider()->createProxy( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 185 | format, kDims, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBackingFit::kExact, |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 186 | SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone); |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 187 | SkASSERT(proxy); |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 188 | proxy->instantiate(dContext->priv().resourceProvider()); |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 189 | |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 190 | GrSwizzle writeSwizzle = caps->getWriteSwizzle(format, GrColorType::kRGBA_8888); |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 191 | |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 192 | int result[result_width()]; |
| 193 | int validResult[result_width()]; |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 194 | |
| 195 | int permutation[kNumOps]; |
| 196 | for (int i = 0; i < kNumOps; ++i) { |
| 197 | permutation[i] = i; |
| 198 | } |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 199 | // Op order permutations. |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 200 | static constexpr int kNumPermutations = 100; |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 201 | // For a given number of chainability groups, this is the number of random combinability reuslts |
| 202 | // we will test. |
| 203 | static constexpr int kNumCombinabilitiesPerGrouping = 20; |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 204 | SkRandom random; |
| 205 | bool repeat = false; |
| 206 | Combinable combinable; |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 207 | GrDrawingManager* drawingMgr = dContext->priv().drawingManager(); |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 208 | for (int p = 0; p < kNumPermutations; ++p) { |
| 209 | for (int i = 0; i < kNumOps - 2 && !repeat; ++i) { |
| 210 | // The current implementation of nextULessThan() is biased. :( |
| 211 | unsigned j = i + random.nextULessThan(kNumOps - i); |
| 212 | std::swap(permutation[i], permutation[j]); |
| 213 | } |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 214 | // g is the number of chainable groups that we partition the ops into. |
| 215 | for (int g = 1; g < kNumOps; ++g) { |
| 216 | for (int c = 0; c < kNumCombinabilitiesPerGrouping; ++c) { |
| 217 | init_combinable(g, &combinable, &random); |
| 218 | GrTokenTracker tracker; |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 219 | GrOpFlushState flushState(dContext->priv().getGpu(), |
| 220 | dContext->priv().resourceProvider(), |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 221 | &tracker); |
Adlai Holler | d71b7b0 | 2020-06-08 15:55:00 -0400 | [diff] [blame] | 222 | GrOpsTask opsTask(drawingMgr, |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 223 | dContext->priv().arenas(), |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 224 | GrSurfaceProxyView(proxy, kOrigin, writeSwizzle), |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 225 | dContext->priv().auditTrail()); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 226 | // This assumes the particular values of kRanges. |
| 227 | std::fill_n(result, result_width(), -1); |
| 228 | std::fill_n(validResult, result_width(), -1); |
| 229 | for (int i = 0; i < kNumOps; ++i) { |
| 230 | int value = permutation[i]; |
| 231 | // factor out the repeats and then use the canonical starting position and range |
| 232 | // to determine an actual range. |
| 233 | int j = value % (kNumRanges * kNumOpPositions); |
| 234 | int pos = j % kNumOpPositions; |
| 235 | Range range = kRanges[j / kNumOpPositions]; |
| 236 | range.fOffset += pos; |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 237 | auto op = TestOp::Make(dContext.get(), value, range, result, &combinable); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 238 | op->writeResult(validResult); |
Adlai Holler | d71b7b0 | 2020-06-08 15:55:00 -0400 | [diff] [blame] | 239 | opsTask.addOp(drawingMgr, std::move(op), |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 240 | GrTextureResolveManager(dContext->priv().drawingManager()), |
| 241 | *caps); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 242 | } |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 243 | opsTask.makeClosed(*caps); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 244 | opsTask.prepare(&flushState); |
| 245 | opsTask.execute(&flushState); |
Adlai Holler | d71b7b0 | 2020-06-08 15:55:00 -0400 | [diff] [blame] | 246 | opsTask.endFlush(drawingMgr); |
| 247 | opsTask.disown(drawingMgr); |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 248 | #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] | 249 | if (!std::equal(result, result + result_width(), validResult)) { |
| 250 | repeat = true; |
| 251 | } |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 252 | #endif |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 253 | (void)repeat; |
| 254 | REPORTER_ASSERT(reporter, std::equal(result, result + result_width(), validResult)); |
| 255 | } |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 256 | } |
Brian Salomon | a7682c8 | 2018-10-24 10:04:37 -0400 | [diff] [blame] | 257 | } |
Brian Salomon | 764e546 | 2018-08-21 12:07:00 -0400 | [diff] [blame] | 258 | } |