ProgramCache.h revision ac670c0433d19397d4e36ced2110475b6f54fe26
1/*
2 * Copyright (C) 2010 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 ANDROID_UI_PROGRAM_CACHE_H
18#define ANDROID_UI_PROGRAM_CACHE_H
19
20#include <utils/KeyedVector.h>
21#include <utils/Log.h>
22
23#include <SkXfermode.h>
24
25#include "Program.h"
26
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// Defines
32///////////////////////////////////////////////////////////////////////////////
33
34// Debug
35#define DEBUG_PROGRAM_CACHE 0
36
37// Debug
38#if DEBUG_PROGRAM_CACHE
39    #define PROGRAM_LOGD(...) LOGD(__VA_ARGS__)
40#else
41    #define PROGRAM_LOGD(...)
42#endif
43
44#define PROGRAM_KEY_TEXTURE 0x1
45#define PROGRAM_KEY_A8_TEXTURE 0x2
46#define PROGRAM_KEY_BITMAP 0x4
47#define PROGRAM_KEY_GRADIENT 0x8
48#define PROGRAM_KEY_BITMAP_FIRST 0x10
49#define PROGRAM_KEY_COLOR_MATRIX 0x20
50#define PROGRAM_KEY_COLOR_LIGHTING 0x40
51#define PROGRAM_KEY_COLOR_BLEND 0x80
52
53// Support only the 12 Porter-Duff modes for now
54#define PROGRAM_MAX_XFERMODE 0xC
55#define PROGRAM_XFERMODE_SHADER_SHIFT 24
56#define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20
57
58///////////////////////////////////////////////////////////////////////////////
59// Types
60///////////////////////////////////////////////////////////////////////////////
61
62typedef uint32_t programid;
63
64///////////////////////////////////////////////////////////////////////////////
65// Cache
66///////////////////////////////////////////////////////////////////////////////
67
68/**
69 * Describe the features required for a given program. The features
70 * determine the generation of both the vertex and fragment shaders.
71 * A ProgramDescription must be used in conjunction with a ProgramCache.
72 */
73struct ProgramDescription {
74    enum ColorModifier {
75        kColorNone,
76        kColorMatrix,
77        kColorLighting,
78        kColorBlend
79    };
80
81    ProgramDescription():
82        hasTexture(false), hasAlpha8Texture(false),
83        hasBitmap(false), hasGradient(false), shadersMode(SkXfermode::kClear_Mode),
84        colorOp(kColorNone), colorMode(SkXfermode::kClear_Mode) {
85    }
86
87    // Texturing
88    bool hasTexture;
89    bool hasAlpha8Texture;
90
91    // Shaders
92    bool hasBitmap;
93    bool hasGradient;
94    SkXfermode::Mode shadersMode;
95    bool isBitmapFirst;
96
97    // Color operations
98    int colorOp;
99    SkXfermode::Mode colorMode;
100
101    programid key() const {
102        programid key = 0;
103        if (hasTexture) key |= PROGRAM_KEY_TEXTURE;
104        if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE;
105        if (hasBitmap) key |= PROGRAM_KEY_BITMAP;
106        if (hasGradient) key |= PROGRAM_KEY_GRADIENT;
107        if (isBitmapFirst) key  |= PROGRAM_KEY_BITMAP_FIRST;
108        if (hasBitmap && hasGradient) {
109            key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT;
110        }
111        switch (colorOp) {
112            case kColorMatrix:
113                key |= PROGRAM_KEY_COLOR_MATRIX;
114                break;
115            case kColorLighting:
116                key |= PROGRAM_KEY_COLOR_LIGHTING;
117                break;
118            case kColorBlend:
119                key |= PROGRAM_KEY_COLOR_BLEND;
120                key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT;
121                break;
122            case kColorNone:
123                break;
124        }
125        return key;
126    }
127}; // struct ProgramDescription
128
129/**
130 * Generates and caches program. Programs are generated based on
131 * ProgramDescriptions.
132 */
133class ProgramCache {
134public:
135    ProgramCache();
136    ~ProgramCache();
137
138    Program* get(const ProgramDescription& description);
139
140    void clear();
141
142private:
143    Program* generateProgram(const ProgramDescription& description, programid key);
144    String8 generateVertexShader(const ProgramDescription& description);
145    String8 generateFragmentShader(const ProgramDescription& description);
146    void generatePorterDuffBlend(String8& shader, const char* name, SkXfermode::Mode mode);
147
148    KeyedVector<programid, Program*> mCache;
149
150}; // class ProgramCache
151
152}; // namespace uirenderer
153}; // namespace android
154
155#endif // ANDROID_UI_PROGRAM_CACHE_H
156