blob: 0603389d1b70ce49b8efd89ed789f1db611e18f4 [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
Chris Craik96a5c4c2015-01-27 15:46:35 -080020#include "font/FontUtil.h"
21#include "font/CacheTexture.h"
22#include "font/CachedGlyphInfo.h"
23#include "font/Font.h"
24#include "utils/SortedList.h"
25
Romain Guye3a9b242013-01-08 11:15:30 -080026#include <utils/LruCache.h>
Romain Guy694b5192010-07-21 21:33:20 -070027#include <utils/Vector.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070028#include <utils/StrongPointer.h>
Romain Guy694b5192010-07-21 21:33:20 -070029
Romain Guy694b5192010-07-21 21:33:20 -070030#include <SkPaint.h>
31
32#include <GLES2/gl2.h>
33
Dan Morrille4d9a012013-03-28 18:10:43 -070034#ifdef ANDROID_ENABLE_RENDERSCRIPT
Tim Murray250b1cf2013-08-01 14:49:22 -070035#include "RenderScript.h"
Chris Craikf2d8ccc2013-02-13 16:14:17 -080036namespace RSC {
37 class Element;
38 class RS;
39 class ScriptIntrinsicBlur;
Tim Murray250b1cf2013-08-01 14:49:22 -070040 class sp;
Chris Craikf2d8ccc2013-02-13 16:14:17 -080041}
Dan Morrille4d9a012013-03-28 18:10:43 -070042#endif
Chris Craikf2d8ccc2013-02-13 16:14:17 -080043
Romain Guy694b5192010-07-21 21:33:20 -070044namespace android {
45namespace uirenderer {
46
Victoria Lease1e546812013-06-25 14:25:17 -070047class OpenGLRenderer;
48
Chris Craik96a5c4c2015-01-27 15:46:35 -080049class TextSetupFunctor {
Victoria Lease1e546812013-06-25 14:25:17 -070050public:
Victoria Lease1e546812013-06-25 14:25:17 -070051 TextSetupFunctor(OpenGLRenderer* renderer, float x, float y, bool pureTranslate,
Chris Craik96a5c4c2015-01-27 15:46:35 -080052 int alpha, SkXfermode::Mode mode, const SkPaint* paint)
53 : renderer(renderer)
54 , x(x)
55 , y(y)
56 , pureTranslate(pureTranslate)
57 , alpha(alpha)
58 , mode(mode)
59 , paint(paint) {
Victoria Lease1e546812013-06-25 14:25:17 -070060 }
Victoria Lease1e546812013-06-25 14:25:17 -070061
Chris Craike2bb3802015-03-13 15:07:52 -070062 void setup(GLenum glyphFormat);
63
64 void draw(CacheTexture& texture, bool linearFiltering);
Victoria Lease1e546812013-06-25 14:25:17 -070065
66 OpenGLRenderer* renderer;
67 float x;
68 float y;
69 bool pureTranslate;
70 int alpha;
71 SkXfermode::Mode mode;
Chris Craikd218a922014-01-02 17:13:34 -080072 const SkPaint* paint;
Victoria Lease1e546812013-06-25 14:25:17 -070073};
74
Romain Guy694b5192010-07-21 21:33:20 -070075class FontRenderer {
76public:
77 FontRenderer();
78 ~FontRenderer();
79
Victoria Lease1e546812013-06-25 14:25:17 -070080 void flushLargeCaches(Vector<CacheTexture*>& cacheTextures);
Chet Haase9a824562011-12-16 15:44:59 -080081 void flushLargeCaches();
Romain Guy694b5192010-07-21 21:33:20 -070082
Romain Guyb45c0c92010-08-26 20:35:23 -070083 void setGammaTable(const uint8_t* gammaTable) {
84 mGammaTable = gammaTable;
85 }
86
Chris Craik59744b72014-07-01 17:56:52 -070087 void setFont(const SkPaint* paint, const SkMatrix& matrix);
Chet Haasee816bae2012-08-09 13:39:02 -070088
Chris Craik59744b72014-07-01 17:56:52 -070089 void precache(const SkPaint* paint, const char* text, int numGlyphs, const SkMatrix& matrix);
Romain Guycf51a412013-04-08 19:40:31 -070090 void endPrecaching();
Chet Haasee816bae2012-08-09 13:39:02 -070091
Romain Guy671d6cf2012-01-18 12:39:17 -080092 // bounds is an out parameter
Chris Craikd218a922014-01-02 17:13:34 -080093 bool renderPosText(const SkPaint* paint, const Rect* clip, const char *text,
94 uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, const float* positions,
Chris Craik96a5c4c2015-01-27 15:46:35 -080095 Rect* bounds, TextSetupFunctor* functor, bool forceFinish = true);
Chris Craik527a3aa2013-03-04 10:19:31 -080096
Romain Guy97771732012-02-28 18:17:02 -080097 // bounds is an out parameter
Chris Craikd218a922014-01-02 17:13:34 -080098 bool renderTextOnPath(const SkPaint* paint, const Rect* clip, const char *text,
99 uint32_t startIndex, uint32_t len, int numGlyphs, const SkPath* path,
Chris Craik96a5c4c2015-01-27 15:46:35 -0800100 float hOffset, float vOffset, Rect* bounds, TextSetupFunctor* functor);
Romain Guy694b5192010-07-21 21:33:20 -0700101
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700102 struct DropShadow {
103 uint32_t width;
104 uint32_t height;
105 uint8_t* image;
106 int32_t penX;
107 int32_t penY;
108 };
109
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
Chris Craikd218a922014-01-02 17:13:34 -0800112 DropShadow renderDropShadow(const SkPaint* paint, const char *text, uint32_t startIndex,
Derek Sollenbergere392c812014-05-21 11:25:22 -0400113 uint32_t len, int numGlyphs, float radius, const float* positions);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700114
Romain Guy257ae352013-03-20 16:31:12 -0700115 void setTextureFiltering(bool linearFiltering) {
Romain Guy80872462012-09-04 16:42:01 -0700116 mLinearFiltering = linearFiltering;
Romain Guy694b5192010-07-21 21:33:20 -0700117 }
118
Victoria Lease1e546812013-06-25 14:25:17 -0700119 uint32_t getCacheSize(GLenum format) const;
Romain Guyc15008e2010-11-10 11:59:15 -0800120
Romain Guy9b1204b2012-09-04 15:22:57 -0700121private:
Romain Guy694b5192010-07-21 21:33:20 -0700122 friend class Font;
123
Romain Guyb45c0c92010-08-26 20:35:23 -0700124 const uint8_t* mGammaTable;
125
Chet Haase2a47c142011-12-14 15:22:56 -0800126 void allocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase9a824562011-12-16 15:44:59 -0800127 void deallocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase7de0cb12011-12-05 16:35:38 -0800128 void initTextTexture();
Victoria Lease1e546812013-06-25 14:25:17 -0700129 CacheTexture* createCacheTexture(int width, int height, GLenum format, bool allocate);
Chet Haase7de0cb12011-12-05 16:35:38 -0800130 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
Chet Haasef942cf12012-08-30 09:06:46 -0700131 uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
Victoria Lease1e546812013-06-25 14:25:17 -0700132 CacheTexture* cacheBitmapInTexture(Vector<CacheTexture*>& cacheTextures, const SkGlyph& glyph,
133 uint32_t* startX, uint32_t* startY);
Romain Guy694b5192010-07-21 21:33:20 -0700134
135 void flushAllAndInvalidate();
Romain Guy694b5192010-07-21 21:33:20 -0700136
137 void checkInit();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800138 void initRender(const Rect* clip, Rect* bounds, TextSetupFunctor* functor);
Romain Guy671d6cf2012-01-18 12:39:17 -0800139 void finishRender();
Romain Guy694b5192010-07-21 21:33:20 -0700140
Victoria Lease1e546812013-06-25 14:25:17 -0700141 void issueDrawCommand(Vector<CacheTexture*>& cacheTextures);
Romain Guy694b5192010-07-21 21:33:20 -0700142 void issueDrawCommand();
Romain Guy97771732012-02-28 18:17:02 -0800143 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
144 float x2, float y2, float u2, float v2,
145 float x3, float y3, float u3, float v3,
146 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guyd71dd362011-12-12 19:03:35 -0800147 void appendMeshQuad(float x1, float y1, float u1, float v1,
148 float x2, float y2, float u2, float v2,
149 float x3, float y3, float u3, float v3,
Chet Haase7de0cb12011-12-05 16:35:38 -0800150 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy97771732012-02-28 18:17:02 -0800151 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
152 float x2, float y2, float u2, float v2,
153 float x3, float y3, float u3, float v3,
154 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy694b5192010-07-21 21:33:20 -0700155
Romain Guy9b1204b2012-09-04 15:22:57 -0700156 void removeFont(const Font* font);
157
158 void checkTextureUpdate();
159
160 void setTextureDirty() {
161 mUploadTexture = true;
162 }
163
Chet Haase7de0cb12011-12-05 16:35:38 -0800164 uint32_t mSmallCacheWidth;
165 uint32_t mSmallCacheHeight;
Chet Haaseeb32a492012-08-31 13:54:03 -0700166 uint32_t mLargeCacheWidth;
167 uint32_t mLargeCacheHeight;
Romain Guy694b5192010-07-21 21:33:20 -0700168
Victoria Lease1e546812013-06-25 14:25:17 -0700169 Vector<CacheTexture*> mACacheTextures;
170 Vector<CacheTexture*> mRGBACacheTextures;
Romain Guy694b5192010-07-21 21:33:20 -0700171
Romain Guy09147fb2010-07-22 13:08:20 -0700172 Font* mCurrentFont;
Romain Guye3a9b242013-01-08 11:15:30 -0800173 LruCache<Font::FontDescription, Font*> mActiveFonts;
Romain Guy694b5192010-07-21 21:33:20 -0700174
Chet Haase7de0cb12011-12-05 16:35:38 -0800175 CacheTexture* mCurrentCacheTexture;
Chet Haase7de0cb12011-12-05 16:35:38 -0800176
Romain Guy694b5192010-07-21 21:33:20 -0700177 bool mUploadTexture;
178
Chris Craik96a5c4c2015-01-27 15:46:35 -0800179 TextSetupFunctor* mFunctor;
Romain Guy09147fb2010-07-22 13:08:20 -0700180 const Rect* mClip;
Romain Guy5b3b3522010-10-27 18:57:51 -0700181 Rect* mBounds;
182 bool mDrawn;
Romain Guy09147fb2010-07-22 13:08:20 -0700183
Romain Guy694b5192010-07-21 21:33:20 -0700184 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700185
Romain Guye8cb9c142010-10-04 14:14:11 -0700186 bool mLinearFiltering;
187
Dan Morrille4d9a012013-03-28 18:10:43 -0700188#ifdef ANDROID_ENABLE_RENDERSCRIPT
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800189 // RS constructs
Tim Murray250b1cf2013-08-01 14:49:22 -0700190 RSC::sp<RSC::RS> mRs;
191 RSC::sp<const RSC::Element> mRsElement;
192 RSC::sp<RSC::ScriptIntrinsicBlur> mRsScript;
Dan Morrille4d9a012013-03-28 18:10:43 -0700193#endif
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800194
Romain Guy9b1204b2012-09-04 15:22:57 -0700195 static void computeGaussianWeights(float* weights, int32_t radius);
196 static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700197 int32_t width, int32_t height);
Romain Guy9b1204b2012-09-04 15:22:57 -0700198 static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700199 int32_t width, int32_t height);
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800200
201 // the input image handle may have its pointer replaced (to avoid copies)
Derek Sollenbergere392c812014-05-21 11:25:22 -0400202 void blurImage(uint8_t** image, int32_t width, int32_t height, float radius);
Romain Guy694b5192010-07-21 21:33:20 -0700203};
204
205}; // namespace uirenderer
206}; // namespace android
207
Romain Guy5b3b3522010-10-27 18:57:51 -0700208#endif // ANDROID_HWUI_FONT_RENDERER_H