Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "SkInternalAtlasTextContext.h" |
| 9 | #include "GrContext.h" |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 10 | #include "GrContextPriv.h" |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 11 | #include "SkAtlasTextContext.h" |
| 12 | #include "SkAtlasTextRenderer.h" |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 13 | #include "text/GrGlyphCache.h" |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 14 | |
| 15 | SkAtlasTextRenderer* SkGetAtlasTextRendererFromInternalContext( |
| 16 | class SkInternalAtlasTextContext& internal) { |
| 17 | return internal.renderer(); |
| 18 | } |
| 19 | |
| 20 | ////////////////////////////////////////////////////////////////////////////// |
| 21 | |
| 22 | std::unique_ptr<SkInternalAtlasTextContext> SkInternalAtlasTextContext::Make( |
| 23 | sk_sp<SkAtlasTextRenderer> renderer) { |
| 24 | return std::unique_ptr<SkInternalAtlasTextContext>( |
| 25 | new SkInternalAtlasTextContext(std::move(renderer))); |
| 26 | } |
| 27 | |
| 28 | SkInternalAtlasTextContext::SkInternalAtlasTextContext(sk_sp<SkAtlasTextRenderer> renderer) |
| 29 | : fRenderer(std::move(renderer)) { |
| 30 | GrContextOptions options; |
| 31 | options.fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo; |
| 32 | options.fMinDistanceFieldFontSize = 0.f; |
| 33 | options.fGlyphsAsPathsFontSize = SK_ScalarInfinity; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 34 | options.fDistanceFieldGlyphVerticesAlwaysHaveW = GrContextOptions::Enable::kYes; |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 35 | fGrContext = GrContext::MakeMock(nullptr, options); |
| 36 | } |
| 37 | |
| 38 | SkInternalAtlasTextContext::~SkInternalAtlasTextContext() { |
| 39 | if (fDistanceFieldAtlas.fProxy) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 40 | #ifdef SK_DEBUG |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 41 | auto atlasManager = fGrContext->contextPriv().getAtlasManager(); |
| 42 | if (atlasManager) { |
| 43 | unsigned int numProxies; |
| 44 | atlasManager->getProxies(kA8_GrMaskFormat, &numProxies); |
| 45 | SkASSERT(1 == numProxies); |
| 46 | } |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 47 | #endif |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 48 | fRenderer->deleteTexture(fDistanceFieldAtlas.fTextureHandle); |
| 49 | } |
| 50 | } |
| 51 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 52 | GrGlyphCache* SkInternalAtlasTextContext::glyphCache() { |
| 53 | return fGrContext->contextPriv().getGlyphCache(); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | GrTextBlobCache* SkInternalAtlasTextContext::textBlobCache() { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 57 | return fGrContext->contextPriv().getTextBlobCache(); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | GrDeferredUploadToken SkInternalAtlasTextContext::addInlineUpload( |
| 61 | GrDeferredTextureUploadFn&& upload) { |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 62 | auto token = fTokenTracker.nextDrawToken(); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 63 | fInlineUploads.append(&fArena, InlineUpload{std::move(upload), token}); |
| 64 | return token; |
| 65 | } |
| 66 | |
| 67 | GrDeferredUploadToken SkInternalAtlasTextContext::addASAPUpload( |
| 68 | GrDeferredTextureUploadFn&& upload) { |
| 69 | fASAPUploads.append(&fArena, std::move(upload)); |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 70 | return fTokenTracker.nextTokenToFlush(); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void SkInternalAtlasTextContext::recordDraw(const void* srcVertexData, int glyphCnt, |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 74 | const SkMatrix& matrix, void* targetHandle) { |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 75 | auto vertexDataSize = sizeof(SkAtlasTextRenderer::SDFVertex) * 4 * glyphCnt; |
| 76 | auto vertexData = fArena.makeArrayDefault<char>(vertexDataSize); |
| 77 | memcpy(vertexData, srcVertexData, vertexDataSize); |
| 78 | for (int i = 0; i < 4 * glyphCnt; ++i) { |
| 79 | auto* vertex = reinterpret_cast<SkAtlasTextRenderer::SDFVertex*>(vertexData) + i; |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 80 | // GrTextContext encodes a texture index into the lower bit of each texture coord. |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 81 | // This isn't expected by SkAtlasTextRenderer subclasses. |
Mike Reed | 1fda024 | 2018-04-04 15:39:46 -0400 | [diff] [blame] | 82 | vertex->fTextureCoordX /= 2; |
| 83 | vertex->fTextureCoordY /= 2; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 84 | matrix.mapHomogeneousPoints(&vertex->fPosition, &vertex->fPosition, 1); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 85 | } |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 86 | fDraws.append(&fArena, |
| 87 | Draw{glyphCnt, fTokenTracker.issueDrawToken(), targetHandle, vertexData}); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void SkInternalAtlasTextContext::flush() { |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 91 | auto* atlasManager = fGrContext->contextPriv().getAtlasManager(); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 92 | if (!fDistanceFieldAtlas.fProxy) { |
Robert Phillips | f3690dd | 2018-02-20 15:18:59 -0500 | [diff] [blame] | 93 | unsigned int numProxies; |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 94 | fDistanceFieldAtlas.fProxy = atlasManager->getProxies(kA8_GrMaskFormat, &numProxies)->get(); |
Robert Phillips | f3690dd | 2018-02-20 15:18:59 -0500 | [diff] [blame] | 95 | SkASSERT(1 == numProxies); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 96 | fDistanceFieldAtlas.fTextureHandle = |
| 97 | fRenderer->createTexture(SkAtlasTextRenderer::AtlasFormat::kA8, |
| 98 | fDistanceFieldAtlas.fProxy->width(), |
| 99 | fDistanceFieldAtlas.fProxy->height()); |
| 100 | } |
| 101 | GrDeferredTextureUploadWritePixelsFn writePixelsFn = |
| 102 | [this](GrTextureProxy* proxy, int left, int top, int width, int height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 103 | GrColorType colorType, const void* data, size_t rowBytes) -> bool { |
| 104 | SkASSERT(GrColorType::kAlpha_8 == colorType); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 105 | SkASSERT(proxy == this->fDistanceFieldAtlas.fProxy); |
| 106 | void* handle = fDistanceFieldAtlas.fTextureHandle; |
| 107 | this->fRenderer->setTextureData(handle, data, left, top, width, height, rowBytes); |
| 108 | return true; |
| 109 | }; |
| 110 | for (const auto& upload : fASAPUploads) { |
| 111 | upload(writePixelsFn); |
| 112 | } |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 113 | auto inlineUpload = fInlineUploads.begin(); |
Brian Salomon | cf1aa53 | 2017-11-29 14:58:59 -0500 | [diff] [blame] | 114 | for (const auto& draw : fDraws) { |
| 115 | while (inlineUpload != fInlineUploads.end() && inlineUpload->fToken == draw.fToken) { |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 116 | inlineUpload->fUpload(writePixelsFn); |
| 117 | ++inlineUpload; |
| 118 | } |
Brian Salomon | cf1aa53 | 2017-11-29 14:58:59 -0500 | [diff] [blame] | 119 | auto vertices = reinterpret_cast<const SkAtlasTextRenderer::SDFVertex*>(draw.fVertexData); |
| 120 | fRenderer->drawSDFGlyphs(draw.fTargetHandle, fDistanceFieldAtlas.fTextureHandle, vertices, |
| 121 | draw.fGlyphCnt); |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 122 | fTokenTracker.flushToken(); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 123 | } |
| 124 | fASAPUploads.reset(); |
| 125 | fInlineUploads.reset(); |
| 126 | fDraws.reset(); |
| 127 | fArena.reset(); |
| 128 | } |