blob: 405db09c17767d9e8acfb5b0af66ccccfd6c1920 [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 Guy694b5192010-07-21 21:33:20 -070020#include <utils/Vector.h>
Romain Guy694b5192010-07-21 21:33:20 -070021
Romain Guy694b5192010-07-21 21:33:20 -070022#include <SkPaint.h>
23
24#include <GLES2/gl2.h>
25
Romain Guy9f5dab32012-09-04 12:55:44 -070026#include "font/FontUtil.h"
27#include "font/CacheTexture.h"
28#include "font/CachedGlyphInfo.h"
29#include "font/Font.h"
Romain Guy51769a62010-07-23 00:28:00 -070030#include "Properties.h"
Romain Guy09147fb2010-07-22 13:08:20 -070031
Romain Guy694b5192010-07-21 21:33:20 -070032namespace android {
33namespace uirenderer {
34
Romain Guy726aeba2011-06-01 14:52:00 -070035///////////////////////////////////////////////////////////////////////////////
Romain Guy726aeba2011-06-01 14:52:00 -070036// Renderer
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guy694b5192010-07-21 21:33:20 -070039class FontRenderer {
40public:
41 FontRenderer();
42 ~FontRenderer();
43
Chet Haase9a824562011-12-16 15:44:59 -080044 void flushLargeCaches();
Romain Guy694b5192010-07-21 21:33:20 -070045
Romain Guyb45c0c92010-08-26 20:35:23 -070046 void setGammaTable(const uint8_t* gammaTable) {
47 mGammaTable = gammaTable;
48 }
49
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -070050 void setFont(SkPaint* paint, uint32_t fontId, float fontSize);
Chet Haasee816bae2012-08-09 13:39:02 -070051
52 void precache(SkPaint* paint, const char* text, int numGlyphs);
53
Romain Guy671d6cf2012-01-18 12:39:17 -080054 // bounds is an out parameter
Romain Guy5b3b3522010-10-27 18:57:51 -070055 bool renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
56 uint32_t len, int numGlyphs, int x, int y, Rect* bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -080057 // bounds is an out parameter
58 bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
59 uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds);
Romain Guy97771732012-02-28 18:17:02 -080060 // bounds is an out parameter
61 bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
62 uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds);
Romain Guy694b5192010-07-21 21:33:20 -070063
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070064 struct DropShadow {
Romain Guy1e45aae2010-08-13 19:39:53 -070065 DropShadow() { };
66
67 DropShadow(const DropShadow& dropShadow):
68 width(dropShadow.width), height(dropShadow.height),
69 image(dropShadow.image), penX(dropShadow.penX),
70 penY(dropShadow.penY) {
71 }
72
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070073 uint32_t width;
74 uint32_t height;
75 uint8_t* image;
76 int32_t penX;
77 int32_t penY;
78 };
79
80 // After renderDropShadow returns, the called owns the memory in DropShadow.image
81 // and is responsible for releasing it when it's done with it
82 DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
Raph Levien416a8472012-07-19 22:48:17 -070083 uint32_t len, int numGlyphs, uint32_t radius, const float* positions);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070084
Romain Guye8cb9c142010-10-04 14:14:11 -070085 GLuint getTexture(bool linearFiltering = false) {
Romain Guy694b5192010-07-21 21:33:20 -070086 checkInit();
Romain Guyae91c4c2012-05-14 14:00:27 -070087
Romain Guy80872462012-09-04 16:42:01 -070088 mCurrentCacheTexture->setLinearFiltering(linearFiltering);
89 mLinearFiltering = linearFiltering;
Romain Guye8cb9c142010-10-04 14:14:11 -070090
Romain Guy80872462012-09-04 16:42:01 -070091 return mCurrentCacheTexture->getTextureId();
Romain Guy694b5192010-07-21 21:33:20 -070092 }
93
Chet Haase7de0cb12011-12-05 16:35:38 -080094 uint32_t getCacheSize() const {
95 uint32_t size = 0;
Chet Haase378e9192012-08-15 15:54:54 -070096 for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
97 CacheTexture* cacheTexture = mCacheTextures[i];
Romain Guy80872462012-09-04 16:42:01 -070098 if (cacheTexture && cacheTexture->getTexture()) {
99 size += cacheTexture->getWidth() * cacheTexture->getHeight();
Chet Haase378e9192012-08-15 15:54:54 -0700100 }
Chet Haase7de0cb12011-12-05 16:35:38 -0800101 }
102 return size;
Romain Guyc15008e2010-11-10 11:59:15 -0800103 }
104
Romain Guy9b1204b2012-09-04 15:22:57 -0700105private:
Romain Guy694b5192010-07-21 21:33:20 -0700106 friend class Font;
107
Romain Guyb45c0c92010-08-26 20:35:23 -0700108 const uint8_t* mGammaTable;
109
Chet Haase2a47c142011-12-14 15:22:56 -0800110 void allocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase9a824562011-12-16 15:44:59 -0800111 void deallocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase7de0cb12011-12-05 16:35:38 -0800112 void initTextTexture();
Romain Guy97771732012-02-28 18:17:02 -0800113 CacheTexture* createCacheTexture(int width, int height, bool allocate);
Chet Haase7de0cb12011-12-05 16:35:38 -0800114 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
Chet Haasef942cf12012-08-30 09:06:46 -0700115 uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
Chet Haase378e9192012-08-15 15:54:54 -0700116 CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY);
Romain Guy694b5192010-07-21 21:33:20 -0700117
118 void flushAllAndInvalidate();
119 void initVertexArrayBuffers();
120
121 void checkInit();
Romain Guy671d6cf2012-01-18 12:39:17 -0800122 void initRender(const Rect* clip, Rect* bounds);
123 void finishRender();
Romain Guy694b5192010-07-21 21:33:20 -0700124
125 void issueDrawCommand();
Romain Guy97771732012-02-28 18:17:02 -0800126 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
127 float x2, float y2, float u2, float v2,
128 float x3, float y3, float u3, float v3,
129 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guyd71dd362011-12-12 19:03:35 -0800130 void appendMeshQuad(float x1, float y1, float u1, float v1,
131 float x2, float y2, float u2, float v2,
132 float x3, float y3, float u3, float v3,
Chet Haase7de0cb12011-12-05 16:35:38 -0800133 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy97771732012-02-28 18:17:02 -0800134 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
135 float x2, float y2, float u2, float v2,
136 float x3, float y3, float u3, float v3,
137 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy694b5192010-07-21 21:33:20 -0700138
Romain Guy9b1204b2012-09-04 15:22:57 -0700139 void removeFont(const Font* font);
140
141 void checkTextureUpdate();
142
143 void setTextureDirty() {
144 mUploadTexture = true;
145 }
146
Chet Haase7de0cb12011-12-05 16:35:38 -0800147 uint32_t mSmallCacheWidth;
148 uint32_t mSmallCacheHeight;
Chet Haaseeb32a492012-08-31 13:54:03 -0700149 uint32_t mLargeCacheWidth;
150 uint32_t mLargeCacheHeight;
Romain Guy694b5192010-07-21 21:33:20 -0700151
Chet Haase378e9192012-08-15 15:54:54 -0700152 Vector<CacheTexture*> mCacheTextures;
Romain Guy694b5192010-07-21 21:33:20 -0700153
Romain Guy09147fb2010-07-22 13:08:20 -0700154 Font* mCurrentFont;
Romain Guy694b5192010-07-21 21:33:20 -0700155 Vector<Font*> mActiveFonts;
156
Chet Haase7de0cb12011-12-05 16:35:38 -0800157 CacheTexture* mCurrentCacheTexture;
158 CacheTexture* mLastCacheTexture;
Chet Haase7de0cb12011-12-05 16:35:38 -0800159
Romain Guy694b5192010-07-21 21:33:20 -0700160 bool mUploadTexture;
161
162 // Pointer to vertex data to speed up frame to frame work
Romain Guy9b1204b2012-09-04 15:22:57 -0700163 float* mTextMesh;
Romain Guy694b5192010-07-21 21:33:20 -0700164 uint32_t mCurrentQuadIndex;
165 uint32_t mMaxNumberOfQuads;
166
167 uint32_t mIndexBufferID;
168
Romain Guy09147fb2010-07-22 13:08:20 -0700169 const Rect* mClip;
Romain Guy5b3b3522010-10-27 18:57:51 -0700170 Rect* mBounds;
171 bool mDrawn;
Romain Guy09147fb2010-07-22 13:08:20 -0700172
Romain Guy694b5192010-07-21 21:33:20 -0700173 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700174
Romain Guye8cb9c142010-10-04 14:14:11 -0700175 bool mLinearFiltering;
176
Romain Guy9b1204b2012-09-04 15:22:57 -0700177 /** We should consider multi-threading this code or using Renderscript **/
178 static void computeGaussianWeights(float* weights, int32_t radius);
179 static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700180 int32_t width, int32_t height);
Romain Guy9b1204b2012-09-04 15:22:57 -0700181 static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700182 int32_t width, int32_t height);
Romain Guy9b1204b2012-09-04 15:22:57 -0700183 static void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius);
Romain Guy694b5192010-07-21 21:33:20 -0700184};
185
186}; // namespace uirenderer
187}; // namespace android
188
Romain Guy5b3b3522010-10-27 18:57:51 -0700189#endif // ANDROID_HWUI_FONT_RENDERER_H