blob: dd9c40f89ad0f26caffc2a38772247ed7f17ab94 [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
Chris Craik5e00c7c2016-07-06 16:10:09 -070017#pragma once
Romain Guy694b5192010-07-21 21:33:20 -070018
Chris Craik96a5c4c2015-01-27 15:46:35 -080019#include "font/FontUtil.h"
20#include "font/CacheTexture.h"
21#include "font/CachedGlyphInfo.h"
22#include "font/Font.h"
sergeyvaf102be2016-09-09 18:02:07 -070023#ifdef BUGREPORT_FONT_CACHE_USAGE
24#include "font/FontCacheHistoryTracker.h"
25#endif
Chris Craik96a5c4c2015-01-27 15:46:35 -080026
Romain Guye3a9b242013-01-08 11:15:30 -080027#include <utils/LruCache.h>
sergeyvbaf29e72016-09-08 11:09:34 -070028#include <utils/String8.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070029#include <utils/StrongPointer.h>
Romain Guy694b5192010-07-21 21:33:20 -070030
Romain Guy694b5192010-07-21 21:33:20 -070031#include <SkPaint.h>
32
33#include <GLES2/gl2.h>
34
John Reck272a6852015-07-29 16:48:58 -070035#include <vector>
36
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 Guy694b5192010-07-21 21:33:20 -070047namespace android {
48namespace uirenderer {
49
Chris Craika1717272015-11-19 13:02:43 -080050class BakedOpState;
51class BakedOpRenderer;
Chris Craike4db79d2015-12-22 16:32:23 -080052struct ClipBase;
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(
Chris Craika1717272015-11-19 13:02:43 -080057 BakedOpRenderer* renderer,
58 const BakedOpState* bakedState,
Chris Craike4db79d2015-12-22 16:32:23 -080059 const ClipBase* clip,
Chris Craika1717272015-11-19 13:02:43 -080060 float x, float y, bool pureTranslate,
Mike Reed260ab722016-10-07 15:59:20 -040061 int alpha, SkBlendMode mode, const SkPaint* paint)
Chris Craik96a5c4c2015-01-27 15:46:35 -080062 : renderer(renderer)
Chris Craika1717272015-11-19 13:02:43 -080063 , bakedState(bakedState)
Chris Craik15c3f192015-12-03 12:16:56 -080064 , clip(clip)
Chris Craik96a5c4c2015-01-27 15:46:35 -080065 , x(x)
66 , y(y)
67 , pureTranslate(pureTranslate)
68 , alpha(alpha)
69 , mode(mode)
70 , paint(paint) {
Victoria Lease1e546812013-06-25 14:25:17 -070071 }
Victoria Lease1e546812013-06-25 14:25:17 -070072
Chris Craike2bb3802015-03-13 15:07:52 -070073 void draw(CacheTexture& texture, bool linearFiltering);
Victoria Lease1e546812013-06-25 14:25:17 -070074
Chris Craika1717272015-11-19 13:02:43 -080075 BakedOpRenderer* renderer;
76 const BakedOpState* bakedState;
Chris Craike4db79d2015-12-22 16:32:23 -080077 const ClipBase* clip;
Victoria Lease1e546812013-06-25 14:25:17 -070078 float x;
79 float y;
80 bool pureTranslate;
81 int alpha;
Mike Reed260ab722016-10-07 15:59:20 -040082 SkBlendMode mode;
Chris Craikd218a922014-01-02 17:13:34 -080083 const SkPaint* paint;
Victoria Lease1e546812013-06-25 14:25:17 -070084};
85
Romain Guy694b5192010-07-21 21:33:20 -070086class FontRenderer {
87public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -070088 explicit FontRenderer(const uint8_t* gammaTable);
Romain Guy694b5192010-07-21 21:33:20 -070089 ~FontRenderer();
90
John Reck272a6852015-07-29 16:48:58 -070091 void flushLargeCaches(std::vector<CacheTexture*>& cacheTextures);
Chet Haase9a824562011-12-16 15:44:59 -080092 void flushLargeCaches();
Romain Guy694b5192010-07-21 21:33:20 -070093
Chris Craik59744b72014-07-01 17:56:52 -070094 void setFont(const SkPaint* paint, const SkMatrix& matrix);
Chet Haasee816bae2012-08-09 13:39:02 -070095
Chris Craike8c3c812016-02-05 20:10:50 -080096 void precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, const SkMatrix& matrix);
Romain Guycf51a412013-04-08 19:40:31 -070097 void endPrecaching();
Chet Haasee816bae2012-08-09 13:39:02 -070098
Chris Craike8c3c812016-02-05 20:10:50 -080099 bool renderPosText(const SkPaint* paint, const Rect* clip, const glyph_t* glyphs,
Chris Craika1717272015-11-19 13:02:43 -0800100 int numGlyphs, int x, int y, const float* positions,
101 Rect* outBounds, TextDrawFunctor* functor, bool forceFinish = true);
Chris Craik527a3aa2013-03-04 10:19:31 -0800102
Chris Craike8c3c812016-02-05 20:10:50 -0800103 bool renderTextOnPath(const SkPaint* paint, const Rect* clip, const glyph_t* glyphs,
Chris Craika1717272015-11-19 13:02:43 -0800104 int numGlyphs, const SkPath* path,
105 float hOffset, float vOffset, Rect* outBounds, TextDrawFunctor* functor);
Romain Guy694b5192010-07-21 21:33:20 -0700106
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700107 struct DropShadow {
108 uint32_t width;
109 uint32_t height;
110 uint8_t* image;
111 int32_t penX;
112 int32_t penY;
113 };
114
115 // After renderDropShadow returns, the called owns the memory in DropShadow.image
116 // and is responsible for releasing it when it's done with it
Chris Craike8c3c812016-02-05 20:10:50 -0800117 DropShadow renderDropShadow(const SkPaint* paint, const glyph_t *glyphs, int numGlyphs,
Chris Craika1717272015-11-19 13:02:43 -0800118 float radius, const float* positions);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700119
Romain Guy257ae352013-03-20 16:31:12 -0700120 void setTextureFiltering(bool linearFiltering) {
Romain Guy80872462012-09-04 16:42:01 -0700121 mLinearFiltering = linearFiltering;
Romain Guy694b5192010-07-21 21:33:20 -0700122 }
123
sergeyvbaf29e72016-09-08 11:09:34 -0700124 uint32_t getSize() const;
125 void dumpMemoryUsage(String8& log) const;
Romain Guyc15008e2010-11-10 11:59:15 -0800126
sergeyvaf102be2016-09-09 18:02:07 -0700127#ifdef BUGREPORT_FONT_CACHE_USAGE
128 FontCacheHistoryTracker& historyTracker() { return mHistoryTracker; }
129#endif
Romain Guy694b5192010-07-21 21:33:20 -0700130
Romain Guy9b1204b2012-09-04 15:22:57 -0700131private:
Romain Guy694b5192010-07-21 21:33:20 -0700132 friend class Font;
133
Romain Guyb45c0c92010-08-26 20:35:23 -0700134 const uint8_t* mGammaTable;
135
Chet Haase2a47c142011-12-14 15:22:56 -0800136 void allocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase9a824562011-12-16 15:44:59 -0800137 void deallocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase7de0cb12011-12-05 16:35:38 -0800138 void initTextTexture();
Victoria Lease1e546812013-06-25 14:25:17 -0700139 CacheTexture* createCacheTexture(int width, int height, GLenum format, bool allocate);
Chet Haase7de0cb12011-12-05 16:35:38 -0800140 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
Chet Haasef942cf12012-08-30 09:06:46 -0700141 uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
John Reck272a6852015-07-29 16:48:58 -0700142 CacheTexture* cacheBitmapInTexture(std::vector<CacheTexture*>& cacheTextures, const SkGlyph& glyph,
Victoria Lease1e546812013-06-25 14:25:17 -0700143 uint32_t* startX, uint32_t* startY);
Romain Guy694b5192010-07-21 21:33:20 -0700144
145 void flushAllAndInvalidate();
Romain Guy694b5192010-07-21 21:33:20 -0700146
147 void checkInit();
Chris Craik82840732015-04-03 09:37:49 -0700148 void initRender(const Rect* clip, Rect* bounds, TextDrawFunctor* functor);
Romain Guy671d6cf2012-01-18 12:39:17 -0800149 void finishRender();
Romain Guy694b5192010-07-21 21:33:20 -0700150
John Reck272a6852015-07-29 16:48:58 -0700151 void issueDrawCommand(std::vector<CacheTexture*>& cacheTextures);
Romain Guy694b5192010-07-21 21:33:20 -0700152 void issueDrawCommand();
Romain Guy97771732012-02-28 18:17:02 -0800153 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
154 float x2, float y2, float u2, float v2,
155 float x3, float y3, float u3, float v3,
156 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guyd71dd362011-12-12 19:03:35 -0800157 void appendMeshQuad(float x1, float y1, float u1, float v1,
158 float x2, float y2, float u2, float v2,
159 float x3, float y3, float u3, float v3,
Chet Haase7de0cb12011-12-05 16:35:38 -0800160 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy97771732012-02-28 18:17:02 -0800161 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
162 float x2, float y2, float u2, float v2,
163 float x3, float y3, float u3, float v3,
164 float x4, float y4, float u4, float v4, CacheTexture* texture);
Romain Guy694b5192010-07-21 21:33:20 -0700165
Romain Guy9b1204b2012-09-04 15:22:57 -0700166 void checkTextureUpdate();
167
168 void setTextureDirty() {
169 mUploadTexture = true;
170 }
171
sergeyvbaf29e72016-09-08 11:09:34 -0700172 const std::vector<CacheTexture*>& cacheTexturesForFormat(GLenum format) const;
173 uint32_t getCacheSize(GLenum format) const;
174 uint32_t getFreeCacheSize(GLenum format) const;
175
Chet Haase7de0cb12011-12-05 16:35:38 -0800176 uint32_t mSmallCacheWidth;
177 uint32_t mSmallCacheHeight;
Chet Haaseeb32a492012-08-31 13:54:03 -0700178 uint32_t mLargeCacheWidth;
179 uint32_t mLargeCacheHeight;
Romain Guy694b5192010-07-21 21:33:20 -0700180
John Reck272a6852015-07-29 16:48:58 -0700181 std::vector<CacheTexture*> mACacheTextures;
182 std::vector<CacheTexture*> mRGBACacheTextures;
Romain Guy694b5192010-07-21 21:33:20 -0700183
Romain Guy09147fb2010-07-22 13:08:20 -0700184 Font* mCurrentFont;
Romain Guye3a9b242013-01-08 11:15:30 -0800185 LruCache<Font::FontDescription, Font*> mActiveFonts;
Romain Guy694b5192010-07-21 21:33:20 -0700186
Chet Haase7de0cb12011-12-05 16:35:38 -0800187 CacheTexture* mCurrentCacheTexture;
Chet Haase7de0cb12011-12-05 16:35:38 -0800188
Romain Guy694b5192010-07-21 21:33:20 -0700189 bool mUploadTexture;
190
Chris Craik82840732015-04-03 09:37:49 -0700191 TextDrawFunctor* mFunctor;
Romain Guy09147fb2010-07-22 13:08:20 -0700192 const Rect* mClip;
Romain Guy5b3b3522010-10-27 18:57:51 -0700193 Rect* mBounds;
194 bool mDrawn;
Romain Guy09147fb2010-07-22 13:08:20 -0700195
Romain Guy694b5192010-07-21 21:33:20 -0700196 bool mInitialized;
Alex Sakhartchouk89a524a2010-08-02 17:52:30 -0700197
Romain Guye8cb9c142010-10-04 14:14:11 -0700198 bool mLinearFiltering;
199
sergeyvaf102be2016-09-09 18:02:07 -0700200#ifdef BUGREPORT_FONT_CACHE_USAGE
201 FontCacheHistoryTracker mHistoryTracker;
202#endif
203
Dan Morrille4d9a012013-03-28 18:10:43 -0700204#ifdef ANDROID_ENABLE_RENDERSCRIPT
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800205 // RS constructs
Tim Murray250b1cf2013-08-01 14:49:22 -0700206 RSC::sp<RSC::RS> mRs;
207 RSC::sp<const RSC::Element> mRsElement;
208 RSC::sp<RSC::ScriptIntrinsicBlur> mRsScript;
Dan Morrille4d9a012013-03-28 18:10:43 -0700209#endif
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800210
Romain Guy9b1204b2012-09-04 15:22:57 -0700211 static void computeGaussianWeights(float* weights, int32_t radius);
212 static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700213 int32_t width, int32_t height);
Romain Guy9b1204b2012-09-04 15:22:57 -0700214 static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
Romain Guy1e45aae2010-08-13 19:39:53 -0700215 int32_t width, int32_t height);
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800216
217 // the input image handle may have its pointer replaced (to avoid copies)
Derek Sollenbergere392c812014-05-21 11:25:22 -0400218 void blurImage(uint8_t** image, int32_t width, int32_t height, float radius);
Romain Guy694b5192010-07-21 21:33:20 -0700219};
220
221}; // namespace uirenderer
222}; // namespace android