blob: d18163a94cf334a7367db7a691772c35e129af01 [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
Mathias Agopian3f844832013-08-07 21:24:32 -070022#include <utils/KeyedVector.h>
Chia-I Wub027f802017-11-29 14:00:52 -080023#include <utils/Singleton.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070024#include <utils/TypeHelpers.h>
25
26#include "Description.h"
27
28namespace android {
29
30class Description;
Peiyong Lin2542cb02018-04-30 17:29:53 -070031class Formatter;
Mathias Agopian3f844832013-08-07 21:24:32 -070032class Program;
33class String8;
34
35/*
36 * This class generates GLSL programs suitable to handle a given
37 * Description. It's responsible for figuring out what to
38 * generate from a Description.
39 * It also maintains a cache of these Programs.
40 */
41class ProgramCache : public Singleton<ProgramCache> {
42public:
43 /*
44 * Key is used to retrieve a Program in the cache.
45 * A Key is generated from a Description.
46 */
47 class Key {
48 friend class ProgramCache;
49 typedef uint32_t key_t;
50 key_t mKey;
Chia-I Wub027f802017-11-29 14:00:52 -080051
Mathias Agopian3f844832013-08-07 21:24:32 -070052 public:
53 enum {
Chia-I Wu7e65bc02018-01-11 14:31:38 -080054 BLEND_SHIFT = 0,
55 BLEND_MASK = 1 << BLEND_SHIFT,
56 BLEND_PREMULT = 1 << BLEND_SHIFT,
57 BLEND_NORMAL = 0 << BLEND_SHIFT,
Mathias Agopian3f844832013-08-07 21:24:32 -070058
Chia-I Wu7e65bc02018-01-11 14:31:38 -080059 OPACITY_SHIFT = 1,
60 OPACITY_MASK = 1 << OPACITY_SHIFT,
61 OPACITY_OPAQUE = 1 << OPACITY_SHIFT,
62 OPACITY_TRANSLUCENT = 0 << OPACITY_SHIFT,
Mathias Agopian3f844832013-08-07 21:24:32 -070063
Chia-I Wu7e65bc02018-01-11 14:31:38 -080064 ALPHA_SHIFT = 2,
65 ALPHA_MASK = 1 << ALPHA_SHIFT,
66 ALPHA_LT_ONE = 1 << ALPHA_SHIFT,
67 ALPHA_EQ_ONE = 0 << ALPHA_SHIFT,
Mathias Agopian3f844832013-08-07 21:24:32 -070068
Chia-I Wu7e65bc02018-01-11 14:31:38 -080069 TEXTURE_SHIFT = 3,
70 TEXTURE_MASK = 3 << TEXTURE_SHIFT,
71 TEXTURE_OFF = 0 << TEXTURE_SHIFT,
72 TEXTURE_EXT = 1 << TEXTURE_SHIFT,
73 TEXTURE_2D = 2 << TEXTURE_SHIFT,
Mathias Agopianff2ed702013-09-01 21:36:12 -070074
Chia-I Wu7e65bc02018-01-11 14:31:38 -080075 COLOR_MATRIX_SHIFT = 5,
76 COLOR_MATRIX_MASK = 1 << COLOR_MATRIX_SHIFT,
77 COLOR_MATRIX_OFF = 0 << COLOR_MATRIX_SHIFT,
78 COLOR_MATRIX_ON = 1 << COLOR_MATRIX_SHIFT,
Romain Guy88d37dd2017-05-26 17:57:05 -070079
Chia-I Wu7e65bc02018-01-11 14:31:38 -080080 INPUT_TF_SHIFT = 6,
81 INPUT_TF_MASK = 3 << INPUT_TF_SHIFT,
82 INPUT_TF_LINEAR = 0 << INPUT_TF_SHIFT,
83 INPUT_TF_SRGB = 1 << INPUT_TF_SHIFT,
Chia-I Wu131d3762018-01-11 14:35:27 -080084 INPUT_TF_ST2084 = 2 << INPUT_TF_SHIFT,
Peiyong Lina3fb7d62018-04-11 17:41:47 -070085 INPUT_TF_HLG = 3 << INPUT_TF_SHIFT,
Chia-I Wu7e65bc02018-01-11 14:31:38 -080086
87 OUTPUT_TF_SHIFT = 8,
88 OUTPUT_TF_MASK = 3 << OUTPUT_TF_SHIFT,
89 OUTPUT_TF_LINEAR = 0 << OUTPUT_TF_SHIFT,
90 OUTPUT_TF_SRGB = 1 << OUTPUT_TF_SHIFT,
Chia-I Wu131d3762018-01-11 14:35:27 -080091 OUTPUT_TF_ST2084 = 2 << OUTPUT_TF_SHIFT,
Peiyong Lina3fb7d62018-04-11 17:41:47 -070092 OUTPUT_TF_HLG = 3 << OUTPUT_TF_SHIFT,
Chia-I Wu131d3762018-01-11 14:35:27 -080093
Peiyong Lina3fb7d62018-04-11 17:41:47 -070094 Y410_BT2020_SHIFT = 10,
Chia-I Wu131d3762018-01-11 14:35:27 -080095 Y410_BT2020_MASK = 1 << Y410_BT2020_SHIFT,
96 Y410_BT2020_OFF = 0 << Y410_BT2020_SHIFT,
97 Y410_BT2020_ON = 1 << Y410_BT2020_SHIFT,
Mathias Agopian3f844832013-08-07 21:24:32 -070098 };
99
Chia-I Wub027f802017-11-29 14:00:52 -0800100 inline Key() : mKey(0) {}
101 inline Key(const Key& rhs) : mKey(rhs.mKey) {}
Mathias Agopian3f844832013-08-07 21:24:32 -0700102
103 inline Key& set(key_t mask, key_t value) {
104 mKey = (mKey & ~mask) | value;
105 return *this;
106 }
107
Chia-I Wub027f802017-11-29 14:00:52 -0800108 inline bool isTexturing() const { return (mKey & TEXTURE_MASK) != TEXTURE_OFF; }
109 inline int getTextureTarget() const { return (mKey & TEXTURE_MASK); }
110 inline bool isPremultiplied() const { return (mKey & BLEND_MASK) == BLEND_PREMULT; }
111 inline bool isOpaque() const { return (mKey & OPACITY_MASK) == OPACITY_OPAQUE; }
112 inline bool hasAlpha() const { return (mKey & ALPHA_MASK) == ALPHA_LT_ONE; }
113 inline bool hasColorMatrix() const { return (mKey & COLOR_MATRIX_MASK) == COLOR_MATRIX_ON; }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800114 inline int getInputTF() const { return (mKey & INPUT_TF_MASK); }
115 inline int getOutputTF() const { return (mKey & OUTPUT_TF_MASK); }
Chia-I Wu131d3762018-01-11 14:35:27 -0800116 inline bool isY410BT2020() const { return (mKey & Y410_BT2020_MASK) == Y410_BT2020_ON; }
Mathias Agopian3f844832013-08-07 21:24:32 -0700117
118 // this is the definition of a friend function -- not a method of class Needs
119 friend inline int strictly_order_type(const Key& lhs, const Key& rhs) {
Romain Guy88d37dd2017-05-26 17:57:05 -0700120 return (lhs.mKey < rhs.mKey) ? 1 : 0;
Mathias Agopian3f844832013-08-07 21:24:32 -0700121 }
122 };
123
124 ProgramCache();
125 ~ProgramCache();
126
127 // useProgram lookup a suitable program in the cache or generates one
128 // if none can be found.
129 void useProgram(const Description& description);
130
131private:
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700132 // Generate shaders to populate the cache
133 void primeCache();
Mathias Agopian3f844832013-08-07 21:24:32 -0700134 // compute a cache Key from a Description
135 static Key computeKey(const Description& description);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700136 // Generate EOTF based from Key.
137 static void generateEOTF(Formatter& fs, const Key& needs);
138 // Generate OOTF based from Key.
139 static void generateOOTF(Formatter& fs, const Key& needs);
140 // Generate OETF based from Key.
141 static void generateOETF(Formatter& fs, const Key& needs);
Mathias Agopian3f844832013-08-07 21:24:32 -0700142 // generates a program from the Key
143 static Program* generateProgram(const Key& needs);
144 // generates the vertex shader from the Key
145 static String8 generateVertexShader(const Key& needs);
146 // generates the fragment shader from the Key
147 static String8 generateFragmentShader(const Key& needs);
148
149 // Key/Value map used for caching Programs. Currently the cache
150 // is never shrunk.
151 DefaultKeyedVector<Key, Program*> mCache;
152};
153
Mathias Agopian3f844832013-08-07 21:24:32 -0700154ANDROID_BASIC_TYPES_TRAITS(ProgramCache::Key)
155
156} /* namespace android */
157
158#endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */