blob: aa7e776c44ad7c627d1a7be78ac428d462632e23 [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
Victoria Lease1e546812013-06-25 14:25:17 -070020#include <utils/Functor.h>
Romain Guye3a9b242013-01-08 11:15:30 -080021#include <utils/LruCache.h>
Romain Guy694b5192010-07-21 21:33:20 -070022#include <utils/Vector.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070023#include <utils/StrongPointer.h>
Romain Guy694b5192010-07-21 21:33:20 -070024
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkPaint.h>
26
27#include <GLES2/gl2.h>
28
Romain Guy9f5dab32012-09-04 12:55:44 -070029#include "font/FontUtil.h"
30#include "font/CacheTexture.h"
31#include "font/CachedGlyphInfo.h"
32#include "font/Font.h"
Romain Guy115096f2013-03-19 11:32:41 -070033#include "utils/SortedList.h"
Romain Guye3a9b242013-01-08 11:15:30 -080034#include "Matrix.h"
Romain Guy51769a62010-07-23 00:28:00 -070035#include "Properties.h"
Romain Guy09147fb2010-07-22 13:08:20 -070036
Dan Morrille4d9a012013-03-28 18:10:43 -070037#ifdef ANDROID_ENABLE_RENDERSCRIPT
Tim Murray250b1cf2013-08-01 14:49:22 -070038#include "RenderScript.h"
Chris Craikf2d8ccc2013-02-13 16:14:17 -080039namespace RSC {
40 class Element;
41 class RS;
42 class ScriptIntrinsicBlur;
Tim Murray250b1cf2013-08-01 14:49:22 -070043 class sp;
Chris Craikf2d8ccc2013-02-13 16:14:17 -080044}
Dan Morrille4d9a012013-03-28 18:10:43 -070045#endif
Chris Craikf2d8ccc2013-02-13 16:14:17 -080046
Romain Guy257ae352013-03-20 16:31:12 -070047class Functor;
48
Romain Guy694b5192010-07-21 21:33:20 -070049namespace android {
50namespace uirenderer {
51
Victoria Lease1e546812013-06-25 14:25:17 -070052class OpenGLRenderer;
53
Romain Guy726aeba2011-06-01 14:52:00 -070054///////////////////////////////////////////////////////////////////////////////
Victoria Lease1e546812013-06-25 14:25:17 -070055// TextSetupFunctor
56///////////////////////////////////////////////////////////////////////////////
57class TextSetupFunctor: public Functor {
58public:
59 struct Data {
60 Data(GLenum glyphFormat) : glyphFormat(glyphFormat) {
61 }
62
63 GLenum glyphFormat;
64 };
65
66 TextSetupFunctor(OpenGLRenderer* renderer, float x, float y, bool pureTranslate,
67 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
68 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
69 alpha(alpha), mode(mode), paint(paint) {
70 }
71 ~TextSetupFunctor() { }
72
73 status_t operator ()(int what, void* data);
74
75 OpenGLRenderer* renderer;
76 float x;
77 float y;
78 bool pureTranslate;
79 int alpha;
80 SkXfermode::Mode mode;
81 SkPaint* paint;
82};
83
84///////////////////////////////////////////////////////////////////////////////
85// FontRenderer
Romain Guy726aeba2011-06-01 14:52:00 -070086///////////////////////////////////////////////////////////////////////////////
87
Romain Guy694b5192010-07-21 21:33:20 -070088class FontRenderer {
89public:
90 FontRenderer();
91 ~FontRenderer();
92
Victoria Lease1e546812013-06-25 14:25:17 -070093 void flushLargeCaches(Vector<CacheTexture*>& cacheTextures);
Chet Haase9a824562011-12-16 15:44:59 -080094 void flushLargeCaches();
Romain Guy694b5192010-07-21 21:33:20 -070095
Romain Guyb45c0c92010-08-26 20:35:23 -070096 void setGammaTable(const uint8_t* gammaTable) {
97 mGammaTable = gammaTable;
98 }
99
Romain Guye3a9b242013-01-08 11:15:30 -0800100 void setFont(SkPaint* paint, const mat4& matrix);
Chet Haasee816bae2012-08-09 13:39:02 -0700101
Romain Guye3a9b242013-01-08 11:15:30 -0800102 void precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix);
Romain Guycf51a412013-04-08 19:40:31 -0700103 void endPrecaching();
Chet Haasee816bae2012-08-09 13:39:02 -0700104
Romain Guy671d6cf2012-01-18 12:39:17 -0800105 // bounds is an out parameter
Romain Guy671d6cf2012-01-18 12:39:17 -0800106 bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
Romain Guy257ae352013-03-20 16:31:12 -0700107 uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -0800108 Functor* functor, bool forceFinish = true);
109
Romain Guy97771732012-02-28 18:17:02 -0800110 // bounds is an out parameter
111 bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
Victoria Lease1e546812013-06-25 14:25:17 -0700112 uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds,
113 Functor* functor);
Romain Guy694b5192010-07-21 21:33:20 -0700114
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700115 struct DropShadow {
Romain Guy1e45aae2010-08-13 19:39:53 -0700116 DropShadow() { };
117
118 DropShadow(const DropShadow& dropShadow):
119 width(dropShadow.width), height(dropShadow.height),
120 image(dropShadow.image), penX(dropShadow.penX),
121 penY(dropShadow.penY) {
122 }
123
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700124 uint32_t width;
125 uint32_t height;
126 uint8_t* image;
127 int32_t penX;
128 int32_t penY;
129 };
130
131 // After renderDropShadow returns, the called owns the memory in DropShadow.image
132 // and is responsible for releasing it when it's done with it
133 DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
Raph Levien416a8472012-07-19 22:48:17 -0700134 uint32_t len, int numGlyphs, uint32_t radius, const float* positions);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700135
Romain Guy257ae352013-03-20 16:31:12 -0700136 void setTextureFiltering(bool linearFiltering) {
Romain Guy80872462012-09-04 16:42:01 -0700137 mLinearFiltering = linearFiltering;
Romain Guy694b5192010-07-21 21:33:20 -0700138 }
139
Victoria Lease1e546812013-06-25 14:25:17 -0700140 uint32_t getCacheSize(GLenum format) const;
Romain Guyc15008e2010-11-10 11:59:15 -0800141
Romain Guy9b1204b2012-09-04 15:22:57 -0700142private:
Romain Guy694b5192010-07-21 21:33:20 -0700143 friend class Font;
144
Romain Guyb45c0c92010-08-26 20:35:23 -0700145 const uint8_t* mGammaTable;
146
Chet Haase2a47c142011-12-14 15:22:56 -0800147 void allocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase9a824562011-12-16 15:44:59 -0800148 void deallocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase7de0cb12011-12-05 16:35:38 -0800149 void initTextTexture();
Victoria Lease1e546812013-06-25 14:25:17 -0700150 CacheTexture* createCacheTexture(int width, int height, GLenum format, bool allocate);
Chet Haase7de0cb12011-12-05 16:35:38 -0800151 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
Chet Haasef942cf12012-08-30 09:06:46 -0700152 uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
Victoria Lease1e546812013-06-25 14:25:17 -0700153 CacheTexture* cacheBitmapInTexture(Vector<CacheTexture*>& cacheTextures, const SkGlyph& glyph,
154 uint32_t* startX, uint32_t* startY);
Romain Guy694b5192010-07-21 21:33:20 -0700155
156 void flushAllAndInvalidate();
Romain Guy694b5192010-07-21 21:33:20 -0700157
158 void checkInit();
Romain Guy257ae352013-03-20 16:31:12 -0700159 void initRender(const Rect* clip, Rect* bounds, Functor* functor);
Romain Guy671d6cf2012-01-18 12:39:17 -0800160 void finishRender();
Romain Guy694b5192010-07-21 21:33:20 -0700161
Victoria Lease1e546812013-06-25 14:25:17 -0700162 void issueDrawCommand(Vector<CacheTexture*>& cacheTextures);
Romain Guy694b5192010-07-21 21:33:20 -0700163 void issueDrawCommand();
Romain Guy97771732012-02-28 18:17:02 -0800164 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
165 float x2, float y2, float u2, float v2,
166 float x3, float y3, float u3, float v3,
167 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guyd71dd362011-12-12 19:03:35 -0800168 void appendMeshQuad(float x1, float y1, float u1, float v1,
169 float x2, float y2, float u2, float v2,
170 float x3, float y3, float u3, float v3,
Chet Haase7de0cb12011-12-05 16:35:38 -0800171 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy97771732012-02-28 18:17:02 -0800172 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
173 float x2, float y2, float u2, float v2,
174 float x3, float y3, float u3, float v3,
175 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy694b5192010-07-21 21:33:20 -0700176
Romain Guy9b1204b2012-09-04 15:22:57 -0700177 void removeFont(const Font* font);
178
179 void checkTextureUpdate();
180
181 void setTextureDirty() {
182 mUploadTexture = true;
183 }
184
Chet Haase7de0cb12011-12-05 16:35:38 -0800185 uint32_t mSmallCacheWidth;
186 uint32_t mSmallCacheHeight;
Chet Haaseeb32a492012-08-31 13:54:03 -0700187 uint32_t mLargeCacheWidth;
188 uint32_t mLargeCacheHeight;
Romain Guy694b5192010-07-21 21:33:20 -0700189
Victoria Lease1e546812013-06-25 14:25:17 -0700190 Vector<CacheTexture*> mACacheTextures;
191 Vector<CacheTexture*> mRGBACacheTextures;
Romain Guy694b5192010-07-21 21:33:20 -0700192
Romain Guy09147fb2010-07-22 13:08:20 -0700193 Font* mCurrentFont;
Romain Guye3a9b242013-01-08 11:15:30 -0800194 LruCache<Font::FontDescription, Font*> mActiveFonts;
Romain Guy694b5192010-07-21 21:33:20 -0700195
Chet Haase7de0cb12011-12-05 16:35:38 -0800196 CacheTexture* mCurrentCacheTexture;
Chet Haase7de0cb12011-12-05 16:35:38 -0800197
Romain Guy694b5192010-07-21 21:33:20 -0700198 bool mUploadTexture;
199
Romain Guy257ae352013-03-20 16:31:12 -0700200 Functor* mFunctor;
Romain Guy09147fb2010-07-22 13:08:20 -0700201 const Rect* mClip;
Romain Guy5b3b3522010-10-27 18:57:51 -0700202 Rect* mBounds;
203 bool mDrawn;
Romain Guy09147fb2010-07-22 13:08:20 -0700204
Romain Guy694b5192010-07-21 21:33:20 -0700205 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700206
Romain Guye8cb9c142010-10-04 14:14:11 -0700207 bool mLinearFiltering;
208
Dan Morrille4d9a012013-03-28 18:10:43 -0700209#ifdef ANDROID_ENABLE_RENDERSCRIPT
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800210 // RS constructs
Tim Murray250b1cf2013-08-01 14:49:22 -0700211 RSC::sp<RSC::RS> mRs;
212 RSC::sp<const RSC::Element> mRsElement;
213 RSC::sp<RSC::ScriptIntrinsicBlur> mRsScript;
Dan Morrille4d9a012013-03-28 18:10:43 -0700214#endif
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800215
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