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_HWUI_GAMMA_FONT_RENDERER_H
18 #define ANDROID_HWUI_GAMMA_FONT_RENDERER_H
19 
20 #include <SkPaint.h>
21 
22 #include "FontRenderer.h"
23 #include "Program.h"
24 
25 namespace android {
26 namespace uirenderer {
27 
28 class GammaFontRenderer {
29 public:
30     virtual ~GammaFontRenderer();
31 
32     virtual void clear() = 0;
33     virtual void flush() = 0;
34 
35     virtual FontRenderer& getFontRenderer(const SkPaint* paint) = 0;
36 
37     virtual uint32_t getFontRendererCount() const = 0;
38     virtual uint32_t getFontRendererSize(uint32_t fontRenderer, GLenum format) const = 0;
39 
40     virtual void describe(ProgramDescription& description, const SkPaint* paint) const = 0;
41     virtual void setupProgram(ProgramDescription& description, Program& program) const = 0;
42 
43     virtual void endPrecaching() = 0;
44 
45     static GammaFontRenderer* createRenderer();
46 
47 protected:
48     GammaFontRenderer();
49 
50     int mBlackThreshold;
51     int mWhiteThreshold;
52 
53     float mGamma;
54 };
55 
56 class ShaderGammaFontRenderer: public GammaFontRenderer {
57 public:
~ShaderGammaFontRenderer()58     ~ShaderGammaFontRenderer() {
59         delete mRenderer;
60     }
61 
clear()62     void clear() override {
63         delete mRenderer;
64         mRenderer = nullptr;
65     }
66 
flush()67     void flush() override {
68         if (mRenderer) {
69             mRenderer->flushLargeCaches();
70         }
71     }
72 
getFontRenderer(const SkPaint * paint)73     FontRenderer& getFontRenderer(const SkPaint* paint) override {
74         if (!mRenderer) {
75             mRenderer = new FontRenderer;
76         }
77         return *mRenderer;
78     }
79 
getFontRendererCount()80     uint32_t getFontRendererCount() const override {
81         return 1;
82     }
83 
getFontRendererSize(uint32_t fontRenderer,GLenum format)84     uint32_t getFontRendererSize(uint32_t fontRenderer, GLenum format) const override {
85         return mRenderer ? mRenderer->getCacheSize(format) : 0;
86     }
87 
88     void describe(ProgramDescription& description, const SkPaint* paint) const override;
89     void setupProgram(ProgramDescription& description, Program& program) const override;
90 
91     void endPrecaching() override;
92 
93 private:
94     ShaderGammaFontRenderer(bool multiGamma);
95 
96     FontRenderer* mRenderer;
97     bool mMultiGamma;
98 
99     friend class GammaFontRenderer;
100 };
101 
102 class LookupGammaFontRenderer: public GammaFontRenderer {
103 public:
~LookupGammaFontRenderer()104     ~LookupGammaFontRenderer() {
105         delete mRenderer;
106     }
107 
clear()108     void clear() override {
109         delete mRenderer;
110         mRenderer = nullptr;
111     }
112 
flush()113     void flush() override {
114         if (mRenderer) {
115             mRenderer->flushLargeCaches();
116         }
117     }
118 
getFontRenderer(const SkPaint * paint)119     FontRenderer& getFontRenderer(const SkPaint* paint) override {
120         if (!mRenderer) {
121             mRenderer = new FontRenderer;
122             mRenderer->setGammaTable(&mGammaTable[0]);
123         }
124         return *mRenderer;
125     }
126 
getFontRendererCount()127     uint32_t getFontRendererCount() const override {
128         return 1;
129     }
130 
getFontRendererSize(uint32_t fontRenderer,GLenum format)131     uint32_t getFontRendererSize(uint32_t fontRenderer, GLenum format) const override {
132         return mRenderer ? mRenderer->getCacheSize(format) : 0;
133     }
134 
describe(ProgramDescription & description,const SkPaint * paint)135     void describe(ProgramDescription& description, const SkPaint* paint) const override {
136     }
137 
setupProgram(ProgramDescription & description,Program & program)138     void setupProgram(ProgramDescription& description, Program& program) const override {
139     }
140 
141     void endPrecaching() override;
142 
143 private:
144     LookupGammaFontRenderer();
145 
146     FontRenderer* mRenderer;
147     uint8_t mGammaTable[256];
148 
149     friend class GammaFontRenderer;
150 };
151 
152 class Lookup3GammaFontRenderer: public GammaFontRenderer {
153 public:
154     void clear() override;
155     void flush() override;
156 
157     FontRenderer& getFontRenderer(const SkPaint* paint) override;
158 
getFontRendererCount()159     uint32_t getFontRendererCount() const override {
160         return kGammaCount;
161     }
162 
getFontRendererSize(uint32_t fontRenderer,GLenum format)163     uint32_t getFontRendererSize(uint32_t fontRenderer, GLenum format) const override {
164         if (fontRenderer >= kGammaCount) return 0;
165 
166         if (!mRenderers[fontRenderer]) return 0;
167 
168         return mRenderers[fontRenderer]->getCacheSize(format);
169     }
170 
describe(ProgramDescription & description,const SkPaint * paint)171     void describe(ProgramDescription& description, const SkPaint* paint) const override {
172     }
173 
setupProgram(ProgramDescription & description,Program & program)174     void setupProgram(ProgramDescription& description, Program& program) const override {
175     }
176 
177     void endPrecaching() override;
178 
179 private:
180     Lookup3GammaFontRenderer();
181 
182     enum Gamma {
183         kGammaDefault = 0,
184         kGammaBlack = 1,
185         kGammaWhite = 2,
186         kGammaCount = 3
187     };
188 
189     FontRenderer* getRenderer(Gamma gamma);
190 
191     uint32_t mRenderersUsageCount[kGammaCount];
192     std::unique_ptr<FontRenderer> mRenderers[kGammaCount];
193 
194     uint8_t mGammaTable[256 * kGammaCount];
195 
196     friend class GammaFontRenderer;
197 };
198 
199 }; // namespace uirenderer
200 }; // namespace android
201 
202 #endif // ANDROID_HWUI_GAMMA_FONT_RENDERER_H
203