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) |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 24 | : fHandle(handle) |
| 25 | , fContext(std::move(context)) |
| 26 | , fWidth(width) |
| 27 | , fHeight(height) |
| 28 | , fMatrixStack(sizeof(SkMatrix), 4) |
| 29 | , fSaveCnt(0) { |
| 30 | fMatrixStack.push_back(); |
| 31 | this->accessCTM()->reset(); |
| 32 | } |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 33 | |
| 34 | SkAtlasTextTarget::~SkAtlasTextTarget() { fContext->renderer()->targetDeleted(fHandle); } |
| 35 | |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 36 | int SkAtlasTextTarget::save() { |
| 37 | const auto& currCTM = this->ctm(); |
| 38 | *static_cast<SkMatrix*>(fMatrixStack.push_back()) = currCTM; |
| 39 | return fSaveCnt++; |
| 40 | } |
| 41 | |
| 42 | void SkAtlasTextTarget::restore() { |
| 43 | if (fSaveCnt) { |
| 44 | fMatrixStack.pop_back(); |
| 45 | fSaveCnt--; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void SkAtlasTextTarget::restoreToCount(int count) { |
| 50 | while (fSaveCnt > count) { |
| 51 | this->restore(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void SkAtlasTextTarget::translate(SkScalar dx, SkScalar dy) { |
| 56 | this->accessCTM()->preTranslate(dx, dy); |
| 57 | } |
| 58 | |
| 59 | void SkAtlasTextTarget::scale(SkScalar sx, SkScalar sy) { this->accessCTM()->preScale(sx, sy); } |
| 60 | |
| 61 | void SkAtlasTextTarget::rotate(SkScalar degrees) { this->accessCTM()->preRotate(degrees); } |
| 62 | |
| 63 | void SkAtlasTextTarget::rotate(SkScalar degrees, SkScalar px, SkScalar py) { |
| 64 | this->accessCTM()->preRotate(degrees, px, py); |
| 65 | } |
| 66 | |
| 67 | void SkAtlasTextTarget::skew(SkScalar sx, SkScalar sy) { this->accessCTM()->preSkew(sx, sy); } |
| 68 | |
| 69 | void SkAtlasTextTarget::concat(const SkMatrix& matrix) { this->accessCTM()->preConcat(matrix); } |
| 70 | |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 71 | ////////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | static const GrColorSpaceInfo kColorSpaceInfo(nullptr, kRGBA_8888_GrPixelConfig); |
| 74 | |
| 75 | ////////////////////////////////////////////////////////////////////////////// |
| 76 | |
| 77 | class SkInternalAtlasTextTarget : public GrTextUtils::Target, public SkAtlasTextTarget { |
| 78 | public: |
| 79 | SkInternalAtlasTextTarget(sk_sp<SkAtlasTextContext> context, int width, int height, |
| 80 | void* handle) |
| 81 | : GrTextUtils::Target(width, height, kColorSpaceInfo) |
| 82 | , SkAtlasTextTarget(std::move(context), width, height, handle) {} |
| 83 | |
| 84 | /** GrTextUtils::Target overrides */ |
| 85 | |
| 86 | void addDrawOp(const GrClip&, std::unique_ptr<GrAtlasTextOp> op) override; |
| 87 | |
| 88 | void drawPath(const GrClip&, const SkPath&, const SkPaint&, const SkMatrix& viewMatrix, |
| 89 | const SkMatrix* pathMatrix, const SkIRect& clipBounds) override { |
| 90 | SkDebugf("Path glyph??"); |
| 91 | } |
| 92 | |
| 93 | void makeGrPaint(GrMaskFormat, const SkPaint& skPaint, const SkMatrix&, |
| 94 | GrPaint* grPaint) override { |
| 95 | grPaint->setColor4f(SkColorToPremulGrColor4fLegacy(skPaint.getColor())); |
| 96 | } |
| 97 | |
| 98 | /** SkAtlasTextTarget overrides */ |
| 99 | |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 100 | void drawText(const SkGlyphID[], const SkPoint[], int glyphCnt, uint32_t color, |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 101 | const SkAtlasTextFont&) override; |
| 102 | void flush() override; |
| 103 | |
| 104 | private: |
| 105 | uint32_t fColor; |
| 106 | using SkAtlasTextTarget::fWidth; |
| 107 | using SkAtlasTextTarget::fHeight; |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 108 | SkTArray<std::unique_ptr<GrAtlasTextOp>, true> fOps; |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | ////////////////////////////////////////////////////////////////////////////// |
| 112 | |
| 113 | std::unique_ptr<SkAtlasTextTarget> SkAtlasTextTarget::Make(sk_sp<SkAtlasTextContext> context, |
| 114 | int width, int height, void* handle) { |
| 115 | return std::unique_ptr<SkAtlasTextTarget>( |
| 116 | new SkInternalAtlasTextTarget(std::move(context), width, height, handle)); |
| 117 | } |
| 118 | |
| 119 | ////////////////////////////////////////////////////////////////////////////// |
| 120 | |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 121 | void SkInternalAtlasTextTarget::drawText(const SkGlyphID glyphs[], const SkPoint positions[], |
| 122 | int glyphCnt, uint32_t color, |
| 123 | const SkAtlasTextFont& font) { |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 124 | SkPaint paint; |
| 125 | paint.setAntiAlias(true); |
| 126 | paint.setTypeface(font.refTypeface()); |
| 127 | paint.setTextSize(font.size()); |
| 128 | paint.setStyle(SkPaint::kFill_Style); |
Brian Salomon | 0c1c2b3 | 2017-11-20 13:13:01 -0500 | [diff] [blame] | 129 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 130 | |
| 131 | // 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] | 132 | // and then overwrite the generated op's color when addDrawOp() is called. |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 133 | fColor = color; |
| 134 | |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 135 | SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, kUnknown_SkPixelGeometry); |
| 136 | auto* grContext = this->context()->internal().grContext(); |
| 137 | auto bounds = SkIRect::MakeWH(fWidth, fHeight); |
| 138 | auto atlasTextContext = grContext->contextPriv().drawingManager()->getAtlasTextContext(); |
Brian Salomon | 0c1c2b3 | 2017-11-20 13:13:01 -0500 | [diff] [blame] | 139 | size_t byteLength = sizeof(SkGlyphID) * glyphCnt; |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 140 | const SkScalar* pos = &positions->fX; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 141 | atlasTextContext->drawPosText(grContext, this, GrNoClip(), paint, this->ctm(), props, |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 142 | (const char*)glyphs, byteLength, pos, 2, {0, 0}, bounds); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void SkInternalAtlasTextTarget::addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) { |
| 146 | SkASSERT(clip.quickContains(SkRect::MakeIWH(fWidth, fHeight))); |
| 147 | // The SkAtlasTextRenderer currently only handles grayscale SDF glyphs. |
| 148 | if (op->maskType() != GrAtlasTextOp::kGrayscaleDistanceField_MaskType) { |
| 149 | return; |
| 150 | } |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 151 | const GrCaps& caps = *this->context()->internal().grContext()->caps(); |
| 152 | op->finalizeForTextTarget(fColor, caps); |
| 153 | int n = SkTMin(kMaxBatchLookBack, fOps.count()); |
| 154 | for (int i = 0; i < n; ++i) { |
| 155 | GrAtlasTextOp* other = fOps.fromBack(i).get(); |
| 156 | if (other->combineIfPossible(op.get(), caps)) { |
| 157 | return; |
| 158 | } |
| 159 | if (GrRectsOverlap(op->bounds(), other->bounds())) { |
| 160 | break; |
| 161 | } |
| 162 | } |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 163 | op->visitProxies([](GrSurfaceProxy*) {}); |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 164 | fOps.emplace_back(std::move(op)); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void SkInternalAtlasTextTarget::flush() { |
| 168 | for (int i = 0; i < fOps.count(); ++i) { |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 169 | fOps[i]->executeForTextTarget(this); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 170 | } |
| 171 | this->context()->internal().flush(); |
| 172 | fOps.reset(); |
| 173 | } |
| 174 | |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 175 | void GrAtlasTextOp::finalizeForTextTarget(uint32_t color, const GrCaps& caps) { |
| 176 | for (int i = 0; i < fGeoCount; ++i) { |
| 177 | fGeoData[i].fColor = color; |
| 178 | } |
| 179 | this->finalize(caps, nullptr /* applied clip */, GrPixelConfigIsClamped::kNo); |
| 180 | } |
| 181 | |
| 182 | void GrAtlasTextOp::executeForTextTarget(SkAtlasTextTarget* target) { |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 183 | FlushInfo flushInfo; |
| 184 | SkAutoGlyphCache glyphCache; |
| 185 | auto& context = target->context()->internal(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 186 | auto* atlasGlyphCache = context.grContext()->contextPriv().getAtlasGlyphCache(); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 187 | for (int i = 0; i < fGeoCount; ++i) { |
| 188 | GrAtlasTextBlob::VertexRegenerator regenerator( |
| 189 | fGeoData[i].fBlob, fGeoData[i].fRun, fGeoData[i].fSubRun, fGeoData[i].fViewMatrix, |
Brian Salomon | 778a2c9 | 2017-11-27 12:18:04 -0500 | [diff] [blame] | 190 | fGeoData[i].fX, fGeoData[i].fY, fGeoData[i].fColor, &context, atlasGlyphCache, |
| 191 | &glyphCache); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 192 | GrAtlasTextBlob::VertexRegenerator::Result result; |
| 193 | do { |
| 194 | result = regenerator.regenerate(); |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 195 | context.recordDraw(result.fFirstVertex, result.fGlyphsRegenerated, |
| 196 | fGeoData[i].fViewMatrix, target->handle()); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 197 | if (!result.fFinished) { |
| 198 | // Make space in the atlas so we can continue generating vertices. |
| 199 | context.flush(); |
| 200 | } |
| 201 | } while (!result.fFinished); |
| 202 | } |
| 203 | } |