blob: bacd3aadba4f303dfa93af1e99e45add6aa4a789 [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
17#ifndef ANDROID_UI_FONT_RENDERER_H
18#define ANDROID_UI_FONT_RENDERER_H
19
20#include <utils/String8.h>
21#include <utils/Vector.h>
22#include <utils/KeyedVector.h>
23
24#include <SkScalerContext.h>
25#include <SkPaint.h>
26
27#include <GLES2/gl2.h>
28
Romain Guy09147fb2010-07-22 13:08:20 -070029#include "Rect.h"
Romain Guy51769a62010-07-23 00:28:00 -070030#include "Properties.h"
Romain Guy09147fb2010-07-22 13:08:20 -070031
Romain Guy694b5192010-07-21 21:33:20 -070032namespace android {
33namespace uirenderer {
34
35class FontRenderer;
36
Romain Guy51769a62010-07-23 00:28:00 -070037/**
38 * Represents a font, defined by a Skia font id and a font size. A font is used
39 * to generate glyphs and cache them in the FontState.
40 */
Romain Guy694b5192010-07-21 21:33:20 -070041class Font {
42public:
43 ~Font();
44
Romain Guy51769a62010-07-23 00:28:00 -070045 /**
46 * Renders the specified string of text.
47 */
48 void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Romain Guy694b5192010-07-21 21:33:20 -070049 int numGlyphs, int x, int y);
Romain Guy51769a62010-07-23 00:28:00 -070050 /**
51 * Creates a new font associated with the specified font state.
52 */
Romain Guy694b5192010-07-21 21:33:20 -070053 static Font* create(FontRenderer* state, uint32_t fontId, float fontSize);
54
55protected:
Romain Guy694b5192010-07-21 21:33:20 -070056 friend class FontRenderer;
57
Romain Guy694b5192010-07-21 21:33:20 -070058 struct CachedGlyphInfo {
59 // Has the cache been invalidated?
60 bool mIsValid;
61 // Location of the cached glyph in the bitmap
62 // in case we need to resize the texture
63 uint32_t mBitmapWidth;
64 uint32_t mBitmapHeight;
65 // Also cache texture coords for the quad
66 float mBitmapMinU;
67 float mBitmapMinV;
68 float mBitmapMaxU;
69 float mBitmapMaxV;
70 // Minimize how much we call freetype
71 uint32_t mGlyphIndex;
72 uint32_t mAdvanceX;
73 uint32_t mAdvanceY;
74 // Values below contain a glyph's origin in the bitmap
75 uint32_t mBitmapLeft;
76 uint32_t mBitmapTop;
77 };
78
Romain Guy694b5192010-07-21 21:33:20 -070079 Font(FontRenderer* state, uint32_t fontId, float fontSize);
80
81 DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs;
82
Romain Guybd0e6aa2010-07-22 18:50:12 -070083 void invalidateTextureCache();
84
Romain Guy694b5192010-07-21 21:33:20 -070085 CachedGlyphInfo *cacheGlyph(SkPaint* paint, int32_t glyph);
86 void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph);
87 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y);
Romain Guybd0e6aa2010-07-22 18:50:12 -070088
89 FontRenderer* mState;
90 uint32_t mFontId;
91 float mFontSize;
Romain Guy694b5192010-07-21 21:33:20 -070092};
93
94class FontRenderer {
95public:
96 FontRenderer();
97 ~FontRenderer();
98
99 void init();
100 void deinit();
101
102 void setFont(uint32_t fontId, float fontSize);
Romain Guy51769a62010-07-23 00:28:00 -0700103 void renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
104 uint32_t len, int numGlyphs, int x, int y);
Romain Guy694b5192010-07-21 21:33:20 -0700105
106 GLuint getTexture() {
107 checkInit();
108 return mTextureId;
109 }
110
111protected:
112 friend class Font;
113
114 struct CacheTextureLine {
115 uint16_t mMaxHeight;
116 uint16_t mMaxWidth;
117 uint32_t mCurrentRow;
118 uint32_t mCurrentCol;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700119 bool mDirty;
Romain Guy694b5192010-07-21 21:33:20 -0700120
Romain Guy51769a62010-07-23 00:28:00 -0700121 CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow,
Romain Guy694b5192010-07-21 21:33:20 -0700122 uint32_t currentCol):
Romain Guy51769a62010-07-23 00:28:00 -0700123 mMaxHeight(maxHeight),
124 mMaxWidth(maxWidth),
125 mCurrentRow(currentRow),
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700126 mCurrentCol(currentCol),
127 mDirty(false) {
Romain Guy694b5192010-07-21 21:33:20 -0700128 }
129
130 bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) {
131 if (glyph.fHeight > mMaxHeight) {
132 return false;
133 }
134
135 if (mCurrentCol + glyph.fWidth < mMaxWidth) {
136 *retOriginX = mCurrentCol;
137 *retOriginY = mCurrentRow;
138 mCurrentCol += glyph.fWidth;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700139 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -0700140 return true;
141 }
142
143 return false;
144 }
145 };
146
147 uint32_t getCacheWidth() const {
148 return mCacheWidth;
149 }
150
151 uint32_t getCacheHeight() const {
152 return mCacheHeight;
153 }
154
155 void initTextTexture();
Romain Guy694b5192010-07-21 21:33:20 -0700156 bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);
157
158 void flushAllAndInvalidate();
159 void initVertexArrayBuffers();
160
161 void checkInit();
162
163 void issueDrawCommand();
Romain Guy694b5192010-07-21 21:33:20 -0700164 void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2,
165 float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3,
166 float x4, float y4, float z4, float u4, float v4);
167
168 uint32_t mCacheWidth;
169 uint32_t mCacheHeight;
170
Romain Guy694b5192010-07-21 21:33:20 -0700171 Vector<CacheTextureLine*> mCacheLines;
172
Romain Guy09147fb2010-07-22 13:08:20 -0700173 Font* mCurrentFont;
Romain Guy694b5192010-07-21 21:33:20 -0700174 Vector<Font*> mActiveFonts;
175
176 // Texture to cache glyph bitmaps
177 unsigned char* mTextTexture;
178 GLuint mTextureId;
Alex Sakhartchouk9b9902d2010-07-23 14:45:49 -0700179 void checkTextureUpdate();
Romain Guy694b5192010-07-21 21:33:20 -0700180 bool mUploadTexture;
181
182 // Pointer to vertex data to speed up frame to frame work
183 float *mTextMeshPtr;
184 uint32_t mCurrentQuadIndex;
185 uint32_t mMaxNumberOfQuads;
186
187 uint32_t mIndexBufferID;
188
Romain Guy09147fb2010-07-22 13:08:20 -0700189 const Rect* mClip;
190
Romain Guy694b5192010-07-21 21:33:20 -0700191 bool mInitialized;
192};
193
194}; // namespace uirenderer
195}; // namespace android
196
197#endif // ANDROID_UI_FONT_RENDERER_H