blob: e6362520fc2b2e3b59e43b18343dfd86ced7bfd9 [file] [log] [blame]
bsalomon@google.comae4f96a2012-05-18 19:54:48 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon@google.com396e61f2012-10-25 19:00:29 +00008#ifndef GrBackendEffectFactory_DEFINED
9#define GrBackendEffectFactory_DEFINED
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000010
11#include "GrTypes.h"
bsalomon@google.com8e520fc2012-05-18 20:06:45 +000012#include "SkTemplates.h"
commit-bot@chromium.org86b0de42014-05-28 20:02:17 +000013#include "SkThread.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000014#include "SkTypes.h"
bsalomon848faf02014-07-11 10:01:02 -070015#include "SkTArray.h"
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000016
bsalomon@google.comd698f772012-10-25 13:22:00 +000017class GrGLEffect;
twiz@google.coma5e65ec2012-08-02 15:15:16 +000018class GrGLCaps;
bsalomon@google.comc7818882013-03-20 19:19:53 +000019class GrDrawEffect;
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000020
bsalomon848faf02014-07-11 10:01:02 -070021/**
bsalomon929f29a2014-07-17 07:55:11 -070022 * Used by effects to build their keys. It incorporates each per-effect key into a larger shader key.
bsalomon848faf02014-07-11 10:01:02 -070023 */
24class GrEffectKeyBuilder {
25public:
26 GrEffectKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
27 SkASSERT(0 == fData->count() % sizeof(uint32_t));
28 }
29
30 void add32(uint32_t v) {
31 ++fCount;
32 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
33 }
34
bsalomon929f29a2014-07-17 07:55:11 -070035 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
36 add*() call. */
37 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
38 SkASSERT(count > 0);
39 fCount += count;
40 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
41 }
42
bsalomon848faf02014-07-11 10:01:02 -070043 size_t size() const { return sizeof(uint32_t) * fCount; }
44
45private:
46 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
47 int fCount; // number of uint32_ts added to fData by the effect.
48};
49
bsalomon929f29a2014-07-17 07:55:11 -070050/**
bsalomon63e99f72014-07-21 08:03:14 -070051 * This class is used to pass the key that was created for a GrGLEffect back to it
52 * when it emits code. It may allow the emit step to skip calculations that were
53 * performed when computing the key.
54 */
55class GrEffectKey {
56public:
57 GrEffectKey(const uint32_t* key, int count) : fKey(key), fCount(count) {
58 SkASSERT(0 == reinterpret_cast<intptr_t>(key) % sizeof(uint32_t));
59 }
60
61 /** Gets the uint32_t values that the effect inserted into the key. */
62 uint32_t get32(int index) const {
63 SkASSERT(index >=0 && index < fCount);
64 return fKey[index];
65 }
66
67 /** Gets the number of uint32_t values that the effect inserted into the key. */
68 int count32() const { return fCount; }
69
70private:
71 const uint32_t* fKey; // unowned ptr into the larger key.
72 int fCount; // number of uint32_ts inserted by the effect into its key.
73};
74
75/**
bsalomon929f29a2014-07-17 07:55:11 -070076 * Given a GrEffect of a particular type, creates the corresponding graphics-backend-specific
77 * effect object. It also tracks equivalence of shaders generated via a key. The factory for an
78 * effect is accessed via GrEffect::getFactory(). Each factory instance is assigned an ID at
79 * construction. The ID of GrEffect::getFactory() is used as a type identifier. Thus, a GrEffect
80 * subclass must always return the same object from getFactory() and that factory object must be
81 * unique to the GrEffect subclass (and unique from any further derived subclasses).
82 *
83 * Rather than subclassing this class themselves, it is recommended that GrEffect authors use
84 * the templated subclass GrTBackendEffectFactory by writing their getFactory() method as:
85 *
86 * const GrBackendEffectFactory& MyEffect::getFactory() const {
87 * return GrTBackendEffectFactory<MyEffect>::getInstance();
88 * }
89 *
90 * Using GrTBackendEffectFactory places a few constraints on the effect. See that class's comments.
91 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000092class GrBackendEffectFactory : SkNoncopyable {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000093public:
bsalomon929f29a2014-07-17 07:55:11 -070094 /**
95 * Generates an effect's key. The key is based on the aspects of the GrEffect object's
96 * configuration that affect GLSL code generation. Two GrEffect instances that would cause
97 * this->createGLInstance()->emitCode() to produce different code must produce different keys.
98 */
99 virtual void getGLEffectKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*) const = 0;
100
101 /**
102 * Creates a GrGLEffect instance that is used both to generate code for the GrEffect in a GLSL
103 * program and to manage updating uniforms for the program when it is used.
104 */
bsalomon@google.comc7818882013-03-20 19:19:53 +0000105 virtual GrGLEffect* createGLInstance(const GrDrawEffect&) const = 0;
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000106
bsalomon929f29a2014-07-17 07:55:11 -0700107 /**
108 * Produces a human-reable name for the effect.
109 */
bsalomon@google.com289efe02012-05-21 20:57:59 +0000110 virtual const char* name() const = 0;
111
bsalomon929f29a2014-07-17 07:55:11 -0700112 /**
113 * A unique value for every instance of this factory. It is automatically incorporated into the
114 * effect's key. This allows keys generated by getGLEffectKey() to only be unique within a
115 * GrEffect subclass and not necessarily across subclasses.
116 */
117 uint32_t effectClassID() const { return fEffectClassID; }
118
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000119protected:
bsalomon929f29a2014-07-17 07:55:11 -0700120 GrBackendEffectFactory() : fEffectClassID(GenID()) {}
121 virtual ~GrBackendEffectFactory() {}
122
123private:
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000124 enum {
bsalomon@google.com021fc732012-10-25 12:47:42 +0000125 kIllegalEffectClassID = 0,
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000126 };
127
bsalomon929f29a2014-07-17 07:55:11 -0700128 static uint32_t GenID() {
bsalomon@google.com021fc732012-10-25 12:47:42 +0000129 // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000130 // atomic inc returns the old value not the incremented value. So we add
131 // 1 to the returned value.
bsalomon929f29a2014-07-17 07:55:11 -0700132 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrEffectClassID)) + 1;
133 if (!id) {
134 SkFAIL("This should never wrap as it should only be called once for each GrEffect "
135 "subclass.");
136 }
bsalomon848faf02014-07-11 10:01:02 -0700137 return id;
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000138 }
139
bsalomon929f29a2014-07-17 07:55:11 -0700140 const uint32_t fEffectClassID;
bsalomon@google.com021fc732012-10-25 12:47:42 +0000141 static int32_t fCurrEffectClassID;
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000142};
143
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000144#endif