2012-05-18 19:54:48 +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.
|
|
|
|
*/
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
#ifndef GrBackendProcessorFactory_DEFINED
|
|
|
|
#define GrBackendProcessorFactory_DEFINED
|
2012-05-18 19:54:48 +00:00
|
|
|
|
|
|
|
#include "GrTypes.h"
|
2012-05-18 20:06:45 +00:00
|
|
|
#include "SkTemplates.h"
|
2014-05-28 20:02:17 +00:00
|
|
|
#include "SkThread.h"
|
2013-09-18 13:00:55 +00:00
|
|
|
#include "SkTypes.h"
|
2014-07-11 17:01:02 +00:00
|
|
|
#include "SkTArray.h"
|
2012-05-18 19:54:48 +00:00
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
class GrGLProcessor;
|
2012-08-02 15:15:16 +00:00
|
|
|
class GrGLCaps;
|
2014-09-23 16:50:21 +00:00
|
|
|
class GrProcessor;
|
2012-05-18 19:54:48 +00:00
|
|
|
|
2014-07-11 17:01:02 +00:00
|
|
|
/**
|
2014-10-15 18:25:21 +00:00
|
|
|
* Used by processors to build their keys. It incorporates each per-processor key into a larger shader
|
|
|
|
* key.
|
2014-07-11 17:01:02 +00:00
|
|
|
*/
|
2014-09-23 16:50:21 +00:00
|
|
|
class GrProcessorKeyBuilder {
|
2014-07-11 17:01:02 +00:00
|
|
|
public:
|
2014-09-23 16:50:21 +00:00
|
|
|
GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
|
2014-07-11 17:01:02 +00:00
|
|
|
SkASSERT(0 == fData->count() % sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void add32(uint32_t v) {
|
|
|
|
++fCount;
|
|
|
|
fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:55:11 +00:00
|
|
|
/** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
|
|
|
|
add*() call. */
|
|
|
|
uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
|
|
|
|
SkASSERT(count > 0);
|
|
|
|
fCount += count;
|
|
|
|
return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
|
|
|
|
}
|
|
|
|
|
2014-07-11 17:01:02 +00:00
|
|
|
size_t size() const { return sizeof(uint32_t) * fCount; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
|
2014-10-15 18:25:21 +00:00
|
|
|
int fCount; // number of uint32_ts added to fData by the processor.
|
2014-07-11 17:01:02 +00:00
|
|
|
};
|
|
|
|
|
2014-07-21 15:03:14 +00:00
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* This class is used to pass the key that was created for a GrGLProcessor back to it
|
2014-07-21 15:03:14 +00:00
|
|
|
* when it emits code. It may allow the emit step to skip calculations that were
|
|
|
|
* performed when computing the key.
|
|
|
|
*/
|
2014-09-23 16:50:21 +00:00
|
|
|
class GrProcessorKey {
|
2014-07-21 15:03:14 +00:00
|
|
|
public:
|
2014-09-23 16:50:21 +00:00
|
|
|
GrProcessorKey(const uint32_t* key, int count) : fKey(key), fCount(count) {
|
2014-07-21 15:03:14 +00:00
|
|
|
SkASSERT(0 == reinterpret_cast<intptr_t>(key) % sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
2014-10-15 18:25:21 +00:00
|
|
|
/** Gets the uint32_t values that the processor inserted into the key. */
|
2014-07-21 15:03:14 +00:00
|
|
|
uint32_t get32(int index) const {
|
|
|
|
SkASSERT(index >=0 && index < fCount);
|
|
|
|
return fKey[index];
|
|
|
|
}
|
|
|
|
|
2014-10-15 18:25:21 +00:00
|
|
|
/** Gets the number of uint32_t values that the processor inserted into the key. */
|
2014-07-21 15:03:14 +00:00
|
|
|
int count32() const { return fCount; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const uint32_t* fKey; // unowned ptr into the larger key.
|
2014-10-15 18:25:21 +00:00
|
|
|
int fCount; // number of uint32_ts inserted by the processor into its key.
|
2014-07-21 15:03:14 +00:00
|
|
|
};
|
|
|
|
|
2014-07-17 14:55:11 +00:00
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* Given a GrProcessor of a particular type, creates the corresponding graphics-backend-specific
|
2014-10-15 18:25:21 +00:00
|
|
|
* processor object. It also tracks equivalence of shaders generated via a key. The factory for an
|
|
|
|
* processor is accessed via GrProcessor::getFactory(). Each factory instance is assigned an ID at
|
2014-09-23 16:50:21 +00:00
|
|
|
* construction. The ID of GrProcessor::getFactory() is used as a type identifier. Thus, a
|
|
|
|
* GrProcessor subclass must always return the same object from getFactory() and that factory object
|
|
|
|
* must be unique to the GrProcessor subclass (and unique from any further derived subclasses).
|
2014-07-17 14:55:11 +00:00
|
|
|
*
|
2014-09-23 16:50:21 +00:00
|
|
|
* Rather than subclassing this class themselves, it is recommended that GrProcessor authors use
|
2014-10-15 18:25:21 +00:00
|
|
|
* the templated subclass GrTBackendProcessorFactory by writing their getFactory() method as:
|
2014-07-17 14:55:11 +00:00
|
|
|
*
|
2014-10-15 18:25:21 +00:00
|
|
|
* const GrBackendProcessorFactory& MyProcessor::getFactory() const {
|
|
|
|
* return GrTBackendProcessorFactory<MyProcessor>::getInstance();
|
2014-07-17 14:55:11 +00:00
|
|
|
* }
|
|
|
|
*
|
2014-10-15 18:25:21 +00:00
|
|
|
* Using GrTBackendProcessorFactory places a few constraints on the processor. See that class's
|
|
|
|
* comments.
|
2014-07-17 14:55:11 +00:00
|
|
|
*/
|
2014-09-23 16:50:21 +00:00
|
|
|
class GrBackendProcessorFactory : SkNoncopyable {
|
2012-05-18 19:54:48 +00:00
|
|
|
public:
|
2014-07-17 14:55:11 +00:00
|
|
|
/**
|
2014-10-15 18:25:21 +00:00
|
|
|
* Generates an processor's key. The key is based on the aspects of the GrProcessor object's
|
2014-09-23 16:50:21 +00:00
|
|
|
* configuration that affect GLSL code generation. Two GrProcessor instances that would cause
|
2014-07-17 14:55:11 +00:00
|
|
|
* this->createGLInstance()->emitCode() to produce different code must produce different keys.
|
|
|
|
*/
|
2014-09-23 16:50:21 +00:00
|
|
|
virtual void getGLProcessorKey(const GrProcessor&, const GrGLCaps&,
|
|
|
|
GrProcessorKeyBuilder*) const = 0;
|
2012-05-18 19:54:48 +00:00
|
|
|
|
2014-07-17 14:55:11 +00:00
|
|
|
/**
|
2014-10-15 18:25:21 +00:00
|
|
|
* Produces a human-reable name for the v.
|
2014-07-17 14:55:11 +00:00
|
|
|
*/
|
Revert of Makes GrGLProgramDesc's key store the lengths as well as offsets of the effect keys. (https://codereview.chromium.org/379113004/)
Reason for revert:
Most likely candidate for Valgrind failures:
[21:10:08.755668] ==3036== Use of uninitialised value of size 8
[21:10:08.755753] ==3036== at 0x734AB2: GrGpuGL::ProgramCache::getProgram(GrGLProgramDesc const&, GrEffectStage const**, GrEffectStage const**) (GrGpuGL_program.cpp:107)
[21:10:08.755788] ==3036== by 0x734ED2: GrGpuGL::flushGraphicsState(GrGpu::DrawType, GrDeviceCoordTexture const*) (GrGpuGL_program.cpp:253)
[21:10:08.755811] ==3036== by 0x6E81C2: GrGpu::setupClipAndFlushState(GrGpu::DrawType, GrDeviceCoordTexture const*, GrDrawState::AutoRestoreEffects*, SkRect const*) (GrGpu.cpp:350)
[21:10:08.755837] ==3036== by 0x6E9BE8: GrGpu::onDraw(GrDrawTarget::DrawInfo const&) (GrGpu.cpp:390)
[21:10:08.755858] ==3036== by 0x6EEECE: GrInOrderDrawBuffer::flush() (GrDrawTarget.h:506)
[21:10:08.755879] ==3036== by 0x6D0EB4: GrContext::flush(int) (GrContext.cpp:1327)
[21:10:08.755900] ==3036== by 0x6D3F8F: GrContext::writeTexturePixels(GrTexture*, int, int, int, int, GrPixelConfig, void const*, unsigned long, unsigned int) (GrContext.cpp:1349)
[21:10:08.755922] ==3036== by 0x6D39D7: GrContext::writeRenderTargetPixels(GrRenderTarget*, int, int, int, int, GrPixelConfig, void const*, unsigned long, unsigned int) (GrContext.cpp:1632)
[21:10:08.755949] ==3036== by 0x6FFDF3: GrRenderTarget::writePixels(int, int, int, int, GrPixelConfig, void const*, unsigned long, unsigned int) (GrRenderTarget.cpp:45)
[21:10:08.755978] ==3036== by 0x735563: SkGpuDevice::onWritePixels(SkImageInfo const&, void const*, unsigned long, int, int) (SkGpuDevice.cpp:280)
[21:10:08.756003] ==3036== by 0x57A048: SkBaseDevice::writePixels(SkImageInfo const&, void const*, unsigned long, int, int) (SkDevice.cpp:106)
[21:10:08.756025] ==3036== by 0x56D0AE: SkCanvas::writePixels(SkImageInfo const&, void const*, unsigned long, int, int) (SkCanvas.cpp:700)
[21:10:08.756050] ==3036== by 0x56D156: SkCanvas::writePixels(SkBitmap const&, int, int) (SkCanvas.cpp:652)
[21:10:08.756077] ==3036== by 0x5109B6: test_WritePixels(skiatest::Reporter*, GrContextFactory*) (WritePixelsTest.cpp:464)
[21:10:08.756099] ==3036== by 0x51114C: skiatest::WritePixelsClass::onRun(skiatest::Reporter*) (WritePixelsTest.cpp:361)
[21:10:08.756122] ==3036== by 0x406BE8: skiatest::Test::run() (Test.cpp:107)
[21:10:08.756145] ==3036== by 0x4064C2: SkTestRunnable::run() (skia_test.cpp:109)
[21:10:08.756167] ==3036== by 0x405D1A: tool_main(int, char**) (skia_test.cpp:221)
[21:10:08.756189] ==3036== by 0x405F75: main (skia_test.cpp:239)
[21:10:08.756211] ==3036== Uninitialised value was created by a stack allocation
[21:10:08.756233] ==3036== at 0x734CC8: GrGpuGL::flushGraphicsState(GrGpu::DrawType, GrDeviceCoordTexture const*) (GrGpuGL_program.cpp:213)
Original issue's description:
> Makes GrGLProgramDesc's key store the lengths as well as offsets of the effect keys.
>
> Makes it possible to use GrBackendEffectFactories other than GrTBEF by moving meta-key generation out of GrTBEF.
>
> Cleans up docs around GrBackendEffectFactory.
>
> Committed: https://skia.googlesource.com/skia/+/c0ea398aff8254e31152cbb94c9ab6150428e252
R=robertphillips@google.com, jvanverth@google.com, bsalomon@google.com
TBR=bsalomon@google.com, jvanverth@google.com, robertphillips@google.com
NOTREECHECKS=true
NOTRY=true
Author: mtklein@google.com
Review URL: https://codereview.chromium.org/394213002
2014-07-16 13:16:43 +00:00
|
|
|
virtual const char* name() const = 0;
|
2014-07-16 02:41:17 +00:00
|
|
|
|
2014-07-17 14:55:11 +00:00
|
|
|
/**
|
|
|
|
* A unique value for every instance of this factory. It is automatically incorporated into the
|
2014-10-15 18:25:21 +00:00
|
|
|
* processor's key. This allows keys generated by getGLProcessorKey() to only be unique within a
|
2014-09-23 16:50:21 +00:00
|
|
|
* GrProcessor subclass and not necessarily across subclasses.
|
2014-07-17 14:55:11 +00:00
|
|
|
*/
|
2014-10-16 01:34:46 +00:00
|
|
|
uint32_t classID() const { return fProcessorClassID; }
|
2014-07-17 14:55:11 +00:00
|
|
|
|
2012-05-18 19:54:48 +00:00
|
|
|
protected:
|
2014-10-16 01:34:46 +00:00
|
|
|
GrBackendProcessorFactory() : fProcessorClassID(GenClassID()) {}
|
2014-09-23 16:50:21 +00:00
|
|
|
virtual ~GrBackendProcessorFactory() {}
|
2014-07-17 14:55:11 +00:00
|
|
|
|
|
|
|
private:
|
2012-05-18 19:54:48 +00:00
|
|
|
enum {
|
2014-10-16 01:34:46 +00:00
|
|
|
kIllegalProcessorClassID = 0,
|
2012-05-18 19:54:48 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 01:34:46 +00:00
|
|
|
static uint32_t GenClassID() {
|
|
|
|
// fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
|
2012-05-18 19:54:48 +00:00
|
|
|
// atomic inc returns the old value not the incremented value. So we add
|
|
|
|
// 1 to the returned value.
|
2014-10-16 01:34:46 +00:00
|
|
|
uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrProcessorClassID)) + 1;
|
2014-07-17 14:55:11 +00:00
|
|
|
if (!id) {
|
2014-09-23 16:50:21 +00:00
|
|
|
SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
|
2014-07-17 14:55:11 +00:00
|
|
|
"subclass.");
|
|
|
|
}
|
2014-07-11 17:01:02 +00:00
|
|
|
return id;
|
2012-05-18 19:54:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:34:46 +00:00
|
|
|
const uint32_t fProcessorClassID;
|
|
|
|
static int32_t fCurrProcessorClassID;
|
2012-05-18 19:54:48 +00:00
|
|
|
};
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
class GrFragmentProcessor;
|
|
|
|
class GrGeometryProcessor;
|
|
|
|
class GrGLFragmentProcessor;
|
|
|
|
class GrGLGeometryProcessor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend processor factory cannot actually create anything, it is up to subclasses to implement
|
|
|
|
* a create binding which matches Gr to GL in a type safe way
|
|
|
|
*/
|
|
|
|
|
|
|
|
class GrBackendFragmentProcessorFactory : public GrBackendProcessorFactory {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates a GrGLProcessor instance that is used both to generate code for the GrProcessor in a
|
|
|
|
* GLSL program and to manage updating uniforms for the program when it is used.
|
|
|
|
*/
|
|
|
|
virtual GrGLFragmentProcessor* createGLInstance(const GrFragmentProcessor&) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GrBackendGeometryProcessorFactory : public GrBackendProcessorFactory {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates a GrGLProcessor instance that is used both to generate code for the GrProcessor in a
|
|
|
|
* GLSL program and to manage updating uniforms for the program when it is used.
|
|
|
|
*/
|
|
|
|
virtual GrGLGeometryProcessor* createGLInstance(const GrGeometryProcessor&) const = 0;
|
|
|
|
};
|
|
|
|
|
2012-05-18 19:54:48 +00:00
|
|
|
#endif
|