jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrBitmapTextContext.h" |
| 9 | #include "GrAtlas.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 10 | #include "GrDefaultGeoProcFactory.h" |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame] | 12 | #include "GrFontCache.h" |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 13 | #include "GrFontScaler.h" |
| 14 | #include "GrIndexBuffer.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 15 | #include "GrStrokeInfo.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 16 | #include "GrTexturePriv.h" |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 17 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 18 | #include "SkAutoKern.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 19 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 20 | #include "SkDraw.h" |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 21 | #include "SkDrawProcs.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 22 | #include "SkGlyphCache.h" |
| 23 | #include "SkGpuDevice.h" |
| 24 | #include "SkGr.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 25 | #include "SkPath.h" |
| 26 | #include "SkRTConf.h" |
| 27 | #include "SkStrokeRec.h" |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 28 | #include "SkTextMapStateProc.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 29 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 30 | #include "effects/GrBitmapTextGeoProc.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 31 | #include "effects/GrSimpleTextureEffect.h" |
| 32 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 33 | SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, |
| 34 | "Dump the contents of the font cache before every purge."); |
| 35 | |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 36 | namespace { |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 37 | static const size_t kLCDTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16); |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 38 | |
| 39 | // position + local coord |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 40 | static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16); |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 41 | |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 42 | static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16); |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 43 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 44 | static const int kVerticesPerGlyph = 4; |
| 45 | static const int kIndicesPerGlyph = 6; |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 48 | GrBitmapTextContext::GrBitmapTextContext(GrContext* context, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 49 | SkGpuDevice* gpuDevice, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 50 | const SkDeviceProperties& properties) |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 51 | : GrTextContext(context, gpuDevice, properties) { |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 52 | fStrike = NULL; |
| 53 | |
| 54 | fCurrTexture = NULL; |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 55 | fEffectTextureUniqueID = SK_InvalidUniqueID; |
| 56 | |
| 57 | fVertices = NULL; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 58 | fCurrVertex = 0; |
| 59 | fAllocVertexCount = 0; |
| 60 | fTotalVertexCount = 0; |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 61 | |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 62 | fVertexBounds.setLargestInverted(); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 63 | } |
| 64 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 65 | GrBitmapTextContext* GrBitmapTextContext::Create(GrContext* context, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 66 | SkGpuDevice* gpuDevice, |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 67 | const SkDeviceProperties& props) { |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 68 | return SkNEW_ARGS(GrBitmapTextContext, (context, gpuDevice, props)); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 69 | } |
| 70 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 71 | bool GrBitmapTextContext::canDraw(const SkPaint& paint, const SkMatrix& viewMatrix) { |
| 72 | return !SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix); |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 73 | } |
| 74 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 75 | inline void GrBitmapTextContext::init(GrRenderTarget* rt, const GrClip& clip, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 76 | const GrPaint& paint, const SkPaint& skPaint, |
| 77 | const SkIRect& regionClipBounds) { |
| 78 | GrTextContext::init(rt, clip, paint, skPaint, regionClipBounds); |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 79 | |
| 80 | fStrike = NULL; |
| 81 | |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 82 | fCurrTexture = NULL; |
jvanverth | 63b9dc8 | 2014-08-28 10:39:40 -0700 | [diff] [blame] | 83 | fCurrVertex = 0; |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 84 | |
| 85 | fVertices = NULL; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 86 | fAllocVertexCount = 0; |
| 87 | fTotalVertexCount = 0; |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 88 | } |
| 89 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 90 | void GrBitmapTextContext::onDrawText(GrRenderTarget* rt, const GrClip& clip, |
| 91 | const GrPaint& paint, const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 92 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 93 | const char text[], size_t byteLength, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 94 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 95 | SkASSERT(byteLength == 0 || text != NULL); |
| 96 | |
| 97 | // nothing to draw |
| 98 | if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { |
| 99 | return; |
| 100 | } |
| 101 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 102 | this->init(rt, clip, paint, skPaint, regionClipBounds); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 103 | |
| 104 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 105 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 106 | SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &viewMatrix); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 107 | SkGlyphCache* cache = autoCache.getCache(); |
| 108 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 109 | |
| 110 | // transform our starting point |
| 111 | { |
| 112 | SkPoint loc; |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 113 | viewMatrix.mapXY(x, y, &loc); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 114 | x = loc.fX; |
| 115 | y = loc.fY; |
| 116 | } |
| 117 | |
| 118 | // need to measure first |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 119 | int numGlyphs; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 120 | if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 121 | SkVector stopVector; |
| 122 | numGlyphs = MeasureText(cache, glyphCacheProc, text, byteLength, &stopVector); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 123 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 124 | SkScalar stopX = stopVector.fX; |
| 125 | SkScalar stopY = stopVector.fY; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 126 | |
| 127 | if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 128 | stopX = SkScalarHalf(stopX); |
| 129 | stopY = SkScalarHalf(stopY); |
| 130 | } |
| 131 | x -= stopX; |
| 132 | y -= stopY; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 133 | } else { |
| 134 | numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 135 | } |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 136 | fTotalVertexCount = kVerticesPerGlyph*numGlyphs; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 137 | |
| 138 | const char* stop = text + byteLength; |
| 139 | |
| 140 | SkAutoKern autokern; |
| 141 | |
| 142 | SkFixed fxMask = ~0; |
| 143 | SkFixed fyMask = ~0; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 144 | SkScalar halfSampleX, halfSampleY; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 145 | if (cache->isSubpixel()) { |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 146 | halfSampleX = halfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 147 | SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 148 | if (kX_SkAxisAlignment == baseline) { |
| 149 | fyMask = 0; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 150 | halfSampleY = SK_ScalarHalf; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 151 | } else if (kY_SkAxisAlignment == baseline) { |
| 152 | fxMask = 0; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 153 | halfSampleX = SK_ScalarHalf; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 154 | } |
| 155 | } else { |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 156 | halfSampleX = halfSampleY = SK_ScalarHalf; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 157 | } |
| 158 | |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 159 | Sk48Dot16 fx = SkScalarTo48Dot16(x + halfSampleX); |
| 160 | Sk48Dot16 fy = SkScalarTo48Dot16(y + halfSampleY); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 161 | |
joshualitt | 73483d9 | 2014-12-23 07:58:02 -0800 | [diff] [blame] | 162 | // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix, but for |
| 163 | // performance reasons we just invert here instead |
| 164 | if (!viewMatrix.invert(&fLocalMatrix)) { |
joshualitt | 787bb48 | 2015-01-02 10:54:26 -0800 | [diff] [blame] | 165 | SkDebugf("Cannot invert viewmatrix\n"); |
| 166 | return; |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 167 | } |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 168 | |
| 169 | while (text < stop) { |
| 170 | const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask); |
| 171 | |
| 172 | fx += autokern.adjust(glyph); |
| 173 | |
| 174 | if (glyph.fWidth) { |
| 175 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 176 | glyph.getSubXFixed(), |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 177 | glyph.getSubYFixed(), |
| 178 | GrGlyph::kCoverage_MaskStyle), |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 179 | Sk48Dot16FloorToInt(fx), |
| 180 | Sk48Dot16FloorToInt(fy), |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 181 | fontScaler); |
| 182 | } |
| 183 | |
| 184 | fx += glyph.fAdvanceX; |
| 185 | fy += glyph.fAdvanceY; |
| 186 | } |
| 187 | |
| 188 | this->finish(); |
| 189 | } |
| 190 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 191 | void GrBitmapTextContext::onDrawPosText(GrRenderTarget* rt, const GrClip& clip, |
| 192 | const GrPaint& paint, const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 193 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 194 | const char text[], size_t byteLength, |
| 195 | const SkScalar pos[], int scalarsPerPosition, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 196 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 197 | SkASSERT(byteLength == 0 || text != NULL); |
| 198 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 199 | |
| 200 | // nothing to draw |
| 201 | if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { |
| 202 | return; |
| 203 | } |
| 204 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 205 | this->init(rt, clip, paint, skPaint, regionClipBounds); |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 206 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 207 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 208 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 209 | SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &viewMatrix); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 210 | SkGlyphCache* cache = autoCache.getCache(); |
| 211 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 212 | |
joshualitt | 73483d9 | 2014-12-23 07:58:02 -0800 | [diff] [blame] | 213 | // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix, but for |
| 214 | // performance reasons we just invert here instead |
| 215 | if (!viewMatrix.invert(&fLocalMatrix)) { |
joshualitt | 787bb48 | 2015-01-02 10:54:26 -0800 | [diff] [blame] | 216 | SkDebugf("Cannot invert viewmatrix\n"); |
| 217 | return; |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 218 | } |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 219 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 220 | int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL); |
| 221 | fTotalVertexCount = kVerticesPerGlyph*numGlyphs; |
| 222 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 223 | const char* stop = text + byteLength; |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 224 | SkTextAlignProc alignProc(fSkPaint.getTextAlign()); |
joshualitt | 73483d9 | 2014-12-23 07:58:02 -0800 | [diff] [blame] | 225 | SkTextMapStateProc tmsProc(viewMatrix, offset, scalarsPerPosition); |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 226 | SkScalar halfSampleX = 0, halfSampleY = 0; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 227 | |
| 228 | if (cache->isSubpixel()) { |
| 229 | // maybe we should skip the rounding if linearText is set |
joshualitt | 73483d9 | 2014-12-23 07:58:02 -0800 | [diff] [blame] | 230 | SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 231 | |
| 232 | SkFixed fxMask = ~0; |
| 233 | SkFixed fyMask = ~0; |
| 234 | if (kX_SkAxisAlignment == baseline) { |
| 235 | fyMask = 0; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 236 | halfSampleY = SK_ScalarHalf; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 237 | } else if (kY_SkAxisAlignment == baseline) { |
| 238 | fxMask = 0; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 239 | halfSampleX = SK_ScalarHalf; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
| 243 | while (text < stop) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 244 | SkPoint tmsLoc; |
| 245 | tmsProc(pos, &tmsLoc); |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 246 | Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + halfSampleX); |
| 247 | Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + halfSampleY); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 248 | |
| 249 | const SkGlyph& glyph = glyphCacheProc(cache, &text, |
| 250 | fx & fxMask, fy & fyMask); |
| 251 | |
| 252 | if (glyph.fWidth) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 253 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 254 | glyph.getSubXFixed(), |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 255 | glyph.getSubYFixed(), |
| 256 | GrGlyph::kCoverage_MaskStyle), |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 257 | Sk48Dot16FloorToInt(fx), |
| 258 | Sk48Dot16FloorToInt(fy), |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 259 | fontScaler); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 260 | } |
| 261 | pos += scalarsPerPosition; |
| 262 | } |
| 263 | } else { |
| 264 | while (text < stop) { |
| 265 | const char* currentText = text; |
| 266 | const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0); |
| 267 | |
| 268 | if (metricGlyph.fWidth) { |
| 269 | SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;) |
| 270 | SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;) |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 271 | SkPoint tmsLoc; |
| 272 | tmsProc(pos, &tmsLoc); |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 273 | SkPoint alignLoc; |
| 274 | alignProc(tmsLoc, metricGlyph, &alignLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 275 | |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 276 | Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + halfSampleX); |
| 277 | Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + halfSampleY); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 278 | |
| 279 | // have to call again, now that we've been "aligned" |
| 280 | const SkGlyph& glyph = glyphCacheProc(cache, ¤tText, |
| 281 | fx & fxMask, fy & fyMask); |
| 282 | // the assumption is that the metrics haven't changed |
| 283 | SkASSERT(prevAdvX == glyph.fAdvanceX); |
| 284 | SkASSERT(prevAdvY == glyph.fAdvanceY); |
| 285 | SkASSERT(glyph.fWidth); |
| 286 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 287 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 288 | glyph.getSubXFixed(), |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 289 | glyph.getSubYFixed(), |
| 290 | GrGlyph::kCoverage_MaskStyle), |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 291 | Sk48Dot16FloorToInt(fx), |
| 292 | Sk48Dot16FloorToInt(fy), |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 293 | fontScaler); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 294 | } |
| 295 | pos += scalarsPerPosition; |
| 296 | } |
| 297 | } |
| 298 | } else { // not subpixel |
| 299 | |
| 300 | if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
| 301 | while (text < stop) { |
| 302 | // the last 2 parameters are ignored |
| 303 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 304 | |
| 305 | if (glyph.fWidth) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 306 | SkPoint tmsLoc; |
| 307 | tmsProc(pos, &tmsLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 308 | |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 309 | Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + SK_ScalarHalf); //halfSampleX; |
| 310 | Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + SK_ScalarHalf); //halfSampleY; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 311 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 312 | glyph.getSubXFixed(), |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 313 | glyph.getSubYFixed(), |
| 314 | GrGlyph::kCoverage_MaskStyle), |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 315 | Sk48Dot16FloorToInt(fx), |
| 316 | Sk48Dot16FloorToInt(fy), |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 317 | fontScaler); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 318 | } |
| 319 | pos += scalarsPerPosition; |
| 320 | } |
| 321 | } else { |
| 322 | while (text < stop) { |
| 323 | // the last 2 parameters are ignored |
| 324 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 325 | |
| 326 | if (glyph.fWidth) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 327 | SkPoint tmsLoc; |
| 328 | tmsProc(pos, &tmsLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 329 | |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 330 | SkPoint alignLoc; |
| 331 | alignProc(tmsLoc, glyph, &alignLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 332 | |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 333 | Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + SK_ScalarHalf); //halfSampleX; |
| 334 | Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + SK_ScalarHalf); //halfSampleY; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 335 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 336 | glyph.getSubXFixed(), |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 337 | glyph.getSubYFixed(), |
| 338 | GrGlyph::kCoverage_MaskStyle), |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 339 | Sk48Dot16FloorToInt(fx), |
| 340 | Sk48Dot16FloorToInt(fy), |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 341 | fontScaler); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 342 | } |
| 343 | pos += scalarsPerPosition; |
| 344 | } |
| 345 | } |
| 346 | } |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 347 | |
| 348 | this->finish(); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 349 | } |
| 350 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 351 | static size_t get_vertex_stride(GrMaskFormat maskFormat) { |
| 352 | switch (maskFormat) { |
| 353 | case kA8_GrMaskFormat: |
| 354 | return kGrayTextVASize; |
| 355 | case kARGB_GrMaskFormat: |
| 356 | return kColorTextVASize; |
| 357 | default: |
| 358 | return kLCDTextVASize; |
| 359 | } |
| 360 | } |
| 361 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 362 | static void* alloc_vertices(GrDrawTarget* drawTarget, |
| 363 | int numVertices, |
| 364 | GrMaskFormat maskFormat) { |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 365 | if (numVertices <= 0) { |
| 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | // set up attributes |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 370 | void* vertices = NULL; |
| 371 | bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 372 | get_vertex_stride(maskFormat), |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 373 | 0, |
| 374 | &vertices, |
| 375 | NULL); |
| 376 | GrAlwaysAssert(success); |
| 377 | return vertices; |
| 378 | } |
| 379 | |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame] | 380 | inline bool GrBitmapTextContext::uploadGlyph(GrGlyph* glyph, GrFontScaler* scaler) { |
| 381 | if (!fStrike->glyphTooLargeForAtlas(glyph)) { |
| 382 | if (fStrike->addGlyphToAtlas(glyph, scaler)) { |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | // try to clear out an unused plot before we flush |
| 387 | if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) && |
| 388 | fStrike->addGlyphToAtlas(glyph, scaler)) { |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | if (c_DumpFontCache) { |
| 393 | #ifdef SK_DEVELOPER |
| 394 | fContext->getFontCache()->dump(); |
| 395 | #endif |
| 396 | } |
| 397 | |
| 398 | // before we purge the cache, we must flush any accumulated draws |
| 399 | this->flush(); |
| 400 | fContext->flush(); |
| 401 | |
| 402 | // we should have an unused plot now |
| 403 | if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) && |
| 404 | fStrike->addGlyphToAtlas(glyph, scaler)) { |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | // we should never get here |
| 409 | SkASSERT(false); |
| 410 | } |
| 411 | |
| 412 | return false; |
| 413 | } |
| 414 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 415 | void GrBitmapTextContext::appendGlyph(GrGlyph::PackedID packed, |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 416 | int vx, int vy, |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 417 | GrFontScaler* scaler) { |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 418 | if (NULL == fDrawTarget) { |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | if (NULL == fStrike) { |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 423 | fStrike = fContext->getFontCache()->getStrike(scaler); |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 424 | } |
| 425 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 426 | GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
| 427 | if (NULL == glyph || glyph->fBounds.isEmpty()) { |
| 428 | return; |
| 429 | } |
| 430 | |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 431 | int x = vx + glyph->fBounds.fLeft; |
| 432 | int y = vy + glyph->fBounds.fTop; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 433 | |
| 434 | // keep them as ints until we've done the clip-test |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 435 | int width = glyph->fBounds.width(); |
| 436 | int height = glyph->fBounds.height(); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 437 | |
| 438 | // check if we clipped out |
qiankun.miao | 06fb35f | 2015-01-29 18:36:52 -0800 | [diff] [blame] | 439 | if (fClipRect.quickReject(x, y, x + width, y + height)) { |
| 440 | return; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 441 | } |
| 442 | |
joshualitt | c262582 | 2014-12-18 16:40:54 -0800 | [diff] [blame] | 443 | // If the glyph is too large we fall back to paths |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame] | 444 | if (NULL == glyph->fPlot && !uploadGlyph(glyph, scaler)) { |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 445 | if (NULL == glyph->fPath) { |
| 446 | SkPath* path = SkNEW(SkPath); |
| 447 | if (!scaler->getGlyphPath(glyph->glyphID(), path)) { |
| 448 | // flag the glyph as being dead? |
| 449 | delete path; |
| 450 | return; |
| 451 | } |
| 452 | glyph->fPath = path; |
| 453 | } |
| 454 | |
bsalomon | ec87dc6 | 2014-10-14 10:52:00 -0700 | [diff] [blame] | 455 | // flush any accumulated draws before drawing this glyph as a path. |
| 456 | this->flush(); |
| 457 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 458 | SkMatrix translate; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 459 | translate.setTranslate(SkIntToScalar(vx), SkIntToScalar(vy)); |
joshualitt | c262582 | 2014-12-18 16:40:54 -0800 | [diff] [blame] | 460 | SkPath tmpPath(*glyph->fPath); |
| 461 | tmpPath.transform(translate); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 462 | GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 463 | fContext->drawPath(fRenderTarget, fClip, fPaint, SkMatrix::I(), tmpPath, strokeInfo); |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 464 | |
| 465 | // remove this glyph from the vertices we need to allocate |
| 466 | fTotalVertexCount -= kVerticesPerGlyph; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 467 | return; |
| 468 | } |
| 469 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 470 | SkASSERT(glyph->fPlot); |
| 471 | GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); |
| 472 | glyph->fPlot->setDrawToken(drawToken); |
| 473 | |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 474 | // the current texture/maskformat must match what the glyph needs |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 475 | GrTexture* texture = glyph->fPlot->texture(); |
| 476 | SkASSERT(texture); |
| 477 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 478 | if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fAllocVertexCount) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 479 | this->flush(); |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 480 | fCurrTexture = texture; |
| 481 | fCurrTexture->ref(); |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 482 | fCurrMaskFormat = glyph->fMaskFormat; |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 483 | } |
| 484 | |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 485 | if (NULL == fVertices) { |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 486 | int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads(); |
| 487 | fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices); |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 488 | fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, fCurrMaskFormat); |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 489 | } |
| 490 | |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 491 | SkRect r; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 492 | r.fLeft = SkIntToScalar(x); |
| 493 | r.fTop = SkIntToScalar(y); |
| 494 | r.fRight = r.fLeft + SkIntToScalar(width); |
| 495 | r.fBottom = r.fTop + SkIntToScalar(height); |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 496 | |
reed | 10d0327 | 2014-10-01 09:24:12 -0700 | [diff] [blame] | 497 | fVertexBounds.joinNonEmptyArg(r); |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 498 | |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 499 | int u0 = glyph->fAtlasLocation.fX; |
| 500 | int v0 = glyph->fAtlasLocation.fY; |
| 501 | int u1 = u0 + width; |
| 502 | int v1 = v0 + height; |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 503 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 504 | size_t vertSize = get_vertex_stride(fCurrMaskFormat); |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 505 | intptr_t vertex = reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex; |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 506 | |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 507 | // V0 |
| 508 | SkPoint* position = reinterpret_cast<SkPoint*>(vertex); |
| 509 | position->set(r.fLeft, r.fTop); |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 510 | if (kA8_GrMaskFormat == fCurrMaskFormat) { |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 511 | SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 512 | *color = fPaint.getColor(); |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 513 | } |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 514 | SkIPoint16* textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - |
| 515 | sizeof(SkIPoint16)); |
| 516 | textureCoords->set(u0, v0); |
| 517 | vertex += vertSize; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 518 | |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 519 | // V1 |
| 520 | position = reinterpret_cast<SkPoint*>(vertex); |
| 521 | position->set(r.fLeft, r.fBottom); |
| 522 | if (kA8_GrMaskFormat == fCurrMaskFormat) { |
| 523 | SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 524 | *color = fPaint.getColor(); |
| 525 | } |
| 526 | textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - sizeof(SkIPoint16)); |
| 527 | textureCoords->set(u0, v1); |
| 528 | vertex += vertSize; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 529 | |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 530 | // V2 |
| 531 | position = reinterpret_cast<SkPoint*>(vertex); |
| 532 | position->set(r.fRight, r.fBottom); |
| 533 | if (kA8_GrMaskFormat == fCurrMaskFormat) { |
| 534 | SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 535 | *color = fPaint.getColor(); |
| 536 | } |
| 537 | textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - sizeof(SkIPoint16)); |
| 538 | textureCoords->set(u1, v1); |
| 539 | vertex += vertSize; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 540 | |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 541 | // V3 |
| 542 | position = reinterpret_cast<SkPoint*>(vertex); |
| 543 | position->set(r.fRight, r.fTop); |
| 544 | if (kA8_GrMaskFormat == fCurrMaskFormat) { |
| 545 | SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint)); |
| 546 | *color = fPaint.getColor(); |
| 547 | } |
| 548 | textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - sizeof(SkIPoint16)); |
| 549 | textureCoords->set(u1, v0); |
| 550 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 551 | fCurrVertex += 4; |
| 552 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 553 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 554 | void GrBitmapTextContext::flush() { |
| 555 | if (NULL == fDrawTarget) { |
| 556 | return; |
| 557 | } |
| 558 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 559 | if (fCurrVertex > 0) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 560 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 561 | pipelineBuilder.setFromPaint(fPaint, fRenderTarget, fClip); |
joshualitt | 780b11e | 2014-11-18 09:40:40 -0800 | [diff] [blame] | 562 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 563 | // setup our sampler state for our text texture/atlas |
| 564 | SkASSERT(SkIsAlign4(fCurrVertex)); |
| 565 | SkASSERT(fCurrTexture); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 566 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 567 | SkASSERT(fStrike); |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 568 | GrColor color = fPaint.getColor(); |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 569 | switch (fCurrMaskFormat) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 570 | // Color bitmap text |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 571 | case kARGB_GrMaskFormat: { |
| 572 | int a = fSkPaint.getAlpha(); |
| 573 | color = SkColorSetARGB(a, a, a, a); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 574 | break; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 575 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 576 | // LCD text |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 577 | case kA565_GrMaskFormat: { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 578 | // TODO: move supportsRGBCoverage check to setupCoverageEffect and only add LCD |
| 579 | // processor if the xp can support it. For now we will simply assume that if |
| 580 | // fUseLCDText is true, then we have a known color output. |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 581 | const GrXPFactory* xpFactory = pipelineBuilder.getXPFactory(); |
| 582 | if (!xpFactory->supportsRGBCoverage(0, kRGBA_GrColorComponentFlags)) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 583 | SkDebugf("LCD Text will not draw correctly.\n"); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 584 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 585 | break; |
| 586 | } |
| 587 | // Grayscale/BW text |
| 588 | case kA8_GrMaskFormat: |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 589 | break; |
| 590 | default: |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 591 | SkFAIL("Unexpected mask format."); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 592 | } |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 593 | |
jvanverth | be03bb6 | 2015-03-10 11:53:39 -0700 | [diff] [blame] | 594 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 595 | uint32_t textureUniqueID = fCurrTexture->getUniqueID(); |
| 596 | if (textureUniqueID != fEffectTextureUniqueID || |
| 597 | fCachedGeometryProcessor->color() != color || |
| 598 | !fCachedGeometryProcessor->localMatrix().cheapEqualTo(fLocalMatrix)) { |
| 599 | // This will be ignored in the non A8 case |
| 600 | bool opaqueVertexColors = GrColorIsOpaque(fPaint.getColor()); |
| 601 | fCachedGeometryProcessor.reset(GrBitmapTextGeoProc::Create(color, |
| 602 | fCurrTexture, |
| 603 | params, |
| 604 | fCurrMaskFormat, |
| 605 | opaqueVertexColors, |
| 606 | fLocalMatrix)); |
| 607 | fEffectTextureUniqueID = textureUniqueID; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 608 | } |
| 609 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 610 | int nGlyphs = fCurrVertex / kVerticesPerGlyph; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 611 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 612 | fDrawTarget->drawIndexedInstances(&pipelineBuilder, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 613 | fCachedGeometryProcessor.get(), |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 614 | kTriangles_GrPrimitiveType, |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 615 | nGlyphs, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 616 | kVerticesPerGlyph, |
| 617 | kIndicesPerGlyph, |
| 618 | &fVertexBounds); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 619 | |
| 620 | fDrawTarget->resetVertexSource(); |
| 621 | fVertices = NULL; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 622 | fAllocVertexCount = 0; |
| 623 | // reset to be those that are left |
| 624 | fTotalVertexCount -= fCurrVertex; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 625 | fCurrVertex = 0; |
| 626 | fVertexBounds.setLargestInverted(); |
| 627 | SkSafeSetNull(fCurrTexture); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | inline void GrBitmapTextContext::finish() { |
| 632 | this->flush(); |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 633 | fTotalVertexCount = 0; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 634 | |
| 635 | GrTextContext::finish(); |
| 636 | } |
| 637 | |