blob: 442f4e2714a2c13276ed572b65293a9e6043358a [file] [log] [blame]
Romain Guy694b5192010-07-21 21:33:20 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_FONT_RENDERER_H
18#define ANDROID_HWUI_FONT_RENDERER_H
Romain Guy694b5192010-07-21 21:33:20 -070019
Romain Guye3a9b242013-01-08 11:15:30 -080020#include <utils/LruCache.h>
Romain Guy694b5192010-07-21 21:33:20 -070021#include <utils/Vector.h>
Romain Guy694b5192010-07-21 21:33:20 -070022
Romain Guy694b5192010-07-21 21:33:20 -070023#include <SkPaint.h>
24
25#include <GLES2/gl2.h>
26
Romain Guy9f5dab32012-09-04 12:55:44 -070027#include "font/FontUtil.h"
28#include "font/CacheTexture.h"
29#include "font/CachedGlyphInfo.h"
30#include "font/Font.h"
Romain Guy115096f2013-03-19 11:32:41 -070031#include "utils/SortedList.h"
Romain Guye3a9b242013-01-08 11:15:30 -080032#include "Matrix.h"
Romain Guy51769a62010-07-23 00:28:00 -070033#include "Properties.h"
Romain Guy09147fb2010-07-22 13:08:20 -070034
Chris Craikf2d8ccc2013-02-13 16:14:17 -080035namespace RSC {
36 class Element;
37 class RS;
38 class ScriptIntrinsicBlur;
39}
40
Romain Guy694b5192010-07-21 21:33:20 -070041namespace android {
42namespace uirenderer {
43
Romain Guy726aeba2011-06-01 14:52:00 -070044///////////////////////////////////////////////////////////////////////////////
Romain Guy726aeba2011-06-01 14:52:00 -070045// Renderer
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy694b5192010-07-21 21:33:20 -070048class FontRenderer {
49public:
50 FontRenderer();
51 ~FontRenderer();
52
Chet Haase9a824562011-12-16 15:44:59 -080053 void flushLargeCaches();
Romain Guy694b5192010-07-21 21:33:20 -070054
Romain Guyb45c0c92010-08-26 20:35:23 -070055 void setGammaTable(const uint8_t* gammaTable) {
56 mGammaTable = gammaTable;
57 }
58
Romain Guye3a9b242013-01-08 11:15:30 -080059 void setFont(SkPaint* paint, const mat4& matrix);
Chet Haasee816bae2012-08-09 13:39:02 -070060
Romain Guye3a9b242013-01-08 11:15:30 -080061 void precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix);
Chet Haasee816bae2012-08-09 13:39:02 -070062
Romain Guy671d6cf2012-01-18 12:39:17 -080063 // bounds is an out parameter
Romain Guy671d6cf2012-01-18 12:39:17 -080064 bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
65 uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds);
Romain Guy97771732012-02-28 18:17:02 -080066 // bounds is an out parameter
67 bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
68 uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds);
Romain Guy694b5192010-07-21 21:33:20 -070069
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070070 struct DropShadow {
Romain Guy1e45aae2010-08-13 19:39:53 -070071 DropShadow() { };
72
73 DropShadow(const DropShadow& dropShadow):
74 width(dropShadow.width), height(dropShadow.height),
75 image(dropShadow.image), penX(dropShadow.penX),
76 penY(dropShadow.penY) {
77 }
78
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070079 uint32_t width;
80 uint32_t height;
81 uint8_t* image;
82 int32_t penX;
83 int32_t penY;
84 };
85
86 // After renderDropShadow returns, the called owns the memory in DropShadow.image
87 // and is responsible for releasing it when it's done with it
88 DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
Raph Levien416a8472012-07-19 22:48:17 -070089 uint32_t len, int numGlyphs, uint32_t radius, const float* positions);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070090
Romain Guye8cb9c142010-10-04 14:14:11 -070091 GLuint getTexture(bool linearFiltering = false) {
Romain Guy694b5192010-07-21 21:33:20 -070092 checkInit();
Romain Guyae91c4c2012-05-14 14:00:27 -070093
Romain Guy80872462012-09-04 16:42:01 -070094 mCurrentCacheTexture->setLinearFiltering(linearFiltering);
95 mLinearFiltering = linearFiltering;
Romain Guye8cb9c142010-10-04 14:14:11 -070096
Romain Guy80872462012-09-04 16:42:01 -070097 return mCurrentCacheTexture->getTextureId();
Romain Guy694b5192010-07-21 21:33:20 -070098 }
99
Chet Haase7de0cb12011-12-05 16:35:38 -0800100 uint32_t getCacheSize() const {
101 uint32_t size = 0;
Chet Haase378e9192012-08-15 15:54:54 -0700102 for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
103 CacheTexture* cacheTexture = mCacheTextures[i];
Romain Guy80872462012-09-04 16:42:01 -0700104 if (cacheTexture && cacheTexture->getTexture()) {
105 size += cacheTexture->getWidth() * cacheTexture->getHeight();
Chet Haase378e9192012-08-15 15:54:54 -0700106 }
Chet Haase7de0cb12011-12-05 16:35:38 -0800107 }
108 return size;
Romain Guyc15008e2010-11-10 11:59:15 -0800109 }
110
Romain Guy9b1204b2012-09-04 15:22:57 -0700111private:
Romain Guy694b5192010-07-21 21:33:20 -0700112 friend class Font;
113
Romain Guyb45c0c92010-08-26 20:35:23 -0700114 const uint8_t* mGammaTable;
115
Chet Haase2a47c142011-12-14 15:22:56 -0800116 void allocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase9a824562011-12-16 15:44:59 -0800117 void deallocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase7de0cb12011-12-05 16:35:38 -0800118 void initTextTexture();
Romain Guy97771732012-02-28 18:17:02 -0800119 CacheTexture* createCacheTexture(int width, int height, bool allocate);
Chet Haase7de0cb12011-12-05 16:35:38 -0800120 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
Chet Haasef942cf12012-08-30 09:06:46 -0700121 uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
Chet Haase378e9192012-08-15 15:54:54 -0700122 CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY);
Romain Guy694b5192010-07-21 21:33:20 -0700123
124 void flushAllAndInvalidate();
125 void initVertexArrayBuffers();
126
127 void checkInit();
Romain Guy671d6cf2012-01-18 12:39:17 -0800128 void initRender(const Rect* clip, Rect* bounds);
129 void finishRender();
Romain Guy694b5192010-07-21 21:33:20 -0700130
131 void issueDrawCommand();
Romain Guy97771732012-02-28 18:17:02 -0800132 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
133 float x2, float y2, float u2, float v2,
134 float x3, float y3, float u3, float v3,
135 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guyd71dd362011-12-12 19:03:35 -0800136 void appendMeshQuad(float x1, float y1, float u1, float v1,
137 float x2, float y2, float u2, float v2,
138 float x3, float y3, float u3, float v3,
Chet Haase7de0cb12011-12-05 16:35:38 -0800139 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy97771732012-02-28 18:17:02 -0800140 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
141 float x2, float y2, float u2, float v2,
142 float x3, float y3, float u3, float v3,
143 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy694b5192010-07-21 21:33:20 -0700144
Romain Guy9b1204b2012-09-04 15:22:57 -0700145 void removeFont(const Font* font);
146
Sangkyu Lee7bb3cfe2012-11-16 00:03:17 +0900147 void updateDrawParams();
Romain Guy9b1204b2012-09-04 15:22:57 -0700148 void checkTextureUpdate();
149
150 void setTextureDirty() {
151 mUploadTexture = true;
152 }
153
Chet Haase7de0cb12011-12-05 16:35:38 -0800154 uint32_t mSmallCacheWidth;
155 uint32_t mSmallCacheHeight;
Chet Haaseeb32a492012-08-31 13:54:03 -0700156 uint32_t mLargeCacheWidth;
157 uint32_t mLargeCacheHeight;
Romain Guy694b5192010-07-21 21:33:20 -0700158
Chet Haase378e9192012-08-15 15:54:54 -0700159 Vector<CacheTexture*> mCacheTextures;
Romain Guy694b5192010-07-21 21:33:20 -0700160
Romain Guy09147fb2010-07-22 13:08:20 -0700161 Font* mCurrentFont;
Romain Guye3a9b242013-01-08 11:15:30 -0800162 LruCache<Font::FontDescription, Font*> mActiveFonts;
Romain Guy694b5192010-07-21 21:33:20 -0700163
Chet Haase7de0cb12011-12-05 16:35:38 -0800164 CacheTexture* mCurrentCacheTexture;
Chet Haase7de0cb12011-12-05 16:35:38 -0800165
Romain Guy694b5192010-07-21 21:33:20 -0700166 bool mUploadTexture;
167
168 // Pointer to vertex data to speed up frame to frame work
Romain Guy9b1204b2012-09-04 15:22:57 -0700169 float* mTextMesh;
Romain Guy694b5192010-07-21 21:33:20 -0700170 uint32_t mCurrentQuadIndex;
Sangkyu Lee7bb3cfe2012-11-16 00:03:17 +0900171 uint32_t mLastQuadIndex;
Romain Guy694b5192010-07-21 21:33:20 -0700172 uint32_t mMaxNumberOfQuads;
173
174 uint32_t mIndexBufferID;
175
Romain Guy09147fb2010-07-22 13:08:20 -0700176 const Rect* mClip;
Romain Guy5b3b3522010-10-27 18:57:51 -0700177 Rect* mBounds;
178 bool mDrawn;
Romain Guy09147fb2010-07-22 13:08:20 -0700179
Romain Guy694b5192010-07-21 21:33:20 -0700180 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700181
Romain Guye8cb9c142010-10-04 14:14:11 -0700182 bool mLinearFiltering;
183
Romain Guy115096f2013-03-19 11:32:41 -0700184 struct TextBatch {
185 TextBatch(): offset(NULL), count(0), texture(NULL) {
186 }
187
188 TextBatch(uint16_t* offset, uint32_t count, CacheTexture* texture):
189 offset(offset), count(count), texture(texture) {
190 }
191
192 static int compare(const TextBatch& lhs, const TextBatch& rhs) {
193 return lhs.texture->getTextureId() - rhs.texture->getTextureId();
194 }
195
196 friend inline int strictly_order_type(const TextBatch& lhs, const TextBatch& rhs) {
197 return compare(lhs, rhs) < 0;
198 }
199
200 friend inline int compare_type(const TextBatch& lhs, const TextBatch& rhs) {
201 return compare(lhs, rhs);
202 }
203
204 uint16_t* offset;
205 uint32_t count;
206 CacheTexture* texture;
207 };
208
209 SortedList<TextBatch> mDrawBatch;
Sangkyu Lee7bb3cfe2012-11-16 00:03:17 +0900210
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800211 // RS constructs
212 sp<RSC::RS> mRs;
213 sp<const RSC::Element> mRsElement;
214 sp<RSC::ScriptIntrinsicBlur> mRsScript;
215
Romain Guy9b1204b2012-09-04 15:22:57 -0700216 static void computeGaussianWeights(float* weights, int32_t radius);
217 static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700218 int32_t width, int32_t height);
Romain Guy9b1204b2012-09-04 15:22:57 -0700219 static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700220 int32_t width, int32_t height);
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800221
222 // the input image handle may have its pointer replaced (to avoid copies)
223 void blurImage(uint8_t** image, int32_t width, int32_t height, int32_t radius);
Romain Guy694b5192010-07-21 21:33:20 -0700224};
225
226}; // namespace uirenderer
227}; // namespace android
228
Romain Guy5b3b3522010-10-27 18:57:51 -0700229#endif // ANDROID_HWUI_FONT_RENDERER_H