blob: 8ff2523a73b49d5e7c1d2cc45bb802684987c47e [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com375ff852012-06-29 18:37:57 +00008#include "GrTextContext.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00009#include "GrContext.h"
joshualitt570d2f82015-02-25 13:19:48 -080010#include "GrFontScaler.h"
joshualitt0a42e682015-12-10 13:20:58 -080011#include "GrTextUtils.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000012
joshualitt9c328182015-03-23 08:13:04 -070013#include "SkDrawFilter.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000014#include "SkGlyphCache.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070015#include "SkGrPriv.h"
halcanary33779752015-10-27 14:01:05 -070016#include "SkTextBlobRunIterator.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
robertphillipsf6703fa2015-09-01 05:36:47 -070018GrTextContext::GrTextContext(GrContext* context, const SkSurfaceProps& surfaceProps)
halcanary96fcdcc2015-08-27 07:41:13 -070019 : fFallbackTextContext(nullptr)
joshualitt6e8cd962015-03-20 10:30:14 -070020 , fContext(context)
robertphillipsf6703fa2015-09-01 05:36:47 -070021 , fSurfaceProps(surfaceProps) {
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000022}
tomhudson@google.com375ff852012-06-29 18:37:57 +000023
robertphillips2d70dcb2015-10-06 07:38:23 -070024GrTextContext::~GrTextContext() {
25 delete fFallbackTextContext;
26}
jvanverth8c27a182014-10-14 08:45:50 -070027
robertphillips433625e2015-12-04 06:58:16 -080028void GrTextContext::drawText(GrDrawContext* dc,
robertphillipsf6703fa2015-09-01 05:36:47 -070029 const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080030 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070031 const char text[], size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070032 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
robertphillipsf6703fa2015-09-01 05:36:47 -070033 if (fContext->abandoned()) {
robertphillipsccb1b572015-05-27 11:02:55 -070034 return;
35 }
36
jvanverthaab626c2014-10-16 08:04:39 -070037 GrTextContext* textContext = this;
38 do {
robertphillips6ee690e2015-12-02 08:57:50 -080039 if (textContext->canDraw(skPaint, viewMatrix)) {
robertphillips433625e2015-12-04 06:58:16 -080040 textContext->onDrawText(dc, clip, paint, skPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -070041 text, byteLength, x, y, clipBounds);
joshualitt6e8cd962015-03-20 10:30:14 -070042 return;
jvanverthaab626c2014-10-16 08:04:39 -070043 }
44 textContext = textContext->fFallbackTextContext;
45 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070046
joshualitt6e8cd962015-03-20 10:30:14 -070047 // fall back to drawing as a path
joshualitt0a42e682015-12-10 13:20:58 -080048 GrTextUtils::DrawTextAsPath(fContext, dc, clip, skPaint, viewMatrix, text, byteLength, x, y,
49 clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070050}
51
robertphillips433625e2015-12-04 06:58:16 -080052void GrTextContext::drawPosText(GrDrawContext* dc,
robertphillipsf6703fa2015-09-01 05:36:47 -070053 const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080054 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070055 const char text[], size_t byteLength,
56 const SkScalar pos[], int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -070057 const SkPoint& offset, const SkIRect& clipBounds) {
robertphillipsf6703fa2015-09-01 05:36:47 -070058 if (fContext->abandoned()) {
robertphillipsccb1b572015-05-27 11:02:55 -070059 return;
60 }
61
jvanverth8c27a182014-10-14 08:45:50 -070062 GrTextContext* textContext = this;
63 do {
robertphillips6ee690e2015-12-02 08:57:50 -080064 if (textContext->canDraw(skPaint, viewMatrix)) {
robertphillips433625e2015-12-04 06:58:16 -080065 textContext->onDrawPosText(dc, clip, paint, skPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -070066 text, byteLength, pos,
joshualitt6e8cd962015-03-20 10:30:14 -070067 scalarsPerPosition, offset, clipBounds);
68 return;
jvanverth8c27a182014-10-14 08:45:50 -070069 }
70 textContext = textContext->fFallbackTextContext;
71 } while (textContext);
72
joshualitt6e8cd962015-03-20 10:30:14 -070073 // fall back to drawing as a path
joshualitt0a42e682015-12-10 13:20:58 -080074 GrTextUtils::DrawPosTextAsPath(fContext, dc, fSurfaceProps, clip, skPaint, viewMatrix, text,
75 byteLength, pos, scalarsPerPosition, offset, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070076}
77
robertphillips9c240a12015-05-28 07:45:59 -070078bool GrTextContext::ShouldDisableLCD(const SkPaint& paint) {
egdaniel27b63352015-09-15 13:13:50 -070079 if (!SkXfermode::AsMode(paint.getXfermode(), nullptr) ||
robertphillips9c240a12015-05-28 07:45:59 -070080 paint.getMaskFilter() ||
81 paint.getRasterizer() ||
robertphillips9c240a12015-05-28 07:45:59 -070082 paint.getPathEffect() ||
83 paint.isFakeBoldText() ||
84 paint.getStyle() != SkPaint::kFill_Style)
85 {
86 return true;
87 }
88 return false;
89}
90
robertphillipsfcf78292015-06-19 11:49:52 -070091uint32_t GrTextContext::FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint) {
robertphillips9c240a12015-05-28 07:45:59 -070092 uint32_t flags = paint.getFlags();
93
94 if (!paint.isLCDRenderText() || !paint.isAntiAlias()) {
95 return flags;
96 }
97
robertphillipsfcf78292015-06-19 11:49:52 -070098 if (kUnknown_SkPixelGeometry == surfaceProps.pixelGeometry() || ShouldDisableLCD(paint)) {
robertphillips9c240a12015-05-28 07:45:59 -070099 flags &= ~SkPaint::kLCDRenderText_Flag;
100 flags |= SkPaint::kGenA8FromLCD_Flag;
101 }
102
103 return flags;
104}
105
robertphillips433625e2015-12-04 06:58:16 -0800106void GrTextContext::drawTextBlob(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700107 const GrClip& clip, const SkPaint& skPaint,
joshualitt9c328182015-03-23 08:13:04 -0700108 const SkMatrix& viewMatrix, const SkTextBlob* blob,
109 SkScalar x, SkScalar y,
110 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
cdaltone68f7362015-03-25 14:02:37 -0700111 SkPaint runPaint = skPaint;
joshualitt9c328182015-03-23 08:13:04 -0700112
halcanary33779752015-10-27 14:01:05 -0700113 SkTextBlobRunIterator it(blob);
cdaltone68f7362015-03-25 14:02:37 -0700114 for (;!it.done(); it.next()) {
115 size_t textLen = it.glyphCount() * sizeof(uint16_t);
116 const SkPoint& offset = it.offset();
117 // applyFontToPaint() always overwrites the exact same attributes,
118 // so it is safe to not re-seed the paint for this reason.
119 it.applyFontToPaint(&runPaint);
120
121 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
122 // A false return from filter() means we should abort the current draw.
123 runPaint = skPaint;
124 continue;
joshualitt9c328182015-03-23 08:13:04 -0700125 }
cdaltone68f7362015-03-25 14:02:37 -0700126
robertphillipsfcf78292015-06-19 11:49:52 -0700127 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
cdaltone68f7362015-03-25 14:02:37 -0700128
129 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700130 if (!SkPaintToGrPaint(fContext, runPaint, viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700131 return;
132 }
cdaltone68f7362015-03-25 14:02:37 -0700133
134 switch (it.positioning()) {
135 case SkTextBlob::kDefault_Positioning:
robertphillips433625e2015-12-04 06:58:16 -0800136 this->drawText(dc, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700137 textLen, x + offset.x(), y + offset.y(), clipBounds);
138 break;
139 case SkTextBlob::kHorizontal_Positioning:
robertphillips433625e2015-12-04 06:58:16 -0800140 this->drawPosText(dc, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700141 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
142 break;
143 case SkTextBlob::kFull_Positioning:
robertphillips433625e2015-12-04 06:58:16 -0800144 this->drawPosText(dc, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700145 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
146 break;
147 default:
148 SkFAIL("unhandled positioning mode");
149 }
150
151 if (drawFilter) {
152 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
153 runPaint = skPaint;
154 }
155 }
joshualitt9c328182015-03-23 08:13:04 -0700156}
157
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000158static void GlyphCacheAuxProc(void* data) {
159 GrFontScaler* scaler = (GrFontScaler*)data;
160 SkSafeUnref(scaler);
161}
162
163GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
164 void* auxData;
halcanary96fcdcc2015-08-27 07:41:13 -0700165 GrFontScaler* scaler = nullptr;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000166
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000167 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
168 scaler = (GrFontScaler*)auxData;
169 }
halcanary96fcdcc2015-08-27 07:41:13 -0700170 if (nullptr == scaler) {
halcanary385fe4d2015-08-26 13:07:48 -0700171 scaler = new GrFontScaler(cache);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000172 cache->setAuxProc(GlyphCacheAuxProc, scaler);
173 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000174
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000175 return scaler;
176}