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