blob: 6b9dec4719cbd71baf04451bc1b51b8415f290a3 [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/CacheTexture.h"
20#include "font/CachedGlyphInfo.h"
21#include "font/Font.h"
John Reck1bcacfd2017-11-03 10:12:19 -070022#include "font/FontUtil.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
Tim Murray250b1cf2013-08-01 14:49:22 -070037#include "RenderScript.h"
Chris Craikf2d8ccc2013-02-13 16:14:17 -080038namespace RSC {
John Reck1bcacfd2017-11-03 10:12:19 -070039class Element;
40class RS;
41class ScriptIntrinsicBlur;
42class sp;
Chris Craikf2d8ccc2013-02-13 16:14:17 -080043}
44
Romain Guy694b5192010-07-21 21:33:20 -070045namespace android {
46namespace uirenderer {
47
Chris Craika1717272015-11-19 13:02:43 -080048class BakedOpState;
49class BakedOpRenderer;
Chris Craike4db79d2015-12-22 16:32:23 -080050struct ClipBase;
Victoria Lease1e546812013-06-25 14:25:17 -070051
Chris Craik82840732015-04-03 09:37:49 -070052class TextDrawFunctor {
Victoria Lease1e546812013-06-25 14:25:17 -070053public:
John Reck1bcacfd2017-11-03 10:12:19 -070054 TextDrawFunctor(BakedOpRenderer* renderer, const BakedOpState* bakedState, const ClipBase* clip,
55 float x, float y, bool pureTranslate, int alpha, SkBlendMode mode,
56 const SkPaint* paint)
57 : renderer(renderer)
58 , bakedState(bakedState)
59 , clip(clip)
60 , x(x)
61 , y(y)
62 , pureTranslate(pureTranslate)
63 , alpha(alpha)
64 , mode(mode)
65 , paint(paint) {}
Victoria Lease1e546812013-06-25 14:25:17 -070066
Chris Craike2bb3802015-03-13 15:07:52 -070067 void draw(CacheTexture& texture, bool linearFiltering);
Victoria Lease1e546812013-06-25 14:25:17 -070068
Chris Craika1717272015-11-19 13:02:43 -080069 BakedOpRenderer* renderer;
70 const BakedOpState* bakedState;
Chris Craike4db79d2015-12-22 16:32:23 -080071 const ClipBase* clip;
Victoria Lease1e546812013-06-25 14:25:17 -070072 float x;
73 float y;
74 bool pureTranslate;
75 int alpha;
Mike Reed260ab722016-10-07 15:59:20 -040076 SkBlendMode mode;
Chris Craikd218a922014-01-02 17:13:34 -080077 const SkPaint* paint;
Victoria Lease1e546812013-06-25 14:25:17 -070078};
79
Romain Guy694b5192010-07-21 21:33:20 -070080class FontRenderer {
81public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -070082 explicit FontRenderer(const uint8_t* gammaTable);
Romain Guy694b5192010-07-21 21:33:20 -070083 ~FontRenderer();
84
John Reck272a6852015-07-29 16:48:58 -070085 void flushLargeCaches(std::vector<CacheTexture*>& cacheTextures);
Chet Haase9a824562011-12-16 15:44:59 -080086 void flushLargeCaches();
Romain Guy694b5192010-07-21 21:33:20 -070087
Chris Craik59744b72014-07-01 17:56:52 -070088 void setFont(const SkPaint* paint, const SkMatrix& matrix);
Chet Haasee816bae2012-08-09 13:39:02 -070089
John Reck1bcacfd2017-11-03 10:12:19 -070090 void precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs,
91 const SkMatrix& matrix);
Romain Guycf51a412013-04-08 19:40:31 -070092 void endPrecaching();
Chet Haasee816bae2012-08-09 13:39:02 -070093
John Reck1bcacfd2017-11-03 10:12:19 -070094 bool renderPosText(const SkPaint* paint, const Rect* clip, const glyph_t* glyphs, int numGlyphs,
95 int x, int y, const float* positions, Rect* outBounds,
96 TextDrawFunctor* functor, bool forceFinish = true);
Chris Craik527a3aa2013-03-04 10:19:31 -080097
Chris Craike8c3c812016-02-05 20:10:50 -080098 bool renderTextOnPath(const SkPaint* paint, const Rect* clip, const glyph_t* glyphs,
John Reck1bcacfd2017-11-03 10:12:19 -070099 int numGlyphs, const SkPath* path, float hOffset, float vOffset,
100 Rect* outBounds, TextDrawFunctor* 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
John Reck1bcacfd2017-11-03 10:12:19 -0700112 DropShadow renderDropShadow(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs,
113 float radius, const float* positions);
Alex Sakhartchoukf18136c2010-08-06 14:49:04 -0700114
John Reck1bcacfd2017-11-03 10:12:19 -0700115 void setTextureFiltering(bool linearFiltering) { mLinearFiltering = linearFiltering; }
Romain Guy694b5192010-07-21 21:33:20 -0700116
sergeyvbaf29e72016-09-08 11:09:34 -0700117 uint32_t getSize() const;
118 void dumpMemoryUsage(String8& log) const;
Romain Guyc15008e2010-11-10 11:59:15 -0800119
sergeyvaf102be2016-09-09 18:02:07 -0700120#ifdef BUGREPORT_FONT_CACHE_USAGE
121 FontCacheHistoryTracker& historyTracker() { return mHistoryTracker; }
122#endif
Romain Guy694b5192010-07-21 21:33:20 -0700123
Romain Guy9b1204b2012-09-04 15:22:57 -0700124private:
Romain Guy694b5192010-07-21 21:33:20 -0700125 friend class Font;
126
Romain Guyb45c0c92010-08-26 20:35:23 -0700127 const uint8_t* mGammaTable;
128
Chet Haase2a47c142011-12-14 15:22:56 -0800129 void allocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase9a824562011-12-16 15:44:59 -0800130 void deallocateTextureMemory(CacheTexture* cacheTexture);
Chet Haase7de0cb12011-12-05 16:35:38 -0800131 void initTextTexture();
Victoria Lease1e546812013-06-25 14:25:17 -0700132 CacheTexture* createCacheTexture(int width, int height, GLenum format, bool allocate);
John Reck1bcacfd2017-11-03 10:12:19 -0700133 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, uint32_t* retOriginX,
134 uint32_t* retOriginY, bool precaching);
135 CacheTexture* cacheBitmapInTexture(std::vector<CacheTexture*>& cacheTextures,
136 const SkGlyph& glyph, uint32_t* startX, uint32_t* startY);
Romain Guy694b5192010-07-21 21:33:20 -0700137
138 void flushAllAndInvalidate();
Romain Guy694b5192010-07-21 21:33:20 -0700139
140 void checkInit();
Chris Craik82840732015-04-03 09:37:49 -0700141 void initRender(const Rect* clip, Rect* bounds, TextDrawFunctor* functor);
Romain Guy671d6cf2012-01-18 12:39:17 -0800142 void finishRender();
Romain Guy694b5192010-07-21 21:33:20 -0700143
John Reck272a6852015-07-29 16:48:58 -0700144 void issueDrawCommand(std::vector<CacheTexture*>& cacheTextures);
Romain Guy694b5192010-07-21 21:33:20 -0700145 void issueDrawCommand();
John Reck1bcacfd2017-11-03 10:12:19 -0700146 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1, float x2, float y2, float u2,
147 float v2, float x3, float y3, float u3, float v3, float x4, float y4,
148 float u4, float v4, CacheTexture* texture);
149 void appendMeshQuad(float x1, float y1, float u1, float v1, float x2, float y2, float u2,
150 float v2, float x3, float y3, float u3, float v3, float x4, float y4,
151 float u4, float v4, CacheTexture* texture);
152 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1, float x2, float y2, float u2,
153 float v2, float x3, float y3, float u3, float v3, float x4, float y4,
154 float u4, float v4, CacheTexture* texture);
Romain Guy694b5192010-07-21 21:33:20 -0700155
Romain Guy9b1204b2012-09-04 15:22:57 -0700156 void checkTextureUpdate();
157
John Reck1bcacfd2017-11-03 10:12:19 -0700158 void setTextureDirty() { mUploadTexture = true; }
Romain Guy9b1204b2012-09-04 15:22:57 -0700159
sergeyvbaf29e72016-09-08 11:09:34 -0700160 const std::vector<CacheTexture*>& cacheTexturesForFormat(GLenum format) const;
161 uint32_t getCacheSize(GLenum format) const;
162 uint32_t getFreeCacheSize(GLenum format) const;
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
John Reck272a6852015-07-29 16:48:58 -0700169 std::vector<CacheTexture*> mACacheTextures;
170 std::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 Craik82840732015-04-03 09:37:49 -0700179 TextDrawFunctor* 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
sergeyvaf102be2016-09-09 18:02:07 -0700188#ifdef BUGREPORT_FONT_CACHE_USAGE
189 FontCacheHistoryTracker mHistoryTracker;
190#endif
191
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800192 // RS constructs
Tim Murray250b1cf2013-08-01 14:49:22 -0700193 RSC::sp<RSC::RS> mRs;
194 RSC::sp<const RSC::Element> mRsElement;
195 RSC::sp<RSC::ScriptIntrinsicBlur> mRsScript;
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800196
Romain Guy9b1204b2012-09-04 15:22:57 -0700197 static void computeGaussianWeights(float* weights, int32_t radius);
John Reck1bcacfd2017-11-03 10:12:19 -0700198 static void horizontalBlur(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest,
199 int32_t width, int32_t height);
200 static void verticalBlur(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest,
201 int32_t width, int32_t height);
Chris Craikf2d8ccc2013-02-13 16:14:17 -0800202
203 // the input image handle may have its pointer replaced (to avoid copies)
Derek Sollenbergere392c812014-05-21 11:25:22 -0400204 void blurImage(uint8_t** image, int32_t width, int32_t height, float radius);
Romain Guy694b5192010-07-21 21:33:20 -0700205};
206
John Reck1bcacfd2017-11-03 10:12:19 -0700207}; // namespace uirenderer
208}; // namespace android