Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 21 | #include <renderengine/private/Description.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 22 | #include <utils/KeyedVector.h> |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 23 | #include <utils/Singleton.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 24 | #include <utils/TypeHelpers.h> |
| 25 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 28 | class String8; |
| 29 | |
| 30 | namespace renderengine { |
| 31 | |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame^] | 32 | struct Description; |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 33 | |
| 34 | namespace gl { |
| 35 | |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 36 | class Formatter; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 37 | class Program; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * This class generates GLSL programs suitable to handle a given |
| 41 | * Description. It's responsible for figuring out what to |
| 42 | * generate from a Description. |
| 43 | * It also maintains a cache of these Programs. |
| 44 | */ |
| 45 | class ProgramCache : public Singleton<ProgramCache> { |
| 46 | public: |
| 47 | /* |
| 48 | * Key is used to retrieve a Program in the cache. |
| 49 | * A Key is generated from a Description. |
| 50 | */ |
| 51 | class Key { |
| 52 | friend class ProgramCache; |
| 53 | typedef uint32_t key_t; |
| 54 | key_t mKey; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 55 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 56 | public: |
| 57 | enum { |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 58 | BLEND_SHIFT = 0, |
| 59 | BLEND_MASK = 1 << BLEND_SHIFT, |
| 60 | BLEND_PREMULT = 1 << BLEND_SHIFT, |
| 61 | BLEND_NORMAL = 0 << BLEND_SHIFT, |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 62 | |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 63 | OPACITY_SHIFT = 1, |
| 64 | OPACITY_MASK = 1 << OPACITY_SHIFT, |
| 65 | OPACITY_OPAQUE = 1 << OPACITY_SHIFT, |
| 66 | OPACITY_TRANSLUCENT = 0 << OPACITY_SHIFT, |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 67 | |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 68 | ALPHA_SHIFT = 2, |
| 69 | ALPHA_MASK = 1 << ALPHA_SHIFT, |
| 70 | ALPHA_LT_ONE = 1 << ALPHA_SHIFT, |
| 71 | ALPHA_EQ_ONE = 0 << ALPHA_SHIFT, |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 72 | |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 73 | TEXTURE_SHIFT = 3, |
| 74 | TEXTURE_MASK = 3 << TEXTURE_SHIFT, |
| 75 | TEXTURE_OFF = 0 << TEXTURE_SHIFT, |
| 76 | TEXTURE_EXT = 1 << TEXTURE_SHIFT, |
| 77 | TEXTURE_2D = 2 << TEXTURE_SHIFT, |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 78 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 79 | INPUT_TRANSFORM_MATRIX_SHIFT = 5, |
| 80 | INPUT_TRANSFORM_MATRIX_MASK = 1 << INPUT_TRANSFORM_MATRIX_SHIFT, |
| 81 | INPUT_TRANSFORM_MATRIX_OFF = 0 << INPUT_TRANSFORM_MATRIX_SHIFT, |
| 82 | INPUT_TRANSFORM_MATRIX_ON = 1 << INPUT_TRANSFORM_MATRIX_SHIFT, |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 83 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 84 | OUTPUT_TRANSFORM_MATRIX_SHIFT = 6, |
| 85 | OUTPUT_TRANSFORM_MATRIX_MASK = 1 << OUTPUT_TRANSFORM_MATRIX_SHIFT, |
| 86 | OUTPUT_TRANSFORM_MATRIX_OFF = 0 << OUTPUT_TRANSFORM_MATRIX_SHIFT, |
| 87 | OUTPUT_TRANSFORM_MATRIX_ON = 1 << OUTPUT_TRANSFORM_MATRIX_SHIFT, |
| 88 | |
| 89 | INPUT_TF_SHIFT = 7, |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 90 | INPUT_TF_MASK = 3 << INPUT_TF_SHIFT, |
| 91 | INPUT_TF_LINEAR = 0 << INPUT_TF_SHIFT, |
| 92 | INPUT_TF_SRGB = 1 << INPUT_TF_SHIFT, |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 93 | INPUT_TF_ST2084 = 2 << INPUT_TF_SHIFT, |
Peiyong Lin | a3fb7d6 | 2018-04-11 17:41:47 -0700 | [diff] [blame] | 94 | INPUT_TF_HLG = 3 << INPUT_TF_SHIFT, |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 95 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 96 | OUTPUT_TF_SHIFT = 9, |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 97 | OUTPUT_TF_MASK = 3 << OUTPUT_TF_SHIFT, |
| 98 | OUTPUT_TF_LINEAR = 0 << OUTPUT_TF_SHIFT, |
| 99 | OUTPUT_TF_SRGB = 1 << OUTPUT_TF_SHIFT, |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 100 | OUTPUT_TF_ST2084 = 2 << OUTPUT_TF_SHIFT, |
Peiyong Lin | a3fb7d6 | 2018-04-11 17:41:47 -0700 | [diff] [blame] | 101 | OUTPUT_TF_HLG = 3 << OUTPUT_TF_SHIFT, |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 102 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 103 | Y410_BT2020_SHIFT = 11, |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 104 | Y410_BT2020_MASK = 1 << Y410_BT2020_SHIFT, |
| 105 | Y410_BT2020_OFF = 0 << Y410_BT2020_SHIFT, |
| 106 | Y410_BT2020_ON = 1 << Y410_BT2020_SHIFT, |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 109 | inline Key() : mKey(0) {} |
| 110 | inline Key(const Key& rhs) : mKey(rhs.mKey) {} |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 111 | |
| 112 | inline Key& set(key_t mask, key_t value) { |
| 113 | mKey = (mKey & ~mask) | value; |
| 114 | return *this; |
| 115 | } |
| 116 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 117 | inline bool isTexturing() const { return (mKey & TEXTURE_MASK) != TEXTURE_OFF; } |
| 118 | inline int getTextureTarget() const { return (mKey & TEXTURE_MASK); } |
| 119 | inline bool isPremultiplied() const { return (mKey & BLEND_MASK) == BLEND_PREMULT; } |
| 120 | inline bool isOpaque() const { return (mKey & OPACITY_MASK) == OPACITY_OPAQUE; } |
| 121 | inline bool hasAlpha() const { return (mKey & ALPHA_MASK) == ALPHA_LT_ONE; } |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 122 | inline bool hasInputTransformMatrix() const { |
| 123 | return (mKey & INPUT_TRANSFORM_MATRIX_MASK) == INPUT_TRANSFORM_MATRIX_ON; |
| 124 | } |
| 125 | inline bool hasOutputTransformMatrix() const { |
| 126 | return (mKey & OUTPUT_TRANSFORM_MATRIX_MASK) == OUTPUT_TRANSFORM_MATRIX_ON; |
| 127 | } |
| 128 | inline bool hasTransformMatrix() const { |
| 129 | return hasInputTransformMatrix() || hasOutputTransformMatrix(); |
| 130 | } |
Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 131 | inline int getInputTF() const { return (mKey & INPUT_TF_MASK); } |
| 132 | inline int getOutputTF() const { return (mKey & OUTPUT_TF_MASK); } |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 133 | |
| 134 | // When HDR and non-HDR contents are mixed, or different types of HDR contents are |
| 135 | // mixed, we will do a tone mapping process to tone map the input content to output |
| 136 | // content. Currently, the following conversions handled, they are: |
| 137 | // * SDR -> HLG |
| 138 | // * SDR -> PQ |
| 139 | // * HLG -> PQ |
| 140 | inline bool needsToneMapping() const { |
| 141 | int inputTF = getInputTF(); |
| 142 | int outputTF = getOutputTF(); |
| 143 | |
| 144 | // Return false when converting from SDR to SDR. |
| 145 | if (inputTF == Key::INPUT_TF_SRGB && outputTF == Key::OUTPUT_TF_LINEAR) { |
| 146 | return false; |
| 147 | } |
| 148 | if (inputTF == Key::INPUT_TF_LINEAR && outputTF == Key::OUTPUT_TF_SRGB) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | inputTF >>= Key::INPUT_TF_SHIFT; |
| 153 | outputTF >>= Key::OUTPUT_TF_SHIFT; |
| 154 | return inputTF != outputTF; |
| 155 | } |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 156 | inline bool isY410BT2020() const { return (mKey & Y410_BT2020_MASK) == Y410_BT2020_ON; } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 157 | |
| 158 | // this is the definition of a friend function -- not a method of class Needs |
| 159 | friend inline int strictly_order_type(const Key& lhs, const Key& rhs) { |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 160 | return (lhs.mKey < rhs.mKey) ? 1 : 0; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 161 | } |
| 162 | }; |
| 163 | |
| 164 | ProgramCache(); |
| 165 | ~ProgramCache(); |
| 166 | |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 167 | // Generate shaders to populate the cache |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 168 | void primeCache(bool useColorManagement); |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 169 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 170 | // useProgram lookup a suitable program in the cache or generates one |
| 171 | // if none can be found. |
| 172 | void useProgram(const Description& description); |
| 173 | |
| 174 | private: |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 175 | // compute a cache Key from a Description |
| 176 | static Key computeKey(const Description& description); |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 177 | // Generate EOTF based from Key. |
| 178 | static void generateEOTF(Formatter& fs, const Key& needs); |
Peiyong Lin | 53f320e | 2018-04-23 17:31:06 -0700 | [diff] [blame] | 179 | // Generate necessary tone mapping methods for OOTF. |
| 180 | static void generateToneMappingProcess(Formatter& fs, const Key& needs); |
Peiyong Lin | 2542cb0 | 2018-04-30 17:29:53 -0700 | [diff] [blame] | 181 | // Generate OOTF based from Key. |
| 182 | static void generateOOTF(Formatter& fs, const Key& needs); |
| 183 | // Generate OETF based from Key. |
| 184 | static void generateOETF(Formatter& fs, const Key& needs); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 185 | // generates a program from the Key |
| 186 | static Program* generateProgram(const Key& needs); |
| 187 | // generates the vertex shader from the Key |
| 188 | static String8 generateVertexShader(const Key& needs); |
| 189 | // generates the fragment shader from the Key |
| 190 | static String8 generateFragmentShader(const Key& needs); |
| 191 | |
| 192 | // Key/Value map used for caching Programs. Currently the cache |
| 193 | // is never shrunk. |
| 194 | DefaultKeyedVector<Key, Program*> mCache; |
| 195 | }; |
| 196 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 197 | } // namespace gl |
| 198 | } // namespace renderengine |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 199 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 200 | ANDROID_BASIC_TYPES_TRAITS(renderengine::gl::ProgramCache::Key) |
| 201 | |
| 202 | } // namespace android |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 203 | |
| 204 | #endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */ |