blob: 3e4f13314756734cfb5c7957a1db0f969e9da2e9 [file] [log] [blame]
joshualittb0a8a372014-09-23 09:50:21 -07001/*
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
8#ifndef GrBackendProcessorFactory_DEFINED
9#define GrBackendProcessorFactory_DEFINED
10
11#include "GrTypes.h"
12#include "SkTemplates.h"
13#include "SkThread.h"
14#include "SkTypes.h"
15#include "SkTArray.h"
16
17class GrGLProcessor;
18class GrGLCaps;
19class GrProcessor;
20
21/**
bsalomonb762cb52014-10-15 11:25:21 -070022 * Used by processors to build their keys. It incorporates each per-processor key into a larger shader
23 * key.
joshualittb0a8a372014-09-23 09:50:21 -070024 */
25class GrProcessorKeyBuilder {
26public:
27 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
28 SkASSERT(0 == fData->count() % sizeof(uint32_t));
29 }
30
31 void add32(uint32_t v) {
32 ++fCount;
33 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
34 }
35
36 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
37 add*() call. */
38 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
39 SkASSERT(count > 0);
40 fCount += count;
41 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
42 }
43
44 size_t size() const { return sizeof(uint32_t) * fCount; }
45
46private:
47 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
bsalomonb762cb52014-10-15 11:25:21 -070048 int fCount; // number of uint32_ts added to fData by the processor.
joshualittb0a8a372014-09-23 09:50:21 -070049};
50
51/**
52 * This class is used to pass the key that was created for a GrGLProcessor back to it
53 * when it emits code. It may allow the emit step to skip calculations that were
54 * performed when computing the key.
55 */
56class GrProcessorKey {
57public:
58 GrProcessorKey(const uint32_t* key, int count) : fKey(key), fCount(count) {
59 SkASSERT(0 == reinterpret_cast<intptr_t>(key) % sizeof(uint32_t));
60 }
61
bsalomonb762cb52014-10-15 11:25:21 -070062 /** Gets the uint32_t values that the processor inserted into the key. */
joshualittb0a8a372014-09-23 09:50:21 -070063 uint32_t get32(int index) const {
64 SkASSERT(index >=0 && index < fCount);
65 return fKey[index];
66 }
67
bsalomonb762cb52014-10-15 11:25:21 -070068 /** Gets the number of uint32_t values that the processor inserted into the key. */
joshualittb0a8a372014-09-23 09:50:21 -070069 int count32() const { return fCount; }
70
71private:
72 const uint32_t* fKey; // unowned ptr into the larger key.
bsalomonb762cb52014-10-15 11:25:21 -070073 int fCount; // number of uint32_ts inserted by the processor into its key.
joshualittb0a8a372014-09-23 09:50:21 -070074};
75
76/**
77 * Given a GrProcessor of a particular type, creates the corresponding graphics-backend-specific
bsalomonb762cb52014-10-15 11:25:21 -070078 * processor object. It also tracks equivalence of shaders generated via a key. The factory for an
79 * processor is accessed via GrProcessor::getFactory(). Each factory instance is assigned an ID at
joshualittb0a8a372014-09-23 09:50:21 -070080 * construction. The ID of GrProcessor::getFactory() is used as a type identifier. Thus, a
81 * GrProcessor subclass must always return the same object from getFactory() and that factory object
82 * must be unique to the GrProcessor subclass (and unique from any further derived subclasses).
83 *
84 * Rather than subclassing this class themselves, it is recommended that GrProcessor authors use
bsalomonb762cb52014-10-15 11:25:21 -070085 * the templated subclass GrTBackendProcessorFactory by writing their getFactory() method as:
joshualittb0a8a372014-09-23 09:50:21 -070086 *
bsalomonb762cb52014-10-15 11:25:21 -070087 * const GrBackendProcessorFactory& MyProcessor::getFactory() const {
88 * return GrTBackendProcessorFactory<MyProcessor>::getInstance();
joshualittb0a8a372014-09-23 09:50:21 -070089 * }
90 *
bsalomonb762cb52014-10-15 11:25:21 -070091 * Using GrTBackendProcessorFactory places a few constraints on the processor. See that class's
92 * comments.
joshualittb0a8a372014-09-23 09:50:21 -070093 */
94class GrBackendProcessorFactory : SkNoncopyable {
95public:
96 /**
bsalomonb762cb52014-10-15 11:25:21 -070097 * Generates an processor's key. The key is based on the aspects of the GrProcessor object's
joshualittb0a8a372014-09-23 09:50:21 -070098 * configuration that affect GLSL code generation. Two GrProcessor instances that would cause
99 * this->createGLInstance()->emitCode() to produce different code must produce different keys.
100 */
101 virtual void getGLProcessorKey(const GrProcessor&, const GrGLCaps&,
102 GrProcessorKeyBuilder*) const = 0;
103
104 /**
bsalomonb762cb52014-10-15 11:25:21 -0700105 * Produces a human-reable name for the v.
joshualittb0a8a372014-09-23 09:50:21 -0700106 */
107 virtual const char* name() const = 0;
108
109 /**
110 * A unique value for every instance of this factory. It is automatically incorporated into the
bsalomonb762cb52014-10-15 11:25:21 -0700111 * processor's key. This allows keys generated by getGLProcessorKey() to only be unique within a
joshualittb0a8a372014-09-23 09:50:21 -0700112 * GrProcessor subclass and not necessarily across subclasses.
113 */
bsalomonf2765412014-10-15 18:34:46 -0700114 uint32_t classID() const { return fProcessorClassID; }
joshualittb0a8a372014-09-23 09:50:21 -0700115
116protected:
bsalomonf2765412014-10-15 18:34:46 -0700117 GrBackendProcessorFactory() : fProcessorClassID(GenClassID()) {}
joshualittb0a8a372014-09-23 09:50:21 -0700118 virtual ~GrBackendProcessorFactory() {}
119
120private:
121 enum {
bsalomonf2765412014-10-15 18:34:46 -0700122 kIllegalProcessorClassID = 0,
joshualittb0a8a372014-09-23 09:50:21 -0700123 };
124
bsalomonf2765412014-10-15 18:34:46 -0700125 static uint32_t GenClassID() {
126 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
joshualittb0a8a372014-09-23 09:50:21 -0700127 // atomic inc returns the old value not the incremented value. So we add
128 // 1 to the returned value.
bsalomonf2765412014-10-15 18:34:46 -0700129 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrProcessorClassID)) + 1;
joshualittb0a8a372014-09-23 09:50:21 -0700130 if (!id) {
131 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
132 "subclass.");
133 }
134 return id;
135 }
136
bsalomonf2765412014-10-15 18:34:46 -0700137 const uint32_t fProcessorClassID;
138 static int32_t fCurrProcessorClassID;
joshualittb0a8a372014-09-23 09:50:21 -0700139};
140
141class GrFragmentProcessor;
142class GrGeometryProcessor;
143class GrGLFragmentProcessor;
144class GrGLGeometryProcessor;
145
146/**
147 * Backend processor factory cannot actually create anything, it is up to subclasses to implement
148 * a create binding which matches Gr to GL in a type safe way
149 */
150
151class GrBackendFragmentProcessorFactory : public GrBackendProcessorFactory {
152public:
153 /**
154 * Creates a GrGLProcessor instance that is used both to generate code for the GrProcessor in a
155 * GLSL program and to manage updating uniforms for the program when it is used.
156 */
157 virtual GrGLFragmentProcessor* createGLInstance(const GrFragmentProcessor&) const = 0;
158};
159
160class GrBackendGeometryProcessorFactory : public GrBackendProcessorFactory {
161public:
162 /**
163 * Creates a GrGLProcessor instance that is used both to generate code for the GrProcessor in a
164 * GLSL program and to manage updating uniforms for the program when it is used.
165 */
166 virtual GrGLGeometryProcessor* createGLInstance(const GrGeometryProcessor&) const = 0;
167};
168
169#endif