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 <utils/LruCache.h>
     21 #include <utils/Vector.h>
     22 
     23 #include <SkPaint.h>
     24 
     25 #include <GLES2/gl2.h>
     26 
     27 #include "font/FontUtil.h"
     28 #include "font/CacheTexture.h"
     29 #include "font/CachedGlyphInfo.h"
     30 #include "font/Font.h"
     31 #include "utils/SortedList.h"
     32 #include "Matrix.h"
     33 #include "Properties.h"
     34 
     35 namespace RSC {
     36     class Element;
     37     class RS;
     38     class ScriptIntrinsicBlur;
     39 }
     40 
     41 class Functor;
     42 
     43 namespace android {
     44 namespace uirenderer {
     45 
     46 ///////////////////////////////////////////////////////////////////////////////
     47 // Renderer
     48 ///////////////////////////////////////////////////////////////////////////////
     49 
     50 class FontRenderer {
     51 public:
     52     FontRenderer();
     53     ~FontRenderer();
     54 
     55     void flushLargeCaches();
     56 
     57     void setGammaTable(const uint8_t* gammaTable) {
     58         mGammaTable = gammaTable;
     59     }
     60 
     61     void setFont(SkPaint* paint, const mat4& matrix);
     62 
     63     void precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix);
     64     void endPrecaching();
     65 
     66     // bounds is an out parameter
     67     bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
     68             uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds,
     69             Functor* functor, bool forceFinish = true);
     70 
     71     // bounds is an out parameter
     72     bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
     73             uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds);
     74 
     75     struct DropShadow {
     76         DropShadow() { };
     77 
     78         DropShadow(const DropShadow& dropShadow):
     79             width(dropShadow.width), height(dropShadow.height),
     80             image(dropShadow.image), penX(dropShadow.penX),
     81             penY(dropShadow.penY) {
     82         }
     83 
     84         uint32_t width;
     85         uint32_t height;
     86         uint8_t* image;
     87         int32_t penX;
     88         int32_t penY;
     89     };
     90 
     91     // After renderDropShadow returns, the called owns the memory in DropShadow.image
     92     // and is responsible for releasing it when it's done with it
     93     DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
     94             uint32_t len, int numGlyphs, uint32_t radius, const float* positions);
     95 
     96     void setTextureFiltering(bool linearFiltering) {
     97         mLinearFiltering = linearFiltering;
     98     }
     99 
    100     uint32_t getCacheSize() const;
    101 
    102 private:
    103     friend class Font;
    104 
    105     static const uint32_t gMaxNumberOfQuads = 2048;
    106 
    107     const uint8_t* mGammaTable;
    108 
    109     void allocateTextureMemory(CacheTexture* cacheTexture);
    110     void deallocateTextureMemory(CacheTexture* cacheTexture);
    111     void initTextTexture();
    112     CacheTexture* createCacheTexture(int width, int height, bool allocate);
    113     void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
    114             uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
    115     CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY);
    116 
    117     void flushAllAndInvalidate();
    118     void initVertexArrayBuffers();
    119 
    120     void checkInit();
    121     void initRender(const Rect* clip, Rect* bounds, Functor* functor);
    122     void finishRender();
    123 
    124     void issueDrawCommand();
    125     void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
    126             float x2, float y2, float u2, float v2,
    127             float x3, float y3, float u3, float v3,
    128             float x4, float y4, float u4, float v4, CacheTexture* texture);
    129     void appendMeshQuad(float x1, float y1, float u1, float v1,
    130             float x2, float y2, float u2, float v2,
    131             float x3, float y3, float u3, float v3,
    132             float x4, float y4, float u4, float v4, CacheTexture* texture);
    133     void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
    134             float x2, float y2, float u2, float v2,
    135             float x3, float y3, float u3, float v3,
    136             float x4, float y4, float u4, float v4, CacheTexture* texture);
    137 
    138     void removeFont(const Font* font);
    139 
    140     void checkTextureUpdate();
    141 
    142     void setTextureDirty() {
    143         mUploadTexture = true;
    144     }
    145 
    146     uint32_t mSmallCacheWidth;
    147     uint32_t mSmallCacheHeight;
    148     uint32_t mLargeCacheWidth;
    149     uint32_t mLargeCacheHeight;
    150 
    151     Vector<CacheTexture*> mCacheTextures;
    152 
    153     Font* mCurrentFont;
    154     LruCache<Font::FontDescription, Font*> mActiveFonts;
    155 
    156     CacheTexture* mCurrentCacheTexture;
    157 
    158     bool mUploadTexture;
    159 
    160     uint32_t mIndexBufferID;
    161 
    162     Functor* mFunctor;
    163     const Rect* mClip;
    164     Rect* mBounds;
    165     bool mDrawn;
    166 
    167     bool mInitialized;
    168 
    169     bool mLinearFiltering;
    170 
    171     // RS constructs
    172     sp<RSC::RS> mRs;
    173     sp<const RSC::Element> mRsElement;
    174     sp<RSC::ScriptIntrinsicBlur> mRsScript;
    175 
    176     static void computeGaussianWeights(float* weights, int32_t radius);
    177     static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
    178             int32_t width, int32_t height);
    179     static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
    180             int32_t width, int32_t height);
    181 
    182     // the input image handle may have its pointer replaced (to avoid copies)
    183     void blurImage(uint8_t** image, int32_t width, int32_t height, int32_t radius);
    184 };
    185 
    186 }; // namespace uirenderer
    187 }; // namespace android
    188 
    189 #endif // ANDROID_HWUI_FONT_RENDERER_H
    190