blob: 8f5304d7ce6137312a47e75f5101d6d8d58a370f [file] [log] [blame]
Romain Guyac670c02010-07-27 17:39:27 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_UI_PROGRAM_CACHE_H
18#define ANDROID_UI_PROGRAM_CACHE_H
19
20#include <utils/KeyedVector.h>
21#include <utils/Log.h>
Romain Guy06f96e22010-07-30 19:18:16 -070022#include <utils/String8.h>
Romain Guyac670c02010-07-27 17:39:27 -070023
Romain Guy889f8d12010-07-29 14:37:42 -070024#include <GLES2/gl2.h>
25
Romain Guyac670c02010-07-27 17:39:27 -070026#include <SkXfermode.h>
27
28#include "Program.h"
29
30namespace android {
31namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
37// Debug
Romain Guya5aed0d2010-09-09 14:42:43 -070038#define DEBUG_PROGRAM_CACHE 1
Romain Guyac670c02010-07-27 17:39:27 -070039
40// Debug
41#if DEBUG_PROGRAM_CACHE
42 #define PROGRAM_LOGD(...) LOGD(__VA_ARGS__)
43#else
44 #define PROGRAM_LOGD(...)
45#endif
46
47#define PROGRAM_KEY_TEXTURE 0x1
48#define PROGRAM_KEY_A8_TEXTURE 0x2
49#define PROGRAM_KEY_BITMAP 0x4
50#define PROGRAM_KEY_GRADIENT 0x8
51#define PROGRAM_KEY_BITMAP_FIRST 0x10
52#define PROGRAM_KEY_COLOR_MATRIX 0x20
53#define PROGRAM_KEY_COLOR_LIGHTING 0x40
54#define PROGRAM_KEY_COLOR_BLEND 0x80
Romain Guy889f8d12010-07-29 14:37:42 -070055#define PROGRAM_KEY_BITMAP_NPOT 0x100
56
57#define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600
58#define PROGRAM_KEY_BITMAP_WRAPT_MASK 0x1800
Romain Guyac670c02010-07-27 17:39:27 -070059
Romain Guy48daa542010-08-10 19:21:34 -070060// Encode the xfermodes on 6 bits
61#define PROGRAM_MAX_XFERMODE 0x1f
62#define PROGRAM_XFERMODE_SHADER_SHIFT 26
Romain Guyac670c02010-07-27 17:39:27 -070063#define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20
Romain Guya5aed0d2010-09-09 14:42:43 -070064#define PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT 14
Romain Guyac670c02010-07-27 17:39:27 -070065
Romain Guy889f8d12010-07-29 14:37:42 -070066#define PROGRAM_BITMAP_WRAPS_SHIFT 9
67#define PROGRAM_BITMAP_WRAPT_SHIFT 11
68
Romain Guyac670c02010-07-27 17:39:27 -070069///////////////////////////////////////////////////////////////////////////////
70// Types
71///////////////////////////////////////////////////////////////////////////////
72
73typedef uint32_t programid;
74
75///////////////////////////////////////////////////////////////////////////////
76// Cache
77///////////////////////////////////////////////////////////////////////////////
78
79/**
80 * Describe the features required for a given program. The features
81 * determine the generation of both the vertex and fragment shaders.
82 * A ProgramDescription must be used in conjunction with a ProgramCache.
83 */
84struct ProgramDescription {
85 enum ColorModifier {
86 kColorNone,
87 kColorMatrix,
88 kColorLighting,
89 kColorBlend
90 };
91
92 ProgramDescription():
93 hasTexture(false), hasAlpha8Texture(false),
Romain Guy889f8d12010-07-29 14:37:42 -070094 hasBitmap(false), isBitmapNpot(false), hasGradient(false),
95 shadersMode(SkXfermode::kClear_Mode), isBitmapFirst(false),
96 bitmapWrapS(GL_CLAMP_TO_EDGE), bitmapWrapT(GL_CLAMP_TO_EDGE),
Romain Guya5aed0d2010-09-09 14:42:43 -070097 colorOp(kColorNone), colorMode(SkXfermode::kClear_Mode),
98 framebufferMode(SkXfermode::kClear_Mode) {
Romain Guyac670c02010-07-27 17:39:27 -070099 }
100
101 // Texturing
102 bool hasTexture;
103 bool hasAlpha8Texture;
104
105 // Shaders
106 bool hasBitmap;
Romain Guy889f8d12010-07-29 14:37:42 -0700107 bool isBitmapNpot;
Romain Guyac670c02010-07-27 17:39:27 -0700108 bool hasGradient;
109 SkXfermode::Mode shadersMode;
110 bool isBitmapFirst;
Romain Guy889f8d12010-07-29 14:37:42 -0700111 GLenum bitmapWrapS;
112 GLenum bitmapWrapT;
Romain Guyac670c02010-07-27 17:39:27 -0700113
114 // Color operations
115 int colorOp;
116 SkXfermode::Mode colorMode;
117
Romain Guya5aed0d2010-09-09 14:42:43 -0700118 // Framebuffer blending (requires Extensions.hasFramebufferFetch())
119 // Ignored for all values < SkXfermode::kPlus_Mode
120 SkXfermode::Mode framebufferMode;
121
Romain Guy889f8d12010-07-29 14:37:42 -0700122 inline uint32_t getEnumForWrap(GLenum wrap) const {
123 switch (wrap) {
124 case GL_CLAMP_TO_EDGE:
125 return 0;
126 case GL_REPEAT:
127 return 1;
128 case GL_MIRRORED_REPEAT:
129 return 2;
130 }
131 return 0;
132 }
133
Romain Guyac670c02010-07-27 17:39:27 -0700134 programid key() const {
135 programid key = 0;
136 if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
137 if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
Romain Guy889f8d12010-07-29 14:37:42 -0700138 if (hasBitmap) {
139 key |= PROGRAM_KEY_BITMAP;
140 if (isBitmapNpot) {
141 key |= PROGRAM_KEY_BITMAP_NPOT;
142 key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT;
143 key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT;
144 }
145 }
Romain Guyac670c02010-07-27 17:39:27 -0700146 if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
147 if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST;
148 if (hasBitmap && hasGradient) {
149 key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
150 }
151 switch (colorOp) {
152 case kColorMatrix:
153 key |= PROGRAM_KEY_COLOR_MATRIX;
154 break;
155 case kColorLighting:
156 key |= PROGRAM_KEY_COLOR_LIGHTING;
157 break;
158 case kColorBlend:
159 key |= PROGRAM_KEY_COLOR_BLEND;
160 key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
161 break;
162 case kColorNone:
163 break;
164 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700165 key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT;
Romain Guyac670c02010-07-27 17:39:27 -0700166 return key;
167 }
168}; // struct ProgramDescription
169
170/**
171 * Generates and caches program. Programs are generated based on
172 * ProgramDescriptions.
173 */
174class ProgramCache {
175public:
176 ProgramCache();
177 ~ProgramCache();
178
179 Program* get(const ProgramDescription& description);
180
181 void clear();
182
183private:
184 Program* generateProgram(const ProgramDescription& description, programid key);
185 String8 generateVertexShader(const ProgramDescription& description);
186 String8 generateFragmentShader(const ProgramDescription& description);
Romain Guy48daa542010-08-10 19:21:34 -0700187 void generateBlend(String8& shader, const char* name, SkXfermode::Mode mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700188 void generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT);
Romain Guyac670c02010-07-27 17:39:27 -0700189
Romain Guydb1938e2010-08-02 18:50:22 -0700190 void printLongString(const String8& shader) const;
191
Romain Guyac670c02010-07-27 17:39:27 -0700192 KeyedVector<programid, Program*> mCache;
Romain Guyac670c02010-07-27 17:39:27 -0700193}; // class ProgramCache
194
195}; // namespace uirenderer
196}; // namespace android
197
198#endif // ANDROID_UI_PROGRAM_CACHE_H