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