2018-08-21 16:07:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2020-07-17 19:40:13 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrMemoryPool.h"
|
|
|
|
#include "src/gpu/GrOpFlushState.h"
|
2019-08-22 20:19:24 +00:00
|
|
|
#include "src/gpu/GrOpsTask.h"
|
2020-04-06 17:57:30 +00:00
|
|
|
#include "src/gpu/GrProxyProvider.h"
|
2020-07-17 19:40:13 +00:00
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/ops/GrOp.h"
|
|
|
|
#include "tests/Test.h"
|
2020-08-06 12:58:25 +00:00
|
|
|
#include <iterator>
|
2018-08-21 16:07:00 +00:00
|
|
|
|
2018-10-24 14:04:37 +00:00
|
|
|
// We create Ops that write a value into a range of a buffer. We create ranges from
|
|
|
|
// kNumOpPositions starting positions x kRanges canonical ranges. We repeat each range kNumRepeats
|
|
|
|
// times (with a different value written by each of the repeats).
|
|
|
|
namespace {
|
|
|
|
struct Range {
|
|
|
|
unsigned fOffset;
|
|
|
|
unsigned fLength;
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr int kNumOpPositions = 4;
|
|
|
|
static constexpr Range kRanges[] = {{0, 4,}, {1, 2}};
|
|
|
|
static constexpr int kNumRanges = (int)SK_ARRAY_COUNT(kRanges);
|
|
|
|
static constexpr int kNumRepeats = 2;
|
|
|
|
static constexpr int kNumOps = kNumRepeats * kNumOpPositions * kNumRanges;
|
2018-08-21 16:07:00 +00:00
|
|
|
|
2018-10-24 14:04:37 +00:00
|
|
|
static constexpr uint64_t fact(int n) {
|
2018-08-21 16:07:00 +00:00
|
|
|
assert(n > 0);
|
|
|
|
return n > 1 ? n * fact(n - 1) : 1;
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:04:37 +00:00
|
|
|
// How wide should our result buffer be to hold values written by the ranges of the ops.
|
|
|
|
static constexpr unsigned result_width() {
|
|
|
|
unsigned maxLength = 0;
|
|
|
|
for (size_t i = 0; i < kNumRanges; ++i) {
|
|
|
|
maxLength = maxLength > kRanges[i].fLength ? maxLength : kRanges[i].fLength;
|
|
|
|
}
|
|
|
|
return kNumOpPositions + maxLength - 1;
|
|
|
|
}
|
2018-08-21 16:07:00 +00:00
|
|
|
|
2018-10-24 14:04:37 +00:00
|
|
|
// Number of possible allowable binary chainings among the kNumOps ops.
|
|
|
|
static constexpr int kNumCombinableValues = fact(kNumOps) / fact(kNumOps - 2);
|
|
|
|
using Combinable = std::array<GrOp::CombineResult, kNumCombinableValues>;
|
2018-08-21 16:07:00 +00:00
|
|
|
|
2018-11-14 18:56:37 +00:00
|
|
|
/**
|
|
|
|
* The index in Combinable for the result for combining op 'b' into op 'a', i.e. the result of
|
|
|
|
* op[a]->combineIfPossible(op[b]).
|
|
|
|
*/
|
|
|
|
int64_t combinable_index(int a, int b) {
|
2018-08-21 16:07:00 +00:00
|
|
|
SkASSERT(b != a);
|
2018-10-24 14:04:37 +00:00
|
|
|
// Each index gets kNumOps - 1 contiguous bools
|
2018-11-14 18:56:37 +00:00
|
|
|
int64_t aOffset = a * (kNumOps - 1);
|
2018-10-24 14:04:37 +00:00
|
|
|
// Within a's range we have one value each other op, but not one for a itself.
|
|
|
|
int64_t bIdxInA = b < a ? b : b - 1;
|
2018-11-14 18:56:37 +00:00
|
|
|
return aOffset + bIdxInA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a legal set of combinability results for the ops. The likelihood that any two ops
|
|
|
|
* in a group can merge is randomly chosen.
|
|
|
|
*/
|
|
|
|
static void init_combinable(int numGroups, Combinable* combinable, SkRandom* random) {
|
|
|
|
SkScalar mergeProbability = random->nextUScalar1();
|
|
|
|
std::fill_n(combinable->begin(), kNumCombinableValues, GrOp::CombineResult::kCannotCombine);
|
|
|
|
SkTDArray<int> groups[kNumOps];
|
|
|
|
for (int i = 0; i < kNumOps; ++i) {
|
|
|
|
auto& group = groups[random->nextULessThan(numGroups)];
|
|
|
|
for (int g = 0; g < group.count(); ++g) {
|
|
|
|
int j = group[g];
|
|
|
|
if (random->nextUScalar1() < mergeProbability) {
|
|
|
|
(*combinable)[combinable_index(i, j)] = GrOp::CombineResult::kMerged;
|
|
|
|
} else {
|
|
|
|
(*combinable)[combinable_index(i, j)] = GrOp::CombineResult::kMayChain;
|
|
|
|
}
|
|
|
|
if (random->nextUScalar1() < mergeProbability) {
|
|
|
|
(*combinable)[combinable_index(j, i)] = GrOp::CombineResult::kMerged;
|
|
|
|
} else {
|
|
|
|
(*combinable)[combinable_index(j, i)] = GrOp::CombineResult::kMayChain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
group.push_back(i);
|
|
|
|
}
|
2018-08-21 16:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple test op. It has an integer position, p. When it executes it writes p into an array
|
|
|
|
* of ints at index p and p+1. It takes a bitfield that indicates allowed pair-wise chainings.
|
|
|
|
*/
|
|
|
|
class TestOp : public GrOp {
|
|
|
|
public:
|
|
|
|
DEFINE_OP_CLASS_ID
|
|
|
|
|
2020-07-17 19:40:13 +00:00
|
|
|
static std::unique_ptr<TestOp> Make(GrRecordingContext* context, int value, const Range& range,
|
2018-10-24 14:04:37 +00:00
|
|
|
int result[], const Combinable* combinable) {
|
2019-02-04 18:26:26 +00:00
|
|
|
GrOpMemoryPool* pool = context->priv().opMemoryPool();
|
2018-10-24 14:04:37 +00:00
|
|
|
return pool->allocate<TestOp>(value, range, result, combinable);
|
2018-08-21 16:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "TestOp"; }
|
|
|
|
|
2018-10-24 14:04:37 +00:00
|
|
|
void writeResult(int result[]) const {
|
|
|
|
for (const auto& op : ChainRange<TestOp>(this)) {
|
|
|
|
for (const auto& vr : op.fValueRanges) {
|
|
|
|
for (unsigned i = 0; i < vr.fRange.fLength; ++i) {
|
|
|
|
result[vr.fRange.fOffset + i] = vr.fValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 16:07:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class ::GrOpMemoryPool; // for ctor
|
|
|
|
|
2018-10-24 14:04:37 +00:00
|
|
|
TestOp(int value, const Range& range, int result[], const Combinable* combinable)
|
|
|
|
: INHERITED(ClassID()), fResult(result), fCombinable(combinable) {
|
|
|
|
fValueRanges.push_back({value, range});
|
|
|
|
this->setBounds(SkRect::MakeXYWH(range.fOffset, 0, range.fOffset + range.fLength, 1),
|
2019-10-01 19:14:44 +00:00
|
|
|
HasAABloat::kNo, IsHairline::kNo);
|
2018-08-21 16:07:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 17:23:45 +00:00
|
|
|
void onPrePrepare(GrRecordingContext*,
|
2020-04-01 20:22:00 +00:00
|
|
|
const GrSurfaceProxyView* writeView,
|
2020-03-18 17:23:45 +00:00
|
|
|
GrAppliedClip*,
|
2020-09-11 13:33:54 +00:00
|
|
|
const GrXferProcessor::DstProxyView&,
|
|
|
|
GrXferBarrierFlags renderPassXferBarriers) override {}
|
2020-03-18 17:23:45 +00:00
|
|
|
|
2018-08-21 16:07:00 +00:00
|
|
|
void onPrepare(GrOpFlushState*) override {}
|
|
|
|
|
2018-11-14 18:56:37 +00:00
|
|
|
void onExecute(GrOpFlushState*, const SkRect& chainBounds) override {
|
2018-08-21 16:07:00 +00:00
|
|
|
for (auto& op : ChainRange<TestOp>(this)) {
|
|
|
|
op.writeResult(fResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 19:51:00 +00:00
|
|
|
CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas* arenas,
|
|
|
|
const GrCaps&) override {
|
|
|
|
// This op doesn't use the arenas, but make sure the GrOpsTask is sending it
|
|
|
|
SkASSERT(arenas);
|
|
|
|
(void) arenas;
|
2018-10-24 14:04:37 +00:00
|
|
|
auto that = t->cast<TestOp>();
|
2018-11-14 18:56:37 +00:00
|
|
|
int v0 = fValueRanges[0].fValue;
|
|
|
|
int v1 = that->fValueRanges[0].fValue;
|
|
|
|
auto result = (*fCombinable)[combinable_index(v0, v1)];
|
2018-10-24 14:04:37 +00:00
|
|
|
if (result == GrOp::CombineResult::kMerged) {
|
|
|
|
std::move(that->fValueRanges.begin(), that->fValueRanges.end(),
|
|
|
|
std::back_inserter(fValueRanges));
|
|
|
|
}
|
|
|
|
return result;
|
2018-08-21 16:07:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:04:37 +00:00
|
|
|
struct ValueRange {
|
|
|
|
int fValue;
|
|
|
|
Range fRange;
|
|
|
|
};
|
|
|
|
std::vector<ValueRange> fValueRanges;
|
2018-08-21 16:07:00 +00:00
|
|
|
int* fResult;
|
2018-10-24 14:04:37 +00:00
|
|
|
const Combinable* fCombinable;
|
2018-08-21 16:07:00 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GrOp;
|
2018-08-21 16:07:00 +00:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests adding kNumOps to an op list with all possible allowed chaining configurations. Tests
|
|
|
|
* adding the ops in all possible orders and verifies that the chained executions don't violate
|
|
|
|
* painter's order.
|
|
|
|
*/
|
|
|
|
DEF_GPUTEST(OpChainTest, reporter, /*ctxInfo*/) {
|
2020-07-17 19:40:13 +00:00
|
|
|
sk_sp<GrDirectContext> dContext = GrDirectContext::MakeMock(nullptr);
|
|
|
|
SkASSERT(dContext);
|
|
|
|
const GrCaps* caps = dContext->priv().caps();
|
2020-02-07 19:17:25 +00:00
|
|
|
static constexpr SkISize kDims = {kNumOps + 1, 1};
|
2018-08-21 16:07:00 +00:00
|
|
|
|
2020-07-17 19:40:13 +00:00
|
|
|
const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
|
|
|
|
GrRenderable::kYes);
|
2018-11-16 20:43:41 +00:00
|
|
|
|
2019-10-29 15:26:01 +00:00
|
|
|
static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
|
2020-07-17 19:40:13 +00:00
|
|
|
auto proxy = dContext->priv().proxyProvider()->createProxy(
|
2020-07-21 13:27:25 +00:00
|
|
|
format, kDims, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBackingFit::kExact,
|
2020-02-14 15:47:18 +00:00
|
|
|
SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone);
|
2018-08-21 16:07:00 +00:00
|
|
|
SkASSERT(proxy);
|
2020-07-17 19:40:13 +00:00
|
|
|
proxy->instantiate(dContext->priv().resourceProvider());
|
2019-10-29 15:26:01 +00:00
|
|
|
|
2020-07-17 19:40:13 +00:00
|
|
|
GrSwizzle writeSwizzle = caps->getWriteSwizzle(format, GrColorType::kRGBA_8888);
|
2019-10-29 15:26:01 +00:00
|
|
|
|
2018-10-24 14:04:37 +00:00
|
|
|
int result[result_width()];
|
|
|
|
int validResult[result_width()];
|
2018-08-21 16:07:00 +00:00
|
|
|
|
|
|
|
int permutation[kNumOps];
|
|
|
|
for (int i = 0; i < kNumOps; ++i) {
|
|
|
|
permutation[i] = i;
|
|
|
|
}
|
2018-11-14 18:56:37 +00:00
|
|
|
// Op order permutations.
|
2018-10-24 14:04:37 +00:00
|
|
|
static constexpr int kNumPermutations = 100;
|
2018-11-14 18:56:37 +00:00
|
|
|
// For a given number of chainability groups, this is the number of random combinability reuslts
|
|
|
|
// we will test.
|
|
|
|
static constexpr int kNumCombinabilitiesPerGrouping = 20;
|
2018-10-24 14:04:37 +00:00
|
|
|
SkRandom random;
|
|
|
|
bool repeat = false;
|
|
|
|
Combinable combinable;
|
2020-07-17 19:40:13 +00:00
|
|
|
GrDrawingManager* drawingMgr = dContext->priv().drawingManager();
|
2018-10-24 14:04:37 +00:00
|
|
|
for (int p = 0; p < kNumPermutations; ++p) {
|
|
|
|
for (int i = 0; i < kNumOps - 2 && !repeat; ++i) {
|
|
|
|
// The current implementation of nextULessThan() is biased. :(
|
|
|
|
unsigned j = i + random.nextULessThan(kNumOps - i);
|
|
|
|
std::swap(permutation[i], permutation[j]);
|
|
|
|
}
|
2018-11-14 18:56:37 +00:00
|
|
|
// g is the number of chainable groups that we partition the ops into.
|
|
|
|
for (int g = 1; g < kNumOps; ++g) {
|
|
|
|
for (int c = 0; c < kNumCombinabilitiesPerGrouping; ++c) {
|
|
|
|
init_combinable(g, &combinable, &random);
|
|
|
|
GrTokenTracker tracker;
|
2020-07-17 19:40:13 +00:00
|
|
|
GrOpFlushState flushState(dContext->priv().getGpu(),
|
|
|
|
dContext->priv().resourceProvider(),
|
2019-04-02 15:52:03 +00:00
|
|
|
&tracker);
|
2020-06-08 19:55:00 +00:00
|
|
|
GrOpsTask opsTask(drawingMgr,
|
2020-07-17 19:40:13 +00:00
|
|
|
dContext->priv().arenas(),
|
2020-04-01 20:22:00 +00:00
|
|
|
GrSurfaceProxyView(proxy, kOrigin, writeSwizzle),
|
2020-07-17 19:40:13 +00:00
|
|
|
dContext->priv().auditTrail());
|
2018-11-14 18:56:37 +00:00
|
|
|
// This assumes the particular values of kRanges.
|
|
|
|
std::fill_n(result, result_width(), -1);
|
|
|
|
std::fill_n(validResult, result_width(), -1);
|
|
|
|
for (int i = 0; i < kNumOps; ++i) {
|
|
|
|
int value = permutation[i];
|
|
|
|
// factor out the repeats and then use the canonical starting position and range
|
|
|
|
// to determine an actual range.
|
|
|
|
int j = value % (kNumRanges * kNumOpPositions);
|
|
|
|
int pos = j % kNumOpPositions;
|
|
|
|
Range range = kRanges[j / kNumOpPositions];
|
|
|
|
range.fOffset += pos;
|
2020-07-17 19:40:13 +00:00
|
|
|
auto op = TestOp::Make(dContext.get(), value, range, result, &combinable);
|
2018-11-14 18:56:37 +00:00
|
|
|
op->writeResult(validResult);
|
2020-06-08 19:55:00 +00:00
|
|
|
opsTask.addOp(drawingMgr, std::move(op),
|
2020-07-17 19:40:13 +00:00
|
|
|
GrTextureResolveManager(dContext->priv().drawingManager()),
|
|
|
|
*caps);
|
2018-11-14 18:56:37 +00:00
|
|
|
}
|
2020-07-17 19:40:13 +00:00
|
|
|
opsTask.makeClosed(*caps);
|
2019-08-22 20:19:24 +00:00
|
|
|
opsTask.prepare(&flushState);
|
|
|
|
opsTask.execute(&flushState);
|
2020-06-08 19:55:00 +00:00
|
|
|
opsTask.endFlush(drawingMgr);
|
|
|
|
opsTask.disown(drawingMgr);
|
2018-10-24 14:04:37 +00:00
|
|
|
#if 0 // Useful to repeat a random configuration that fails the test while debugger attached.
|
2018-11-14 18:56:37 +00:00
|
|
|
if (!std::equal(result, result + result_width(), validResult)) {
|
|
|
|
repeat = true;
|
|
|
|
}
|
2018-10-24 14:04:37 +00:00
|
|
|
#endif
|
2018-11-14 18:56:37 +00:00
|
|
|
(void)repeat;
|
|
|
|
REPORTER_ASSERT(reporter, std::equal(result, result + result_width(), validResult));
|
|
|
|
}
|
2018-08-21 16:07:00 +00:00
|
|
|
}
|
2018-10-24 14:04:37 +00:00
|
|
|
}
|
2018-08-21 16:07:00 +00:00
|
|
|
}
|