2017-01-11 15:32:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2017-05-08 14:43:33 +00:00
|
|
|
#include <random>
|
2017-03-09 14:03:58 +00:00
|
|
|
#include "GrClip.h"
|
2017-01-11 15:32:34 +00:00
|
|
|
#include "GrContext.h"
|
|
|
|
#include "GrGpuResource.h"
|
|
|
|
#include "GrRenderTargetContext.h"
|
|
|
|
#include "GrRenderTargetContextPriv.h"
|
|
|
|
#include "GrResourceProvider.h"
|
|
|
|
#include "glsl/GrGLSLFragmentProcessor.h"
|
|
|
|
#include "glsl/GrGLSLFragmentShaderBuilder.h"
|
2017-07-14 16:00:13 +00:00
|
|
|
#include "ops/GrMeshDrawOp.h"
|
2017-06-15 13:59:23 +00:00
|
|
|
#include "ops/GrRectOpFactory.h"
|
2017-01-11 15:32:34 +00:00
|
|
|
|
|
|
|
namespace {
|
2017-07-14 16:00:13 +00:00
|
|
|
class TestOp : public GrMeshDrawOp {
|
2017-01-11 15:32:34 +00:00
|
|
|
public:
|
|
|
|
DEFINE_OP_CLASS_ID
|
2017-08-11 13:40:37 +00:00
|
|
|
static std::unique_ptr<GrDrawOp> Make(std::unique_ptr<GrFragmentProcessor> fp) {
|
2017-07-14 16:00:13 +00:00
|
|
|
return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp)));
|
|
|
|
}
|
|
|
|
|
2017-09-14 12:27:37 +00:00
|
|
|
const char* name() const override { return "TestOp"; }
|
|
|
|
|
2017-09-14 18:11:24 +00:00
|
|
|
void visitProxies(const VisitProxyFunc& func) const override {
|
2017-09-14 12:27:37 +00:00
|
|
|
fProcessors.visitProxies(func);
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:00:13 +00:00
|
|
|
FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
|
|
|
|
|
2017-09-20 13:53:22 +00:00
|
|
|
RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
|
|
|
|
GrPixelConfigIsClamped dstIsClamped) override {
|
2017-07-14 16:00:13 +00:00
|
|
|
static constexpr GrProcessorAnalysisColor kUnknownColor;
|
|
|
|
GrColor overrideColor;
|
|
|
|
fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
|
2017-09-20 13:53:22 +00:00
|
|
|
dstIsClamped, &overrideColor);
|
2017-07-14 16:00:13 +00:00
|
|
|
return RequiresDstTexture::kNo;
|
2017-03-09 18:50:43 +00:00
|
|
|
}
|
2017-01-11 15:32:34 +00:00
|
|
|
|
|
|
|
private:
|
2017-08-11 13:40:37 +00:00
|
|
|
TestOp(std::unique_ptr<GrFragmentProcessor> fp)
|
|
|
|
: INHERITED(ClassID()), fProcessors(std::move(fp)) {
|
2017-07-14 16:00:13 +00:00
|
|
|
this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
|
|
|
|
}
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-08-09 20:02:19 +00:00
|
|
|
void onPrepareDraws(Target* target) override { return; }
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-07-14 16:00:13 +00:00
|
|
|
bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
|
|
|
|
|
|
|
|
GrProcessorSet fProcessors;
|
|
|
|
|
|
|
|
typedef GrMeshDrawOp INHERITED;
|
2017-01-11 15:32:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
|
|
|
|
* of resources owned by child FPs.
|
|
|
|
*/
|
|
|
|
class TestFP : public GrFragmentProcessor {
|
|
|
|
public:
|
|
|
|
struct Image {
|
2017-05-12 18:49:16 +00:00
|
|
|
Image(sk_sp<GrTextureProxy> proxy, GrIOType ioType) : fProxy(proxy), fIOType(ioType) {}
|
|
|
|
sk_sp<GrTextureProxy> fProxy;
|
2017-01-11 15:32:34 +00:00
|
|
|
GrIOType fIOType;
|
|
|
|
};
|
2017-08-11 13:40:37 +00:00
|
|
|
static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
|
|
|
|
return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
|
2017-01-11 15:32:34 +00:00
|
|
|
}
|
2017-08-11 13:40:37 +00:00
|
|
|
static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
|
|
|
|
const SkTArray<sk_sp<GrBuffer>>& buffers,
|
|
|
|
const SkTArray<Image>& images) {
|
|
|
|
return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers, images));
|
2017-01-11 15:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "test"; }
|
|
|
|
|
|
|
|
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
|
|
|
|
// We don't really care about reusing these.
|
|
|
|
static int32_t gKey = 0;
|
|
|
|
b->add32(sk_atomic_inc(&gKey));
|
|
|
|
}
|
|
|
|
|
2017-08-11 13:40:37 +00:00
|
|
|
std::unique_ptr<GrFragmentProcessor> clone() const override {
|
|
|
|
return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
|
2017-07-28 17:41:51 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 15:32:34 +00:00
|
|
|
private:
|
2017-06-15 16:07:18 +00:00
|
|
|
TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
|
2017-02-22 20:28:38 +00:00
|
|
|
const SkTArray<sk_sp<GrBuffer>>& buffers,
|
2017-01-11 15:32:34 +00:00
|
|
|
const SkTArray<Image>& images)
|
2017-10-09 14:54:08 +00:00
|
|
|
: INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4),
|
|
|
|
fImages(4) {
|
2017-02-22 20:28:38 +00:00
|
|
|
for (const auto& proxy : proxies) {
|
2017-06-15 16:07:18 +00:00
|
|
|
this->addTextureSampler(&fSamplers.emplace_back(proxy));
|
2017-01-11 15:32:34 +00:00
|
|
|
}
|
|
|
|
for (const auto& buffer : buffers) {
|
|
|
|
this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
|
|
|
|
}
|
|
|
|
for (const Image& image : images) {
|
2017-05-12 18:49:16 +00:00
|
|
|
fImages.emplace_back(image.fProxy, image.fIOType,
|
|
|
|
GrSLMemoryModel::kNone, GrSLRestrict::kNo);
|
2017-06-15 16:07:18 +00:00
|
|
|
this->addImageStorageAccess(&fImages.back());
|
2017-01-11 15:32:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 13:40:37 +00:00
|
|
|
TestFP(std::unique_ptr<GrFragmentProcessor> child)
|
2017-10-09 14:54:08 +00:00
|
|
|
: INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4),
|
|
|
|
fImages(4) {
|
2017-01-11 15:32:34 +00:00
|
|
|
this->registerChildProcessor(std::move(child));
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:27:23 +00:00
|
|
|
explicit TestFP(const TestFP& that)
|
2017-10-09 14:54:08 +00:00
|
|
|
: INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4), fBuffers(4),
|
|
|
|
fImages(4) {
|
2017-07-28 17:41:51 +00:00
|
|
|
for (int i = 0; i < that.fSamplers.count(); ++i) {
|
|
|
|
fSamplers.emplace_back(that.fSamplers[i]);
|
|
|
|
this->addTextureSampler(&fSamplers.back());
|
|
|
|
}
|
|
|
|
for (int i = 0; i < that.fBuffers.count(); ++i) {
|
|
|
|
fBuffers.emplace_back(that.fBuffers[i]);
|
|
|
|
this->addBufferAccess(&fBuffers.back());
|
|
|
|
}
|
|
|
|
for (int i = 0; i < that.fImages.count(); ++i) {
|
|
|
|
fImages.emplace_back(that.fImages[i]);
|
|
|
|
this->addImageStorageAccess(&fImages.back());
|
|
|
|
}
|
2017-07-31 20:27:23 +00:00
|
|
|
for (int i = 0; i < that.numChildProcessors(); ++i) {
|
|
|
|
this->registerChildProcessor(that.childProcessor(i).clone());
|
2017-07-28 17:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:32:34 +00:00
|
|
|
virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
|
|
|
|
class TestGLSLFP : public GrGLSLFragmentProcessor {
|
|
|
|
public:
|
|
|
|
TestGLSLFP() {}
|
|
|
|
void emitCode(EmitArgs& args) override {
|
|
|
|
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
|
|
|
fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
return new TestGLSLFP();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
|
|
|
|
|
|
|
|
GrTAllocator<TextureSampler> fSamplers;
|
|
|
|
GrTAllocator<BufferAccess> fBuffers;
|
|
|
|
GrTAllocator<ImageStorageAccess> fImages;
|
2017-01-27 15:59:27 +00:00
|
|
|
typedef GrFragmentProcessor INHERITED;
|
2017-01-11 15:32:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:24:47 +00:00
|
|
|
template <typename T>
|
|
|
|
inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
|
|
|
|
*refCnt = resource->fRefCnt;
|
|
|
|
*readCnt = resource->fPendingReads;
|
|
|
|
*writeCnt = resource->fPendingWrites;
|
2017-01-11 15:32:34 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 23:18:38 +00:00
|
|
|
void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
|
2017-02-22 20:28:38 +00:00
|
|
|
*refCnt = proxy->getBackingRefCnt_TestOnly();
|
|
|
|
*readCnt = proxy->getPendingReadCnt_TestOnly();
|
|
|
|
*writeCnt = proxy->getPendingWriteCnt_TestOnly();
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:32:34 +00:00
|
|
|
DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
|
|
|
|
GrContext* context = ctxInfo.grContext();
|
|
|
|
|
2017-07-27 20:16:25 +00:00
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fOrigin = kTopLeft_GrSurfaceOrigin;
|
2017-01-11 15:32:34 +00:00
|
|
|
desc.fWidth = 10;
|
|
|
|
desc.fHeight = 10;
|
2017-07-27 20:16:25 +00:00
|
|
|
desc.fConfig = kRGBA_8888_GrPixelConfig;
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-07-28 17:41:51 +00:00
|
|
|
for (bool makeClone : {false, true}) {
|
|
|
|
for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
|
|
|
|
sk_sp<GrRenderTargetContext> renderTargetContext(
|
|
|
|
context->makeDeferredRenderTargetContext( SkBackingFit::kApprox, 1, 1,
|
|
|
|
kRGBA_8888_GrPixelConfig, nullptr));
|
2017-01-11 15:32:34 +00:00
|
|
|
{
|
2017-07-28 17:41:51 +00:00
|
|
|
bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
|
|
|
|
bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
|
|
|
|
sk_sp<GrTextureProxy> proxy1(
|
|
|
|
GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
|
|
|
|
desc, SkBackingFit::kExact,
|
|
|
|
SkBudgeted::kYes));
|
|
|
|
sk_sp<GrTextureProxy> proxy2
|
|
|
|
(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
|
|
|
|
desc, SkBackingFit::kExact,
|
|
|
|
SkBudgeted::kYes));
|
|
|
|
sk_sp<GrTextureProxy> proxy3(
|
|
|
|
GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
|
|
|
|
desc, SkBackingFit::kExact,
|
|
|
|
SkBudgeted::kYes));
|
|
|
|
sk_sp<GrTextureProxy> proxy4(
|
|
|
|
GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
|
|
|
|
desc, SkBackingFit::kExact,
|
|
|
|
SkBudgeted::kYes));
|
|
|
|
sk_sp<GrBuffer> buffer(texelBufferSupport
|
|
|
|
? context->resourceProvider()->createBuffer(
|
|
|
|
1024, GrBufferType::kTexel_GrBufferType,
|
|
|
|
GrAccessPattern::kStatic_GrAccessPattern, 0)
|
|
|
|
: nullptr);
|
|
|
|
{
|
|
|
|
SkTArray<sk_sp<GrTextureProxy>> proxies;
|
|
|
|
SkTArray<sk_sp<GrBuffer>> buffers;
|
|
|
|
SkTArray<TestFP::Image> images;
|
|
|
|
proxies.push_back(proxy1);
|
|
|
|
if (texelBufferSupport) {
|
|
|
|
buffers.push_back(buffer);
|
|
|
|
}
|
|
|
|
if (imageLoadStoreSupport) {
|
|
|
|
images.emplace_back(proxy2, GrIOType::kRead_GrIOType);
|
|
|
|
images.emplace_back(proxy3, GrIOType::kWrite_GrIOType);
|
|
|
|
images.emplace_back(proxy4, GrIOType::kRW_GrIOType);
|
|
|
|
}
|
|
|
|
auto fp = TestFP::Make(std::move(proxies), std::move(buffers),
|
|
|
|
std::move(images));
|
|
|
|
for (int i = 0; i < parentCnt; ++i) {
|
|
|
|
fp = TestFP::Make(std::move(fp));
|
|
|
|
}
|
2017-08-11 13:40:37 +00:00
|
|
|
std::unique_ptr<GrFragmentProcessor> clone;
|
2017-07-28 17:41:51 +00:00
|
|
|
if (makeClone) {
|
|
|
|
clone = fp->clone();
|
|
|
|
}
|
|
|
|
std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
|
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
|
|
|
|
if (clone) {
|
|
|
|
op = TestOp::Make(std::move(clone));
|
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
|
|
|
|
}
|
2017-01-11 15:32:34 +00:00
|
|
|
}
|
2017-07-28 17:41:51 +00:00
|
|
|
int refCnt, readCnt, writeCnt;
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-07-28 17:41:51 +00:00
|
|
|
testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
|
|
|
|
// IO counts should be double if there is a clone of the FP.
|
|
|
|
int ioRefMul = makeClone ? 2 : 1;
|
2017-01-11 15:32:34 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
2017-07-28 17:41:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-07-28 17:41:51 +00:00
|
|
|
if (texelBufferSupport) {
|
|
|
|
testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
|
|
|
|
}
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-07-28 17:41:51 +00:00
|
|
|
if (imageLoadStoreSupport) {
|
|
|
|
testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
|
|
|
|
|
|
|
|
testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
|
|
|
|
|
|
|
|
testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
|
|
|
|
}
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-07-28 17:41:51 +00:00
|
|
|
context->flush();
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-07-28 17:41:51 +00:00
|
|
|
testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
|
2017-01-11 15:32:34 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
2017-07-28 17:41:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-07-28 17:41:51 +00:00
|
|
|
if (texelBufferSupport) {
|
|
|
|
testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
|
|
|
|
}
|
2017-01-11 15:32:34 +00:00
|
|
|
|
2017-07-28 17:41:51 +00:00
|
|
|
if (texelBufferSupport) {
|
|
|
|
testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
|
|
|
|
|
|
|
|
testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
|
|
|
|
|
|
|
|
testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == refCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
|
|
|
|
REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
|
|
|
|
}
|
2017-01-11 15:32:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-27 15:59:27 +00:00
|
|
|
|
|
|
|
// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
|
|
|
|
#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
|
|
|
|
|
2017-07-25 13:43:22 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
|
|
|
DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
|
|
|
|
|
|
|
|
#if GR_TEST_UTILS
|
|
|
|
|
|
|
|
static GrColor input_texel_color(int i, int j) {
|
|
|
|
GrColor color = GrColorPackRGBA((uint8_t)j, (uint8_t)(i + j), (uint8_t)(2 * j - i), (uint8_t)i);
|
2017-01-27 15:59:27 +00:00
|
|
|
return GrPremulColor(color);
|
|
|
|
}
|
|
|
|
|
2017-07-25 13:43:22 +00:00
|
|
|
static GrColor4f input_texel_color4f(int i, int j) {
|
|
|
|
return GrColor4f::FromGrColor(input_texel_color(i, j));
|
|
|
|
}
|
2017-01-27 15:59:27 +00:00
|
|
|
|
2017-08-11 13:40:37 +00:00
|
|
|
void test_draw_op(GrRenderTargetContext* rtc, std::unique_ptr<GrFragmentProcessor> fp,
|
2017-02-22 20:28:38 +00:00
|
|
|
sk_sp<GrTextureProxy> inputDataProxy) {
|
2017-02-09 16:16:46 +00:00
|
|
|
GrPaint paint;
|
2017-10-18 17:15:13 +00:00
|
|
|
paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
|
2017-02-09 16:16:46 +00:00
|
|
|
paint.addColorFragmentProcessor(std::move(fp));
|
|
|
|
paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
|
2017-05-08 14:43:33 +00:00
|
|
|
|
2017-06-15 13:59:23 +00:00
|
|
|
auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
|
|
|
|
SkRect::MakeWH(rtc->width(), rtc->height()),
|
|
|
|
GrAAType::kNone);
|
2017-05-08 14:43:33 +00:00
|
|
|
rtc->addDrawOp(GrNoClip(), std::move(op));
|
2017-02-09 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 13:43:22 +00:00
|
|
|
/** Initializes the two test texture proxies that are available to the FP test factories. */
|
|
|
|
bool init_test_textures(GrContext* context, SkRandom* random, sk_sp<GrTextureProxy> proxies[2]) {
|
|
|
|
static const int kTestTextureSize = 256;
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
|
|
|
|
desc.fWidth = kTestTextureSize;
|
|
|
|
desc.fHeight = kTestTextureSize;
|
|
|
|
desc.fConfig = kRGBA_8888_GrPixelConfig;
|
2017-03-17 14:58:53 +00:00
|
|
|
|
2017-07-25 13:43:22 +00:00
|
|
|
// Put premul data into the RGBA texture that the test FPs can optionally use.
|
|
|
|
std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
|
|
|
|
for (int y = 0; y < kTestTextureSize; ++y) {
|
|
|
|
for (int x = 0; x < kTestTextureSize; ++x) {
|
|
|
|
rgbaData[kTestTextureSize * y + x] =
|
|
|
|
input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
|
|
|
|
rgbaData.get(), kTestTextureSize * sizeof(GrColor));
|
|
|
|
|
|
|
|
// Put random values into the alpha texture that the test FPs can optionally use.
|
|
|
|
desc.fConfig = kAlpha_8_GrPixelConfig;
|
|
|
|
std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
|
|
|
|
for (int y = 0; y < kTestTextureSize; ++y) {
|
|
|
|
for (int x = 0; x < kTestTextureSize; ++x) {
|
|
|
|
alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
|
|
|
|
alphaData.get(), kTestTextureSize);
|
|
|
|
|
|
|
|
return proxies[0] && proxies[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a texture of premul colors used as the output of the fragment processor that precedes
|
|
|
|
// the fragment processor under test. Color values are those provided by input_texel_color().
|
|
|
|
sk_sp<GrTextureProxy> make_input_texture(GrContext* context, int width, int height) {
|
|
|
|
std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
|
|
|
|
for (int y = 0; y < width; ++y) {
|
|
|
|
for (int x = 0; x < height; ++x) {
|
|
|
|
data.get()[width * y + x] = input_texel_color(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
|
|
|
|
desc.fWidth = width;
|
|
|
|
desc.fHeight = height;
|
|
|
|
desc.fConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
|
|
|
|
data.get(), width * sizeof(GrColor));
|
|
|
|
}
|
2017-02-09 16:16:46 +00:00
|
|
|
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
|
2017-01-27 15:59:27 +00:00
|
|
|
GrContext* context = ctxInfo.grContext();
|
2017-07-24 14:16:19 +00:00
|
|
|
using FPFactory = GrFragmentProcessorTestFactory;
|
2017-03-17 14:58:53 +00:00
|
|
|
|
|
|
|
uint32_t seed = 0;
|
|
|
|
if (FLAGS_randomProcessorTest) {
|
|
|
|
std::random_device rd;
|
|
|
|
seed = rd();
|
|
|
|
}
|
|
|
|
// If a non-deterministic bot fails this test, check the output to see what seed it used, then
|
|
|
|
// hard-code that value here:
|
|
|
|
SkRandom random(seed);
|
|
|
|
|
2017-07-25 13:43:22 +00:00
|
|
|
// Make the destination context for the test.
|
|
|
|
static constexpr int kRenderSize = 256;
|
2017-04-24 14:57:28 +00:00
|
|
|
sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
|
2017-07-25 13:43:22 +00:00
|
|
|
SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
|
2017-02-10 17:39:26 +00:00
|
|
|
|
2017-04-06 11:59:41 +00:00
|
|
|
sk_sp<GrTextureProxy> proxies[2];
|
2017-07-25 13:43:22 +00:00
|
|
|
if (!init_test_textures(context, &random, proxies)) {
|
|
|
|
ERRORF(reporter, "Could not create test textures");
|
2017-05-29 19:05:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-06 11:59:41 +00:00
|
|
|
GrProcessorTestData testData(&random, context, rtc.get(), proxies);
|
2017-01-27 15:59:27 +00:00
|
|
|
|
2017-07-25 13:43:22 +00:00
|
|
|
auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
|
2017-01-27 15:59:27 +00:00
|
|
|
|
2017-07-25 15:47:48 +00:00
|
|
|
std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
|
2017-07-25 13:43:22 +00:00
|
|
|
// Because processor factories configure themselves in random ways, this is not exhaustive.
|
2017-01-27 15:59:27 +00:00
|
|
|
for (int i = 0; i < FPFactory::Count(); ++i) {
|
|
|
|
int timesToInvokeFactory = 5;
|
|
|
|
// Increase the number of attempts if the FP has child FPs since optimizations likely depend
|
|
|
|
// on child optimizations being present.
|
2017-08-11 13:40:37 +00:00
|
|
|
std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
|
2017-01-27 15:59:27 +00:00
|
|
|
for (int j = 0; j < fp->numChildProcessors(); ++j) {
|
|
|
|
// This value made a reasonable trade off between time and coverage when this test was
|
|
|
|
// written.
|
|
|
|
timesToInvokeFactory *= FPFactory::Count() / 2;
|
|
|
|
}
|
|
|
|
for (int j = 0; j < timesToInvokeFactory; ++j) {
|
|
|
|
fp = FPFactory::MakeIdx(i, &testData);
|
2017-05-29 16:37:20 +00:00
|
|
|
if (!fp->instantiate(context->resourceProvider())) {
|
2017-05-01 17:12:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-27 15:59:27 +00:00
|
|
|
if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
|
2017-02-15 15:22:23 +00:00
|
|
|
!fp->compatibleWithCoverageAsAlpha()) {
|
2017-01-27 15:59:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-08-11 13:40:37 +00:00
|
|
|
|
|
|
|
// Since we transfer away ownership of the original FP, we make a clone.
|
|
|
|
auto clone = fp->clone();
|
|
|
|
|
|
|
|
test_draw_op(rtc.get(), std::move(fp), inputTexture);
|
2017-07-25 13:43:22 +00:00
|
|
|
memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
|
|
|
|
rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType),
|
|
|
|
readData.get(), 0, 0, 0);
|
2017-01-27 15:59:27 +00:00
|
|
|
bool passing = true;
|
|
|
|
if (0) { // Useful to see what FPs are being tested.
|
|
|
|
SkString children;
|
2017-08-11 13:40:37 +00:00
|
|
|
for (int c = 0; c < clone->numChildProcessors(); ++c) {
|
2017-01-27 15:59:27 +00:00
|
|
|
if (!c) {
|
|
|
|
children.append("(");
|
|
|
|
}
|
2017-08-11 13:40:37 +00:00
|
|
|
children.append(clone->name());
|
|
|
|
children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
|
2017-01-27 15:59:27 +00:00
|
|
|
}
|
2017-08-11 13:40:37 +00:00
|
|
|
SkDebugf("%s %s\n", clone->name(), children.c_str());
|
2017-01-27 15:59:27 +00:00
|
|
|
}
|
2017-07-25 13:43:22 +00:00
|
|
|
for (int y = 0; y < kRenderSize && passing; ++y) {
|
|
|
|
for (int x = 0; x < kRenderSize && passing; ++x) {
|
|
|
|
GrColor input = input_texel_color(x, y);
|
|
|
|
GrColor output = readData.get()[y * kRenderSize + x];
|
2017-08-11 13:40:37 +00:00
|
|
|
if (clone->compatibleWithCoverageAsAlpha()) {
|
2017-01-27 15:59:27 +00:00
|
|
|
// A modulating processor is allowed to modulate either the input color or
|
|
|
|
// just the input alpha.
|
|
|
|
bool legalColorModulation =
|
|
|
|
GrColorUnpackA(output) <= GrColorUnpackA(input) &&
|
|
|
|
GrColorUnpackR(output) <= GrColorUnpackR(input) &&
|
|
|
|
GrColorUnpackG(output) <= GrColorUnpackG(input) &&
|
|
|
|
GrColorUnpackB(output) <= GrColorUnpackB(input);
|
|
|
|
bool legalAlphaModulation =
|
|
|
|
GrColorUnpackA(output) <= GrColorUnpackA(input) &&
|
|
|
|
GrColorUnpackR(output) <= GrColorUnpackA(input) &&
|
|
|
|
GrColorUnpackG(output) <= GrColorUnpackA(input) &&
|
|
|
|
GrColorUnpackB(output) <= GrColorUnpackA(input);
|
|
|
|
if (!legalColorModulation && !legalAlphaModulation) {
|
|
|
|
ERRORF(reporter,
|
|
|
|
"\"Modulating\" processor %s made color/alpha value larger. "
|
2017-03-15 17:35:51 +00:00
|
|
|
"Input: 0x%08x, Output: 0x%08x.",
|
2017-08-11 13:40:37 +00:00
|
|
|
clone->name(), input, output);
|
2017-01-27 15:59:27 +00:00
|
|
|
passing = false;
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 13:43:22 +00:00
|
|
|
GrColor4f input4f = input_texel_color4f(x, y);
|
2017-01-27 15:59:27 +00:00
|
|
|
GrColor4f output4f = GrColor4f::FromGrColor(output);
|
|
|
|
GrColor4f expected4f;
|
2017-08-11 13:40:37 +00:00
|
|
|
if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
|
2017-01-27 15:59:27 +00:00
|
|
|
float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
|
|
|
|
float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
|
|
|
|
float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
|
|
|
|
float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
|
2017-02-09 16:16:46 +00:00
|
|
|
static constexpr float kTol = 4 / 255.f;
|
2017-01-27 15:59:27 +00:00
|
|
|
if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
|
|
|
|
ERRORF(reporter,
|
|
|
|
"Processor %s claimed output for const input doesn't match "
|
2017-02-09 16:16:46 +00:00
|
|
|
"actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
|
|
|
|
"%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
|
|
|
|
fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
|
|
|
|
kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
|
|
|
|
input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
|
|
|
|
output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
|
|
|
|
expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
|
2017-01-27 15:59:27 +00:00
|
|
|
passing = false;
|
|
|
|
}
|
|
|
|
}
|
2017-08-11 13:40:37 +00:00
|
|
|
if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
|
2017-01-27 15:59:27 +00:00
|
|
|
!GrColorIsOpaque(output)) {
|
|
|
|
ERRORF(reporter,
|
|
|
|
"Processor %s claimed opaqueness is preserved but it is not. Input: "
|
2017-03-15 17:35:51 +00:00
|
|
|
"0x%08x, Output: 0x%08x.",
|
2017-08-11 13:40:37 +00:00
|
|
|
clone->name(), input, output);
|
2017-01-27 15:59:27 +00:00
|
|
|
passing = false;
|
|
|
|
}
|
2017-03-15 15:33:12 +00:00
|
|
|
if (!passing) {
|
2017-08-11 13:40:37 +00:00
|
|
|
ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed,
|
|
|
|
clone->dumpInfo().c_str());
|
2017-03-15 15:33:12 +00:00
|
|
|
}
|
2017-01-27 15:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 16:55:44 +00:00
|
|
|
|
2017-07-25 13:43:22 +00:00
|
|
|
// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
|
|
|
|
// progenitors.
|
|
|
|
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
|
|
|
|
GrContext* context = ctxInfo.grContext();
|
|
|
|
|
|
|
|
SkRandom random;
|
|
|
|
|
|
|
|
// Make the destination context for the test.
|
|
|
|
static constexpr int kRenderSize = 1024;
|
|
|
|
sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
|
|
|
|
SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
|
|
|
|
|
|
|
|
sk_sp<GrTextureProxy> proxies[2];
|
|
|
|
if (!init_test_textures(context, &random, proxies)) {
|
|
|
|
ERRORF(reporter, "Could not create test textures");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrProcessorTestData testData(&random, context, rtc.get(), proxies);
|
|
|
|
|
|
|
|
auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
|
|
|
|
std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
|
|
|
|
std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
|
|
|
|
auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
|
|
|
|
|
|
|
// Because processor factories configure themselves in random ways, this is not exhaustive.
|
|
|
|
for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
|
|
|
|
static constexpr int kTimesToInvokeFactory = 10;
|
|
|
|
for (int j = 0; j < kTimesToInvokeFactory; ++j) {
|
|
|
|
auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
|
|
|
|
auto clone = fp->clone();
|
|
|
|
if (!clone) {
|
2017-07-31 20:27:23 +00:00
|
|
|
ERRORF(reporter, "Clone of processor %s failed.", fp->name());
|
2017-07-25 13:43:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-08-11 13:40:37 +00:00
|
|
|
const char* name = fp->name();
|
2017-07-25 13:43:22 +00:00
|
|
|
if (!fp->instantiate(context->resourceProvider()) ||
|
|
|
|
!clone->instantiate(context->resourceProvider())) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-01 20:23:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
|
|
|
|
REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
|
|
|
|
clone->compatibleWithCoverageAsAlpha());
|
|
|
|
REPORTER_ASSERT(reporter, fp->isEqual(*clone));
|
|
|
|
REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
|
|
|
|
REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
|
|
|
|
clone->hasConstantOutputForConstantInput());
|
|
|
|
REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
|
|
|
|
REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
|
2017-07-25 13:43:22 +00:00
|
|
|
// Draw with original and read back the results.
|
2017-08-11 13:40:37 +00:00
|
|
|
test_draw_op(rtc.get(), std::move(fp), inputTexture);
|
2017-07-25 13:43:22 +00:00
|
|
|
memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
|
|
|
|
rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
|
|
|
|
|
|
|
|
// Draw with clone and read back the results.
|
2017-08-11 13:40:37 +00:00
|
|
|
test_draw_op(rtc.get(), std::move(clone), inputTexture);
|
2017-07-25 13:43:22 +00:00
|
|
|
memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
|
|
|
|
rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
|
|
|
|
|
|
|
|
// Check that the results are the same.
|
|
|
|
bool passing = true;
|
|
|
|
for (int y = 0; y < kRenderSize && passing; ++y) {
|
|
|
|
for (int x = 0; x < kRenderSize && passing; ++x) {
|
|
|
|
int idx = y * kRenderSize + x;
|
|
|
|
if (readData1[idx] != readData2[idx]) {
|
|
|
|
ERRORF(reporter,
|
|
|
|
"Processor %s made clone produced different output. "
|
|
|
|
"Input color: 0x%08x, Original Output Color: 0x%08x, "
|
|
|
|
"Clone Output Color: 0x%08x..",
|
2017-08-11 13:40:37 +00:00
|
|
|
name, input_texel_color(x, y), readData1[idx], readData2[idx]);
|
2017-07-25 13:43:22 +00:00
|
|
|
passing = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-31 18:50:44 +00:00
|
|
|
#endif // GR_TEST_UTILS
|
|
|
|
#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
|
|
|
|
#endif // SK_SUPPORT_GPU
|