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 "SkAtlasTextTarget.h" |
| 9 | #include "GrClip.h" |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 10 | #include "GrContextPriv.h" |
| 11 | #include "GrDrawingManager.h" |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 12 | #include "SkAtlasTextContext.h" |
| 13 | #include "SkAtlasTextFont.h" |
| 14 | #include "SkAtlasTextRenderer.h" |
| 15 | #include "SkGr.h" |
| 16 | #include "SkInternalAtlasTextContext.h" |
| 17 | #include "ops/GrAtlasTextOp.h" |
| 18 | #include "text/GrAtlasTextContext.h" |
| 19 | |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 20 | static constexpr int kMaxBatchLookBack = 10; |
| 21 | |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 22 | SkAtlasTextTarget::SkAtlasTextTarget(sk_sp<SkAtlasTextContext> context, int width, int height, |
| 23 | void* handle) |
| 24 | : fHandle(handle), fContext(std::move(context)), fWidth(width), fHeight(height) {} |
| 25 | |
| 26 | SkAtlasTextTarget::~SkAtlasTextTarget() { fContext->renderer()->targetDeleted(fHandle); } |
| 27 | |
| 28 | ////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | static const GrColorSpaceInfo kColorSpaceInfo(nullptr, kRGBA_8888_GrPixelConfig); |
| 31 | |
| 32 | ////////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | class SkInternalAtlasTextTarget : public GrTextUtils::Target, public SkAtlasTextTarget { |
| 35 | public: |
| 36 | SkInternalAtlasTextTarget(sk_sp<SkAtlasTextContext> context, int width, int height, |
| 37 | void* handle) |
| 38 | : GrTextUtils::Target(width, height, kColorSpaceInfo) |
| 39 | , SkAtlasTextTarget(std::move(context), width, height, handle) {} |
| 40 | |
| 41 | /** GrTextUtils::Target overrides */ |
| 42 | |
| 43 | void addDrawOp(const GrClip&, std::unique_ptr<GrAtlasTextOp> op) override; |
| 44 | |
| 45 | void drawPath(const GrClip&, const SkPath&, const SkPaint&, const SkMatrix& viewMatrix, |
| 46 | const SkMatrix* pathMatrix, const SkIRect& clipBounds) override { |
| 47 | SkDebugf("Path glyph??"); |
| 48 | } |
| 49 | |
| 50 | void makeGrPaint(GrMaskFormat, const SkPaint& skPaint, const SkMatrix&, |
| 51 | GrPaint* grPaint) override { |
| 52 | grPaint->setColor4f(SkColorToPremulGrColor4fLegacy(skPaint.getColor())); |
| 53 | } |
| 54 | |
| 55 | /** SkAtlasTextTarget overrides */ |
| 56 | |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 57 | void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color, |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 58 | const SkAtlasTextFont&) override; |
| 59 | void flush() override; |
| 60 | |
| 61 | private: |
| 62 | uint32_t fColor; |
| 63 | using SkAtlasTextTarget::fWidth; |
| 64 | using SkAtlasTextTarget::fHeight; |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 65 | SkTArray<std::unique_ptr<GrAtlasTextOp>, true> fOps; |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | ////////////////////////////////////////////////////////////////////////////// |
| 69 | |
| 70 | std::unique_ptr<SkAtlasTextTarget> SkAtlasTextTarget::Make(sk_sp<SkAtlasTextContext> context, |
| 71 | int width, int height, void* handle) { |
| 72 | return std::unique_ptr<SkAtlasTextTarget>( |
| 73 | new SkInternalAtlasTextTarget(std::move(context), width, height, handle)); |
| 74 | } |
| 75 | |
| 76 | ////////////////////////////////////////////////////////////////////////////// |
| 77 | |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 78 | void SkInternalAtlasTextTarget::drawText(const SkGlyphID glyphs[], const SkPoint positions[], |
| 79 | int glyphCnt, uint32_t color, |
| 80 | const SkAtlasTextFont& font) { |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 81 | SkPaint paint; |
| 82 | paint.setAntiAlias(true); |
| 83 | paint.setTypeface(font.refTypeface()); |
| 84 | paint.setTextSize(font.size()); |
| 85 | paint.setStyle(SkPaint::kFill_Style); |
Brian Salomon | 0c1c2b3 | 2017-11-20 13:13:01 -0500 | [diff] [blame] | 86 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 87 | |
| 88 | // The atlas text context does munging of the paint color. We store the client's color here |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 89 | // and then overwrite the generated op's color when addDrawOp() is called. |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 90 | fColor = color; |
| 91 | |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 92 | SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, kUnknown_SkPixelGeometry); |
| 93 | auto* grContext = this->context()->internal().grContext(); |
| 94 | auto bounds = SkIRect::MakeWH(fWidth, fHeight); |
| 95 | auto atlasTextContext = grContext->contextPriv().drawingManager()->getAtlasTextContext(); |
Brian Salomon | 0c1c2b3 | 2017-11-20 13:13:01 -0500 | [diff] [blame] | 96 | size_t byteLength = sizeof(SkGlyphID) * glyphCnt; |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 97 | const SkScalar* pos = &positions->fX; |
| 98 | atlasTextContext->drawPosText(grContext, this, GrNoClip(), paint, SkMatrix::I(), props, |
| 99 | (const char*)glyphs, byteLength, pos, 2, {0, 0}, bounds); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void SkInternalAtlasTextTarget::addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) { |
| 103 | SkASSERT(clip.quickContains(SkRect::MakeIWH(fWidth, fHeight))); |
| 104 | // The SkAtlasTextRenderer currently only handles grayscale SDF glyphs. |
| 105 | if (op->maskType() != GrAtlasTextOp::kGrayscaleDistanceField_MaskType) { |
| 106 | return; |
| 107 | } |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 108 | const GrCaps& caps = *this->context()->internal().grContext()->caps(); |
| 109 | op->finalizeForTextTarget(fColor, caps); |
| 110 | int n = SkTMin(kMaxBatchLookBack, fOps.count()); |
| 111 | for (int i = 0; i < n; ++i) { |
| 112 | GrAtlasTextOp* other = fOps.fromBack(i).get(); |
| 113 | if (other->combineIfPossible(op.get(), caps)) { |
| 114 | return; |
| 115 | } |
| 116 | if (GrRectsOverlap(op->bounds(), other->bounds())) { |
| 117 | break; |
| 118 | } |
| 119 | } |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 120 | op->visitProxies([](GrSurfaceProxy*) {}); |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 121 | fOps.emplace_back(std::move(op)); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void SkInternalAtlasTextTarget::flush() { |
| 125 | for (int i = 0; i < fOps.count(); ++i) { |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 126 | fOps[i]->executeForTextTarget(this); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 127 | } |
| 128 | this->context()->internal().flush(); |
| 129 | fOps.reset(); |
| 130 | } |
| 131 | |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 132 | void GrAtlasTextOp::finalizeForTextTarget(uint32_t color, const GrCaps& caps) { |
| 133 | for (int i = 0; i < fGeoCount; ++i) { |
| 134 | fGeoData[i].fColor = color; |
| 135 | } |
| 136 | this->finalize(caps, nullptr /* applied clip */, GrPixelConfigIsClamped::kNo); |
| 137 | } |
| 138 | |
| 139 | void GrAtlasTextOp::executeForTextTarget(SkAtlasTextTarget* target) { |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 140 | FlushInfo flushInfo; |
| 141 | SkAutoGlyphCache glyphCache; |
| 142 | auto& context = target->context()->internal(); |
| 143 | auto* atlasGlyphCache = context.grContext()->getAtlasGlyphCache(); |
| 144 | for (int i = 0; i < fGeoCount; ++i) { |
| 145 | GrAtlasTextBlob::VertexRegenerator regenerator( |
| 146 | fGeoData[i].fBlob, fGeoData[i].fRun, fGeoData[i].fSubRun, fGeoData[i].fViewMatrix, |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 147 | fGeoData[i].fX, fGeoData[i].fY, fGeoData[i].fColor, &context, atlasGlyphCache, |
| 148 | &glyphCache); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 149 | GrAtlasTextBlob::VertexRegenerator::Result result; |
| 150 | do { |
| 151 | result = regenerator.regenerate(); |
| 152 | context.recordDraw(result.fFirstVertex, result.fGlyphsRegenerated, target->handle()); |
| 153 | if (!result.fFinished) { |
| 154 | // Make space in the atlas so we can continue generating vertices. |
| 155 | context.flush(); |
| 156 | } |
| 157 | } while (!result.fFinished); |
| 158 | } |
| 159 | } |