2014-09-23 16:50:21 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2017-01-31 18:50:44 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
#if GR_TEST_UTILS
|
|
|
|
|
2017-01-25 20:48:30 +00:00
|
|
|
#include "../private/GrTextureProxy.h"
|
2016-02-17 21:13:44 +00:00
|
|
|
#include "../private/SkTArray.h"
|
2015-04-28 20:31:18 +00:00
|
|
|
#include "GrTestUtils.h"
|
2014-09-23 16:50:21 +00:00
|
|
|
|
|
|
|
class SkMatrix;
|
2015-05-19 16:29:46 +00:00
|
|
|
class GrCaps;
|
2015-07-08 21:26:19 +00:00
|
|
|
class GrContext;
|
2016-10-27 18:47:55 +00:00
|
|
|
class GrRenderTargetContext;
|
2015-09-14 20:16:26 +00:00
|
|
|
struct GrProcessorTestData;
|
2016-11-15 21:44:34 +00:00
|
|
|
class GrTexture;
|
2017-01-09 16:46:10 +00:00
|
|
|
class GrXPFactory;
|
2014-09-23 16:50:21 +00:00
|
|
|
|
|
|
|
namespace GrProcessorUnitTest {
|
2015-09-14 20:16:26 +00:00
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
// Used to access the dummy textures in TestCreate procs.
|
|
|
|
enum {
|
|
|
|
kSkiaPMTextureIdx = 0,
|
|
|
|
kAlphaTextureIdx = 1,
|
|
|
|
};
|
|
|
|
|
2015-09-14 20:16:26 +00:00
|
|
|
/** 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. */
|
2016-06-09 15:01:03 +00:00
|
|
|
sk_sp<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
|
2015-09-14 20:16:26 +00:00
|
|
|
|
2014-12-09 21:31:14 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 21:26:19 +00:00
|
|
|
/*
|
|
|
|
* GrProcessorTestData is an argument struct to TestCreate functions
|
|
|
|
* fTextures are valid textures that can optionally be used to construct
|
2016-11-17 16:38:22 +00:00
|
|
|
* TextureSampler. The first texture has config kSkia8888_GrPixelConfig and the second has
|
2015-07-08 21:26:19 +00:00
|
|
|
* kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
|
|
|
|
* the GrContext.
|
|
|
|
*/
|
|
|
|
struct GrProcessorTestData {
|
|
|
|
GrProcessorTestData(SkRandom* random,
|
|
|
|
GrContext* context,
|
2016-10-27 18:47:55 +00:00
|
|
|
const GrRenderTargetContext* renderTargetContext,
|
2017-01-27 15:59:27 +00:00
|
|
|
GrTexture* const textures[2])
|
2017-02-21 15:04:31 +00:00
|
|
|
: fRandom(random)
|
|
|
|
, fRenderTargetContext(renderTargetContext)
|
|
|
|
, fContext(context) {
|
2017-01-25 20:48:30 +00:00
|
|
|
fProxies[0] = GrSurfaceProxy::MakeWrapped(sk_ref_sp(textures[0]));
|
|
|
|
fProxies[1] = GrSurfaceProxy::MakeWrapped(sk_ref_sp(textures[1]));
|
2015-07-08 21:26:19 +00:00
|
|
|
}
|
|
|
|
SkRandom* fRandom;
|
2016-10-27 18:47:55 +00:00
|
|
|
const GrRenderTargetContext* fRenderTargetContext;
|
2017-01-25 20:48:30 +00:00
|
|
|
|
|
|
|
GrContext* context() { return fContext; }
|
2017-03-15 14:42:12 +00:00
|
|
|
GrResourceProvider* resourceProvider();
|
|
|
|
const GrCaps* caps();
|
2017-02-15 19:19:01 +00:00
|
|
|
sk_sp<GrTextureProxy> textureProxy(int index) { return fProxies[index]; }
|
2017-01-25 20:48:30 +00:00
|
|
|
|
|
|
|
private:
|
2017-02-21 15:04:31 +00:00
|
|
|
GrContext* fContext;
|
2017-02-15 19:19:01 +00:00
|
|
|
sk_sp<GrTextureProxy> fProxies[2];
|
2015-07-08 21:26:19 +00:00
|
|
|
};
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
|
|
|
|
|
|
|
|
class GrProcessor;
|
|
|
|
class GrTexture;
|
|
|
|
|
2017-01-09 16:46:10 +00:00
|
|
|
template <class Processor> class GrProcessorTestFactory : private SkNoncopyable {
|
2014-09-23 16:50:21 +00:00
|
|
|
public:
|
2016-06-09 15:01:03 +00:00
|
|
|
typedef sk_sp<Processor> (*MakeProc)(GrProcessorTestData*);
|
2014-09-23 16:50:21 +00:00
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
GrProcessorTestFactory(MakeProc makeProc) {
|
|
|
|
fMakeProc = makeProc;
|
2014-09-23 16:50:21 +00:00
|
|
|
GetFactories()->push_back(this);
|
|
|
|
}
|
|
|
|
|
2015-09-14 19:26:33 +00:00
|
|
|
/** Pick a random factory function and create a processor. */
|
2016-06-09 15:01:03 +00:00
|
|
|
static sk_sp<Processor> Make(GrProcessorTestData* data) {
|
2014-10-09 20:12:35 +00:00
|
|
|
VerifyFactoryCount();
|
|
|
|
SkASSERT(GetFactories()->count());
|
2015-07-08 21:26:19 +00:00
|
|
|
uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
|
2016-06-09 15:01:03 +00:00
|
|
|
return MakeIdx(idx, data);
|
2015-09-14 19:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Number of registered factory functions */
|
|
|
|
static int Count() { return GetFactories()->count(); }
|
|
|
|
|
|
|
|
/** Use factory function at Index idx to create a processor. */
|
2016-06-09 15:01:03 +00:00
|
|
|
static sk_sp<Processor> MakeIdx(int idx, GrProcessorTestData* data) {
|
2014-09-23 16:50:21 +00:00
|
|
|
GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
|
2017-01-09 16:46:10 +00:00
|
|
|
sk_sp<Processor> processor = factory->fMakeProc(data);
|
|
|
|
SkASSERT(processor);
|
|
|
|
return processor;
|
2014-09-23 16:50:21 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 16:46:10 +00:00
|
|
|
private:
|
|
|
|
/**
|
2014-10-09 20:12:35 +00:00
|
|
|
* A test function which verifies the count of factories.
|
|
|
|
*/
|
|
|
|
static void VerifyFactoryCount();
|
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
MakeProc fMakeProc;
|
2014-09-23 16:50:21 +00:00
|
|
|
|
2014-10-09 20:12:35 +00:00
|
|
|
static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
|
2014-09-23 16:50:21 +00:00
|
|
|
};
|
|
|
|
|
2017-01-09 16:46:10 +00:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
/** GrProcessor subclasses should insert this macro in their declaration to be included in the
|
|
|
|
* program generation unit test.
|
|
|
|
*/
|
|
|
|
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
|
2014-10-09 20:12:35 +00:00
|
|
|
static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \
|
2016-06-09 15:01:03 +00:00
|
|
|
static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
|
2014-09-23 16:50:21 +00:00
|
|
|
|
|
|
|
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
|
2014-10-09 20:12:35 +00:00
|
|
|
static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \
|
2016-06-09 15:01:03 +00:00
|
|
|
static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
|
2014-09-23 16:50:21 +00:00
|
|
|
|
2014-12-11 21:15:13 +00:00
|
|
|
#define GR_DECLARE_XP_FACTORY_TEST \
|
2017-01-09 16:46:10 +00:00
|
|
|
static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \
|
|
|
|
static const GrXPFactory* TestGet(GrProcessorTestData*)
|
2014-12-11 21:15:13 +00:00
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
/** GrProcessor subclasses should insert this macro in their implementation file. They must then
|
|
|
|
* also implement this static function:
|
2015-07-08 21:26:19 +00:00
|
|
|
* GrProcessor* TestCreate(GrProcessorTestData*);
|
2014-09-23 16:50:21 +00:00
|
|
|
*/
|
|
|
|
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
|
2017-01-09 16:46:10 +00:00
|
|
|
GrProcessorTestFactory<GrFragmentProcessor> Effect::gTestFactory(Effect::TestCreate)
|
2017-01-09 16:00:33 +00:00
|
|
|
|
|
|
|
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
|
2017-01-09 16:46:10 +00:00
|
|
|
GrProcessorTestFactory<GrGeometryProcessor> Effect::gTestFactory(Effect::TestCreate)
|
|
|
|
|
|
|
|
#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
|
|
|
|
GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet)
|
2014-09-23 16:50:21 +00:00
|
|
|
|
|
|
|
#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 \
|
2016-06-09 15:01:03 +00:00
|
|
|
static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
|
2014-09-23 16:50:21 +00:00
|
|
|
#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 \
|
2016-06-09 15:01:03 +00:00
|
|
|
static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
|
2014-09-23 16:50:21 +00:00
|
|
|
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
|
|
|
|
|
2017-01-09 16:46:10 +00:00
|
|
|
// 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)
|
|
|
|
|
2017-01-31 18:50:44 +00:00
|
|
|
#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
|
|
|
|
#else // GR_TEST_UTILS
|
|
|
|
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
|
|
|
|
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
|
|
|
|
#define GR_DECLARE_XP_FACTORY_TEST
|
|
|
|
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
|
|
|
|
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
|
|
|
|
#define GR_DEFINE_XP_FACTORY_TEST(...)
|
|
|
|
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
|
|
|
|
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
|
|
|
|
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
|
|
|
|
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
|
|
|
|
#define GR_DECLARE_XP_FACTORY_TEST
|
|
|
|
#define GR_DEFINE_XP_FACTORY_TEST(...)
|
|
|
|
#endif // GR_TEST_UTILS
|
|
|
|
#endif // GrProcessorUnitTest_DEFINED
|