summaryrefslogtreecommitdiffstats
path: root/services/surfaceflinger/RenderEngine/ProgramCache.h
blob: e8b9dcd788dedd5f5b9f275d038d839ba8cbe26b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SF_RENDER_ENGINE_PROGRAMCACHE_H
#define SF_RENDER_ENGINE_PROGRAMCACHE_H

#include <GLES2/gl2.h>

#include <utils/Singleton.h>
#include <utils/KeyedVector.h>
#include <utils/TypeHelpers.h>

#include "Description.h"

namespace android {

class Description;
class Program;
class String8;

/*
 * This class generates GLSL programs suitable to handle a given
 * Description. It's responsible for figuring out what to
 * generate from a Description.
 * It also maintains a cache of these Programs.
 */
class ProgramCache : public Singleton<ProgramCache> {
public:
    /*
     * Key is used to retrieve a Program in the cache.
     * A Key is generated from a Description.
     */
    class Key {
        friend class ProgramCache;
        typedef uint32_t key_t;
        key_t mKey;
    public:
        enum {
            BLEND_PREMULT           =       0x00000001,
            BLEND_NORMAL            =       0x00000000,
            BLEND_MASK              =       0x00000001,

            OPACITY_OPAQUE          =       0x00000002,
            OPACITY_TRANSLUCENT     =       0x00000000,
            OPACITY_MASK            =       0x00000002,

            PLANE_ALPHA_LT_ONE      =       0x00000004,
            PLANE_ALPHA_EQ_ONE      =       0x00000000,
            PLANE_ALPHA_MASK        =       0x00000004,

            TEXTURE_OFF             =       0x00000000,
            TEXTURE_EXT             =       0x00000008,
            TEXTURE_2D              =       0x00000010,
            TEXTURE_MASK            =       0x00000018,

            COLOR_MATRIX_OFF        =       0x00000000,
            COLOR_MATRIX_ON         =       0x00000020,
            COLOR_MATRIX_MASK       =       0x00000020,
        };

        inline Key() : mKey(0) { }
        inline Key(const Key& rhs) : mKey(rhs.mKey) { }

        inline Key& set(key_t mask, key_t value) {
            mKey = (mKey & ~mask) | value;
            return *this;
        }

        inline bool isTexturing() const {
            return (mKey & TEXTURE_MASK) != TEXTURE_OFF;
        }
        inline int getTextureTarget() const {
            return (mKey & TEXTURE_MASK);
        }
        inline bool isPremultiplied() const {
            return (mKey & BLEND_MASK) == BLEND_PREMULT;
        }
        inline bool isOpaque() const {
            return (mKey & OPACITY_MASK) == OPACITY_OPAQUE;
        }
        inline bool hasPlaneAlpha() const {
            return (mKey & PLANE_ALPHA_MASK) == PLANE_ALPHA_LT_ONE;
        }
        inline bool hasColorMatrix() const {
            return (mKey & COLOR_MATRIX_MASK) == COLOR_MATRIX_ON;
        }

        // this is the definition of a friend function -- not a method of class Needs
        friend inline int strictly_order_type(const Key& lhs, const Key& rhs) {
            return  (lhs.mKey < rhs.mKey) ? 1 : 0;
        }
    };

    ProgramCache();
    ~ProgramCache();

    // useProgram lookup a suitable program in the cache or generates one
    // if none can be found.
    void useProgram(const Description& description);

private:
    // compute a cache Key from a Description
    static Key computeKey(const Description& description);
    // generates a program from the Key
    static Program* generateProgram(const Key& needs);
    // generates the vertex shader from the Key
    static String8 generateVertexShader(const Key& needs);
    // generates the fragment shader from the Key
    static String8 generateFragmentShader(const Key& needs);

    // Key/Value map used for caching Programs. Currently the cache
    // is never shrunk.
    DefaultKeyedVector<Key, Program*> mCache;
};


ANDROID_BASIC_TYPES_TRAITS(ProgramCache::Key)

} /* namespace android */

#endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */