1 /* 2 * Copyright 2011 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 "SkDevice.h" 9 #include "SkColorFilter.h" 10 #include "SkDraw.h" 11 #include "SkDrawFilter.h" 12 #include "SkImageFilter.h" 13 #include "SkImageFilterCache.h" 14 #include "SkImagePriv.h" 15 #include "SkImage_Base.h" 16 #include "SkLatticeIter.h" 17 #include "SkLocalMatrixShader.h" 18 #include "SkMatrixPriv.h" 19 #include "SkPatchUtils.h" 20 #include "SkPathMeasure.h" 21 #include "SkPathPriv.h" 22 #include "SkRSXform.h" 23 #include "SkRasterClip.h" 24 #include "SkShader.h" 25 #include "SkSpecialImage.h" 26 #include "SkTLazy.h" 27 #include "SkTextBlobRunIterator.h" 28 #include "SkTextToPathIter.h" 29 #include "SkUtils.h" 30 #include "SkVertices.h" 31 32 SkBaseDevice::SkBaseDevice(const SkImageInfo& info, const SkSurfaceProps& surfaceProps) 33 : fInfo(info) 34 , fSurfaceProps(surfaceProps) 35 { 36 fOrigin = {0, 0}; 37 fCTM.reset(); 38 } 39 40 void SkBaseDevice::setOrigin(const SkMatrix& globalCTM, int x, int y) { 41 fOrigin.set(x, y); 42 fCTM = globalCTM; 43 fCTM.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); 44 } 45 46 void SkBaseDevice::setGlobalCTM(const SkMatrix& ctm) { 47 fCTM = ctm; 48 if (fOrigin.fX | fOrigin.fY) { 49 fCTM.postTranslate(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY)); 50 } 51 } 52 53 bool SkBaseDevice::clipIsWideOpen() const { 54 if (kRect_ClipType == this->onGetClipType()) { 55 SkRegion rgn; 56 this->onAsRgnClip(&rgn); 57 SkASSERT(rgn.isRect()); 58 return rgn.getBounds() == SkIRect::MakeWH(this->width(), this->height()); 59 } else { 60 return false; 61 } 62 } 63 64 SkPixelGeometry SkBaseDevice::CreateInfo::AdjustGeometry(const SkImageInfo& info, 65 TileUsage tileUsage, 66 SkPixelGeometry geo, 67 bool preserveLCDText) { 68 switch (tileUsage) { 69 case kPossible_TileUsage: 70 // (we think) for compatibility with old clients, we assume this layer can support LCD 71 // even though they may not have marked it as opaque... seems like we should update 72 // our callers (reed/robertphilips). 73 break; 74 case kNever_TileUsage: 75 if (!preserveLCDText) { 76 geo = kUnknown_SkPixelGeometry; 77 } 78 break; 79 } 80 return geo; 81 } 82 83 static inline bool is_int(float x) { 84 return x == (float) sk_float_round2int(x); 85 } 86 87 void SkBaseDevice::drawRegion(const SkRegion& region, const SkPaint& paint) { 88 const SkMatrix& ctm = this->ctm(); 89 bool isNonTranslate = ctm.getType() & ~(SkMatrix::kTranslate_Mask); 90 bool complexPaint = paint.getStyle() != SkPaint::kFill_Style || paint.getMaskFilter() || 91 paint.getPathEffect(); 92 bool antiAlias = paint.isAntiAlias() && (!is_int(ctm.getTranslateX()) || 93 !is_int(ctm.getTranslateY())); 94 if (isNonTranslate || complexPaint || antiAlias) { 95 SkPath path; 96 region.getBoundaryPath(&path); 97 return this->drawPath(path, paint, nullptr, false); 98 } 99 100 SkRegion::Iterator it(region); 101 while (!it.done()) { 102 this->drawRect(SkRect::Make(it.rect()), paint); 103 it.next(); 104 } 105 } 106 107 void SkBaseDevice::drawArc(const SkRect& oval, SkScalar startAngle, 108 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) { 109 SkPath path; 110 bool isFillNoPathEffect = SkPaint::kFill_Style == paint.getStyle() && !paint.getPathEffect(); 111 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter, 112 isFillNoPathEffect); 113 this->drawPath(path, paint); 114 } 115 116 void SkBaseDevice::drawDRRect(const SkRRect& outer, 117 const SkRRect& inner, const SkPaint& paint) { 118 SkPath path; 119 path.addRRect(outer); 120 path.addRRect(inner); 121 path.setFillType(SkPath::kEvenOdd_FillType); 122 path.setIsVolatile(true); 123 124 const SkMatrix* preMatrix = nullptr; 125 const bool pathIsMutable = true; 126 this->drawPath(path, paint, preMatrix, pathIsMutable); 127 } 128 129 void SkBaseDevice::drawPatch(const SkPoint cubics[12], const SkColor colors[4], 130 const SkPoint texCoords[4], SkBlendMode bmode, 131 bool interpColorsLinearly, const SkPaint& paint) { 132 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &this->ctm()); 133 auto vertices = SkPatchUtils::MakeVertices(cubics, colors, texCoords, lod.width(), lod.height(), 134 interpColorsLinearly); 135 if (vertices) { 136 this->drawVertices(vertices.get(), bmode, paint); 137 } 138 } 139 140 void SkBaseDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, 141 const SkPaint &paint, SkDrawFilter* drawFilter) { 142 143 SkPaint runPaint = paint; 144 145 SkTextBlobRunIterator it(blob); 146 for (;!it.done(); it.next()) { 147 size_t textLen = it.glyphCount() * sizeof(uint16_t); 148 const SkPoint& offset = it.offset(); 149 // applyFontToPaint() always overwrites the exact same attributes, 150 // so it is safe to not re-seed the paint for this reason. 151 it.applyFontToPaint(&runPaint); 152 153 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { 154 // A false return from filter() means we should abort the current draw. 155 runPaint = paint; 156 continue; 157 } 158 159 runPaint.setFlags(this->filterTextFlags(runPaint)); 160 161 switch (it.positioning()) { 162 case SkTextBlob::kDefault_Positioning: 163 this->drawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint); 164 break; 165 case SkTextBlob::kHorizontal_Positioning: 166 this->drawPosText(it.glyphs(), textLen, it.pos(), 1, 167 SkPoint::Make(x, y + offset.y()), runPaint); 168 break; 169 case SkTextBlob::kFull_Positioning: 170 this->drawPosText(it.glyphs(), textLen, it.pos(), 2, 171 SkPoint::Make(x, y), runPaint); 172 break; 173 default: 174 SK_ABORT("unhandled positioning mode"); 175 } 176 177 if (drawFilter) { 178 // A draw filter may change the paint arbitrarily, so we must re-seed in this case. 179 runPaint = paint; 180 } 181 } 182 } 183 184 void SkBaseDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, 185 const SkPaint& paint) { 186 SkBitmap bm; 187 if (as_IB(image)->getROPixels(&bm, this->imageInfo().colorSpace())) { 188 this->drawBitmap(bm, x, y, paint); 189 } 190 } 191 192 void SkBaseDevice::drawImageRect(const SkImage* image, const SkRect* src, 193 const SkRect& dst, const SkPaint& paint, 194 SkCanvas::SrcRectConstraint constraint) { 195 SkBitmap bm; 196 if (as_IB(image)->getROPixels(&bm, this->imageInfo().colorSpace())) { 197 this->drawBitmapRect(bm, src, dst, paint, constraint); 198 } 199 } 200 201 void SkBaseDevice::drawImageNine(const SkImage* image, const SkIRect& center, 202 const SkRect& dst, const SkPaint& paint) { 203 SkLatticeIter iter(image->width(), image->height(), center, dst); 204 205 SkRect srcR, dstR; 206 while (iter.next(&srcR, &dstR)) { 207 this->drawImageRect(image, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint); 208 } 209 } 210 211 void SkBaseDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, 212 const SkRect& dst, const SkPaint& paint) { 213 SkLatticeIter iter(bitmap.width(), bitmap.height(), center, dst); 214 215 SkRect srcR, dstR; 216 while (iter.next(&srcR, &dstR)) { 217 this->drawBitmapRect(bitmap, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint); 218 } 219 } 220 221 void SkBaseDevice::drawImageLattice(const SkImage* image, 222 const SkCanvas::Lattice& lattice, const SkRect& dst, 223 const SkPaint& paint) { 224 SkLatticeIter iter(lattice, dst); 225 226 SkRect srcR, dstR; 227 SkColor c; 228 bool isFixedColor = false; 229 const SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); 230 231 while (iter.next(&srcR, &dstR, &isFixedColor, &c)) { 232 if (isFixedColor || (srcR.width() <= 1.0f && srcR.height() <= 1.0f && 233 image->readPixels(info, &c, 4, srcR.fLeft, srcR.fTop))) { 234 // Fast draw with drawRect, if this is a patch containing a single color 235 // or if this is a patch containing a single pixel. 236 if (0 != c || !paint.isSrcOver()) { 237 SkPaint paintCopy(paint); 238 int alpha = SkAlphaMul(SkColorGetA(c), SkAlpha255To256(paint.getAlpha())); 239 paintCopy.setColor(SkColorSetA(c, alpha)); 240 this->drawRect(dstR, paintCopy); 241 } 242 } else { 243 this->drawImageRect(image, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint); 244 } 245 } 246 } 247 248 void SkBaseDevice::drawBitmapLattice(const SkBitmap& bitmap, 249 const SkCanvas::Lattice& lattice, const SkRect& dst, 250 const SkPaint& paint) { 251 SkLatticeIter iter(lattice, dst); 252 253 SkRect srcR, dstR; 254 while (iter.next(&srcR, &dstR)) { 255 this->drawBitmapRect(bitmap, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint); 256 } 257 } 258 259 static SkPoint* quad_to_tris(SkPoint tris[6], const SkPoint quad[4]) { 260 tris[0] = quad[0]; 261 tris[1] = quad[1]; 262 tris[2] = quad[2]; 263 264 tris[3] = quad[0]; 265 tris[4] = quad[2]; 266 tris[5] = quad[3]; 267 268 return tris + 6; 269 } 270 271 void SkBaseDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[], 272 const SkRect tex[], const SkColor colors[], int quadCount, 273 SkBlendMode mode, const SkPaint& paint) { 274 const int triCount = quadCount << 1; 275 const int vertexCount = triCount * 3; 276 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag; 277 if (colors) { 278 flags |= SkVertices::kHasColors_BuilderFlag; 279 } 280 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, 0, flags); 281 282 SkPoint* vPos = builder.positions(); 283 SkPoint* vTex = builder.texCoords(); 284 SkColor* vCol = builder.colors(); 285 for (int i = 0; i < quadCount; ++i) { 286 SkPoint tmp[4]; 287 xform[i].toQuad(tex[i].width(), tex[i].height(), tmp); 288 vPos = quad_to_tris(vPos, tmp); 289 290 tex[i].toQuad(tmp); 291 vTex = quad_to_tris(vTex, tmp); 292 293 if (colors) { 294 sk_memset32(vCol, colors[i], 6); 295 vCol += 6; 296 } 297 } 298 SkPaint p(paint); 299 p.setShader(atlas->makeShader()); 300 this->drawVertices(builder.detach().get(), mode, p); 301 } 302 303 /////////////////////////////////////////////////////////////////////////////////////////////////// 304 305 void SkBaseDevice::drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&, 306 SkImage*, const SkMatrix&) {} 307 sk_sp<SkSpecialImage> SkBaseDevice::makeSpecial(const SkBitmap&) { return nullptr; } 308 sk_sp<SkSpecialImage> SkBaseDevice::makeSpecial(const SkImage*) { return nullptr; } 309 sk_sp<SkSpecialImage> SkBaseDevice::snapSpecial() { return nullptr; } 310 311 /////////////////////////////////////////////////////////////////////////////////////////////////// 312 313 bool SkBaseDevice::readPixels(const SkPixmap& pm, int x, int y) { 314 return this->onReadPixels(pm, x, y); 315 } 316 317 bool SkBaseDevice::writePixels(const SkPixmap& pm, int x, int y) { 318 return this->onWritePixels(pm, x, y); 319 } 320 321 bool SkBaseDevice::onWritePixels(const SkPixmap&, int, int) { 322 return false; 323 } 324 325 bool SkBaseDevice::onReadPixels(const SkPixmap&, int x, int y) { 326 return false; 327 } 328 329 bool SkBaseDevice::accessPixels(SkPixmap* pmap) { 330 SkPixmap tempStorage; 331 if (nullptr == pmap) { 332 pmap = &tempStorage; 333 } 334 return this->onAccessPixels(pmap); 335 } 336 337 bool SkBaseDevice::peekPixels(SkPixmap* pmap) { 338 SkPixmap tempStorage; 339 if (nullptr == pmap) { 340 pmap = &tempStorage; 341 } 342 return this->onPeekPixels(pmap); 343 } 344 345 ////////////////////////////////////////////////////////////////////////////////////////// 346 347 static void morphpoints(SkPoint dst[], const SkPoint src[], int count, 348 SkPathMeasure& meas, const SkMatrix& matrix) { 349 SkMatrixPriv::MapXYProc proc = SkMatrixPriv::GetMapXYProc(matrix); 350 351 for (int i = 0; i < count; i++) { 352 SkPoint pos; 353 SkVector tangent; 354 355 proc(matrix, src[i].fX, src[i].fY, &pos); 356 SkScalar sx = pos.fX; 357 SkScalar sy = pos.fY; 358 359 if (!meas.getPosTan(sx, &pos, &tangent)) { 360 // set to 0 if the measure failed, so that we just set dst == pos 361 tangent.set(0, 0); 362 } 363 364 /* This is the old way (that explains our approach but is way too slow 365 SkMatrix matrix; 366 SkPoint pt; 367 368 pt.set(sx, sy); 369 matrix.setSinCos(tangent.fY, tangent.fX); 370 matrix.preTranslate(-sx, 0); 371 matrix.postTranslate(pos.fX, pos.fY); 372 matrix.mapPoints(&dst[i], &pt, 1); 373 */ 374 dst[i].set(pos.fX - tangent.fY * sy, pos.fY + tangent.fX * sy); 375 } 376 } 377 378 /* TODO 379 380 Need differentially more subdivisions when the follow-path is curvy. Not sure how to 381 determine that, but we need it. I guess a cheap answer is let the caller tell us, 382 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out. 383 */ 384 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas, 385 const SkMatrix& matrix) { 386 SkPath::Iter iter(src, false); 387 SkPoint srcP[4], dstP[3]; 388 SkPath::Verb verb; 389 390 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) { 391 switch (verb) { 392 case SkPath::kMove_Verb: 393 morphpoints(dstP, srcP, 1, meas, matrix); 394 dst->moveTo(dstP[0]); 395 break; 396 case SkPath::kLine_Verb: 397 // turn lines into quads to look bendy 398 srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX); 399 srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY); 400 morphpoints(dstP, srcP, 2, meas, matrix); 401 dst->quadTo(dstP[0], dstP[1]); 402 break; 403 case SkPath::kQuad_Verb: 404 morphpoints(dstP, &srcP[1], 2, meas, matrix); 405 dst->quadTo(dstP[0], dstP[1]); 406 break; 407 case SkPath::kConic_Verb: 408 morphpoints(dstP, &srcP[1], 2, meas, matrix); 409 dst->conicTo(dstP[0], dstP[1], iter.conicWeight()); 410 break; 411 case SkPath::kCubic_Verb: 412 morphpoints(dstP, &srcP[1], 3, meas, matrix); 413 dst->cubicTo(dstP[0], dstP[1], dstP[2]); 414 break; 415 case SkPath::kClose_Verb: 416 dst->close(); 417 break; 418 default: 419 SkDEBUGFAIL("unknown verb"); 420 break; 421 } 422 } 423 } 424 425 void SkBaseDevice::drawTextOnPath(const void* text, size_t byteLength, 426 const SkPath& follow, const SkMatrix* matrix, 427 const SkPaint& paint) { 428 SkASSERT(byteLength == 0 || text != nullptr); 429 430 // nothing to draw 431 if (text == nullptr || byteLength == 0) { 432 return; 433 } 434 435 SkTextToPathIter iter((const char*)text, byteLength, paint, true); 436 SkPathMeasure meas(follow, false); 437 SkScalar hOffset = 0; 438 439 // need to measure first 440 if (paint.getTextAlign() != SkPaint::kLeft_Align) { 441 SkScalar pathLen = meas.getLength(); 442 if (paint.getTextAlign() == SkPaint::kCenter_Align) { 443 pathLen = SkScalarHalf(pathLen); 444 } 445 hOffset += pathLen; 446 } 447 448 const SkPath* iterPath; 449 SkScalar xpos; 450 SkMatrix scaledMatrix; 451 SkScalar scale = iter.getPathScale(); 452 453 scaledMatrix.setScale(scale, scale); 454 455 while (iter.next(&iterPath, &xpos)) { 456 if (iterPath) { 457 SkPath tmp; 458 SkMatrix m(scaledMatrix); 459 460 tmp.setIsVolatile(true); 461 m.postTranslate(xpos + hOffset, 0); 462 if (matrix) { 463 m.postConcat(*matrix); 464 } 465 morphpath(&tmp, *iterPath, meas, m); 466 this->drawPath(tmp, iter.getPaint(), nullptr, true); 467 } 468 } 469 } 470 471 #include "SkUtils.h" 472 typedef int (*CountTextProc)(const char* text); 473 static int count_utf16(const char* text) { 474 const uint16_t* prev = (uint16_t*)text; 475 (void)SkUTF16_NextUnichar(&prev); 476 return SkToInt((const char*)prev - text); 477 } 478 static int return_4(const char* text) { return 4; } 479 static int return_2(const char* text) { return 2; } 480 481 void SkBaseDevice::drawTextRSXform(const void* text, size_t len, 482 const SkRSXform xform[], const SkPaint& paint) { 483 CountTextProc proc = nullptr; 484 switch (paint.getTextEncoding()) { 485 case SkPaint::kUTF8_TextEncoding: 486 proc = SkUTF8_CountUTF8Bytes; 487 break; 488 case SkPaint::kUTF16_TextEncoding: 489 proc = count_utf16; 490 break; 491 case SkPaint::kUTF32_TextEncoding: 492 proc = return_4; 493 break; 494 case SkPaint::kGlyphID_TextEncoding: 495 proc = return_2; 496 break; 497 } 498 499 SkPaint localPaint(paint); 500 SkShader* shader = paint.getShader(); 501 502 SkMatrix localM, currM; 503 const void* stopText = (const char*)text + len; 504 while ((const char*)text < (const char*)stopText) { 505 localM.setRSXform(*xform++); 506 currM.setConcat(this->ctm(), localM); 507 SkAutoDeviceCTMRestore adc(this, currM); 508 509 // We want to rotate each glyph by the rsxform, but we don't want to rotate "space" 510 // (i.e. the shader that cares about the ctm) so we have to undo our little ctm trick 511 // with a localmatrixshader so that the shader draws as if there was no change to the ctm. 512 if (shader) { 513 SkMatrix inverse; 514 SkAssertResult(localM.invert(&inverse)); 515 localPaint.setShader(shader->makeWithLocalMatrix(inverse)); 516 } 517 518 int subLen = proc((const char*)text); 519 this->drawText(text, subLen, 0, 0, localPaint); 520 text = (const char*)text + subLen; 521 } 522 } 523 524 ////////////////////////////////////////////////////////////////////////////////////////// 525 526 uint32_t SkBaseDevice::filterTextFlags(const SkPaint& paint) const { 527 uint32_t flags = paint.getFlags(); 528 529 if (!paint.isLCDRenderText() || !paint.isAntiAlias()) { 530 return flags; 531 } 532 533 if (kUnknown_SkPixelGeometry == fSurfaceProps.pixelGeometry() 534 || this->onShouldDisableLCD(paint)) { 535 536 flags &= ~SkPaint::kLCDRenderText_Flag; 537 flags |= SkPaint::kGenA8FromLCD_Flag; 538 } 539 540 return flags; 541 } 542 543 sk_sp<SkSurface> SkBaseDevice::makeSurface(SkImageInfo const&, SkSurfaceProps const&) { 544 return nullptr; 545 } 546 547 ////////////////////////////////////////////////////////////////////////////////////////// 548 549 void SkBaseDevice::LogDrawScaleFactor(const SkMatrix& matrix, SkFilterQuality filterQuality) { 550 #if SK_HISTOGRAMS_ENABLED 551 enum ScaleFactor { 552 kUpscale_ScaleFactor, 553 kNoScale_ScaleFactor, 554 kDownscale_ScaleFactor, 555 kLargeDownscale_ScaleFactor, 556 557 kLast_ScaleFactor = kLargeDownscale_ScaleFactor 558 }; 559 560 float rawScaleFactor = matrix.getMinScale(); 561 562 ScaleFactor scaleFactor; 563 if (rawScaleFactor < 0.5f) { 564 scaleFactor = kLargeDownscale_ScaleFactor; 565 } else if (rawScaleFactor < 1.0f) { 566 scaleFactor = kDownscale_ScaleFactor; 567 } else if (rawScaleFactor > 1.0f) { 568 scaleFactor = kUpscale_ScaleFactor; 569 } else { 570 scaleFactor = kNoScale_ScaleFactor; 571 } 572 573 switch (filterQuality) { 574 case kNone_SkFilterQuality: 575 SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.NoneFilterQuality", scaleFactor, 576 kLast_ScaleFactor + 1); 577 break; 578 case kLow_SkFilterQuality: 579 SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.LowFilterQuality", scaleFactor, 580 kLast_ScaleFactor + 1); 581 break; 582 case kMedium_SkFilterQuality: 583 SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.MediumFilterQuality", scaleFactor, 584 kLast_ScaleFactor + 1); 585 break; 586 case kHigh_SkFilterQuality: 587 SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.HighFilterQuality", scaleFactor, 588 kLast_ScaleFactor + 1); 589 break; 590 } 591 592 // Also log filter quality independent scale factor. 593 SK_HISTOGRAM_ENUMERATION("DrawScaleFactor.AnyFilterQuality", scaleFactor, 594 kLast_ScaleFactor + 1); 595 596 // Also log an overall histogram of filter quality. 597 SK_HISTOGRAM_ENUMERATION("FilterQuality", filterQuality, kLast_SkFilterQuality + 1); 598 #endif 599 } 600 601