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