| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 #include "GrAtlasTextContext.h" | 7 #include "GrAtlasTextContext.h" |
| 8 | 8 |
| 9 #include "GrAtlas.h" | 9 #include "GrAtlas.h" |
| 10 #include "GrBatch.h" | 10 #include "GrBatch.h" |
| 11 #include "GrBatchFontCache.h" | 11 #include "GrBatchFontCache.h" |
| 12 #include "GrBatchTarget.h" | 12 #include "GrBatchTarget.h" |
| 13 #include "GrDefaultGeoProcFactory.h" | 13 #include "GrDefaultGeoProcFactory.h" |
| 14 #include "GrDrawTarget.h" | 14 #include "GrDrawTarget.h" |
| 15 #include "GrFontScaler.h" | 15 #include "GrFontScaler.h" |
| 16 #include "GrIndexBuffer.h" | 16 #include "GrIndexBuffer.h" |
| 17 #include "GrStrokeInfo.h" | 17 #include "GrStrokeInfo.h" |
| 18 #include "GrTextBlobCache.h" |
| 18 #include "GrTexturePriv.h" | 19 #include "GrTexturePriv.h" |
| 19 | 20 |
| 20 #include "SkAutoKern.h" | 21 #include "SkAutoKern.h" |
| 21 #include "SkColorPriv.h" | 22 #include "SkColorPriv.h" |
| 22 #include "SkDraw.h" | 23 #include "SkDraw.h" |
| 23 #include "SkDrawFilter.h" | 24 #include "SkDrawFilter.h" |
| 24 #include "SkDrawProcs.h" | 25 #include "SkDrawProcs.h" |
| 25 #include "SkGlyphCache.h" | 26 #include "SkGlyphCache.h" |
| 26 #include "SkGpuDevice.h" | 27 #include "SkGpuDevice.h" |
| 27 #include "SkGr.h" | 28 #include "SkGr.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 61 |
| 61 // TODO | 62 // TODO |
| 62 // More tests | 63 // More tests |
| 63 // move to SkCache | 64 // move to SkCache |
| 64 // handle textblobs where the whole run is larger than the cache size | 65 // handle textblobs where the whole run is larger than the cache size |
| 65 // TODO implement micro speedy hash map for fast refing of glyphs | 66 // TODO implement micro speedy hash map for fast refing of glyphs |
| 66 | 67 |
| 67 GrAtlasTextContext::GrAtlasTextContext(GrContext* context, | 68 GrAtlasTextContext::GrAtlasTextContext(GrContext* context, |
| 68 SkGpuDevice* gpuDevice, | 69 SkGpuDevice* gpuDevice, |
| 69 const SkDeviceProperties& properties) | 70 const SkDeviceProperties& properties) |
| 70 : INHERITED(context, gpuDevice, propertie
s) { | 71 : INHERITED(context, gpuDevice, properties) { |
| 72 // We overallocate vertices in our textblobs based on the assumption that A8
has the greatest |
| 73 // vertexStride |
| 74 SK_COMPILE_ASSERT(kGrayTextVASize >= kColorTextVASize && kGrayTextVASize >=
kLCDTextVASize, |
| 75 vertex_attribute_changed); |
| 71 fCurrStrike = NULL; | 76 fCurrStrike = NULL; |
| 72 } | 77 fCache = context->getTextBlobCache(); |
| 73 | |
| 74 void GrAtlasTextContext::ClearCacheEntry(uint32_t key, BitmapTextBlob** blob) { | |
| 75 (*blob)->unref(); | |
| 76 } | |
| 77 | |
| 78 GrAtlasTextContext::~GrAtlasTextContext() { | |
| 79 fCache.foreach(&GrAtlasTextContext::ClearCacheEntry); | |
| 80 } | 78 } |
| 81 | 79 |
| 82 GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context, | 80 GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context, |
| 83 SkGpuDevice* gpuDevice, | 81 SkGpuDevice* gpuDevice, |
| 84 const SkDeviceProperties& props)
{ | 82 const SkDeviceProperties& props)
{ |
| 85 return SkNEW_ARGS(GrAtlasTextContext, (context, gpuDevice, props)); | 83 return SkNEW_ARGS(GrAtlasTextContext, (context, gpuDevice, props)); |
| 86 } | 84 } |
| 87 | 85 |
| 88 bool GrAtlasTextContext::canDraw(const GrRenderTarget*, | 86 bool GrAtlasTextContext::canDraw(const GrRenderTarget*, |
| 89 const GrClip&, | 87 const GrClip&, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 103 | 101 |
| 104 | 102 |
| 105 inline SkGlyphCache* GrAtlasTextContext::setupCache(BitmapTextBlob::Run* run, | 103 inline SkGlyphCache* GrAtlasTextContext::setupCache(BitmapTextBlob::Run* run, |
| 106 const SkPaint& skPaint, | 104 const SkPaint& skPaint, |
| 107 const SkMatrix& viewMatrix)
{ | 105 const SkMatrix& viewMatrix)
{ |
| 108 skPaint.getScalerContextDescriptor(&run->fDescriptor, &fDeviceProperties, &v
iewMatrix, false); | 106 skPaint.getScalerContextDescriptor(&run->fDescriptor, &fDeviceProperties, &v
iewMatrix, false); |
| 109 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); | 107 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); |
| 110 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc())
; | 108 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc())
; |
| 111 } | 109 } |
| 112 | 110 |
| 113 inline void GrAtlasTextContext::BlobGlyphCount(int* glyphCount, int* runCount, | |
| 114 const SkTextBlob* blob) { | |
| 115 SkTextBlob::RunIterator itCounter(blob); | |
| 116 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { | |
| 117 *glyphCount += itCounter.glyphCount(); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 GrAtlasTextContext::BitmapTextBlob* GrAtlasTextContext::CreateBlob(int glyphCoun
t, | |
| 122 int runCount)
{ | |
| 123 // We allocate size for the BitmapTextBlob itself, plus size for the vertice
s array, | |
| 124 // and size for the glyphIds array. | |
| 125 SK_COMPILE_ASSERT(kGrayTextVASize >= kColorTextVASize && kGrayTextVASize >=
kLCDTextVASize, | |
| 126 vertex_attribute_changed); | |
| 127 size_t verticesCount = glyphCount * kVerticesPerGlyph * kGrayTextVASize; | |
| 128 size_t length = sizeof(BitmapTextBlob) + | |
| 129 verticesCount + | |
| 130 glyphCount * sizeof(GrGlyph::PackedID) + | |
| 131 sizeof(BitmapTextBlob::Run) * runCount; | |
| 132 | |
| 133 BitmapTextBlob* cacheBlob = SkNEW_PLACEMENT(sk_malloc_throw(length), BitmapT
extBlob); | |
| 134 | |
| 135 // setup offsets for vertices / glyphs | |
| 136 cacheBlob->fVertices = sizeof(BitmapTextBlob) + reinterpret_cast<unsigned ch
ar*>(cacheBlob); | |
| 137 cacheBlob->fGlyphIDs = | |
| 138 reinterpret_cast<GrGlyph::PackedID*>(cacheBlob->fVertices + vertices
Count); | |
| 139 cacheBlob->fRuns = reinterpret_cast<BitmapTextBlob::Run*>(cacheBlob->fGlyphI
Ds + glyphCount); | |
| 140 | |
| 141 // Initialize runs | |
| 142 for (int i = 0; i < runCount; i++) { | |
| 143 SkNEW_PLACEMENT(&cacheBlob->fRuns[i], BitmapTextBlob::Run); | |
| 144 } | |
| 145 cacheBlob->fRunCount = runCount; | |
| 146 return cacheBlob; | |
| 147 } | |
| 148 | |
| 149 void GrAtlasTextContext::drawTextBlob(GrRenderTarget* rt, const GrClip& clip, | 111 void GrAtlasTextContext::drawTextBlob(GrRenderTarget* rt, const GrClip& clip, |
| 150 const SkPaint& skPaint, const SkMatrix& vi
ewMatrix, | 112 const SkPaint& skPaint, const SkMatrix& vi
ewMatrix, |
| 151 const SkTextBlob* blob, SkScalar x, SkScal
ar y, | 113 const SkTextBlob* blob, SkScalar x, SkScal
ar y, |
| 152 SkDrawFilter* drawFilter, const SkIRect& c
lipBounds) { | 114 SkDrawFilter* drawFilter, const SkIRect& c
lipBounds) { |
| 153 BitmapTextBlob* cacheBlob; | 115 uint32_t uniqueID = blob->uniqueID(); |
| 154 BitmapTextBlob** foundBlob = fCache.find(blob->uniqueID()); | 116 BitmapTextBlob* cacheBlob = fCache->find(uniqueID); |
| 155 SkIRect clipRect; | 117 SkIRect clipRect; |
| 156 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect); | 118 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect); |
| 157 | 119 |
| 158 if (foundBlob) { | 120 if (cacheBlob) { |
| 159 cacheBlob = *foundBlob; | |
| 160 if (MustRegenerateBlob(*cacheBlob, skPaint, viewMatrix, x, y)) { | 121 if (MustRegenerateBlob(*cacheBlob, skPaint, viewMatrix, x, y)) { |
| 161 // We have to remake the blob because changes may invalidate our mas
ks. | 122 // We have to remake the blob because changes may invalidate our mas
ks. |
| 162 // TODO we could probably get away reuse most of the time if the poi
nter is unique, | 123 // TODO we could probably get away reuse most of the time if the poi
nter is unique, |
| 163 // but we'd have to clear the subrun information | 124 // but we'd have to clear the subrun information |
| 164 cacheBlob->unref(); | 125 fCache->remove(cacheBlob); |
| 165 int glyphCount = 0; | 126 cacheBlob = fCache->createCachedBlob(blob, kGrayTextVASize); |
| 166 int runCount = 0; | |
| 167 BlobGlyphCount(&glyphCount, &runCount, blob); | |
| 168 cacheBlob = CreateBlob(glyphCount, runCount); | |
| 169 fCache.set(blob->uniqueID(), cacheBlob); | |
| 170 this->regenerateTextBlob(cacheBlob, skPaint, viewMatrix, blob, x, y,
drawFilter, | 127 this->regenerateTextBlob(cacheBlob, skPaint, viewMatrix, blob, x, y,
drawFilter, |
| 171 clipRect); | 128 clipRect); |
| 129 } else { |
| 130 fCache->makeMRU(cacheBlob); |
| 172 } | 131 } |
| 173 } else { | 132 } else { |
| 174 int glyphCount = 0; | 133 cacheBlob = fCache->createCachedBlob(blob, kGrayTextVASize); |
| 175 int runCount = 0; | |
| 176 BlobGlyphCount(&glyphCount, &runCount, blob); | |
| 177 cacheBlob = CreateBlob(glyphCount, runCount); | |
| 178 fCache.set(blob->uniqueID(), cacheBlob); | |
| 179 this->regenerateTextBlob(cacheBlob, skPaint, viewMatrix, blob, x, y, dra
wFilter, clipRect); | 134 this->regenerateTextBlob(cacheBlob, skPaint, viewMatrix, blob, x, y, dra
wFilter, clipRect); |
| 180 } | 135 } |
| 181 | 136 |
| 182 // Though for the time being runs in the textblob can override the paint, th
ey only touch font | 137 // Though for the time being runs in the textblob can override the paint, th
ey only touch font |
| 183 // info. | 138 // info. |
| 184 GrPaint grPaint; | 139 GrPaint grPaint; |
| 185 SkPaint2GrPaintShader(fContext, rt, skPaint, viewMatrix, true, &grPaint); | 140 SkPaint2GrPaintShader(fContext, rt, skPaint, viewMatrix, true, &grPaint); |
| 186 | 141 |
| 187 this->flush(fContext->getTextTarget(), blob, cacheBlob, rt, skPaint, grPaint
, drawFilter, | 142 this->flush(fContext->getTextTarget(), blob, cacheBlob, rt, skPaint, grPaint
, drawFilter, |
| 188 clip, viewMatrix, clipBounds, x, y); | 143 clip, viewMatrix, clipBounds, x, y); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 SkGlyphCache::AttachCache(cache); | 217 SkGlyphCache::AttachCache(cache); |
| 263 } | 218 } |
| 264 } | 219 } |
| 265 | 220 |
| 266 void GrAtlasTextContext::onDrawText(GrRenderTarget* rt, const GrClip& clip, | 221 void GrAtlasTextContext::onDrawText(GrRenderTarget* rt, const GrClip& clip, |
| 267 const GrPaint& paint, const SkPaint& skPaint
, | 222 const GrPaint& paint, const SkPaint& skPaint
, |
| 268 const SkMatrix& viewMatrix, | 223 const SkMatrix& viewMatrix, |
| 269 const char text[], size_t byteLength, | 224 const char text[], size_t byteLength, |
| 270 SkScalar x, SkScalar y, const SkIRect& regio
nClipBounds) { | 225 SkScalar x, SkScalar y, const SkIRect& regio
nClipBounds) { |
| 271 int glyphCount = skPaint.countText(text, byteLength); | 226 int glyphCount = skPaint.countText(text, byteLength); |
| 272 SkAutoTUnref<BitmapTextBlob> blob(CreateBlob(glyphCount, 1)); | 227 SkAutoTUnref<BitmapTextBlob> blob(fCache->createBlob(glyphCount, 1, kGrayTex
tVASize)); |
| 273 blob->fViewMatrix = viewMatrix; | 228 blob->fViewMatrix = viewMatrix; |
| 274 blob->fX = x; | 229 blob->fX = x; |
| 275 blob->fY = y; | 230 blob->fY = y; |
| 276 blob->fStyle = skPaint.getStyle(); | 231 blob->fStyle = skPaint.getStyle(); |
| 277 | 232 |
| 278 SkIRect clipRect; | 233 SkIRect clipRect; |
| 279 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect); | 234 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect); |
| 280 | 235 |
| 281 // setup cache | 236 // setup cache |
| 282 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, viewMatrix)
; | 237 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, viewMatrix)
; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 } | 331 } |
| 377 } | 332 } |
| 378 | 333 |
| 379 void GrAtlasTextContext::onDrawPosText(GrRenderTarget* rt, const GrClip& clip, | 334 void GrAtlasTextContext::onDrawPosText(GrRenderTarget* rt, const GrClip& clip, |
| 380 const GrPaint& paint, const SkPaint& skPa
int, | 335 const GrPaint& paint, const SkPaint& skPa
int, |
| 381 const SkMatrix& viewMatrix, | 336 const SkMatrix& viewMatrix, |
| 382 const char text[], size_t byteLength, | 337 const char text[], size_t byteLength, |
| 383 const SkScalar pos[], int scalarsPerPosit
ion, | 338 const SkScalar pos[], int scalarsPerPosit
ion, |
| 384 const SkPoint& offset, const SkIRect& reg
ionClipBounds) { | 339 const SkPoint& offset, const SkIRect& reg
ionClipBounds) { |
| 385 int glyphCount = skPaint.countText(text, byteLength); | 340 int glyphCount = skPaint.countText(text, byteLength); |
| 386 SkAutoTUnref<BitmapTextBlob> blob(CreateBlob(glyphCount, 1)); | 341 SkAutoTUnref<BitmapTextBlob> blob(fCache->createBlob(glyphCount, 1, kGrayTex
tVASize)); |
| 387 blob->fStyle = skPaint.getStyle(); | 342 blob->fStyle = skPaint.getStyle(); |
| 388 blob->fViewMatrix = viewMatrix; | 343 blob->fViewMatrix = viewMatrix; |
| 389 | 344 |
| 390 SkIRect clipRect; | 345 SkIRect clipRect; |
| 391 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect); | 346 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect); |
| 392 | 347 |
| 393 // setup cache | 348 // setup cache |
| 394 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, viewMatrix)
; | 349 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, viewMatrix)
; |
| 395 this->internalDrawPosText(blob, 0, cache, skPaint, viewMatrix, text, byteLen
gth, pos, | 350 this->internalDrawPosText(blob, 0, cache, skPaint, viewMatrix, text, byteLen
gth, pos, |
| 396 scalarsPerPosition, offset, clipRect); | 351 scalarsPerPosition, offset, clipRect); |
| (...skipping 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1138 | 1093 |
| 1139 GrColor color = grPaint.getColor(); | 1094 GrColor color = grPaint.getColor(); |
| 1140 uint8_t paintAlpha = skPaint.getAlpha(); | 1095 uint8_t paintAlpha = skPaint.getAlpha(); |
| 1141 for (int run = 0; run < cacheBlob->fRunCount; run++) { | 1096 for (int run = 0; run < cacheBlob->fRunCount; run++) { |
| 1142 this->flushRun(target, &pipelineBuilder, cacheBlob, run, color, paintAlp
ha); | 1097 this->flushRun(target, &pipelineBuilder, cacheBlob, run, color, paintAlp
ha); |
| 1143 } | 1098 } |
| 1144 | 1099 |
| 1145 // Now flush big glyphs | 1100 // Now flush big glyphs |
| 1146 this->flushBigGlyphs(cacheBlob, rt, grPaint, clip); | 1101 this->flushBigGlyphs(cacheBlob, rt, grPaint, clip); |
| 1147 } | 1102 } |
| OLD | NEW |