blob: 348b7e3b2b0a69943622fb10f1f5a77dd977b832 [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 Guy257ae352013-03-20 16:31:12 -070041class Functor;
42
Romain Guy694b5192010-07-21 21:33:20 -070043namespace android {
44namespace uirenderer {
45
Romain Guy726aeba2011-06-01 14:52:00 -070046///////////////////////////////////////////////////////////////////////////////
Romain Guy726aeba2011-06-01 14:52:00 -070047// Renderer
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy694b5192010-07-21 21:33:20 -070050class FontRenderer {
51public:
52 FontRenderer();
53 ~FontRenderer();
54
Chet Haase9a824562011-12-16 15:44:59 -080055 void flushLargeCaches();
Romain Guy694b5192010-07-21 21:33:20 -070056
Romain Guyb45c0c92010-08-26 20:35:23 -070057 void setGammaTable(const uint8_t* gammaTable) {
58 mGammaTable = gammaTable;
59 }
60
Romain Guye3a9b242013-01-08 11:15:30 -080061 void setFont(SkPaint* paint, const mat4& matrix);
Chet Haasee816bae2012-08-09 13:39:02 -070062
Romain Guye3a9b242013-01-08 11:15:30 -080063 void precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix);
Romain Guycf51a412013-04-08 19:40:31 -070064 void endPrecaching();
Chet Haasee816bae2012-08-09 13:39:02 -070065
Romain Guy671d6cf2012-01-18 12:39:17 -080066 // bounds is an out parameter
Romain Guy671d6cf2012-01-18 12:39:17 -080067 bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
Romain Guy257ae352013-03-20 16:31:12 -070068 uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds,
69 Functor* functor);
Romain Guy97771732012-02-28 18:17:02 -080070 // bounds is an out parameter
71 bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
72 uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds);
Romain Guy694b5192010-07-21 21:33:20 -070073
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070074 struct DropShadow {
Romain Guy1e45aae2010-08-13 19:39:53 -070075 DropShadow() { };
76
77 DropShadow(const DropShadow& dropShadow):
78 width(dropShadow.width), height(dropShadow.height),
79 image(dropShadow.image), penX(dropShadow.penX),
80 penY(dropShadow.penY) {
81 }
82
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070083 uint32_t width;
84 uint32_t height;
85 uint8_t* image;
86 int32_t penX;
87 int32_t penY;
88 };
89
90 // After renderDropShadow returns, the called owns the memory in DropShadow.image
91 // and is responsible for releasing it when it's done with it
92 DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
Raph Levien416a8472012-07-19 22:48:17 -070093 uint32_t len, int numGlyphs, uint32_t radius, const float* positions);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -070094
Romain Guy257ae352013-03-20 16:31:12 -070095 void setTextureFiltering(bool linearFiltering) {
Romain Guy80872462012-09-04 16:42:01 -070096 mLinearFiltering = linearFiltering;
Romain Guy694b5192010-07-21 21:33:20 -070097 }
98
Romain Guycf51a412013-04-08 19:40:31 -070099 uint32_t getCacheSize() const;
Romain Guyc15008e2010-11-10 11:59:15 -0800100
Romain Guy9b1204b2012-09-04 15:22:57 -0700101private:
Romain Guy694b5192010-07-21 21:33:20 -0700102 friend class Font;
103
Romain Guyb45c0c92010-08-26 20:35:23 -0700104 const uint8_t* mGammaTable;
105
Chet Haase2a47c142011-12-14 15:22:56 -0800106 void allocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase9a824562011-12-16 15:44:59 -0800107 void deallocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase7de0cb12011-12-05 16:35:38 -0800108 void initTextTexture();
Romain Guy97771732012-02-28 18:17:02 -0800109 CacheTexture* createCacheTexture(int width, int height, bool allocate);
Chet Haase7de0cb12011-12-05 16:35:38 -0800110 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
Chet Haasef942cf12012-08-30 09:06:46 -0700111 uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
Chet Haase378e9192012-08-15 15:54:54 -0700112 CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY);
Romain Guy694b5192010-07-21 21:33:20 -0700113
114 void flushAllAndInvalidate();
115 void initVertexArrayBuffers();
116
117 void checkInit();
Romain Guy257ae352013-03-20 16:31:12 -0700118 void initRender(const Rect* clip, Rect* bounds, Functor* functor);
Romain Guy671d6cf2012-01-18 12:39:17 -0800119 void finishRender();
Romain Guy694b5192010-07-21 21:33:20 -0700120
121 void issueDrawCommand();
Romain Guy97771732012-02-28 18:17:02 -0800122 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
123 float x2, float y2, float u2, float v2,
124 float x3, float y3, float u3, float v3,
125 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guyd71dd362011-12-12 19:03:35 -0800126 void appendMeshQuad(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,
Chet Haase7de0cb12011-12-05 16:35:38 -0800129 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy97771732012-02-28 18:17:02 -0800130 void appendRotatedMeshQuad(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,
133 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy694b5192010-07-21 21:33:20 -0700134
Romain Guy9b1204b2012-09-04 15:22:57 -0700135 void removeFont(const Font* font);
136
137 void checkTextureUpdate();
138
139 void setTextureDirty() {
140 mUploadTexture = true;
141 }
142
Chet Haase7de0cb12011-12-05 16:35:38 -0800143 uint32_t mSmallCacheWidth;
144 uint32_t mSmallCacheHeight;
Chet Haaseeb32a492012-08-31 13:54:03 -0700145 uint32_t mLargeCacheWidth;
146 uint32_t mLargeCacheHeight;
Romain Guy694b5192010-07-21 21:33:20 -0700147
Chet Haase378e9192012-08-15 15:54:54 -0700148 Vector<CacheTexture*> mCacheTextures;
Romain Guy694b5192010-07-21 21:33:20 -0700149
Romain Guy09147fb2010-07-22 13:08:20 -0700150 Font* mCurrentFont;
Romain Guye3a9b242013-01-08 11:15:30 -0800151 LruCache<Font::FontDescription, Font*> mActiveFonts;
Romain Guy694b5192010-07-21 21:33:20 -0700152
Chet Haase7de0cb12011-12-05 16:35:38 -0800153 CacheTexture* mCurrentCacheTexture;
Chet Haase7de0cb12011-12-05 16:35:38 -0800154
Romain Guy694b5192010-07-21 21:33:20 -0700155 bool mUploadTexture;
156
Romain Guy694b5192010-07-21 21:33:20 -0700157 uint32_t mMaxNumberOfQuads;
Romain Guy694b5192010-07-21 21:33:20 -0700158 uint32_t mIndexBufferID;
159
Romain Guy257ae352013-03-20 16:31:12 -0700160 Functor* mFunctor;
Romain Guy09147fb2010-07-22 13:08:20 -0700161 const Rect* mClip;
Romain Guy5b3b3522010-10-27 18:57:51 -0700162 Rect* mBounds;
163 bool mDrawn;
Romain Guy09147fb2010-07-22 13:08:20 -0700164
Romain Guy694b5192010-07-21 21:33:20 -0700165 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700166
Romain Guye8cb9c142010-10-04 14:14:11 -0700167 bool mLinearFiltering;
168
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800169 // RS constructs
170 sp<RSC::RS> mRs;
171 sp<const RSC::Element> mRsElement;
172 sp<RSC::ScriptIntrinsicBlur> mRsScript;
173
Romain Guy9b1204b2012-09-04 15:22:57 -0700174 static void computeGaussianWeights(float* weights, int32_t radius);
175 static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700176 int32_t width, int32_t height);
Romain Guy9b1204b2012-09-04 15:22:57 -0700177 static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700178 int32_t width, int32_t height);
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800179
180 // the input image handle may have its pointer replaced (to avoid copies)
181 void blurImage(uint8_t** image, int32_t width, int32_t height, int32_t radius);
Romain Guy694b5192010-07-21 21:33:20 -0700182};
183
184}; // namespace uirenderer
185}; // namespace android
186
Romain Guy5b3b3522010-10-27 18:57:51 -0700187#endif // ANDROID_HWUI_FONT_RENDERER_H