blob: fcbeffd30ee1e68dcf007c5837c2235670fdb102 [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
2 * Copyright 2013 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 SF_RENDER_ENGINE_PROGRAMCACHE_H
18#define SF_RENDER_ENGINE_PROGRAMCACHE_H
19
20#include <GLES2/gl2.h>
21
22#include <utils/Singleton.h>
23#include <utils/KeyedVector.h>
24#include <utils/TypeHelpers.h>
25
26#include "Description.h"
27
28namespace android {
29
30class Description;
31class Program;
32class String8;
33
34/*
35 * This class generates GLSL programs suitable to handle a given
36 * Description. It's responsible for figuring out what to
37 * generate from a Description.
38 * It also maintains a cache of these Programs.
39 */
40class ProgramCache : public Singleton<ProgramCache> {
41public:
42 /*
43 * Key is used to retrieve a Program in the cache.
44 * A Key is generated from a Description.
45 */
46 class Key {
47 friend class ProgramCache;
48 typedef uint32_t key_t;
49 key_t mKey;
50 public:
51 enum {
52 BLEND_PREMULT = 0x00000001,
53 BLEND_NORMAL = 0x00000000,
54 BLEND_MASK = 0x00000001,
55
56 OPACITY_OPAQUE = 0x00000002,
57 OPACITY_TRANSLUCENT = 0x00000000,
58 OPACITY_MASK = 0x00000002,
59
60 PLANE_ALPHA_LT_ONE = 0x00000004,
61 PLANE_ALPHA_EQ_ONE = 0x00000000,
62 PLANE_ALPHA_MASK = 0x00000004,
63
64 TEXTURE_OFF = 0x00000000,
65 TEXTURE_EXT = 0x00000008,
66 TEXTURE_2D = 0x00000010,
67 TEXTURE_MASK = 0x00000018,
68 };
69
70 inline Key() : mKey(0) { }
71 inline Key(const Key& rhs) : mKey(rhs.mKey) { }
72
73 inline Key& set(key_t mask, key_t value) {
74 mKey = (mKey & ~mask) | value;
75 return *this;
76 }
77
78 inline bool isTexturing() const {
79 return (mKey & TEXTURE_MASK) != TEXTURE_OFF;
80 }
81 inline int getTextureTarget() const {
82 return (mKey & TEXTURE_MASK);
83 }
84 inline bool isPremultiplied() const {
85 return (mKey & BLEND_MASK) == BLEND_PREMULT;
86 }
87 inline bool isOpaque() const {
88 return (mKey & OPACITY_MASK) == OPACITY_OPAQUE;
89 }
90 inline bool hasPlaneAlpha() const {
91 return (mKey & PLANE_ALPHA_MASK) == PLANE_ALPHA_LT_ONE;
92 }
93
94 // this is the definition of a friend function -- not a method of class Needs
95 friend inline int strictly_order_type(const Key& lhs, const Key& rhs) {
96 return (lhs.mKey < rhs.mKey) ? 1 : 0;
97 }
98 };
99
100 ProgramCache();
101 ~ProgramCache();
102
103 // useProgram lookup a suitable program in the cache or generates one
104 // if none can be found.
105 void useProgram(const Description& description);
106
107private:
108 // compute a cache Key from a Description
109 static Key computeKey(const Description& description);
110 // generates a program from the Key
111 static Program* generateProgram(const Key& needs);
112 // generates the vertex shader from the Key
113 static String8 generateVertexShader(const Key& needs);
114 // generates the fragment shader from the Key
115 static String8 generateFragmentShader(const Key& needs);
116
117 // Key/Value map used for caching Programs. Currently the cache
118 // is never shrunk.
119 DefaultKeyedVector<Key, Program*> mCache;
120};
121
122
123ANDROID_BASIC_TYPES_TRAITS(ProgramCache::Key)
124
125} /* namespace android */
126
127#endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */