1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/*
* 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
#include "GrContext.h"
#include "GrGpuResource.h"
#include "GrRenderTargetContext.h"
#include "GrRenderTargetContextPriv.h"
#include "GrResourceProvider.h"
#include "glsl/GrGLSLFragmentProcessor.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "ops/GrTestMeshDrawOp.h"
namespace {
class TestOp : public GrTestMeshDrawOp {
public:
DEFINE_OP_CLASS_ID
const char* name() const override { return "TestOp"; }
static std::unique_ptr<GrDrawOp> Make() { return std::unique_ptr<GrDrawOp>(new TestOp); }
private:
TestOp() : INHERITED(ClassID(), SkRect::MakeWH(100, 100), 0xFFFFFFFF) {}
void onPrepareDraws(Target* target) const override { return; }
typedef GrTestMeshDrawOp INHERITED;
};
/**
* 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 {
Image(sk_sp<GrTexture> texture, GrIOType ioType) : fTexture(texture), fIOType(ioType) {}
sk_sp<GrTexture> fTexture;
GrIOType fIOType;
};
static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> child) {
return sk_sp<GrFragmentProcessor>(new TestFP(std::move(child)));
}
static sk_sp<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTexture>>& textures,
const SkTArray<sk_sp<GrBuffer>>& buffers,
const SkTArray<Image>& images) {
return sk_sp<GrFragmentProcessor>(new TestFP(textures, buffers, images));
}
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));
}
void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
// We don't care about optimizing these processors.
inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
}
private:
TestFP(const SkTArray<sk_sp<GrTexture>>& textures, const SkTArray<sk_sp<GrBuffer>>& buffers,
const SkTArray<Image>& images)
: fSamplers(4), fBuffers(4), fImages(4) {
for (const auto& texture : textures) {
this->addTextureSampler(&fSamplers.emplace_back(texture.get()));
}
for (const auto& buffer : buffers) {
this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
}
for (const Image& image : images) {
this->addImageStorageAccess(&fImages.emplace_back(
image.fTexture, image.fIOType, GrSLMemoryModel::kNone, GrSLRestrict::kNo));
}
}
TestFP(sk_sp<GrFragmentProcessor> child) : fSamplers(4), fBuffers(4), fImages(4) {
this->registerChildProcessor(std::move(child));
}
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;
};
}
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;
}
DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
GrContext* context = ctxInfo.grContext();
GrTextureDesc desc;
desc.fConfig = kRGBA_8888_GrPixelConfig;
desc.fWidth = 10;
desc.fHeight = 10;
for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
sk_sp<GrRenderTargetContext> renderTargetContext(context->makeRenderTargetContext(
SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig, nullptr));
{
bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
sk_sp<GrTexture> texture1(
context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
sk_sp<GrTexture> texture2(
context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
sk_sp<GrTexture> texture3(
context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
sk_sp<GrTexture> texture4(
context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
sk_sp<GrBuffer> buffer(texelBufferSupport
? context->resourceProvider()->createBuffer(
1024, GrBufferType::kTexel_GrBufferType,
GrAccessPattern::kStatic_GrAccessPattern, 0)
: nullptr);
{
SkTArray<sk_sp<GrTexture>> textures;
SkTArray<sk_sp<GrBuffer>> buffers;
SkTArray<TestFP::Image> images;
textures.push_back(texture1);
if (texelBufferSupport) {
buffers.push_back(buffer);
}
if (imageLoadStoreSupport) {
images.emplace_back(texture2, GrIOType::kRead_GrIOType);
images.emplace_back(texture3, GrIOType::kWrite_GrIOType);
images.emplace_back(texture4, GrIOType::kRW_GrIOType);
}
std::unique_ptr<GrDrawOp> op(TestOp::Make());
GrPaint paint;
auto fp = TestFP::Make(std::move(textures), std::move(buffers), std::move(images));
for (int i = 0; i < parentCnt; ++i) {
fp = TestFP::Make(std::move(fp));
}
paint.addColorFragmentProcessor(std::move(fp));
renderTargetContext->priv().testingOnly_addDrawOp(std::move(paint), GrAAType::kNone,
std::move(op));
}
int refCnt, readCnt, writeCnt;
testingOnly_getIORefCnts(texture1.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 1 == readCnt);
REPORTER_ASSERT(reporter, 0 == writeCnt);
if (texelBufferSupport) {
testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 1 == readCnt);
REPORTER_ASSERT(reporter, 0 == writeCnt);
}
if (imageLoadStoreSupport) {
testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 1 == readCnt);
REPORTER_ASSERT(reporter, 0 == writeCnt);
testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 0 == readCnt);
REPORTER_ASSERT(reporter, 1 == writeCnt);
testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 1 == readCnt);
REPORTER_ASSERT(reporter, 1 == writeCnt);
}
context->flush();
testingOnly_getIORefCnts(texture1.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 0 == readCnt);
REPORTER_ASSERT(reporter, 0 == writeCnt);
if (texelBufferSupport) {
testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 0 == readCnt);
REPORTER_ASSERT(reporter, 0 == writeCnt);
}
if (texelBufferSupport) {
testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 0 == readCnt);
REPORTER_ASSERT(reporter, 0 == writeCnt);
testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 0 == readCnt);
REPORTER_ASSERT(reporter, 0 == writeCnt);
testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt);
REPORTER_ASSERT(reporter, 1 == refCnt);
REPORTER_ASSERT(reporter, 0 == readCnt);
REPORTER_ASSERT(reporter, 0 == writeCnt);
}
}
}
}
#endif
|