blob: 54d3722df3196cf29e283faf6ad85984bc64fb7c [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;
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;
Chia-I Wub027f802017-11-29 14:00:52 -080050
Mathias Agopian3f844832013-08-07 21:24:32 -070051 public:
52 enum {
Chia-I Wub027f802017-11-29 14:00:52 -080053 BLEND_PREMULT = 0x00000001,
54 BLEND_NORMAL = 0x00000000,
55 BLEND_MASK = 0x00000001,
Mathias Agopian3f844832013-08-07 21:24:32 -070056
Chia-I Wub027f802017-11-29 14:00:52 -080057 OPACITY_OPAQUE = 0x00000002,
58 OPACITY_TRANSLUCENT = 0x00000000,
59 OPACITY_MASK = 0x00000002,
Mathias Agopian3f844832013-08-07 21:24:32 -070060
Chia-I Wub027f802017-11-29 14:00:52 -080061 ALPHA_LT_ONE = 0x00000004,
62 ALPHA_EQ_ONE = 0x00000000,
63 ALPHA_MASK = 0x00000004,
Mathias Agopian3f844832013-08-07 21:24:32 -070064
Chia-I Wub027f802017-11-29 14:00:52 -080065 TEXTURE_OFF = 0x00000000,
66 TEXTURE_EXT = 0x00000008,
67 TEXTURE_2D = 0x00000010,
68 TEXTURE_MASK = 0x00000018,
Mathias Agopianff2ed702013-09-01 21:36:12 -070069
Chia-I Wub027f802017-11-29 14:00:52 -080070 COLOR_MATRIX_OFF = 0x00000000,
71 COLOR_MATRIX_ON = 0x00000020,
72 COLOR_MATRIX_MASK = 0x00000020,
Romain Guy88d37dd2017-05-26 17:57:05 -070073
Chia-I Wub027f802017-11-29 14:00:52 -080074 WIDE_GAMUT_OFF = 0x00000000,
75 WIDE_GAMUT_ON = 0x00000040,
76 WIDE_GAMUT_MASK = 0x00000040,
Mathias Agopian3f844832013-08-07 21:24:32 -070077 };
78
Chia-I Wub027f802017-11-29 14:00:52 -080079 inline Key() : mKey(0) {}
80 inline Key(const Key& rhs) : mKey(rhs.mKey) {}
Mathias Agopian3f844832013-08-07 21:24:32 -070081
82 inline Key& set(key_t mask, key_t value) {
83 mKey = (mKey & ~mask) | value;
84 return *this;
85 }
86
Chia-I Wub027f802017-11-29 14:00:52 -080087 inline bool isTexturing() const { return (mKey & TEXTURE_MASK) != TEXTURE_OFF; }
88 inline int getTextureTarget() const { return (mKey & TEXTURE_MASK); }
89 inline bool isPremultiplied() const { return (mKey & BLEND_MASK) == BLEND_PREMULT; }
90 inline bool isOpaque() const { return (mKey & OPACITY_MASK) == OPACITY_OPAQUE; }
91 inline bool hasAlpha() const { return (mKey & ALPHA_MASK) == ALPHA_LT_ONE; }
92 inline bool hasColorMatrix() const { return (mKey & COLOR_MATRIX_MASK) == COLOR_MATRIX_ON; }
93 inline bool isWideGamut() const { return (mKey & WIDE_GAMUT_MASK) == WIDE_GAMUT_ON; }
Mathias Agopian3f844832013-08-07 21:24:32 -070094
95 // this is the definition of a friend function -- not a method of class Needs
96 friend inline int strictly_order_type(const Key& lhs, const Key& rhs) {
Romain Guy88d37dd2017-05-26 17:57:05 -070097 return (lhs.mKey < rhs.mKey) ? 1 : 0;
Mathias Agopian3f844832013-08-07 21:24:32 -070098 }
99 };
100
101 ProgramCache();
102 ~ProgramCache();
103
104 // useProgram lookup a suitable program in the cache or generates one
105 // if none can be found.
106 void useProgram(const Description& description);
107
108private:
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700109 // Generate shaders to populate the cache
110 void primeCache();
Mathias Agopian3f844832013-08-07 21:24:32 -0700111 // compute a cache Key from a Description
112 static Key computeKey(const Description& description);
113 // generates a program from the Key
114 static Program* generateProgram(const Key& needs);
115 // generates the vertex shader from the Key
116 static String8 generateVertexShader(const Key& needs);
117 // generates the fragment shader from the Key
118 static String8 generateFragmentShader(const Key& needs);
119
120 // Key/Value map used for caching Programs. Currently the cache
121 // is never shrunk.
122 DefaultKeyedVector<Key, Program*> mCache;
123};
124
Mathias Agopian3f844832013-08-07 21:24:32 -0700125ANDROID_BASIC_TYPES_TRAITS(ProgramCache::Key)
126
127} /* namespace android */
128
129#endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */