Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
Romain Guy | bd3055f | 2013-03-13 16:14:47 -0700 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_VIEW |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 19 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 20 | #include <cutils/compiler.h> |
| 21 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 22 | #include <utils/JenkinsHash.h> |
Romain Guy | bd3055f | 2013-03-13 16:14:47 -0700 | [diff] [blame] | 23 | #include <utils/Trace.h> |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 24 | |
Derek Sollenberger | 413995e | 2014-10-13 11:18:53 -0400 | [diff] [blame] | 25 | #include <SkDeviceProperties.h> |
Romain Guy | 14c40b4 | 2013-01-08 18:03:07 -0800 | [diff] [blame] | 26 | #include <SkGlyph.h> |
Victoria Lease | 43b692d | 2013-12-03 15:02:28 -0800 | [diff] [blame] | 27 | #include <SkGlyphCache.h> |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 28 | #include <SkUtils.h> |
| 29 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 30 | #include "FontUtil.h" |
| 31 | #include "Font.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 32 | #include "../Debug.h" |
| 33 | #include "../FontRenderer.h" |
| 34 | #include "../PixelBuffer.h" |
| 35 | #include "../Properties.h" |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | namespace uirenderer { |
| 39 | |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | // Font |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 44 | Font::Font(FontRenderer* state, const Font::FontDescription& desc) : |
Derek Sollenberger | 413995e | 2014-10-13 11:18:53 -0400 | [diff] [blame] | 45 | mState(state), mDescription(desc) { } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 46 | |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 47 | Font::FontDescription::FontDescription(const SkPaint* paint, const SkMatrix& rasterMatrix) |
| 48 | : mLookupTransform(rasterMatrix) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 49 | mFontId = SkTypeface::UniqueID(paint->getTypeface()); |
| 50 | mFontSize = paint->getTextSize(); |
| 51 | mFlags = 0; |
| 52 | if (paint->isFakeBoldText()) { |
| 53 | mFlags |= Font::kFakeBold; |
| 54 | } |
| 55 | mItalicStyle = paint->getTextSkewX(); |
| 56 | mScaleX = paint->getTextScaleX(); |
| 57 | mStyle = paint->getStyle(); |
| 58 | mStrokeWidth = paint->getStrokeWidth(); |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 59 | mAntiAliasing = paint->isAntiAlias(); |
Romain Guy | 2d5945e | 2013-06-18 12:59:25 -0700 | [diff] [blame] | 60 | mHinting = paint->getHinting(); |
Romain Guy | 874f5c6 | 2013-03-01 18:07:35 -0800 | [diff] [blame] | 61 | if (!mLookupTransform.invert(&mInverseLookupTransform)) { |
| 62 | ALOGW("Could not query the inverse lookup transform for this font"); |
| 63 | } |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 64 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 65 | |
| 66 | Font::~Font() { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 67 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { |
| 68 | delete mCachedGlyphs.valueAt(i); |
| 69 | } |
| 70 | } |
| 71 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 72 | hash_t Font::FontDescription::hash() const { |
| 73 | uint32_t hash = JenkinsHashMix(0, mFontId); |
| 74 | hash = JenkinsHashMix(hash, android::hash_type(mFontSize)); |
| 75 | hash = JenkinsHashMix(hash, android::hash_type(mFlags)); |
| 76 | hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle)); |
| 77 | hash = JenkinsHashMix(hash, android::hash_type(mScaleX)); |
| 78 | hash = JenkinsHashMix(hash, android::hash_type(mStyle)); |
| 79 | hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth)); |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 80 | hash = JenkinsHashMix(hash, int(mAntiAliasing)); |
Romain Guy | 2d5945e | 2013-06-18 12:59:25 -0700 | [diff] [blame] | 81 | hash = JenkinsHashMix(hash, android::hash_type(mHinting)); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 82 | hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX])); |
| 83 | hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY])); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 84 | return JenkinsHashWhiten(hash); |
| 85 | } |
| 86 | |
| 87 | int Font::FontDescription::compare(const Font::FontDescription& lhs, |
| 88 | const Font::FontDescription& rhs) { |
| 89 | int deltaInt = int(lhs.mFontId) - int(rhs.mFontId); |
| 90 | if (deltaInt != 0) return deltaInt; |
| 91 | |
| 92 | if (lhs.mFontSize < rhs.mFontSize) return -1; |
| 93 | if (lhs.mFontSize > rhs.mFontSize) return +1; |
| 94 | |
| 95 | if (lhs.mItalicStyle < rhs.mItalicStyle) return -1; |
| 96 | if (lhs.mItalicStyle > rhs.mItalicStyle) return +1; |
| 97 | |
| 98 | deltaInt = int(lhs.mFlags) - int(rhs.mFlags); |
| 99 | if (deltaInt != 0) return deltaInt; |
| 100 | |
| 101 | if (lhs.mScaleX < rhs.mScaleX) return -1; |
| 102 | if (lhs.mScaleX > rhs.mScaleX) return +1; |
| 103 | |
| 104 | deltaInt = int(lhs.mStyle) - int(rhs.mStyle); |
| 105 | if (deltaInt != 0) return deltaInt; |
| 106 | |
| 107 | if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1; |
| 108 | if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1; |
| 109 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 110 | deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing); |
| 111 | if (deltaInt != 0) return deltaInt; |
| 112 | |
Romain Guy | 2d5945e | 2013-06-18 12:59:25 -0700 | [diff] [blame] | 113 | deltaInt = int(lhs.mHinting) - int(rhs.mHinting); |
| 114 | if (deltaInt != 0) return deltaInt; |
| 115 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 116 | if (lhs.mLookupTransform[SkMatrix::kMScaleX] < |
| 117 | rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1; |
| 118 | if (lhs.mLookupTransform[SkMatrix::kMScaleX] > |
| 119 | rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1; |
| 120 | |
| 121 | if (lhs.mLookupTransform[SkMatrix::kMScaleY] < |
| 122 | rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1; |
| 123 | if (lhs.mLookupTransform[SkMatrix::kMScaleY] > |
| 124 | rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1; |
| 125 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 129 | void Font::invalidateTextureCache(CacheTexture* cacheTexture) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 130 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { |
| 131 | CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i); |
Romain Guy | 521dc51 | 2012-09-04 19:10:33 -0700 | [diff] [blame] | 132 | if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 133 | cachedGlyph->mIsValid = false; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 139 | uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 140 | int width = (int) glyph->mBitmapWidth; |
| 141 | int height = (int) glyph->mBitmapHeight; |
| 142 | |
Chris Craik | 39c5e7c | 2014-08-15 15:46:37 -0700 | [diff] [blame] | 143 | int nPenX = x + glyph->mBitmapLeft; |
| 144 | int nPenY = y + glyph->mBitmapTop; |
| 145 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 146 | if (bounds->bottom > nPenY) { |
| 147 | bounds->bottom = nPenY; |
| 148 | } |
| 149 | if (bounds->left > nPenX) { |
| 150 | bounds->left = nPenX; |
| 151 | } |
| 152 | if (bounds->right < nPenX + width) { |
| 153 | bounds->right = nPenX + width; |
| 154 | } |
| 155 | if (bounds->top < nPenY + height) { |
| 156 | bounds->top = nPenY + height; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y, |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 161 | uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 162 | float width = (float) glyph->mBitmapWidth; |
| 163 | float height = (float) glyph->mBitmapHeight; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 164 | |
Chris Craik | 39c5e7c | 2014-08-15 15:46:37 -0700 | [diff] [blame] | 165 | float nPenX = x + glyph->mBitmapLeft; |
| 166 | float nPenY = y + glyph->mBitmapTop + height; |
| 167 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 168 | float u1 = glyph->mBitmapMinU; |
| 169 | float u2 = glyph->mBitmapMaxU; |
| 170 | float v1 = glyph->mBitmapMinV; |
| 171 | float v2 = glyph->mBitmapMaxV; |
| 172 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 173 | mState->appendMeshQuad(nPenX, nPenY, u1, v2, |
| 174 | nPenX + width, nPenY, u2, v2, |
| 175 | nPenX + width, nPenY - height, u2, v1, |
| 176 | nPenX, nPenY - height, u1, v1, glyph->mCacheTexture); |
| 177 | } |
| 178 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 179 | void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y, |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 180 | uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { |
Chris Craik | 39c5e7c | 2014-08-15 15:46:37 -0700 | [diff] [blame] | 181 | float width = (float) glyph->mBitmapWidth; |
| 182 | float height = (float) glyph->mBitmapHeight; |
| 183 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 184 | SkPoint p[4]; |
Chris Craik | 39c5e7c | 2014-08-15 15:46:37 -0700 | [diff] [blame] | 185 | p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + height); |
| 186 | p[1].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop + height); |
| 187 | p[2].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop); |
Romain Guy | 874f5c6 | 2013-03-01 18:07:35 -0800 | [diff] [blame] | 188 | p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop); |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 189 | |
Romain Guy | 874f5c6 | 2013-03-01 18:07:35 -0800 | [diff] [blame] | 190 | mDescription.mInverseLookupTransform.mapPoints(p, 4); |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 191 | |
| 192 | p[0].offset(x, y); |
| 193 | p[1].offset(x, y); |
| 194 | p[2].offset(x, y); |
| 195 | p[3].offset(x, y); |
| 196 | |
| 197 | float u1 = glyph->mBitmapMinU; |
| 198 | float u2 = glyph->mBitmapMaxU; |
| 199 | float v1 = glyph->mBitmapMinV; |
| 200 | float v2 = glyph->mBitmapMaxV; |
| 201 | |
| 202 | mState->appendRotatedMeshQuad( |
Romain Guy | 874f5c6 | 2013-03-01 18:07:35 -0800 | [diff] [blame] | 203 | p[0].x(), p[0].y(), u1, v2, |
| 204 | p[1].x(), p[1].y(), u2, v2, |
| 205 | p[2].x(), p[2].y(), u2, v1, |
| 206 | p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture); |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 209 | void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap, |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 210 | uint32_t bitmapWidth, uint32_t bitmapHeight, Rect* bounds, const float* pos) { |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 211 | int dstX = x + glyph->mBitmapLeft; |
| 212 | int dstY = y + glyph->mBitmapTop; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 213 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 214 | CacheTexture* cacheTexture = glyph->mCacheTexture; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 215 | PixelBuffer* pixelBuffer = cacheTexture->getPixelBuffer(); |
Digish Pandya | b9312a5 | 2014-05-09 15:05:16 +0530 | [diff] [blame] | 216 | |
| 217 | uint32_t formatSize = PixelBuffer::formatSize(pixelBuffer->getFormat()); |
Digish Pandya | c62c1cc | 2014-05-12 14:37:04 +0530 | [diff] [blame] | 218 | uint32_t alpha_channel_offset = PixelBuffer::formatAlphaOffset(pixelBuffer->getFormat()); |
Digish Pandya | b9312a5 | 2014-05-09 15:05:16 +0530 | [diff] [blame] | 219 | uint32_t cacheWidth = cacheTexture->getWidth(); |
| 220 | uint32_t srcStride = formatSize * cacheWidth; |
| 221 | uint32_t startY = glyph->mStartY * srcStride; |
| 222 | uint32_t endY = startY + (glyph->mBitmapHeight * srcStride); |
| 223 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 224 | const uint8_t* cacheBuffer = pixelBuffer->map(); |
| 225 | |
| 226 | for (uint32_t cacheY = startY, bitmapY = dstY * bitmapWidth; cacheY < endY; |
Digish Pandya | b9312a5 | 2014-05-09 15:05:16 +0530 | [diff] [blame] | 227 | cacheY += srcStride, bitmapY += bitmapWidth) { |
| 228 | |
Chris Craik | de25a67 | 2015-06-30 13:12:24 -0700 | [diff] [blame] | 229 | for (uint32_t i = 0; i < glyph->mBitmapWidth; ++i) { |
| 230 | uint8_t* dst = &(bitmap[bitmapY + dstX + i]); |
| 231 | const uint8_t& src = cacheBuffer[ |
| 232 | cacheY + (glyph->mStartX + i)*formatSize + alpha_channel_offset]; |
| 233 | // Add alpha values to a max of 255, full opacity. This is done to handle |
| 234 | // fonts/strings where glyphs overlap. |
| 235 | *dst = std::min(*dst + src, 255); |
Digish Pandya | b9312a5 | 2014-05-09 15:05:16 +0530 | [diff] [blame] | 236 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset, |
| 241 | SkPathMeasure& measure, SkPoint* position, SkVector* tangent) { |
| 242 | const float halfWidth = glyph->mBitmapWidth * 0.5f; |
| 243 | const float height = glyph->mBitmapHeight; |
| 244 | |
| 245 | vOffset += glyph->mBitmapTop + height; |
| 246 | |
| 247 | SkPoint destination[4]; |
Romain Guy | e67307c | 2013-02-11 18:01:20 -0800 | [diff] [blame] | 248 | bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent); |
| 249 | if (!ok) { |
| 250 | ALOGW("The path for drawTextOnPath is empty or null"); |
| 251 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 252 | |
| 253 | // Move along the tangent and offset by the normal |
| 254 | destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset, |
| 255 | -tangent->fY * halfWidth + tangent->fX * vOffset); |
| 256 | destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset, |
| 257 | tangent->fY * halfWidth + tangent->fX * vOffset); |
| 258 | destination[2].set(destination[1].fX + tangent->fY * height, |
| 259 | destination[1].fY - tangent->fX * height); |
| 260 | destination[3].set(destination[0].fX + tangent->fY * height, |
| 261 | destination[0].fY - tangent->fX * height); |
| 262 | |
| 263 | const float u1 = glyph->mBitmapMinU; |
| 264 | const float u2 = glyph->mBitmapMaxU; |
| 265 | const float v1 = glyph->mBitmapMinV; |
| 266 | const float v2 = glyph->mBitmapMaxV; |
| 267 | |
| 268 | mState->appendRotatedMeshQuad( |
Romain Guy | 874f5c6 | 2013-03-01 18:07:35 -0800 | [diff] [blame] | 269 | position->x() + destination[0].x(), |
| 270 | position->y() + destination[0].y(), u1, v2, |
| 271 | position->x() + destination[1].x(), |
| 272 | position->y() + destination[1].y(), u2, v2, |
| 273 | position->x() + destination[2].x(), |
| 274 | position->y() + destination[2].y(), u2, v1, |
| 275 | position->x() + destination[3].x(), |
| 276 | position->y() + destination[3].y(), u1, v1, |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 277 | glyph->mCacheTexture); |
| 278 | } |
| 279 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 280 | CachedGlyphInfo* Font::getCachedGlyph(const SkPaint* paint, glyph_t textUnit, bool precaching) { |
Romain Guy | bd3055f | 2013-03-13 16:14:47 -0700 | [diff] [blame] | 281 | CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(textUnit); |
| 282 | if (cachedGlyph) { |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 283 | // Is the glyph still in texture cache? |
| 284 | if (!cachedGlyph->mIsValid) { |
Derek Sollenberger | 413995e | 2014-10-13 11:18:53 -0400 | [diff] [blame] | 285 | SkDeviceProperties deviceProperties(kUnknown_SkPixelGeometry, 1.0f); |
| 286 | SkAutoGlyphCache autoCache(*paint, &deviceProperties, &mDescription.mLookupTransform); |
Victoria Lease | 43b692d | 2013-12-03 15:02:28 -0800 | [diff] [blame] | 287 | const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), textUnit); |
Victoria Lease | 2ee2d59 | 2013-12-17 13:54:29 -0800 | [diff] [blame] | 288 | updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), cachedGlyph, precaching); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 289 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 290 | } else { |
| 291 | cachedGlyph = cacheGlyph(paint, textUnit, precaching); |
| 292 | } |
| 293 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 294 | return cachedGlyph; |
| 295 | } |
| 296 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 297 | void Font::render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 298 | int numGlyphs, int x, int y, const float* positions) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 299 | render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, nullptr, |
| 300 | 0, 0, nullptr, positions); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 303 | void Font::render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
| 304 | int numGlyphs, const SkPath* path, float hOffset, float vOffset) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 305 | if (numGlyphs == 0 || text == nullptr || len == 0) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 306 | return; |
| 307 | } |
| 308 | |
| 309 | text += start; |
| 310 | |
| 311 | int glyphsCount = 0; |
| 312 | SkFixed prevRsbDelta = 0; |
| 313 | |
| 314 | float penX = 0.0f; |
| 315 | |
| 316 | SkPoint position; |
| 317 | SkVector tangent; |
| 318 | |
| 319 | SkPathMeasure measure(*path, false); |
| 320 | float pathLength = SkScalarToFloat(measure.getLength()); |
| 321 | |
| 322 | if (paint->getTextAlign() != SkPaint::kLeft_Align) { |
| 323 | float textWidth = SkScalarToFloat(paint->measureText(text, len)); |
| 324 | float pathOffset = pathLength; |
| 325 | if (paint->getTextAlign() == SkPaint::kCenter_Align) { |
| 326 | textWidth *= 0.5f; |
| 327 | pathOffset *= 0.5f; |
| 328 | } |
| 329 | penX += pathOffset - textWidth; |
| 330 | } |
| 331 | |
| 332 | while (glyphsCount < numGlyphs && penX < pathLength) { |
| 333 | glyph_t glyph = GET_GLYPH(text); |
| 334 | |
| 335 | if (IS_END_OF_STRING(glyph)) { |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph); |
| 340 | penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta)); |
| 341 | prevRsbDelta = cachedGlyph->mRsbDelta; |
| 342 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 343 | if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 344 | drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent); |
| 345 | } |
| 346 | |
| 347 | penX += SkFixedToFloat(cachedGlyph->mAdvanceX); |
| 348 | |
| 349 | glyphsCount++; |
| 350 | } |
| 351 | } |
| 352 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 353 | void Font::measure(const SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 354 | int numGlyphs, Rect *bounds, const float* positions) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 355 | if (bounds == nullptr) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 356 | ALOGE("No return rectangle provided to measure text"); |
| 357 | return; |
| 358 | } |
| 359 | bounds->set(1e6, -1e6, -1e6, 1e6); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 360 | render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, nullptr, 0, 0, bounds, positions); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 363 | void Font::precache(const SkPaint* paint, const char* text, int numGlyphs) { |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 364 | ATRACE_NAME("Precache Glyphs"); |
Romain Guy | bd3055f | 2013-03-13 16:14:47 -0700 | [diff] [blame] | 365 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 366 | if (numGlyphs == 0 || text == nullptr) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 367 | return; |
| 368 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 369 | |
Romain Guy | bd3055f | 2013-03-13 16:14:47 -0700 | [diff] [blame] | 370 | int glyphsCount = 0; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 371 | while (glyphsCount < numGlyphs) { |
| 372 | glyph_t glyph = GET_GLYPH(text); |
| 373 | |
| 374 | // Reached the end of the string |
| 375 | if (IS_END_OF_STRING(glyph)) { |
| 376 | break; |
| 377 | } |
| 378 | |
Andreas Gampe | edaecc1 | 2014-11-10 20:54:07 -0800 | [diff] [blame] | 379 | getCachedGlyph(paint, glyph, true); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 380 | glyphsCount++; |
| 381 | } |
| 382 | } |
| 383 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 384 | void Font::render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 385 | int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap, |
| 386 | uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 387 | if (numGlyphs == 0 || text == nullptr || len == 0) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | |
| 391 | static RenderGlyph gRenderGlyph[] = { |
| 392 | &android::uirenderer::Font::drawCachedGlyph, |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 393 | &android::uirenderer::Font::drawCachedGlyphTransformed, |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 394 | &android::uirenderer::Font::drawCachedGlyphBitmap, |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 395 | &android::uirenderer::Font::drawCachedGlyphBitmap, |
| 396 | &android::uirenderer::Font::measureCachedGlyph, |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 397 | &android::uirenderer::Font::measureCachedGlyph |
| 398 | }; |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 399 | RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform]; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 400 | |
| 401 | text += start; |
| 402 | int glyphsCount = 0; |
| 403 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 404 | while (glyphsCount < numGlyphs) { |
| 405 | glyph_t glyph = GET_GLYPH(text); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 406 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 407 | // Reached the end of the string |
| 408 | if (IS_END_OF_STRING(glyph)) { |
| 409 | break; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 410 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 411 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 412 | CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 413 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 414 | // If it's still not valid, we couldn't cache it, so we shouldn't |
| 415 | // draw garbage; also skip empty glyphs (spaces) |
| 416 | if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) { |
Alexander Toresson | 3ed1927 | 2013-08-28 16:13:06 +0200 | [diff] [blame] | 417 | int penX = x + (int) roundf(positions[(glyphsCount << 1)]); |
| 418 | int penY = y + (int) roundf(positions[(glyphsCount << 1) + 1]); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 419 | |
Alexander Toresson | 3ed1927 | 2013-08-28 16:13:06 +0200 | [diff] [blame] | 420 | (*this.*render)(cachedGlyph, penX, penY, |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 421 | bitmap, bitmapW, bitmapH, bounds, positions); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 422 | } |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 423 | |
| 424 | glyphsCount++; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 428 | void Font::updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 429 | SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 430 | glyph->mAdvanceX = skiaGlyph.fAdvanceX; |
| 431 | glyph->mAdvanceY = skiaGlyph.fAdvanceY; |
| 432 | glyph->mBitmapLeft = skiaGlyph.fLeft; |
| 433 | glyph->mBitmapTop = skiaGlyph.fTop; |
| 434 | glyph->mLsbDelta = skiaGlyph.fLsbDelta; |
| 435 | glyph->mRsbDelta = skiaGlyph.fRsbDelta; |
| 436 | |
| 437 | uint32_t startX = 0; |
| 438 | uint32_t startY = 0; |
| 439 | |
| 440 | // Get the bitmap for the glyph |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 441 | if (!skiaGlyph.fImage) { |
Victoria Lease | 2ee2d59 | 2013-12-17 13:54:29 -0800 | [diff] [blame] | 442 | skiaGlyphCache->findImage(skiaGlyph); |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 443 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 444 | mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching); |
| 445 | |
| 446 | if (!glyph->mIsValid) { |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | uint32_t endX = startX + skiaGlyph.fWidth; |
| 451 | uint32_t endY = startY + skiaGlyph.fHeight; |
| 452 | |
| 453 | glyph->mStartX = startX; |
| 454 | glyph->mStartY = startY; |
| 455 | glyph->mBitmapWidth = skiaGlyph.fWidth; |
| 456 | glyph->mBitmapHeight = skiaGlyph.fHeight; |
| 457 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 458 | bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0; |
| 459 | if (!empty) { |
| 460 | uint32_t cacheWidth = glyph->mCacheTexture->getWidth(); |
| 461 | uint32_t cacheHeight = glyph->mCacheTexture->getHeight(); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 462 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 463 | glyph->mBitmapMinU = startX / (float) cacheWidth; |
| 464 | glyph->mBitmapMinV = startY / (float) cacheHeight; |
| 465 | glyph->mBitmapMaxU = endX / (float) cacheWidth; |
| 466 | glyph->mBitmapMaxV = endY / (float) cacheHeight; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 467 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 468 | mState->setTextureDirty(); |
| 469 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 472 | CachedGlyphInfo* Font::cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 473 | CachedGlyphInfo* newGlyph = new CachedGlyphInfo(); |
| 474 | mCachedGlyphs.add(glyph, newGlyph); |
| 475 | |
Derek Sollenberger | 413995e | 2014-10-13 11:18:53 -0400 | [diff] [blame] | 476 | SkDeviceProperties deviceProperties(kUnknown_SkPixelGeometry, 1.0f); |
| 477 | SkAutoGlyphCache autoCache(*paint, &deviceProperties, &mDescription.mLookupTransform); |
Victoria Lease | 43b692d | 2013-12-03 15:02:28 -0800 | [diff] [blame] | 478 | const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), glyph); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 479 | newGlyph->mIsValid = false; |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 480 | newGlyph->mGlyphIndex = skiaGlyph.fID; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 481 | |
Victoria Lease | 2ee2d59 | 2013-12-17 13:54:29 -0800 | [diff] [blame] | 482 | updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), newGlyph, precaching); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 483 | |
| 484 | return newGlyph; |
| 485 | } |
| 486 | |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 487 | Font* Font::create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 488 | FontDescription description(paint, matrix); |
| 489 | Font* font = state->mActiveFonts.get(description); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 490 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 491 | if (!font) { |
| 492 | font = new Font(state, description); |
| 493 | state->mActiveFonts.put(description, font); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 494 | } |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 495 | font->mIdentityTransform = matrix.isIdentity(); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 496 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 497 | return font; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | }; // namespace uirenderer |
| 501 | }; // namespace android |