| 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> | 
|  | 21 |  | 
| 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 |  | 
|  | 26 | #include "Description.h" | 
|  | 27 |  | 
|  | 28 | namespace android { | 
|  | 29 |  | 
|  | 30 | class Description; | 
|  | 31 | class Program; | 
|  | 32 | class 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 | */ | 
|  | 40 | class ProgramCache : public Singleton<ProgramCache> { | 
|  | 41 | public: | 
|  | 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; | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 50 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 51 | public: | 
|  | 52 | enum { | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 53 | BLEND_SHIFT = 0, | 
|  | 54 | BLEND_MASK = 1 << BLEND_SHIFT, | 
|  | 55 | BLEND_PREMULT = 1 << BLEND_SHIFT, | 
|  | 56 | BLEND_NORMAL = 0 << BLEND_SHIFT, | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 57 |  | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 58 | OPACITY_SHIFT = 1, | 
|  | 59 | OPACITY_MASK = 1 << OPACITY_SHIFT, | 
|  | 60 | OPACITY_OPAQUE = 1 << OPACITY_SHIFT, | 
|  | 61 | OPACITY_TRANSLUCENT = 0 << OPACITY_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 | ALPHA_SHIFT = 2, | 
|  | 64 | ALPHA_MASK = 1 << ALPHA_SHIFT, | 
|  | 65 | ALPHA_LT_ONE = 1 << ALPHA_SHIFT, | 
|  | 66 | ALPHA_EQ_ONE = 0 << ALPHA_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 | TEXTURE_SHIFT = 3, | 
|  | 69 | TEXTURE_MASK = 3 << TEXTURE_SHIFT, | 
|  | 70 | TEXTURE_OFF = 0 << TEXTURE_SHIFT, | 
|  | 71 | TEXTURE_EXT = 1 << TEXTURE_SHIFT, | 
|  | 72 | TEXTURE_2D = 2 << TEXTURE_SHIFT, | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 73 |  | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 74 | COLOR_MATRIX_SHIFT = 5, | 
|  | 75 | COLOR_MATRIX_MASK = 1 << COLOR_MATRIX_SHIFT, | 
|  | 76 | COLOR_MATRIX_OFF = 0 << COLOR_MATRIX_SHIFT, | 
|  | 77 | COLOR_MATRIX_ON = 1 << COLOR_MATRIX_SHIFT, | 
| Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 78 |  | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 79 | INPUT_TF_SHIFT = 6, | 
|  | 80 | INPUT_TF_MASK = 3 << INPUT_TF_SHIFT, | 
|  | 81 | INPUT_TF_LINEAR = 0 << INPUT_TF_SHIFT, | 
|  | 82 | INPUT_TF_SRGB = 1 << INPUT_TF_SHIFT, | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 83 | INPUT_TF_ST2084 = 2 << INPUT_TF_SHIFT, | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 84 |  | 
|  | 85 | OUTPUT_TF_SHIFT = 8, | 
|  | 86 | OUTPUT_TF_MASK = 3 << OUTPUT_TF_SHIFT, | 
|  | 87 | OUTPUT_TF_LINEAR = 0 << OUTPUT_TF_SHIFT, | 
|  | 88 | OUTPUT_TF_SRGB = 1 << OUTPUT_TF_SHIFT, | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 89 | OUTPUT_TF_ST2084 = 2 << OUTPUT_TF_SHIFT, | 
|  | 90 |  | 
|  | 91 | TONE_MAPPING_SHIFT = 10, | 
|  | 92 | TONE_MAPPING_MASK = 1 << TONE_MAPPING_SHIFT, | 
|  | 93 | TONE_MAPPING_OFF = 0 << TONE_MAPPING_SHIFT, | 
|  | 94 | TONE_MAPPING_ON = 1 << TONE_MAPPING_SHIFT, | 
|  | 95 |  | 
|  | 96 | Y410_BT2020_SHIFT = 11, | 
|  | 97 | Y410_BT2020_MASK = 1 << Y410_BT2020_SHIFT, | 
|  | 98 | Y410_BT2020_OFF = 0 << Y410_BT2020_SHIFT, | 
|  | 99 | Y410_BT2020_ON = 1 << Y410_BT2020_SHIFT, | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 100 | }; | 
|  | 101 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 102 | inline Key() : mKey(0) {} | 
|  | 103 | inline Key(const Key& rhs) : mKey(rhs.mKey) {} | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 104 |  | 
|  | 105 | inline Key& set(key_t mask, key_t value) { | 
|  | 106 | mKey = (mKey & ~mask) | value; | 
|  | 107 | return *this; | 
|  | 108 | } | 
|  | 109 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 110 | inline bool isTexturing() const { return (mKey & TEXTURE_MASK) != TEXTURE_OFF; } | 
|  | 111 | inline int getTextureTarget() const { return (mKey & TEXTURE_MASK); } | 
|  | 112 | inline bool isPremultiplied() const { return (mKey & BLEND_MASK) == BLEND_PREMULT; } | 
|  | 113 | inline bool isOpaque() const { return (mKey & OPACITY_MASK) == OPACITY_OPAQUE; } | 
|  | 114 | inline bool hasAlpha() const { return (mKey & ALPHA_MASK) == ALPHA_LT_ONE; } | 
|  | 115 | inline bool hasColorMatrix() const { return (mKey & COLOR_MATRIX_MASK) == COLOR_MATRIX_ON; } | 
| Chia-I Wu | 7e65bc0 | 2018-01-11 14:31:38 -0800 | [diff] [blame] | 116 | inline int getInputTF() const { return (mKey & INPUT_TF_MASK); } | 
|  | 117 | inline int getOutputTF() const { return (mKey & OUTPUT_TF_MASK); } | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 118 | inline bool hasToneMapping() const { return (mKey & TONE_MAPPING_MASK) == TONE_MAPPING_ON; } | 
|  | 119 | inline bool isY410BT2020() const { return (mKey & Y410_BT2020_MASK) == Y410_BT2020_ON; } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | // this is the definition of a friend function -- not a method of class Needs | 
|  | 122 | friend inline int strictly_order_type(const Key& lhs, const Key& rhs) { | 
| Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 123 | return (lhs.mKey < rhs.mKey) ? 1 : 0; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 124 | } | 
|  | 125 | }; | 
|  | 126 |  | 
|  | 127 | ProgramCache(); | 
|  | 128 | ~ProgramCache(); | 
|  | 129 |  | 
|  | 130 | // useProgram lookup a suitable program in the cache or generates one | 
|  | 131 | // if none can be found. | 
|  | 132 | void useProgram(const Description& description); | 
|  | 133 |  | 
|  | 134 | private: | 
| Riley Andrews | a51fafc | 2014-09-29 13:29:40 -0700 | [diff] [blame] | 135 | // Generate shaders to populate the cache | 
|  | 136 | void primeCache(); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 137 | // compute a cache Key from a Description | 
|  | 138 | static Key computeKey(const Description& description); | 
|  | 139 | // generates a program from the Key | 
|  | 140 | static Program* generateProgram(const Key& needs); | 
|  | 141 | // generates the vertex shader from the Key | 
|  | 142 | static String8 generateVertexShader(const Key& needs); | 
|  | 143 | // generates the fragment shader from the Key | 
|  | 144 | static String8 generateFragmentShader(const Key& needs); | 
|  | 145 |  | 
|  | 146 | // Key/Value map used for caching Programs. Currently the cache | 
|  | 147 | // is never shrunk. | 
|  | 148 | DefaultKeyedVector<Key, Program*> mCache; | 
|  | 149 | }; | 
|  | 150 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 151 | ANDROID_BASIC_TYPES_TRAITS(ProgramCache::Key) | 
|  | 152 |  | 
|  | 153 | } /* namespace android */ | 
|  | 154 |  | 
|  | 155 | #endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */ |