diff options
Diffstat (limited to 'libs/hwui/FontRenderer.h')
-rw-r--r-- | libs/hwui/FontRenderer.h | 234 |
1 files changed, 137 insertions, 97 deletions
diff --git a/libs/hwui/FontRenderer.h b/libs/hwui/FontRenderer.h index 1922812..b767be5 100644 --- a/libs/hwui/FontRenderer.h +++ b/libs/hwui/FontRenderer.h @@ -55,6 +55,79 @@ namespace uirenderer { class FontRenderer; +class CacheTexture { +public: + CacheTexture(){} + CacheTexture(uint8_t* texture, GLuint textureId, uint16_t width, uint16_t height) : + mTexture(texture), mTextureId(textureId), mWidth(width), mHeight(height), + mLinearFiltering(false) {} + ~CacheTexture() { + if (mTexture != NULL) { + delete[] mTexture; + } + if (mTextureId != 0) { + glDeleteTextures(1, &mTextureId); + } + } + + uint8_t* mTexture; + GLuint mTextureId; + uint16_t mWidth; + uint16_t mHeight; + bool mLinearFiltering; +}; + +class CacheTextureLine { +public: + CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow, + uint32_t currentCol, CacheTexture* cacheTexture): + mMaxHeight(maxHeight), + mMaxWidth(maxWidth), + mCurrentRow(currentRow), + mCurrentCol(currentCol), + mDirty(false), + mCacheTexture(cacheTexture){ + } + + bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY); + + uint16_t mMaxHeight; + uint16_t mMaxWidth; + uint32_t mCurrentRow; + uint32_t mCurrentCol; + bool mDirty; + CacheTexture *mCacheTexture; +}; + +struct CachedGlyphInfo { + // Has the cache been invalidated? + bool mIsValid; + // Location of the cached glyph in the bitmap + // in case we need to resize the texture or + // render to bitmap + uint32_t mStartX; + uint32_t mStartY; + uint32_t mBitmapWidth; + uint32_t mBitmapHeight; + // Also cache texture coords for the quad + float mBitmapMinU; + float mBitmapMinV; + float mBitmapMaxU; + float mBitmapMaxV; + // Minimize how much we call freetype + uint32_t mGlyphIndex; + uint32_t mAdvanceX; + uint32_t mAdvanceY; + // Values below contain a glyph's origin in the bitmap + int32_t mBitmapLeft; + int32_t mBitmapTop; + // Auto-kerning + SkFixed mLsbDelta; + SkFixed mRsbDelta; + CacheTextureLine* mCachedTextureLine; +}; + + /////////////////////////////////////////////////////////////////////////////// // Font /////////////////////////////////////////////////////////////////////////////// @@ -78,6 +151,10 @@ public: void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, int numGlyphs, int x, int y, uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0); + + void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, + int numGlyphs, int x, int y, const float* positions); + /** * Creates a new font associated with the specified font state. */ @@ -87,6 +164,8 @@ public: protected: friend class FontRenderer; + typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*, + uint32_t, uint32_t, Rect*, const float*); enum RenderMode { FRAMEBUFFER, @@ -96,52 +175,31 @@ protected: void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap, - uint32_t bitmapW, uint32_t bitmapH, Rect *bounds); + uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions); void measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len, int numGlyphs, Rect *bounds); - struct CachedGlyphInfo { - // Has the cache been invalidated? - bool mIsValid; - // Location of the cached glyph in the bitmap - // in case we need to resize the texture or - // render to bitmap - uint32_t mStartX; - uint32_t mStartY; - uint32_t mBitmapWidth; - uint32_t mBitmapHeight; - // Also cache texture coords for the quad - float mBitmapMinU; - float mBitmapMinV; - float mBitmapMaxU; - float mBitmapMaxV; - // Minimize how much we call freetype - uint32_t mGlyphIndex; - uint32_t mAdvanceX; - uint32_t mAdvanceY; - // Values below contain a glyph's origin in the bitmap - int32_t mBitmapLeft; - int32_t mBitmapTop; - // Auto-kerning - SkFixed mLsbDelta; - SkFixed mRsbDelta; - }; - Font(FontRenderer* state, uint32_t fontId, float fontSize, int flags, uint32_t italicStyle, uint32_t scaleX, SkPaint::Style style, uint32_t strokeWidth); // Cache of glyphs DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs; - void invalidateTextureCache(); + void invalidateTextureCache(CacheTextureLine *cacheLine = NULL); CachedGlyphInfo* cacheGlyph(SkPaint* paint, glyph_t glyph); - void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph); - void measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, Rect *bounds); - void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); - void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y, - uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH); + void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph); + + void measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y, + uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, + Rect* bounds, const float* pos); + void drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y, + uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, + Rect* bounds, const float* pos); + void drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, + uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, + Rect* bounds, const float* pos); CachedGlyphInfo* getCachedGlyph(SkPaint* paint, glyph_t textUnit); @@ -173,19 +231,19 @@ public: void init(); void deinit(); + void flushLargeCaches(); void setGammaTable(const uint8_t* gammaTable) { mGammaTable = gammaTable; } - void setAttributeBindingSlots(int positionSlot, int texCoordSlot) { - mPositionAttrSlot = positionSlot; - mTexcoordAttrSlot = texCoordSlot; - } - void setFont(SkPaint* paint, uint32_t fontId, float fontSize); + // bounds is an out parameter bool renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, Rect* bounds); + // bounds is an out parameter + bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, + uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds); struct DropShadow { DropShadow() { }; @@ -210,23 +268,33 @@ public: GLuint getTexture(bool linearFiltering = false) { checkInit(); - if (linearFiltering != mLinearFiltering) { + if (linearFiltering != mCurrentCacheTexture->mLinearFiltering) { + mCurrentCacheTexture->mLinearFiltering = linearFiltering; mLinearFiltering = linearFiltering; const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST; - glBindTexture(GL_TEXTURE_2D, mTextureId); + glBindTexture(GL_TEXTURE_2D, mCurrentCacheTexture->mTextureId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering); } - return mTextureId; - } - - uint32_t getCacheWidth() const { - return mCacheWidth; + return mCurrentCacheTexture->mTextureId; } - uint32_t getCacheHeight() const { - return mCacheHeight; + uint32_t getCacheSize() const { + uint32_t size = 0; + if (mCacheTextureSmall != NULL && mCacheTextureSmall->mTexture != NULL) { + size += mCacheTextureSmall->mWidth * mCacheTextureSmall->mHeight; + } + if (mCacheTexture128 != NULL && mCacheTexture128->mTexture != NULL) { + size += mCacheTexture128->mWidth * mCacheTexture128->mHeight; + } + if (mCacheTexture256 != NULL && mCacheTexture256->mTexture != NULL) { + size += mCacheTexture256->mWidth * mCacheTexture256->mHeight; + } + if (mCacheTexture512 != NULL && mCacheTexture512->mTexture != NULL) { + size += mCacheTexture512->mWidth * mCacheTexture512->mHeight; + } + return size; } protected: @@ -234,57 +302,31 @@ protected: const uint8_t* mGammaTable; - struct CacheTextureLine { - uint16_t mMaxHeight; - uint16_t mMaxWidth; - uint32_t mCurrentRow; - uint32_t mCurrentCol; - bool mDirty; - - CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow, - uint32_t currentCol): - mMaxHeight(maxHeight), - mMaxWidth(maxWidth), - mCurrentRow(currentRow), - mCurrentCol(currentCol), - mDirty(false) { - } - - bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) { - if (glyph.fHeight + 2 > mMaxHeight) { - return false; - } - - if (mCurrentCol + glyph.fWidth + 2 < mMaxWidth) { - *retOriginX = mCurrentCol + 1; - *retOriginY = mCurrentRow + 1; - mCurrentCol += glyph.fWidth + 2; - mDirty = true; - return true; - } - - return false; - } - }; - - void initTextTexture(bool largeFonts = false); - bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY); + void allocateTextureMemory(CacheTexture* cacheTexture); + void deallocateTextureMemory(CacheTexture* cacheTexture); + void initTextTexture(); + CacheTexture *createCacheTexture(int width, int height, bool allocate); + void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, + uint32_t *retOriginX, uint32_t *retOriginY); void flushAllAndInvalidate(); void initVertexArrayBuffers(); void checkInit(); + void initRender(const Rect* clip, Rect* bounds); + void finishRender(); String16 mLatinPrecache; void precacheLatin(SkPaint* paint); void issueDrawCommand(); - void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2, - float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, - float x4, float y4, float z4, float u4, float v4); + void appendMeshQuad(float x1, float y1, float u1, float v1, + float x2, float y2, float u2, float v2, + float x3, float y3, float u3, float v3, + float x4, float y4, float u4, float v4, CacheTexture* texture); - uint32_t mCacheWidth; - uint32_t mCacheHeight; + uint32_t mSmallCacheWidth; + uint32_t mSmallCacheHeight; Vector<CacheTextureLine*> mCacheLines; uint32_t getRemainingCacheCapacity(); @@ -292,12 +334,13 @@ protected: Font* mCurrentFont; Vector<Font*> mActiveFonts; - // Texture to cache glyph bitmaps - uint8_t* mTextTexture; - const uint8_t* getTextTextureData() const { - return mTextTexture; - } - GLuint mTextureId; + CacheTexture* mCurrentCacheTexture; + CacheTexture* mLastCacheTexture; + CacheTexture* mCacheTextureSmall; + CacheTexture* mCacheTexture128; + CacheTexture* mCacheTexture256; + CacheTexture* mCacheTexture512; + void checkTextureUpdate(); bool mUploadTexture; @@ -308,9 +351,6 @@ protected: uint32_t mIndexBufferID; - int32_t mPositionAttrSlot; - int32_t mTexcoordAttrSlot; - const Rect* mClip; Rect* mBounds; bool mDrawn; |