Home | History | Annotate | Download | only in hwui
      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:
     58     ~ShaderGammaFontRenderer() {
     59         delete mRenderer;
     60     }
     61 
     62     void clear() {
     63         delete mRenderer;
     64         mRenderer = NULL;
     65     }
     66 
     67     void flush() {
     68         if (mRenderer) {
     69             mRenderer->flushLargeCaches();
     70         }
     71     }
     72 
     73     FontRenderer& getFontRenderer(const SkPaint* paint) {
     74         if (!mRenderer) {
     75             mRenderer = new FontRenderer;
     76         }
     77         return *mRenderer;
     78     }
     79 
     80     uint32_t getFontRendererCount() const {
     81         return 1;
     82     }
     83 
     84     uint32_t getFontRendererSize(uint32_t fontRenderer, GLenum format) const {
     85         return mRenderer ? mRenderer->getCacheSize(format) : 0;
     86     }
     87 
     88     void describe(ProgramDescription& description, const SkPaint* paint) const;
     89     void setupProgram(ProgramDescription& description, Program* program) const;
     90 
     91     void endPrecaching();
     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:
    104     ~LookupGammaFontRenderer() {
    105         delete mRenderer;
    106     }
    107 
    108     void clear() {
    109         delete mRenderer;
    110         mRenderer = NULL;
    111     }
    112 
    113     void flush() {
    114         if (mRenderer) {
    115             mRenderer->flushLargeCaches();
    116         }
    117     }
    118 
    119     FontRenderer& getFontRenderer(const SkPaint* paint) {
    120         if (!mRenderer) {
    121             mRenderer = new FontRenderer;
    122             mRenderer->setGammaTable(&mGammaTable[0]);
    123         }
    124         return *mRenderer;
    125     }
    126 
    127     uint32_t getFontRendererCount() const {
    128         return 1;
    129     }
    130 
    131     uint32_t getFontRendererSize(uint32_t fontRenderer, GLenum format) const {
    132         return mRenderer ? mRenderer->getCacheSize(format) : 0;
    133     }
    134 
    135     void describe(ProgramDescription& description, const SkPaint* paint) const {
    136     }
    137 
    138     void setupProgram(ProgramDescription& description, Program* program) const {
    139     }
    140 
    141     void endPrecaching();
    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     ~Lookup3GammaFontRenderer();
    155 
    156     void clear();
    157     void flush();
    158 
    159     FontRenderer& getFontRenderer(const SkPaint* paint);
    160 
    161     uint32_t getFontRendererCount() const {
    162         return kGammaCount;
    163     }
    164 
    165     uint32_t getFontRendererSize(uint32_t fontRenderer, GLenum format) const {
    166         if (fontRenderer >= kGammaCount) return 0;
    167 
    168         FontRenderer* renderer = mRenderers[fontRenderer];
    169         if (!renderer) return 0;
    170 
    171         return renderer->getCacheSize(format);
    172     }
    173 
    174     void describe(ProgramDescription& description, const SkPaint* paint) const {
    175     }
    176 
    177     void setupProgram(ProgramDescription& description, Program* program) const {
    178     }
    179 
    180     void endPrecaching();
    181 
    182 private:
    183     Lookup3GammaFontRenderer();
    184 
    185     enum Gamma {
    186         kGammaDefault = 0,
    187         kGammaBlack = 1,
    188         kGammaWhite = 2,
    189         kGammaCount = 3
    190     };
    191 
    192     FontRenderer* getRenderer(Gamma gamma);
    193 
    194     uint32_t mRenderersUsageCount[kGammaCount];
    195     FontRenderer* mRenderers[kGammaCount];
    196 
    197     uint8_t mGammaTable[256 * kGammaCount];
    198 
    199     friend class GammaFontRenderer;
    200 };
    201 
    202 }; // namespace uirenderer
    203 }; // namespace android
    204 
    205 #endif // ANDROID_HWUI_GAMMA_FONT_RENDERER_H
    206