2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "font/CacheTexture.h"
20 #include "font/CachedGlyphInfo.h"
21 #include "font/Font.h"
22 #include "font/FontUtil.h"
23 #ifdef BUGREPORT_FONT_CACHE_USAGE
24 #include "font/FontCacheHistoryTracker.h"
27 #include <utils/LruCache.h>
28 #include <utils/String8.h>
29 #include <utils/StrongPointer.h>
33 #include <GLES2/gl2.h>
37 #include "RenderScript.h"
41 class ScriptIntrinsicBlur;
46 namespace uirenderer {
49 class BakedOpRenderer;
52 class TextDrawFunctor {
54 TextDrawFunctor(BakedOpRenderer* renderer, const BakedOpState* bakedState, const ClipBase* clip,
55 float x, float y, bool pureTranslate, int alpha, SkBlendMode mode,
58 , bakedState(bakedState)
62 , pureTranslate(pureTranslate)
67 void draw(CacheTexture& texture, bool linearFiltering);
69 BakedOpRenderer* renderer;
70 const BakedOpState* bakedState;
82 explicit FontRenderer(const uint8_t* gammaTable);
85 void flushLargeCaches(std::vector<CacheTexture*>& cacheTextures);
86 void flushLargeCaches();
88 void setFont(const SkPaint* paint, const SkMatrix& matrix);
90 void precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs,
91 const SkMatrix& matrix);
94 bool renderPosText(const SkPaint* paint, const Rect* clip, const glyph_t* glyphs, int numGlyphs,
95 int x, int y, const float* positions, Rect* outBounds,
96 TextDrawFunctor* functor, bool forceFinish = true);
98 bool renderTextOnPath(const SkPaint* paint, const Rect* clip, const glyph_t* glyphs,
99 int numGlyphs, const SkPath* path, float hOffset, float vOffset,
100 Rect* outBounds, TextDrawFunctor* functor);
110 // After renderDropShadow returns, the called owns the memory in DropShadow.image
111 // and is responsible for releasing it when it's done with it
112 DropShadow renderDropShadow(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs,
113 float radius, const float* positions);
115 void setTextureFiltering(bool linearFiltering) { mLinearFiltering = linearFiltering; }
117 uint32_t getSize() const;
118 void dumpMemoryUsage(String8& log) const;
120 #ifdef BUGREPORT_FONT_CACHE_USAGE
121 FontCacheHistoryTracker& historyTracker() { return mHistoryTracker; }
127 const uint8_t* mGammaTable;
129 void allocateTextureMemory(CacheTexture* cacheTexture);
130 void deallocateTextureMemory(CacheTexture* cacheTexture);
131 void initTextTexture();
132 CacheTexture* createCacheTexture(int width, int height, GLenum format, bool allocate);
133 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, uint32_t* retOriginX,
134 uint32_t* retOriginY, bool precaching);
135 CacheTexture* cacheBitmapInTexture(std::vector<CacheTexture*>& cacheTextures,
136 const SkGlyph& glyph, uint32_t* startX, uint32_t* startY);
138 void flushAllAndInvalidate();
141 void initRender(const Rect* clip, Rect* bounds, TextDrawFunctor* functor);
144 void issueDrawCommand(std::vector<CacheTexture*>& cacheTextures);
145 void issueDrawCommand();
146 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1, float x2, float y2, float u2,
147 float v2, float x3, float y3, float u3, float v3, float x4, float y4,
148 float u4, float v4, CacheTexture* texture);
149 void appendMeshQuad(float x1, float y1, float u1, float v1, float x2, float y2, float u2,
150 float v2, float x3, float y3, float u3, float v3, float x4, float y4,
151 float u4, float v4, CacheTexture* texture);
152 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1, float x2, float y2, float u2,
153 float v2, float x3, float y3, float u3, float v3, float x4, float y4,
154 float u4, float v4, CacheTexture* texture);
156 void checkTextureUpdate();
158 void setTextureDirty() { mUploadTexture = true; }
160 const std::vector<CacheTexture*>& cacheTexturesForFormat(GLenum format) const;
161 uint32_t getCacheSize(GLenum format) const;
162 uint32_t getFreeCacheSize(GLenum format) const;
164 uint32_t mSmallCacheWidth;
165 uint32_t mSmallCacheHeight;
166 uint32_t mLargeCacheWidth;
167 uint32_t mLargeCacheHeight;
169 std::vector<CacheTexture*> mACacheTextures;
170 std::vector<CacheTexture*> mRGBACacheTextures;
173 LruCache<Font::FontDescription, Font*> mActiveFonts;
175 CacheTexture* mCurrentCacheTexture;
179 TextDrawFunctor* mFunctor;
186 bool mLinearFiltering;
188 #ifdef BUGREPORT_FONT_CACHE_USAGE
189 FontCacheHistoryTracker mHistoryTracker;
193 RSC::sp<RSC::RS> mRs;
194 RSC::sp<const RSC::Element> mRsElement;
195 RSC::sp<RSC::ScriptIntrinsicBlur> mRsScript;
197 static void computeGaussianWeights(float* weights, int32_t radius);
198 static void horizontalBlur(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest,
199 int32_t width, int32_t height);
200 static void verticalBlur(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest,
201 int32_t width, int32_t height);
203 // the input image handle may have its pointer replaced (to avoid copies)
204 void blurImage(uint8_t** image, int32_t width, int32_t height, float radius);
207 }; // namespace uirenderer
208 }; // namespace android