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_FONT_RENDERER_H
     18 #define ANDROID_HWUI_FONT_RENDERER_H
     19 
     20 #include "font/FontUtil.h"
     21 #include "font/CacheTexture.h"
     22 #include "font/CachedGlyphInfo.h"
     23 #include "font/Font.h"
     24 #ifdef BUGREPORT_FONT_CACHE_USAGE
     25 #include "font/FontCacheHistoryTracker.h"
     26 #endif
     27 
     28 #include <utils/LruCache.h>
     29 #include <utils/String8.h>
     30 #include <utils/StrongPointer.h>
     31 
     32 #include <SkPaint.h>
     33 
     34 #include <GLES2/gl2.h>
     35 
     36 #include <vector>
     37 
     38 #ifdef ANDROID_ENABLE_RENDERSCRIPT
     39 #include "RenderScript.h"
     40 namespace RSC {
     41     class Element;
     42     class RS;
     43     class ScriptIntrinsicBlur;
     44     class sp;
     45 }
     46 #endif
     47 
     48 namespace android {
     49 namespace uirenderer {
     50 
     51 #if HWUI_NEW_OPS
     52 class BakedOpState;
     53 class BakedOpRenderer;
     54 struct ClipBase;
     55 #else
     56 class OpenGLRenderer;
     57 #endif
     58 
     59 class TextDrawFunctor {
     60 public:
     61     TextDrawFunctor(
     62 #if HWUI_NEW_OPS
     63             BakedOpRenderer* renderer,
     64             const BakedOpState* bakedState,
     65             const ClipBase* clip,
     66 #else
     67             OpenGLRenderer* renderer,
     68 #endif
     69             float x, float y, bool pureTranslate,
     70             int alpha, SkXfermode::Mode mode, const SkPaint* paint)
     71         : renderer(renderer)
     72 #if HWUI_NEW_OPS
     73         , bakedState(bakedState)
     74         , clip(clip)
     75 #endif
     76         , x(x)
     77         , y(y)
     78         , pureTranslate(pureTranslate)
     79         , alpha(alpha)
     80         , mode(mode)
     81         , paint(paint) {
     82     }
     83 
     84     void draw(CacheTexture& texture, bool linearFiltering);
     85 
     86 #if HWUI_NEW_OPS
     87     BakedOpRenderer* renderer;
     88     const BakedOpState* bakedState;
     89     const ClipBase* clip;
     90 #else
     91     OpenGLRenderer* renderer;
     92 #endif
     93     float x;
     94     float y;
     95     bool pureTranslate;
     96     int alpha;
     97     SkXfermode::Mode mode;
     98     const SkPaint* paint;
     99 };
    100 
    101 class FontRenderer {
    102 public:
    103     FontRenderer(const uint8_t* gammaTable);
    104     ~FontRenderer();
    105 
    106     void flushLargeCaches(std::vector<CacheTexture*>& cacheTextures);
    107     void flushLargeCaches();
    108 
    109     void setFont(const SkPaint* paint, const SkMatrix& matrix);
    110 
    111     void precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, const SkMatrix& matrix);
    112     void endPrecaching();
    113 
    114     bool renderPosText(const SkPaint* paint, const Rect* clip, const glyph_t* glyphs,
    115             int numGlyphs, int x, int y, const float* positions,
    116             Rect* outBounds, TextDrawFunctor* functor, bool forceFinish = true);
    117 
    118     bool renderTextOnPath(const SkPaint* paint, const Rect* clip, const glyph_t* glyphs,
    119             int numGlyphs, const SkPath* path,
    120             float hOffset, float vOffset, Rect* outBounds, TextDrawFunctor* functor);
    121 
    122     struct DropShadow {
    123         uint32_t width;
    124         uint32_t height;
    125         uint8_t* image;
    126         int32_t penX;
    127         int32_t penY;
    128     };
    129 
    130     // After renderDropShadow returns, the called owns the memory in DropShadow.image
    131     // and is responsible for releasing it when it's done with it
    132     DropShadow renderDropShadow(const SkPaint* paint, const glyph_t *glyphs, int numGlyphs,
    133             float radius, const float* positions);
    134 
    135     void setTextureFiltering(bool linearFiltering) {
    136         mLinearFiltering = linearFiltering;
    137     }
    138 
    139     uint32_t getSize() const;
    140     void dumpMemoryUsage(String8& log) const;
    141 
    142 #ifdef BUGREPORT_FONT_CACHE_USAGE
    143     FontCacheHistoryTracker& historyTracker() { return mHistoryTracker; }
    144 #endif
    145 
    146 private:
    147     friend class Font;
    148 
    149     const uint8_t* mGammaTable;
    150 
    151     void allocateTextureMemory(CacheTexture* cacheTexture);
    152     void deallocateTextureMemory(CacheTexture* cacheTexture);
    153     void initTextTexture();
    154     CacheTexture* createCacheTexture(int width, int height, GLenum format, bool allocate);
    155     void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
    156             uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
    157     CacheTexture* cacheBitmapInTexture(std::vector<CacheTexture*>& cacheTextures, const SkGlyph& glyph,
    158             uint32_t* startX, uint32_t* startY);
    159 
    160     void flushAllAndInvalidate();
    161 
    162     void checkInit();
    163     void initRender(const Rect* clip, Rect* bounds, TextDrawFunctor* functor);
    164     void finishRender();
    165 
    166     void issueDrawCommand(std::vector<CacheTexture*>& cacheTextures);
    167     void issueDrawCommand();
    168     void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
    169             float x2, float y2, float u2, float v2,
    170             float x3, float y3, float u3, float v3,
    171             float x4, float y4, float u4, float v4, CacheTexture* texture);
    172     void appendMeshQuad(float x1, float y1, float u1, float v1,
    173             float x2, float y2, float u2, float v2,
    174             float x3, float y3, float u3, float v3,
    175             float x4, float y4, float u4, float v4, CacheTexture* texture);
    176     void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
    177             float x2, float y2, float u2, float v2,
    178             float x3, float y3, float u3, float v3,
    179             float x4, float y4, float u4, float v4, CacheTexture* texture);
    180 
    181     void checkTextureUpdate();
    182 
    183     void setTextureDirty() {
    184         mUploadTexture = true;
    185     }
    186 
    187     const std::vector<CacheTexture*>& cacheTexturesForFormat(GLenum format) const;
    188     uint32_t getCacheSize(GLenum format) const;
    189     uint32_t getFreeCacheSize(GLenum format) const;
    190 
    191     uint32_t mSmallCacheWidth;
    192     uint32_t mSmallCacheHeight;
    193     uint32_t mLargeCacheWidth;
    194     uint32_t mLargeCacheHeight;
    195 
    196     std::vector<CacheTexture*> mACacheTextures;
    197     std::vector<CacheTexture*> mRGBACacheTextures;
    198 
    199     Font* mCurrentFont;
    200     LruCache<Font::FontDescription, Font*> mActiveFonts;
    201 
    202     CacheTexture* mCurrentCacheTexture;
    203 
    204     bool mUploadTexture;
    205 
    206     TextDrawFunctor* mFunctor;
    207     const Rect* mClip;
    208     Rect* mBounds;
    209     bool mDrawn;
    210 
    211     bool mInitialized;
    212 
    213     bool mLinearFiltering;
    214 
    215 #ifdef BUGREPORT_FONT_CACHE_USAGE
    216     FontCacheHistoryTracker mHistoryTracker;
    217 #endif
    218 
    219 #ifdef ANDROID_ENABLE_RENDERSCRIPT
    220     // RS constructs
    221     RSC::sp<RSC::RS> mRs;
    222     RSC::sp<const RSC::Element> mRsElement;
    223     RSC::sp<RSC::ScriptIntrinsicBlur> mRsScript;
    224 #endif
    225 
    226     static void computeGaussianWeights(float* weights, int32_t radius);
    227     static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
    228             int32_t width, int32_t height);
    229     static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
    230             int32_t width, int32_t height);
    231 
    232     // the input image handle may have its pointer replaced (to avoid copies)
    233     void blurImage(uint8_t** image, int32_t width, int32_t height, float radius);
    234 };
    235 
    236 }; // namespace uirenderer
    237 }; // namespace android
    238 
    239 #endif // ANDROID_HWUI_FONT_RENDERER_H
    240