skia2/include/gpu/GrProcessorUnitTest.h

190 lines
6.9 KiB
C
Raw Normal View History

/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrProcessorUnitTest_DEFINED
#define GrProcessorUnitTest_DEFINED
#include "../private/GrTextureProxy.h"
#include "../private/SkTArray.h"
#include "GrTestUtils.h"
#include "SkTypes.h"
class SkMatrix;
class GrCaps;
class GrContext;
class GrRenderTargetContext;
struct GrProcessorTestData;
class GrTexture;
class GrXPFactory;
namespace GrProcessorUnitTest {
// Used to access the dummy textures in TestCreate procs.
enum {
kSkiaPMTextureIdx = 0,
kAlphaTextureIdx = 1,
};
/** This allows parent FPs to implement a test create with known leaf children in order to avoid
creating an unbounded FP tree which may overflow various shader limits. */
sk_sp<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
}
/*
* GrProcessorTestData is an argument struct to TestCreate functions
* fTextures are valid textures that can optionally be used to construct
* TextureSampler. The first texture has config kSkia8888_GrPixelConfig and the second has
* kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
* the GrContext.
*/
struct GrProcessorTestData {
GrProcessorTestData(SkRandom* random,
GrContext* context,
const GrRenderTargetContext* renderTargetContext,
Revert "Start of rewrite of GrFragmentProcessor optimizations." This reverts commit 85eb4226a4cd8c10a0e3f3ba2f3a60efbb2dd61b. Reason for revert: test failures on Windows, e.g. https://chromium-swarm.appspot.com/task?id=33f9527484414110&refresh=10 Original change's description: > Start of rewrite of GrFragmentProcessor optimizations. > > This adds a replacement for computeInvariantOutput buts does not use it yet. The replacement allows for three types of optimizations: > > * known input color -> known output color for GrFP elimination > * tracking of whether all color processors modulate their input for the "tweak alpha" optimziation > * opaqueness tracking > > This loses some of the generality of computInvariantOutput. It does not track the known output status of individual color components (other than opaque alpha). It does not track whether GrFragmentProcessors read their input color. It doesn't allow a processor that will receive non-constant output to advertise that it produces a constant output. These could probably be added back in the unlikely case that they prove valuable. > > Unlike computeInvariantOutput the optimizations are decided at instantiation time and constant colors are expressed as GrColor4f rather than GrColor. > > Change-Id: I684d3f9050693dde2d28154fa695e049ed8cf61a > Reviewed-on: https://skia-review.googlesource.com/7481 > Reviewed-by: Greg Daniel <egdaniel@google.com> > Commit-Queue: Brian Salomon <bsalomon@google.com> > TBR=egdaniel@google.com,bsalomon@google.com,brianosman@google.com NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Change-Id: I2390df257456013fa74137cb5d7b5a93820c291e Reviewed-on: https://skia-review.googlesource.com/7652 Commit-Queue: Ethan Nicholas <ethannicholas@google.com> Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
2017-01-27 15:34:34 +00:00
GrTexture* textures[2])
: fRandom(random)
, fContext(context)
, fRenderTargetContext(renderTargetContext) {
fTextures[0] = textures[0];
fTextures[1] = textures[1];
fProxies[0] = GrSurfaceProxy::MakeWrapped(sk_ref_sp(textures[0]));
fProxies[1] = GrSurfaceProxy::MakeWrapped(sk_ref_sp(textures[1]));
}
SkRandom* fRandom;
GrContext* fContext;
const GrRenderTargetContext* fRenderTargetContext;
GrTexture* fTextures[2];
GrContext* context() { return fContext; }
GrTexture* texture(int index) { return fTextures[index]; }
sk_sp<GrTextureProxy> textureProxy(int index) {
return sk_ref_sp(fProxies[index]->asTextureProxy());
}
private:
sk_sp<GrSurfaceProxy> fProxies[2];
};
#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
class GrProcessor;
class GrTexture;
template <class Processor> class GrProcessorTestFactory : private SkNoncopyable {
public:
typedef sk_sp<Processor> (*MakeProc)(GrProcessorTestData*);
GrProcessorTestFactory(MakeProc makeProc) {
fMakeProc = makeProc;
GetFactories()->push_back(this);
}
/** Pick a random factory function and create a processor. */
static sk_sp<Processor> Make(GrProcessorTestData* data) {
VerifyFactoryCount();
SkASSERT(GetFactories()->count());
uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
return MakeIdx(idx, data);
}
/** Number of registered factory functions */
static int Count() { return GetFactories()->count(); }
/** Use factory function at Index idx to create a processor. */
static sk_sp<Processor> MakeIdx(int idx, GrProcessorTestData* data) {
GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
sk_sp<Processor> processor = factory->fMakeProc(data);
SkASSERT(processor);
return processor;
}
private:
/**
* A test function which verifies the count of factories.
*/
static void VerifyFactoryCount();
MakeProc fMakeProc;
static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
};
class GrXPFactoryTestFactory : private SkNoncopyable {
public:
using GetFn = const GrXPFactory*(GrProcessorTestData*);
GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { GetFactories()->push_back(this); }
static const GrXPFactory* Get(GrProcessorTestData* data) {
VerifyFactoryCount();
SkASSERT(GetFactories()->count());
uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data);
SkASSERT(xpf);
return xpf;
}
private:
static void VerifyFactoryCount();
GetFn* fGetProc;
static SkTArray<GrXPFactoryTestFactory*, true>* GetFactories();
};
/** GrProcessor subclasses should insert this macro in their declaration to be included in the
* program generation unit test.
*/
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \
static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \
static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
#define GR_DECLARE_XP_FACTORY_TEST \
static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \
static const GrXPFactory* TestGet(GrProcessorTestData*)
/** GrProcessor subclasses should insert this macro in their implementation file. They must then
* also implement this static function:
* GrProcessor* TestCreate(GrProcessorTestData*);
*/
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
GrProcessorTestFactory<GrFragmentProcessor> Effect::gTestFactory(Effect::TestCreate)
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
GrProcessorTestFactory<GrGeometryProcessor> Effect::gTestFactory(Effect::TestCreate)
#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet)
#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
// The unit test relies on static initializers. Just declare the TestCreate function so that
// its definitions will compile.
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
// The unit test relies on static initializers. Just declare the TestCreate function so that
// its definitions will compile.
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
// The unit test relies on static initializers. Just declare the TestGet function so that
// its definitions will compile.
#define GR_DECLARE_XP_FACTORY_TEST \
const GrXPFactory* TestGet(GrProcessorTestData*)
#define GR_DEFINE_XP_FACTORY_TEST(X)
#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
#endif