blob: bf5e43446742b3c8e4a9a1df15f6c19f7d768367 [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
Chia-I Wu56d7b0a2018-10-01 15:13:11 -070020#include <unordered_map>
21
Mathias Agopian3f844832013-08-07 21:24:32 -070022#include <GLES2/gl2.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070023#include <renderengine/private/Description.h>
Chia-I Wub027f802017-11-29 14:00:52 -080024#include <utils/Singleton.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070025#include <utils/TypeHelpers.h>
26
Mathias Agopian3f844832013-08-07 21:24:32 -070027namespace android {
28
Peiyong Lin833074a2018-08-28 11:53:54 -070029class String8;
30
31namespace renderengine {
32
Peiyong Lin70b26ce2018-09-18 19:02:39 -070033struct Description;
Peiyong Lin833074a2018-08-28 11:53:54 -070034
35namespace gl {
36
Peiyong Lin2542cb02018-04-30 17:29:53 -070037class Formatter;
Mathias Agopian3f844832013-08-07 21:24:32 -070038class Program;
Mathias Agopian3f844832013-08-07 21:24:32 -070039
40/*
41 * This class generates GLSL programs suitable to handle a given
42 * Description. It's responsible for figuring out what to
43 * generate from a Description.
44 * It also maintains a cache of these Programs.
45 */
46class ProgramCache : public Singleton<ProgramCache> {
47public:
48 /*
49 * Key is used to retrieve a Program in the cache.
50 * A Key is generated from a Description.
51 */
52 class Key {
53 friend class ProgramCache;
54 typedef uint32_t key_t;
55 key_t mKey;
Chia-I Wub027f802017-11-29 14:00:52 -080056
Mathias Agopian3f844832013-08-07 21:24:32 -070057 public:
58 enum {
Chia-I Wu7e65bc02018-01-11 14:31:38 -080059 BLEND_SHIFT = 0,
60 BLEND_MASK = 1 << BLEND_SHIFT,
61 BLEND_PREMULT = 1 << BLEND_SHIFT,
62 BLEND_NORMAL = 0 << BLEND_SHIFT,
Mathias Agopian3f844832013-08-07 21:24:32 -070063
Chia-I Wu7e65bc02018-01-11 14:31:38 -080064 OPACITY_SHIFT = 1,
65 OPACITY_MASK = 1 << OPACITY_SHIFT,
66 OPACITY_OPAQUE = 1 << OPACITY_SHIFT,
67 OPACITY_TRANSLUCENT = 0 << OPACITY_SHIFT,
Mathias Agopian3f844832013-08-07 21:24:32 -070068
Chia-I Wu7e65bc02018-01-11 14:31:38 -080069 ALPHA_SHIFT = 2,
70 ALPHA_MASK = 1 << ALPHA_SHIFT,
71 ALPHA_LT_ONE = 1 << ALPHA_SHIFT,
72 ALPHA_EQ_ONE = 0 << ALPHA_SHIFT,
Mathias Agopian3f844832013-08-07 21:24:32 -070073
Chia-I Wu7e65bc02018-01-11 14:31:38 -080074 TEXTURE_SHIFT = 3,
75 TEXTURE_MASK = 3 << TEXTURE_SHIFT,
76 TEXTURE_OFF = 0 << TEXTURE_SHIFT,
77 TEXTURE_EXT = 1 << TEXTURE_SHIFT,
78 TEXTURE_2D = 2 << TEXTURE_SHIFT,
Mathias Agopianff2ed702013-09-01 21:36:12 -070079
Peiyong Lina296b0c2018-04-30 16:55:29 -070080 INPUT_TRANSFORM_MATRIX_SHIFT = 5,
81 INPUT_TRANSFORM_MATRIX_MASK = 1 << INPUT_TRANSFORM_MATRIX_SHIFT,
82 INPUT_TRANSFORM_MATRIX_OFF = 0 << INPUT_TRANSFORM_MATRIX_SHIFT,
83 INPUT_TRANSFORM_MATRIX_ON = 1 << INPUT_TRANSFORM_MATRIX_SHIFT,
Romain Guy88d37dd2017-05-26 17:57:05 -070084
Peiyong Lina296b0c2018-04-30 16:55:29 -070085 OUTPUT_TRANSFORM_MATRIX_SHIFT = 6,
86 OUTPUT_TRANSFORM_MATRIX_MASK = 1 << OUTPUT_TRANSFORM_MATRIX_SHIFT,
87 OUTPUT_TRANSFORM_MATRIX_OFF = 0 << OUTPUT_TRANSFORM_MATRIX_SHIFT,
88 OUTPUT_TRANSFORM_MATRIX_ON = 1 << OUTPUT_TRANSFORM_MATRIX_SHIFT,
89
90 INPUT_TF_SHIFT = 7,
Chia-I Wu7e65bc02018-01-11 14:31:38 -080091 INPUT_TF_MASK = 3 << INPUT_TF_SHIFT,
92 INPUT_TF_LINEAR = 0 << INPUT_TF_SHIFT,
93 INPUT_TF_SRGB = 1 << INPUT_TF_SHIFT,
Chia-I Wu131d3762018-01-11 14:35:27 -080094 INPUT_TF_ST2084 = 2 << INPUT_TF_SHIFT,
Peiyong Lina3fb7d62018-04-11 17:41:47 -070095 INPUT_TF_HLG = 3 << INPUT_TF_SHIFT,
Chia-I Wu7e65bc02018-01-11 14:31:38 -080096
Peiyong Lina296b0c2018-04-30 16:55:29 -070097 OUTPUT_TF_SHIFT = 9,
Chia-I Wu7e65bc02018-01-11 14:31:38 -080098 OUTPUT_TF_MASK = 3 << OUTPUT_TF_SHIFT,
99 OUTPUT_TF_LINEAR = 0 << OUTPUT_TF_SHIFT,
100 OUTPUT_TF_SRGB = 1 << OUTPUT_TF_SHIFT,
Chia-I Wu131d3762018-01-11 14:35:27 -0800101 OUTPUT_TF_ST2084 = 2 << OUTPUT_TF_SHIFT,
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700102 OUTPUT_TF_HLG = 3 << OUTPUT_TF_SHIFT,
Chia-I Wu131d3762018-01-11 14:35:27 -0800103
Peiyong Lina296b0c2018-04-30 16:55:29 -0700104 Y410_BT2020_SHIFT = 11,
Chia-I Wu131d3762018-01-11 14:35:27 -0800105 Y410_BT2020_MASK = 1 << Y410_BT2020_SHIFT,
106 Y410_BT2020_OFF = 0 << Y410_BT2020_SHIFT,
107 Y410_BT2020_ON = 1 << Y410_BT2020_SHIFT,
Mathias Agopian3f844832013-08-07 21:24:32 -0700108 };
109
Chia-I Wub027f802017-11-29 14:00:52 -0800110 inline Key() : mKey(0) {}
111 inline Key(const Key& rhs) : mKey(rhs.mKey) {}
Mathias Agopian3f844832013-08-07 21:24:32 -0700112
113 inline Key& set(key_t mask, key_t value) {
114 mKey = (mKey & ~mask) | value;
115 return *this;
116 }
117
Chia-I Wub027f802017-11-29 14:00:52 -0800118 inline bool isTexturing() const { return (mKey & TEXTURE_MASK) != TEXTURE_OFF; }
119 inline int getTextureTarget() const { return (mKey & TEXTURE_MASK); }
120 inline bool isPremultiplied() const { return (mKey & BLEND_MASK) == BLEND_PREMULT; }
121 inline bool isOpaque() const { return (mKey & OPACITY_MASK) == OPACITY_OPAQUE; }
122 inline bool hasAlpha() const { return (mKey & ALPHA_MASK) == ALPHA_LT_ONE; }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700123 inline bool hasInputTransformMatrix() const {
124 return (mKey & INPUT_TRANSFORM_MATRIX_MASK) == INPUT_TRANSFORM_MATRIX_ON;
125 }
126 inline bool hasOutputTransformMatrix() const {
127 return (mKey & OUTPUT_TRANSFORM_MATRIX_MASK) == OUTPUT_TRANSFORM_MATRIX_ON;
128 }
129 inline bool hasTransformMatrix() const {
130 return hasInputTransformMatrix() || hasOutputTransformMatrix();
131 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800132 inline int getInputTF() const { return (mKey & INPUT_TF_MASK); }
133 inline int getOutputTF() const { return (mKey & OUTPUT_TF_MASK); }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700134
135 // When HDR and non-HDR contents are mixed, or different types of HDR contents are
136 // mixed, we will do a tone mapping process to tone map the input content to output
137 // content. Currently, the following conversions handled, they are:
138 // * SDR -> HLG
139 // * SDR -> PQ
140 // * HLG -> PQ
141 inline bool needsToneMapping() const {
142 int inputTF = getInputTF();
143 int outputTF = getOutputTF();
144
145 // Return false when converting from SDR to SDR.
146 if (inputTF == Key::INPUT_TF_SRGB && outputTF == Key::OUTPUT_TF_LINEAR) {
147 return false;
148 }
149 if (inputTF == Key::INPUT_TF_LINEAR && outputTF == Key::OUTPUT_TF_SRGB) {
150 return false;
151 }
152
153 inputTF >>= Key::INPUT_TF_SHIFT;
154 outputTF >>= Key::OUTPUT_TF_SHIFT;
155 return inputTF != outputTF;
156 }
Chia-I Wu131d3762018-01-11 14:35:27 -0800157 inline bool isY410BT2020() const { return (mKey & Y410_BT2020_MASK) == Y410_BT2020_ON; }
Mathias Agopian3f844832013-08-07 21:24:32 -0700158
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700159 // for use by std::unordered_map
160
161 bool operator==(const Key& other) const {
162 return mKey == other.mKey;
Mathias Agopian3f844832013-08-07 21:24:32 -0700163 }
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700164
165 struct Hash {
166 size_t operator()(const Key& key) const {
167 return static_cast<size_t>(key.mKey);
168 }
169 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700170 };
171
172 ProgramCache();
173 ~ProgramCache();
174
Chia-I Wu93e14df2018-06-04 10:10:17 -0700175 // Generate shaders to populate the cache
Peiyong Lin13effd12018-07-24 17:01:47 -0700176 void primeCache(bool useColorManagement);
Chia-I Wu93e14df2018-06-04 10:10:17 -0700177
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700178 size_t getSize() const { return mCache.size(); }
179
Mathias Agopian3f844832013-08-07 21:24:32 -0700180 // useProgram lookup a suitable program in the cache or generates one
181 // if none can be found.
182 void useProgram(const Description& description);
183
184private:
Mathias Agopian3f844832013-08-07 21:24:32 -0700185 // compute a cache Key from a Description
186 static Key computeKey(const Description& description);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700187 // Generate EOTF based from Key.
188 static void generateEOTF(Formatter& fs, const Key& needs);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700189 // Generate necessary tone mapping methods for OOTF.
190 static void generateToneMappingProcess(Formatter& fs, const Key& needs);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700191 // Generate OOTF based from Key.
192 static void generateOOTF(Formatter& fs, const Key& needs);
193 // Generate OETF based from Key.
194 static void generateOETF(Formatter& fs, const Key& needs);
Mathias Agopian3f844832013-08-07 21:24:32 -0700195 // generates a program from the Key
196 static Program* generateProgram(const Key& needs);
197 // generates the vertex shader from the Key
198 static String8 generateVertexShader(const Key& needs);
199 // generates the fragment shader from the Key
200 static String8 generateFragmentShader(const Key& needs);
201
202 // Key/Value map used for caching Programs. Currently the cache
203 // is never shrunk.
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700204 std::unordered_map<Key, Program*, Key::Hash> mCache;
Mathias Agopian3f844832013-08-07 21:24:32 -0700205};
206
Peiyong Lin833074a2018-08-28 11:53:54 -0700207} // namespace gl
208} // namespace renderengine
Mathias Agopian3f844832013-08-07 21:24:32 -0700209
Peiyong Lin833074a2018-08-28 11:53:54 -0700210ANDROID_BASIC_TYPES_TRAITS(renderengine::gl::ProgramCache::Key)
211
212} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700213
214#endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */