| 1 |
/* |
| 2 |
* Copyright 2018 The Android Open Source Project |
| 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 "src/core/SkGlyphRunPainter.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkColorType.h" |
| 14 |
#include "include/core/SkDrawable.h" |
| 15 |
#include "include/core/SkFont.h" |
| 16 |
#include "include/core/SkImageInfo.h" |
| 17 |
#include "include/core/SkMatrix.h" |
| 18 |
#include "include/core/SkPaint.h" |
| 19 |
#include "include/core/SkPath.h" |
| 20 |
#include "include/core/SkPathBuilder.h" |
| 21 |
#include "include/core/SkPoint.h" |
| 22 |
#include "include/core/SkRect.h" |
| 23 |
#include "include/core/SkRefCnt.h" |
| 24 |
#include "include/core/SkScalar.h" |
| 25 |
#include "include/core/SkSpan.h" |
| 26 |
#include "include/core/SkTypes.h" |
| 27 |
#include "include/private/base/SkFloatingPoint.h" |
| 28 |
#include "include/private/base/SkTArray.h" |
| 29 |
#include "src/core/SkDraw.h" |
| 30 |
#include "src/core/SkGlyph.h" |
| 31 |
#include "src/core/SkMask.h" |
| 32 |
#include "src/core/SkMipmap.h" |
| 33 |
#include "src/core/SkScalerContext.h" |
| 34 |
#include "src/core/SkStrike.h" |
| 35 |
#include "src/core/SkStrikeSpec.h" |
| 36 |
#include "src/text/GlyphRun.h" |
| 37 |
|
| 38 |
#include <algorithm> |
| 39 |
#include <tuple> |
| 40 |
|
| 41 |
using namespace skia_private; |
| 42 |
|
| 43 |
using namespace skglyph; |
| 44 |
using namespace sktext; |
| 45 |
|
| 46 |
namespace { |
| 47 |
SkScalerContextFlags compute_scaler_context_flags(const SkColorSpace* cs) { |
| 48 |
// If we're doing linear blending, then we can disable the gamma hacks. |
| 49 |
// Otherwise, leave them on. In either case, we still want the contrast boost: |
| 50 |
// TODO: Can we be even smarter about mask gamma based on the dest transfer function? |
| 51 |
if (cs && cs->gammaIsLinear()) { |
| 52 |
return SkScalerContextFlags::kBoostContrast; |
| 53 |
} else { |
| 54 |
return SkScalerContextFlags::kFakeGammaAndBoostContrast; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// TODO: collect this up into a single class when all the details are worked out. |
| 59 |
// This is duplicate code. The original is in SubRunContainer.cpp. |
| 60 |
std::tuple<SkZip<const SkGlyph*, SkPoint>, SkZip<SkGlyphID, SkPoint>> |
| 61 |
prepare_for_path_drawing(SkStrike* strike, |
| 62 |
SkZip<const SkGlyphID, const SkPoint> source, |
| 63 |
SkZip<const SkGlyph*, SkPoint> acceptedBuffer, |
| 64 |
SkZip<SkGlyphID, SkPoint> rejectedBuffer) { |
| 65 |
int acceptedSize = 0; |
| 66 |
int rejectedSize = 0; |
| 67 |
strike->lock(); |
| 68 |
for (auto [glyphID, pos] : source) { |
| 69 |
if (!SkIsFinite(pos.x(), pos.y())) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
const SkPackedGlyphID packedID{glyphID}; |
| 73 |
switch (SkGlyphDigest digest = strike->digestFor(kPath, packedID); |
| 74 |
digest.actionFor(kPath)) { |
| 75 |
case GlyphAction::kAccept: |
| 76 |
acceptedBuffer[acceptedSize++] = std::make_tuple(strike->glyph(digest), pos); |
| 77 |
break; |
| 78 |
case GlyphAction::kReject: |
| 79 |
rejectedBuffer[rejectedSize++] = std::make_tuple(glyphID, pos); |
| 80 |
break; |
| 81 |
default: |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
strike->unlock(); |
| 86 |
return {acceptedBuffer.first(acceptedSize), rejectedBuffer.first(rejectedSize)}; |
| 87 |
} |
| 88 |
|
| 89 |
// TODO: collect this up into a single class when all the details are worked out. |
| 90 |
// This is duplicate code. The original is in SubRunContainer.cpp. |
| 91 |
std::tuple<SkZip<const SkGlyph*, SkPoint>, SkZip<SkGlyphID, SkPoint>> |
| 92 |
prepare_for_drawable_drawing(SkStrike* strike, |
| 93 |
SkZip<const SkGlyphID, const SkPoint> source, |
| 94 |
SkZip<const SkGlyph*, SkPoint> acceptedBuffer, |
| 95 |
SkZip<SkGlyphID, SkPoint> rejectedBuffer) { |
| 96 |
int acceptedSize = 0; |
| 97 |
int rejectedSize = 0; |
| 98 |
strike->lock(); |
| 99 |
for (auto [glyphID, pos] : source) { |
| 100 |
if (!SkIsFinite(pos.x(), pos.y())) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
const SkPackedGlyphID packedID{glyphID}; |
| 104 |
switch (SkGlyphDigest digest = strike->digestFor(kDrawable, packedID); |
| 105 |
digest.actionFor(kDrawable)) { |
| 106 |
case GlyphAction::kAccept: |
| 107 |
acceptedBuffer[acceptedSize++] = std::make_tuple(strike->glyph(digest), pos); |
| 108 |
break; |
| 109 |
case GlyphAction::kReject: |
| 110 |
rejectedBuffer[rejectedSize++] = std::make_tuple(glyphID, pos); |
| 111 |
break; |
| 112 |
default: |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
strike->unlock(); |
| 117 |
return {acceptedBuffer.first(acceptedSize), rejectedBuffer.first(rejectedSize)}; |
| 118 |
} |
| 119 |
|
| 120 |
std::tuple<SkZip<const SkGlyph*, SkPoint>, SkZip<SkGlyphID, SkPoint>> |
| 121 |
prepare_for_direct_mask_drawing(SkStrike* strike, |
| 122 |
const SkMatrix& creationMatrix, |
| 123 |
SkZip<const SkGlyphID, const SkPoint> source, |
| 124 |
SkZip<const SkGlyph*, SkPoint> acceptedBuffer, |
| 125 |
SkZip<SkGlyphID, SkPoint> rejectedBuffer) { |
| 126 |
const SkIPoint mask = strike->roundingSpec().ignorePositionFieldMask; |
| 127 |
const SkPoint halfSampleFreq = strike->roundingSpec().halfAxisSampleFreq; |
| 128 |
|
| 129 |
// Build up the mapping from source space to device space. Add the rounding constant |
| 130 |
// halfSampleFreq, so we just need to floor to get the device result. |
| 131 |
SkMatrix positionMatrixWithRounding = creationMatrix; |
| 132 |
positionMatrixWithRounding.postTranslate(halfSampleFreq.x(), halfSampleFreq.y()); |
| 133 |
|
| 134 |
int acceptedSize = 0; |
| 135 |
int rejectedSize = 0; |
| 136 |
strike->lock(); |
| 137 |
for (auto [glyphID, pos] : source) { |
| 138 |
if (!SkIsFinite(pos.x(), pos.y())) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
|
| 142 |
const SkPoint mappedPos = positionMatrixWithRounding.mapPoint(pos); |
| 143 |
const SkPackedGlyphID packedGlyphID = SkPackedGlyphID{glyphID, mappedPos, mask}; |
| 144 |
switch (SkGlyphDigest digest = strike->digestFor(kDirectMaskCPU, packedGlyphID); |
| 145 |
digest.actionFor(kDirectMaskCPU)) { |
| 146 |
case GlyphAction::kAccept: { |
| 147 |
const SkPoint roundedPos{SkScalarFloorToScalar(mappedPos.x()), |
| 148 |
SkScalarFloorToScalar(mappedPos.y())}; |
| 149 |
acceptedBuffer[acceptedSize++] = |
| 150 |
std::make_tuple(strike->glyph(digest), roundedPos); |
| 151 |
break; |
| 152 |
} |
| 153 |
case GlyphAction::kReject: |
| 154 |
rejectedBuffer[rejectedSize++] = std::make_tuple(glyphID, pos); |
| 155 |
break; |
| 156 |
default: |
| 157 |
break; |
| 158 |
} |
| 159 |
} |
| 160 |
strike->unlock(); |
| 161 |
|
| 162 |
return {acceptedBuffer.first(acceptedSize), rejectedBuffer.first(rejectedSize)}; |
| 163 |
} |
| 164 |
|
| 165 |
// Same as prepare_for_direct_mask_drawing but accepted points are unmapped source points. |
| 166 |
std::tuple<SkZip<const SkGlyph*, SkPoint>, SkZip<SkGlyphID, SkPoint>> |
| 167 |
prepare_for_direct_bitmap_drawing(SkStrike* strike, |
| 168 |
const SkMatrix& creationMatrix, |
| 169 |
SkZip<const SkGlyphID, const SkPoint> source, |
| 170 |
SkZip<const SkGlyph*, SkPoint> acceptedBuffer, |
| 171 |
SkZip<SkGlyphID, SkPoint> rejectedBuffer) { |
| 172 |
const SkIPoint mask = strike->roundingSpec().ignorePositionFieldMask; |
| 173 |
const SkPoint halfSampleFreq = strike->roundingSpec().halfAxisSampleFreq; |
| 174 |
|
| 175 |
// Build up the mapping from source space to device space. Add the rounding constant |
| 176 |
// halfSampleFreq, so we just need to floor to get the device result. |
| 177 |
SkMatrix positionMatrixWithRounding = creationMatrix; |
| 178 |
positionMatrixWithRounding.postTranslate(halfSampleFreq.x(), halfSampleFreq.y()); |
| 179 |
|
| 180 |
int acceptedSize = 0; |
| 181 |
int rejectedSize = 0; |
| 182 |
strike->lock(); |
| 183 |
for (auto [glyphID, pos] : source) { |
| 184 |
if (!SkIsFinite(pos.x(), pos.y())) { |
| 185 |
continue; |
| 186 |
} |
| 187 |
|
| 188 |
const SkPoint mappedPos = positionMatrixWithRounding.mapPoint(pos); |
| 189 |
const SkPackedGlyphID packedGlyphID = SkPackedGlyphID{glyphID, mappedPos, mask}; |
| 190 |
switch (SkGlyphDigest digest = strike->digestFor(kDirectMaskCPU, packedGlyphID); |
| 191 |
digest.actionFor(kDirectMaskCPU)) { |
| 192 |
case GlyphAction::kAccept: { |
| 193 |
acceptedBuffer[acceptedSize++] = |
| 194 |
std::make_tuple(strike->glyph(digest), pos); |
| 195 |
break; |
| 196 |
} |
| 197 |
case GlyphAction::kReject: |
| 198 |
rejectedBuffer[rejectedSize++] = std::make_tuple(glyphID, pos); |
| 199 |
break; |
| 200 |
default: |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
strike->unlock(); |
| 205 |
|
| 206 |
return {acceptedBuffer.first(acceptedSize), rejectedBuffer.first(rejectedSize)}; |
| 207 |
} |
| 208 |
} // namespace |
| 209 |
|
| 210 |
namespace skcpu { |
| 211 |
GlyphRunListPainter::GlyphRunListPainter(const SkSurfaceProps& props, |
| 212 |
SkColorType colorType, |
| 213 |
SkColorSpace* cs) |
| 214 |
: fDeviceProps{props} |
| 215 |
, fBitmapFallbackProps{props.cloneWithPixelGeometry(kUnknown_SkPixelGeometry)} |
| 216 |
, fColorType{colorType} |
| 217 |
, fScalerContextFlags{compute_scaler_context_flags(cs)} {} |
| 218 |
|
| 219 |
void GlyphRunListPainter::drawForBitmapDevice(SkCanvas* canvas, |
| 220 |
const BitmapDevicePainter* bitmapDevice, |
| 221 |
const sktext::GlyphRunList& glyphRunList, |
| 222 |
const SkPaint& paint, |
| 223 |
const SkMatrix& drawMatrix) { |
| 224 |
STArray<64, const SkGlyph*> acceptedPackedGlyphIDs; |
| 225 |
STArray<64, SkPoint> acceptedPositions; |
| 226 |
STArray<64, SkGlyphID> rejectedGlyphIDs; |
| 227 |
STArray<64, SkPoint> rejectedPositions; |
| 228 |
const int maxGlyphRunSize = glyphRunList.maxGlyphRunSize(); |
| 229 |
acceptedPackedGlyphIDs.resize(maxGlyphRunSize); |
| 230 |
acceptedPositions.resize(maxGlyphRunSize); |
| 231 |
const auto acceptedBuffer = SkMakeZip(acceptedPackedGlyphIDs, acceptedPositions); |
| 232 |
rejectedGlyphIDs.resize(maxGlyphRunSize); |
| 233 |
rejectedPositions.resize(maxGlyphRunSize); |
| 234 |
const auto rejectedBuffer = SkMakeZip(rejectedGlyphIDs, rejectedPositions); |
| 235 |
|
| 236 |
// The bitmap blitters can only draw lcd text to a N32 bitmap in srcOver. Otherwise, |
| 237 |
// convert the lcd text into A8 text. The props communicate this to the scaler. |
| 238 |
auto& props = (kN32_SkColorType == fColorType && paint.isSrcOver()) |
| 239 |
? fDeviceProps |
| 240 |
: fBitmapFallbackProps; |
| 241 |
|
| 242 |
SkPoint drawOrigin = glyphRunList.origin(); |
| 243 |
SkMatrix positionMatrix{drawMatrix}; |
| 244 |
positionMatrix.preTranslate(drawOrigin.x(), drawOrigin.y()); |
| 245 |
for (auto& glyphRun : glyphRunList) { |
| 246 |
const SkFont& runFont = glyphRun.font(); |
| 247 |
|
| 248 |
SkZip<const SkGlyphID, const SkPoint> source = glyphRun.source(); |
| 249 |
|
| 250 |
if (SkStrikeSpec::ShouldDrawAsPath(paint, runFont, positionMatrix)) { |
| 251 |
auto [strikeSpec, strikeToSourceScale] = |
| 252 |
SkStrikeSpec::MakePath(runFont, paint, props, fScalerContextFlags); |
| 253 |
|
| 254 |
auto strike = strikeSpec.findOrCreateStrike(); |
| 255 |
|
| 256 |
{ |
| 257 |
auto [accepted, rejected] = prepare_for_path_drawing(strike.get(), |
| 258 |
source, |
| 259 |
acceptedBuffer, |
| 260 |
rejectedBuffer); |
| 261 |
|
| 262 |
source = rejected; |
| 263 |
// The paint we draw paths with must have the same anti-aliasing state as the |
| 264 |
// runFont allowing the paths to have the same edging as the glyph masks. |
| 265 |
SkPaint pathPaint = paint; |
| 266 |
pathPaint.setAntiAlias(runFont.hasSomeAntiAliasing()); |
| 267 |
|
| 268 |
const bool stroking = pathPaint.getStyle() != SkPaint::kFill_Style; |
| 269 |
const bool hairline = pathPaint.getStrokeWidth() == 0; |
| 270 |
const bool needsExactCTM = pathPaint.getShader() || |
| 271 |
pathPaint.getPathEffect() || |
| 272 |
pathPaint.getMaskFilter() || |
| 273 |
(stroking && !hairline); |
| 274 |
|
| 275 |
if (!needsExactCTM) { |
| 276 |
for (auto [glyph, pos] : accepted) { |
| 277 |
const SkPath* path = glyph->path(); |
| 278 |
SkMatrix m; |
| 279 |
SkPoint translate = drawOrigin + pos; |
| 280 |
m.setScaleTranslate(strikeToSourceScale, strikeToSourceScale, |
| 281 |
translate.x(), translate.y()); |
| 282 |
SkAutoCanvasRestore acr(canvas, true); |
| 283 |
canvas->concat(m); |
| 284 |
canvas->drawPath(*path, pathPaint); |
| 285 |
} |
| 286 |
} else { |
| 287 |
for (auto [glyph, pos] : accepted) { |
| 288 |
const SkPath* path = glyph->path(); |
| 289 |
SkMatrix m; |
| 290 |
SkPoint translate = drawOrigin + pos; |
| 291 |
m.setScaleTranslate(strikeToSourceScale, strikeToSourceScale, |
| 292 |
translate.x(), translate.y()); |
| 293 |
|
| 294 |
SkPathBuilder builder; |
| 295 |
builder.addPath(*path, m); |
| 296 |
builder.setIsVolatile(true); |
| 297 |
canvas->drawPath(builder.detach(), pathPaint); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
if (!source.empty()) { |
| 303 |
auto [accepted, rejected] = prepare_for_drawable_drawing(strike.get(), |
| 304 |
source, |
| 305 |
acceptedBuffer, |
| 306 |
rejectedBuffer); |
| 307 |
source = rejected; |
| 308 |
|
| 309 |
for (auto [glyph, pos] : accepted) { |
| 310 |
SkDrawable* drawable = glyph->drawable(); |
| 311 |
SkMatrix m; |
| 312 |
SkPoint translate = drawOrigin + pos; |
| 313 |
m.setScaleTranslate(strikeToSourceScale, strikeToSourceScale, |
| 314 |
translate.x(), translate.y()); |
| 315 |
SkAutoCanvasRestore acr(canvas, false); |
| 316 |
SkRect drawableBounds = drawable->getBounds(); |
| 317 |
m.mapRect(&drawableBounds); |
| 318 |
canvas->saveLayer(&drawableBounds, &paint); |
| 319 |
drawable->draw(canvas, &m); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
if (!source.empty() && !positionMatrix.hasPerspective()) { |
| 324 |
SkStrikeSpec strikeSpec = SkStrikeSpec::MakeMask( |
| 325 |
runFont, paint, props, fScalerContextFlags, positionMatrix); |
| 326 |
|
| 327 |
auto strike = strikeSpec.findOrCreateStrike(); |
| 328 |
|
| 329 |
auto [accepted, rejected] = prepare_for_direct_mask_drawing(strike.get(), |
| 330 |
positionMatrix, |
| 331 |
source, |
| 332 |
acceptedBuffer, |
| 333 |
rejectedBuffer); |
| 334 |
source = rejected; |
| 335 |
bitmapDevice->paintMasks(accepted, paint); |
| 336 |
} |
| 337 |
if (!source.empty()) { |
| 338 |
// Create a strike is source space to calculate scale information. |
| 339 |
SkStrikeSpec scaleStrikeSpec = SkStrikeSpec::MakeMask( |
| 340 |
runFont, paint, props, fScalerContextFlags, SkMatrix::I()); |
| 341 |
SkBulkGlyphMetrics metrics{scaleStrikeSpec}; |
| 342 |
|
| 343 |
auto glyphIDs = source.get<0>(); |
| 344 |
auto positions = source.get<1>(); |
| 345 |
SkSpan<const SkGlyph*> glyphs = metrics.glyphs(glyphIDs); |
| 346 |
SkScalar maxScale = SK_ScalarMin; |
| 347 |
|
| 348 |
// Calculate the scale that makes the longest edge 1:1 with its side in the cache. |
| 349 |
for (auto [glyph, pos] : SkMakeZip(glyphs, positions)) { |
| 350 |
if (glyph->isEmpty()) { |
| 351 |
continue; |
| 352 |
} |
| 353 |
SkPoint corners[4]; |
| 354 |
SkRect rect = glyph->rect(); |
| 355 |
rect.makeOffset(drawOrigin + pos); |
| 356 |
positionMatrix.mapRectToQuad(corners, rect); |
| 357 |
// left top -> right top |
| 358 |
SkScalar scale = (corners[1] - corners[0]).length() / rect.width(); |
| 359 |
maxScale = std::max(maxScale, scale); |
| 360 |
// right top -> right bottom |
| 361 |
scale = (corners[2] - corners[1]).length() / rect.height(); |
| 362 |
maxScale = std::max(maxScale, scale); |
| 363 |
// right bottom -> left bottom |
| 364 |
scale = (corners[3] - corners[2]).length() / rect.width(); |
| 365 |
maxScale = std::max(maxScale, scale); |
| 366 |
// left bottom -> left top |
| 367 |
scale = (corners[0] - corners[3]).length() / rect.height(); |
| 368 |
maxScale = std::max(maxScale, scale); |
| 369 |
} |
| 370 |
|
| 371 |
if (maxScale <= 0) { |
| 372 |
continue; // to the next run. |
| 373 |
} |
| 374 |
|
| 375 |
if (maxScale * runFont.getSize() > 256) { |
| 376 |
maxScale = 256.0f / runFont.getSize(); |
| 377 |
} |
| 378 |
|
| 379 |
SkMatrix cacheScale = SkMatrix::Scale(maxScale, maxScale); |
| 380 |
SkStrikeSpec strikeSpec = SkStrikeSpec::MakeMask( |
| 381 |
runFont, paint, props, fScalerContextFlags, cacheScale); |
| 382 |
|
| 383 |
auto strike = strikeSpec.findOrCreateStrike(); |
| 384 |
|
| 385 |
auto [accepted, rejected] = prepare_for_direct_bitmap_drawing(strike.get(), |
| 386 |
positionMatrix, |
| 387 |
source, |
| 388 |
acceptedBuffer, |
| 389 |
rejectedBuffer); |
| 390 |
const SkScalar invMaxScale = 1.0f/maxScale; |
| 391 |
for (auto [glyph, srcPos] : accepted) { |
| 392 |
SkMask mask = glyph->mask(); |
| 393 |
// TODO: is this needed will A8 and BW just work? |
| 394 |
if (mask.fFormat != SkMask::kARGB32_Format) { |
| 395 |
continue; |
| 396 |
} |
| 397 |
SkBitmap bm; |
| 398 |
bm.installPixels(SkImageInfo::MakeN32Premul(mask.fBounds.size()), |
| 399 |
const_cast<uint8_t*>(mask.fImage), |
| 400 |
mask.fRowBytes); |
| 401 |
bm.setImmutable(); |
| 402 |
|
| 403 |
// Since the glyph in the cache is scaled by maxScale, its top left vector is too |
| 404 |
// long. Reduce it to find proper positions on the device. |
| 405 |
SkPoint pos = drawOrigin + srcPos |
| 406 |
+ SkPoint::Make(mask.fBounds.left(), mask.fBounds.top())*invMaxScale; |
| 407 |
|
| 408 |
// Calculate the preConcat matrix for drawBitmap to get the rectangle from the |
| 409 |
// glyph cache (which is multiplied by maxScale) to land in the right place. |
| 410 |
SkMatrix translate = SkMatrix::Translate(pos); |
| 411 |
translate.preScale(invMaxScale, invMaxScale); |
| 412 |
|
| 413 |
// Draw the bitmap using the rect from the scaled cache, and not the source |
| 414 |
// rectangle for the glyph. |
| 415 |
bitmapDevice->drawBitmap( |
| 416 |
bm, translate, nullptr, SkFilterMode::kLinear, paint, nullptr); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
// TODO: have the mask stage above reject the glyphs that are too big, and handle the |
| 421 |
// rejects in a more sophisticated stage. |
| 422 |
} |
| 423 |
} |
| 424 |
} // namespace skcpu |