blob: ff4dc4aef94ad8ac46c06b7ae34ae839f7abfdde [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"
Chris Craik96a5c4c2015-01-27 15:46:35 -080024
Romain Guye3a9b242013-01-08 11:15:30 -080025#include <utils/LruCache.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070026#include <utils/StrongPointer.h>
Romain Guy694b5192010-07-21 21:33:20 -070027
Romain Guy694b5192010-07-21 21:33:20 -070028#include <SkPaint.h>
29
30#include <GLES2/gl2.h>
31
John Reck272a6852015-07-29 16:48:58 -070032#include <vector>
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
Chris Craika1717272015-11-19 13:02:43 -080047#if HWUI_NEW_OPS
48class BakedOpState;
49class BakedOpRenderer;
50#else
Victoria Lease1e546812013-06-25 14:25:17 -070051class OpenGLRenderer;
Chris Craika1717272015-11-19 13:02:43 -080052#endif
Victoria Lease1e546812013-06-25 14:25:17 -070053
Chris Craik82840732015-04-03 09:37:49 -070054class TextDrawFunctor {
Victoria Lease1e546812013-06-25 14:25:17 -070055public:
Chris Craika1717272015-11-19 13:02:43 -080056 TextDrawFunctor(
57#if HWUI_NEW_OPS
58 BakedOpRenderer* renderer,
59 const BakedOpState* bakedState,
Chris Craik15c3f192015-12-03 12:16:56 -080060 const Rect* clip,
Chris Craika1717272015-11-19 13:02:43 -080061#else
62 OpenGLRenderer* renderer,
63#endif
64 float x, float y, bool pureTranslate,
Chris Craik96a5c4c2015-01-27 15:46:35 -080065 int alpha, SkXfermode::Mode mode, const SkPaint* paint)
66 : renderer(renderer)
Chris Craika1717272015-11-19 13:02:43 -080067#if HWUI_NEW_OPS
68 , bakedState(bakedState)
Chris Craik15c3f192015-12-03 12:16:56 -080069 , clip(clip)
Chris Craika1717272015-11-19 13:02:43 -080070#endif
Chris Craik96a5c4c2015-01-27 15:46:35 -080071 , x(x)
72 , y(y)
73 , pureTranslate(pureTranslate)
74 , alpha(alpha)
75 , mode(mode)
76 , paint(paint) {
Victoria Lease1e546812013-06-25 14:25:17 -070077 }
Victoria Lease1e546812013-06-25 14:25:17 -070078
Chris Craike2bb3802015-03-13 15:07:52 -070079 void draw(CacheTexture& texture, bool linearFiltering);
Victoria Lease1e546812013-06-25 14:25:17 -070080
Chris Craika1717272015-11-19 13:02:43 -080081#if HWUI_NEW_OPS
82 BakedOpRenderer* renderer;
83 const BakedOpState* bakedState;
Chris Craik15c3f192015-12-03 12:16:56 -080084 const Rect* clip;
Chris Craika1717272015-11-19 13:02:43 -080085#else
Victoria Lease1e546812013-06-25 14:25:17 -070086 OpenGLRenderer* renderer;
Chris Craika1717272015-11-19 13:02:43 -080087#endif
Victoria Lease1e546812013-06-25 14:25:17 -070088 float x;
89 float y;
90 bool pureTranslate;
91 int alpha;
92 SkXfermode::Mode mode;
Chris Craikd218a922014-01-02 17:13:34 -080093 const SkPaint* paint;
Victoria Lease1e546812013-06-25 14:25:17 -070094};
95
Romain Guy694b5192010-07-21 21:33:20 -070096class FontRenderer {
97public:
Chris Craikc08820f2015-09-22 14:22:29 -070098 FontRenderer(const uint8_t* gammaTable);
Romain Guy694b5192010-07-21 21:33:20 -070099 ~FontRenderer();
100
John Reck272a6852015-07-29 16:48:58 -0700101 void flushLargeCaches(std::vector<CacheTexture*>& cacheTextures);
Chet Haase9a824562011-12-16 15:44:59 -0800102 void flushLargeCaches();
Romain Guy694b5192010-07-21 21:33:20 -0700103
Chris Craik59744b72014-07-01 17:56:52 -0700104 void setFont(const SkPaint* paint, const SkMatrix& matrix);
Chet Haasee816bae2012-08-09 13:39:02 -0700105
Chris Craik59744b72014-07-01 17:56:52 -0700106 void precache(const SkPaint* paint, const char* text, int numGlyphs, const SkMatrix& matrix);
Romain Guycf51a412013-04-08 19:40:31 -0700107 void endPrecaching();
Chet Haasee816bae2012-08-09 13:39:02 -0700108
Chris Craikd218a922014-01-02 17:13:34 -0800109 bool renderPosText(const SkPaint* paint, const Rect* clip, const char *text,
Chris Craika1717272015-11-19 13:02:43 -0800110 int numGlyphs, int x, int y, const float* positions,
111 Rect* outBounds, TextDrawFunctor* functor, bool forceFinish = true);
Chris Craik527a3aa2013-03-04 10:19:31 -0800112
Chris Craikd218a922014-01-02 17:13:34 -0800113 bool renderTextOnPath(const SkPaint* paint, const Rect* clip, const char *text,
Chris Craika1717272015-11-19 13:02:43 -0800114 int numGlyphs, const SkPath* path,
115 float hOffset, float vOffset, Rect* outBounds, TextDrawFunctor* functor);
Romain Guy694b5192010-07-21 21:33:20 -0700116
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700117 struct DropShadow {
118 uint32_t width;
119 uint32_t height;
120 uint8_t* image;
121 int32_t penX;
122 int32_t penY;
123 };
124
125 // After renderDropShadow returns, the called owns the memory in DropShadow.image
126 // and is responsible for releasing it when it's done with it
Chris Craika1717272015-11-19 13:02:43 -0800127 DropShadow renderDropShadow(const SkPaint* paint, const char *text, int numGlyphs,
128 float radius, const float* positions);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700129
Romain Guy257ae352013-03-20 16:31:12 -0700130 void setTextureFiltering(bool linearFiltering) {
Romain Guy80872462012-09-04 16:42:01 -0700131 mLinearFiltering = linearFiltering;
Romain Guy694b5192010-07-21 21:33:20 -0700132 }
133
Victoria Lease1e546812013-06-25 14:25:17 -0700134 uint32_t getCacheSize(GLenum format) const;
Romain Guyc15008e2010-11-10 11:59:15 -0800135
Romain Guy9b1204b2012-09-04 15:22:57 -0700136private:
Romain Guy694b5192010-07-21 21:33:20 -0700137 friend class Font;
138
Romain Guyb45c0c92010-08-26 20:35:23 -0700139 const uint8_t* mGammaTable;
140
Chet Haase2a47c142011-12-14 15:22:56 -0800141 void allocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase9a824562011-12-16 15:44:59 -0800142 void deallocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase7de0cb12011-12-05 16:35:38 -0800143 void initTextTexture();
Victoria Lease1e546812013-06-25 14:25:17 -0700144 CacheTexture* createCacheTexture(int width, int height, GLenum format, bool allocate);
Chet Haase7de0cb12011-12-05 16:35:38 -0800145 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
Chet Haasef942cf12012-08-30 09:06:46 -0700146 uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
John Reck272a6852015-07-29 16:48:58 -0700147 CacheTexture* cacheBitmapInTexture(std::vector<CacheTexture*>& cacheTextures, const SkGlyph& glyph,
Victoria Lease1e546812013-06-25 14:25:17 -0700148 uint32_t* startX, uint32_t* startY);
Romain Guy694b5192010-07-21 21:33:20 -0700149
150 void flushAllAndInvalidate();
Romain Guy694b5192010-07-21 21:33:20 -0700151
152 void checkInit();
Chris Craik82840732015-04-03 09:37:49 -0700153 void initRender(const Rect* clip, Rect* bounds, TextDrawFunctor* functor);
Romain Guy671d6cf2012-01-18 12:39:17 -0800154 void finishRender();
Romain Guy694b5192010-07-21 21:33:20 -0700155
John Reck272a6852015-07-29 16:48:58 -0700156 void issueDrawCommand(std::vector<CacheTexture*>& cacheTextures);
Romain Guy694b5192010-07-21 21:33:20 -0700157 void issueDrawCommand();
Romain Guy97771732012-02-28 18:17:02 -0800158 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
159 float x2, float y2, float u2, float v2,
160 float x3, float y3, float u3, float v3,
161 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guyd71dd362011-12-12 19:03:35 -0800162 void appendMeshQuad(float x1, float y1, float u1, float v1,
163 float x2, float y2, float u2, float v2,
164 float x3, float y3, float u3, float v3,
Chet Haase7de0cb12011-12-05 16:35:38 -0800165 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy97771732012-02-28 18:17:02 -0800166 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
167 float x2, float y2, float u2, float v2,
168 float x3, float y3, float u3, float v3,
169 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy694b5192010-07-21 21:33:20 -0700170
Romain Guy9b1204b2012-09-04 15:22:57 -0700171 void checkTextureUpdate();
172
173 void setTextureDirty() {
174 mUploadTexture = true;
175 }
176
Chet Haase7de0cb12011-12-05 16:35:38 -0800177 uint32_t mSmallCacheWidth;
178 uint32_t mSmallCacheHeight;
Chet Haaseeb32a492012-08-31 13:54:03 -0700179 uint32_t mLargeCacheWidth;
180 uint32_t mLargeCacheHeight;
Romain Guy694b5192010-07-21 21:33:20 -0700181
John Reck272a6852015-07-29 16:48:58 -0700182 std::vector<CacheTexture*> mACacheTextures;
183 std::vector<CacheTexture*> mRGBACacheTextures;
Romain Guy694b5192010-07-21 21:33:20 -0700184
Romain Guy09147fb2010-07-22 13:08:20 -0700185 Font* mCurrentFont;
Romain Guye3a9b242013-01-08 11:15:30 -0800186 LruCache<Font::FontDescription, Font*> mActiveFonts;
Romain Guy694b5192010-07-21 21:33:20 -0700187
Chet Haase7de0cb12011-12-05 16:35:38 -0800188 CacheTexture* mCurrentCacheTexture;
Chet Haase7de0cb12011-12-05 16:35:38 -0800189
Romain Guy694b5192010-07-21 21:33:20 -0700190 bool mUploadTexture;
191
Chris Craik82840732015-04-03 09:37:49 -0700192 TextDrawFunctor* mFunctor;
Romain Guy09147fb2010-07-22 13:08:20 -0700193 const Rect* mClip;
Romain Guy5b3b3522010-10-27 18:57:51 -0700194 Rect* mBounds;
195 bool mDrawn;
Romain Guy09147fb2010-07-22 13:08:20 -0700196
Romain Guy694b5192010-07-21 21:33:20 -0700197 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700198
Romain Guye8cb9c142010-10-04 14:14:11 -0700199 bool mLinearFiltering;
200
Dan Morrille4d9a012013-03-28 18:10:43 -0700201#ifdef ANDROID_ENABLE_RENDERSCRIPT
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800202 // RS constructs
Tim Murray250b1cf2013-08-01 14:49:22 -0700203 RSC::sp<RSC::RS> mRs;
204 RSC::sp<const RSC::Element> mRsElement;
205 RSC::sp<RSC::ScriptIntrinsicBlur> mRsScript;
Dan Morrille4d9a012013-03-28 18:10:43 -0700206#endif
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800207
Romain Guy9b1204b2012-09-04 15:22:57 -0700208 static void computeGaussianWeights(float* weights, int32_t radius);
209 static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700210 int32_t width, int32_t height);
Romain Guy9b1204b2012-09-04 15:22:57 -0700211 static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700212 int32_t width, int32_t height);
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800213
214 // the input image handle may have its pointer replaced (to avoid copies)
Derek Sollenbergere392c812014-05-21 11:25:22 -0400215 void blurImage(uint8_t** image, int32_t width, int32_t height, float radius);
Romain Guy694b5192010-07-21 21:33:20 -0700216};
217
218}; // namespace uirenderer
219}; // namespace android
220
Romain Guy5b3b3522010-10-27 18:57:51 -0700221#endif // ANDROID_HWUI_FONT_RENDERER_H