void SkLinearBitmapPipeline::Stage<Base, kSize, Next>::initStage(Next* next, Args&& ... args) { SkASSERTF(sizeof(Variant) <= sizeof(fSpace), "Size Variant: %d, Space: %d", sizeof(Variant), sizeof(fSpace)); new (&fSpace) Variant(next, std::forward<Args>(args)...); fStageCloner = [this](Next* nextClone, void* addr) { new (addr) Variant(nextClone, (const Variant&)*this->get()); }; fIsInitialized = true; };
static void test_blender(std::string resourceName, skiatest::Reporter* reporter) { std::string fileName = resourceName + ".png"; sk_sp<SkImage> image = GetResourceAsImage(fileName.c_str()); if (image == nullptr) { ERRORF(reporter, "image is NULL"); return; } SkBitmap bm; if (!as_IB(image)->getROPixels(&bm)) { ERRORF(reporter, "Could not read resource"); return; } SkPixmap pixmap; bm.peekPixels(&pixmap); SkASSERTF(pixmap.colorType() == kN32_SkColorType, "colorType: %d", pixmap.colorType()); SkASSERT(pixmap.alphaType() != kUnpremul_SkAlphaType); const uint32_t* src = pixmap.addr32(); const int width = pixmap.rowBytesAsPixels(); SkASSERT(width > 0); SkASSERT(width < 4000); SkAutoTArray<uint32_t> correctDst(width); SkAutoTArray<uint32_t> testDst(width); for (int y = 0; y < pixmap.height(); y++) { // TODO: zero is not the most interesting dst to test srcover... sk_bzero(correctDst.get(), width * sizeof(uint32_t)); sk_bzero(testDst.get(), width * sizeof(uint32_t)); brute_force_srcover_srgb_srgb(correctDst.get(), src, width, width); SkOpts:: srcover_srgb_srgb( testDst.get(), src, width, width); for (int x = 0; x < width; x++) { REPORTER_ASSERT_MESSAGE( reporter, correctDst[x] == testDst[x], mismatch_message(resourceName, x, y, src[x], correctDst[x], testDst[x])); if (correctDst[x] != testDst[x]) break; } src += width; } }
static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) { #ifdef SK_DEBUG SkRect correct = *rect; #endif // crbug.com/373785 ~~> xPad = 4x yPad // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x const SkScalar yPad = 2.5f * paint.getTextSize(), xPad = 4.0f * yPad; rect->outset(xPad, yPad); #ifdef SK_DEBUG SkPaint::FontMetrics metrics; paint.getFontMetrics(&metrics); correct.fLeft += metrics.fXMin; correct.fTop += metrics.fTop; correct.fRight += metrics.fXMax; correct.fBottom += metrics.fBottom; // See skia:2862 for why we ignore small text sizes. SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct), "%f %f %f %f vs. %f %f %f %f\n", -xPad, -yPad, +xPad, +yPad, metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom); #endif }
static int valid_unit_divide(SkScalar numer, SkScalar denom, SkScalar* ratio) { SkASSERT(ratio); if (numer < 0) { numer = -numer; denom = -denom; } if (denom == 0 || numer == 0 || numer >= denom) { return 0; } SkScalar r = numer / denom; if (SkScalarIsNaN(r)) { return 0; } SkASSERTF(r >= 0 && r < SK_Scalar1, "numer %f, denom %f, r %f", numer, denom, r); if (r == 0) { // catch underflow if numer <<<< denom return 0; } *ratio = r; return 1; }
static void copy_v(void* dst, const S* src, int n, Rest&&... rest) { SkASSERTF(((uintptr_t)dst & (alignof(S)-1)) == 0, "Expected %p to be aligned for at least %zu bytes.", dst, alignof(S)); sk_careful_memcpy(dst, src, n*sizeof(S)); copy_v(SkTAddOffset<void>(dst, n*sizeof(S)), std::forward<Rest>(rest)...); }
void SkPicturePlayback::handleOp(SkReadBuffer* reader, DrawType op, uint32_t size, SkCanvas* canvas, const SkMatrix& initialMatrix) { switch (op) { case NOOP: { SkASSERT(size >= 4); reader->skip(size - 4); } break; case CLIP_PATH: { const SkPath& path = fPictureData->getPath(reader); uint32_t packed = reader->readInt(); SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); bool doAA = ClipParams_unpackDoAA(packed); size_t offsetToRestore = reader->readInt(); SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); canvas->clipPath(path, regionOp, doAA); if (canvas->isClipEmpty() && offsetToRestore) { reader->skip(offsetToRestore - reader->offset()); } } break; case CLIP_REGION: { SkRegion region; reader->readRegion(®ion); uint32_t packed = reader->readInt(); SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); size_t offsetToRestore = reader->readInt(); SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); canvas->clipRegion(region, regionOp); if (canvas->isClipEmpty() && offsetToRestore) { reader->skip(offsetToRestore - reader->offset()); } } break; case CLIP_RECT: { SkRect rect; reader->readRect(&rect); uint32_t packed = reader->readInt(); SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); bool doAA = ClipParams_unpackDoAA(packed); size_t offsetToRestore = reader->readInt(); SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); canvas->clipRect(rect, regionOp, doAA); if (canvas->isClipEmpty() && offsetToRestore) { reader->skip(offsetToRestore - reader->offset()); } } break; case CLIP_RRECT: { SkRRect rrect; reader->readRRect(&rrect); uint32_t packed = reader->readInt(); SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); bool doAA = ClipParams_unpackDoAA(packed); size_t offsetToRestore = reader->readInt(); SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); canvas->clipRRect(rrect, regionOp, doAA); if (canvas->isClipEmpty() && offsetToRestore) { reader->skip(offsetToRestore - reader->offset()); } } break; case PUSH_CULL: break; // Deprecated, safe to ignore both push and pop. case POP_CULL: break; case CONCAT: { SkMatrix matrix; reader->readMatrix(&matrix); canvas->concat(matrix); break; } case DRAW_ANNOTATION: { SkRect rect; reader->readRect(&rect); SkString key; reader->readString(&key); canvas->drawAnnotation(rect, key.c_str(), reader->readByteArrayAsData().get()); } break; case DRAW_ATLAS: { const SkPaint* paint = fPictureData->getPaint(reader); const SkImage* atlas = fPictureData->getImage(reader); const uint32_t flags = reader->readUInt(); const int count = reader->readUInt(); const SkRSXform* xform = (const SkRSXform*)reader->skip(count * sizeof(SkRSXform)); const SkRect* tex = (const SkRect*)reader->skip(count * sizeof(SkRect)); const SkColor* colors = nullptr; SkXfermode::Mode mode = SkXfermode::kDst_Mode; if (flags & DRAW_ATLAS_HAS_COLORS) { colors = (const SkColor*)reader->skip(count * sizeof(SkColor)); mode = (SkXfermode::Mode)reader->readUInt(); } const SkRect* cull = nullptr; if (flags & DRAW_ATLAS_HAS_CULL) { cull = (const SkRect*)reader->skip(sizeof(SkRect)); } canvas->drawAtlas(atlas, xform, tex, colors, count, mode, cull, paint); } break; case DRAW_BITMAP: { const SkPaint* paint = fPictureData->getPaint(reader); const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader)); SkPoint loc; reader->readPoint(&loc); canvas->drawBitmap(bitmap, loc.fX, loc.fY, paint); } break; case DRAW_BITMAP_RECT: { const SkPaint* paint = fPictureData->getPaint(reader); const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader)); SkRect storage; const SkRect* src = get_rect_ptr(reader, &storage); // may be null SkRect dst; reader->readRect(&dst); // required SkCanvas::SrcRectConstraint constraint = (SkCanvas::SrcRectConstraint)reader->readInt(); canvas->legacy_drawBitmapRect(bitmap, src, dst, paint, constraint); } break; case DRAW_BITMAP_MATRIX: { const SkPaint* paint = fPictureData->getPaint(reader); const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader)); SkMatrix matrix; reader->readMatrix(&matrix); SkAutoCanvasRestore acr(canvas, true); canvas->concat(matrix); canvas->drawBitmap(bitmap, 0, 0, paint); } break; case DRAW_BITMAP_NINE: { const SkPaint* paint = fPictureData->getPaint(reader); const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader)); SkIRect src; reader->readIRect(&src); SkRect dst; reader->readRect(&dst); canvas->drawBitmapNine(bitmap, src, dst, paint); } break; case DRAW_CLEAR: canvas->clear(reader->readInt()); break; case DRAW_DATA: { // This opcode is now dead, just need to skip it for backwards compatibility size_t length = reader->readInt(); (void)reader->skip(length); // skip handles padding the read out to a multiple of 4 } break; case DRAW_DRAWABLE: canvas->drawDrawable(fPictureData->getDrawable(reader)); break; case DRAW_DRAWABLE_MATRIX: { SkMatrix matrix; reader->readMatrix(&matrix); SkDrawable* drawable = fPictureData->getDrawable(reader); canvas->drawDrawable(drawable, &matrix); } break; case DRAW_DRRECT: { const SkPaint* paint = fPictureData->getPaint(reader); SkRRect outer, inner; reader->readRRect(&outer); reader->readRRect(&inner); if (paint) { canvas->drawDRRect(outer, inner, *paint); } } break; case BEGIN_COMMENT_GROUP: { SkString tmp; reader->readString(&tmp); // deprecated (M44) break; } case COMMENT: { SkString tmp; reader->readString(&tmp); reader->readString(&tmp); // deprecated (M44) break; } case END_COMMENT_GROUP: // deprecated (M44) break; case DRAW_IMAGE: { const SkPaint* paint = fPictureData->getPaint(reader); const SkImage* image = fPictureData->getImage(reader); SkPoint loc; reader->readPoint(&loc); canvas->drawImage(image, loc.fX, loc.fY, paint); } break; case DRAW_IMAGE_NINE: { const SkPaint* paint = fPictureData->getPaint(reader); const SkImage* image = fPictureData->getImage(reader); SkIRect center; reader->readIRect(¢er); SkRect dst; reader->readRect(&dst); canvas->drawImageNine(image, center, dst, paint); } break; case DRAW_IMAGE_RECT_STRICT: case DRAW_IMAGE_RECT: { const SkPaint* paint = fPictureData->getPaint(reader); const SkImage* image = fPictureData->getImage(reader); SkRect storage; const SkRect* src = get_rect_ptr(reader, &storage); // may be null SkRect dst; reader->readRect(&dst); // required // DRAW_IMAGE_RECT_STRICT assumes this constraint, and doesn't store it SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint; if (DRAW_IMAGE_RECT == op) { // newer op-code stores the constraint explicitly constraint = (SkCanvas::SrcRectConstraint)reader->readInt(); } canvas->legacy_drawImageRect(image, src, dst, paint, constraint); } break; case DRAW_OVAL: { const SkPaint* paint = fPictureData->getPaint(reader); SkRect rect; reader->readRect(&rect); if (paint) { canvas->drawOval(rect, *paint); } } break; case DRAW_PAINT: { const SkPaint* paint = fPictureData->getPaint(reader); if (paint) { canvas->drawPaint(*paint); } } break; case DRAW_PATCH: { const SkPaint* paint = fPictureData->getPaint(reader); const SkPoint* cubics = (const SkPoint*)reader->skip(SkPatchUtils::kNumCtrlPts * sizeof(SkPoint)); uint32_t flag = reader->readInt(); const SkColor* colors = nullptr; if (flag & DRAW_VERTICES_HAS_COLORS) { colors = (const SkColor*)reader->skip(SkPatchUtils::kNumCorners * sizeof(SkColor)); } const SkPoint* texCoords = nullptr; if (flag & DRAW_VERTICES_HAS_TEXS) { texCoords = (const SkPoint*)reader->skip(SkPatchUtils::kNumCorners * sizeof(SkPoint)); } sk_sp<SkXfermode> xfer; if (flag & DRAW_VERTICES_HAS_XFER) { int mode = reader->readInt(); if (mode < 0 || mode > SkXfermode::kLastMode) { mode = SkXfermode::kModulate_Mode; } xfer = SkXfermode::Make((SkXfermode::Mode)mode); } if (paint) { canvas->drawPatch(cubics, colors, texCoords, std::move(xfer), *paint); } } break; case DRAW_PATH: { const SkPaint* paint = fPictureData->getPaint(reader); if (paint) { canvas->drawPath(fPictureData->getPath(reader), *paint); } } break; case DRAW_PICTURE: canvas->drawPicture(fPictureData->getPicture(reader)); break; case DRAW_PICTURE_MATRIX_PAINT: { const SkPaint* paint = fPictureData->getPaint(reader); SkMatrix matrix; reader->readMatrix(&matrix); const SkPicture* pic = fPictureData->getPicture(reader); canvas->drawPicture(pic, &matrix, paint); } break; case DRAW_POINTS: { const SkPaint* paint = fPictureData->getPaint(reader); SkCanvas::PointMode mode = (SkCanvas::PointMode)reader->readInt(); size_t count = reader->readInt(); const SkPoint* pts = (const SkPoint*)reader->skip(sizeof(SkPoint)* count); if (paint) { canvas->drawPoints(mode, count, pts, *paint); } } break; case DRAW_POS_TEXT: { const SkPaint* paint = fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); size_t points = reader->readInt(); const SkPoint* pos = (const SkPoint*)reader->skip(points * sizeof(SkPoint)); if (paint) { canvas->drawPosText(text.text(), text.length(), pos, *paint); } } break; case DRAW_POS_TEXT_TOP_BOTTOM: { const SkPaint* paint = fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); size_t points = reader->readInt(); const SkPoint* pos = (const SkPoint*)reader->skip(points * sizeof(SkPoint)); const SkScalar top = reader->readScalar(); const SkScalar bottom = reader->readScalar(); if (!canvas->quickRejectY(top, bottom) && paint) { canvas->drawPosText(text.text(), text.length(), pos, *paint); } } break; case DRAW_POS_TEXT_H: { const SkPaint* paint = fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); size_t xCount = reader->readInt(); const SkScalar constY = reader->readScalar(); const SkScalar* xpos = (const SkScalar*)reader->skip(xCount * sizeof(SkScalar)); if (paint) { canvas->drawPosTextH(text.text(), text.length(), xpos, constY, *paint); } } break; case DRAW_POS_TEXT_H_TOP_BOTTOM: { const SkPaint* paint = fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); size_t xCount = reader->readInt(); const SkScalar* xpos = (const SkScalar*)reader->skip((3 + xCount) * sizeof(SkScalar)); const SkScalar top = *xpos++; const SkScalar bottom = *xpos++; const SkScalar constY = *xpos++; if (!canvas->quickRejectY(top, bottom) && paint) { canvas->drawPosTextH(text.text(), text.length(), xpos, constY, *paint); } } break; case DRAW_RECT: { const SkPaint* paint = fPictureData->getPaint(reader); SkRect rect; reader->readRect(&rect); if (paint) { canvas->drawRect(rect, *paint); } } break; case DRAW_RRECT: { const SkPaint* paint = fPictureData->getPaint(reader); SkRRect rrect; reader->readRRect(&rrect); if (paint) { canvas->drawRRect(rrect, *paint); } } break; case DRAW_SPRITE: { /* const SkPaint* paint = */ fPictureData->getPaint(reader); /* const SkBitmap bitmap = */ shallow_copy(fPictureData->getBitmap(reader)); /* int left = */ reader->readInt(); /* int top = */ reader->readInt(); // drawSprite removed dec-2015 } break; case DRAW_TEXT: { const SkPaint* paint = fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); SkScalar x = reader->readScalar(); SkScalar y = reader->readScalar(); if (paint) { canvas->drawText(text.text(), text.length(), x, y, *paint); } } break; case DRAW_TEXT_BLOB: { const SkPaint* paint = fPictureData->getPaint(reader); const SkTextBlob* blob = fPictureData->getTextBlob(reader); SkScalar x = reader->readScalar(); SkScalar y = reader->readScalar(); if (paint) { canvas->drawTextBlob(blob, x, y, *paint); } } break; case DRAW_TEXT_TOP_BOTTOM: { const SkPaint* paint = fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); const SkScalar* ptr = (const SkScalar*)reader->skip(4 * sizeof(SkScalar)); // ptr[0] == x // ptr[1] == y // ptr[2] == top // ptr[3] == bottom if (!canvas->quickRejectY(ptr[2], ptr[3]) && paint) { canvas->drawText(text.text(), text.length(), ptr[0], ptr[1], *paint); } } break; case DRAW_TEXT_ON_PATH: { const SkPaint* paint = fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); const SkPath& path = fPictureData->getPath(reader); SkMatrix matrix; reader->readMatrix(&matrix); if (paint) { canvas->drawTextOnPath(text.text(), text.length(), path, &matrix, *paint); } } break; case DRAW_TEXT_RSXFORM: { const SkPaint* paint = fPictureData->getPaint(reader); int count = reader->readInt(); uint32_t flags = reader->read32(); TextContainer text; get_text(reader, &text); const SkRSXform* xform = (const SkRSXform*)reader->skip(count * sizeof(SkRSXform)); const SkRect* cull = nullptr; if (flags & DRAW_TEXT_RSXFORM_HAS_CULL) { cull = (const SkRect*)reader->skip(sizeof(SkRect)); } canvas->drawTextRSXform(text.text(), text.length(), xform, cull, *paint); } break; case DRAW_VERTICES: { sk_sp<SkXfermode> xfer; const SkPaint* paint = fPictureData->getPaint(reader); DrawVertexFlags flags = (DrawVertexFlags)reader->readInt(); SkCanvas::VertexMode vmode = (SkCanvas::VertexMode)reader->readInt(); int vCount = reader->readInt(); const SkPoint* verts = (const SkPoint*)reader->skip(vCount * sizeof(SkPoint)); const SkPoint* texs = nullptr; const SkColor* colors = nullptr; const uint16_t* indices = nullptr; int iCount = 0; if (flags & DRAW_VERTICES_HAS_TEXS) { texs = (const SkPoint*)reader->skip(vCount * sizeof(SkPoint)); } if (flags & DRAW_VERTICES_HAS_COLORS) { colors = (const SkColor*)reader->skip(vCount * sizeof(SkColor)); } if (flags & DRAW_VERTICES_HAS_INDICES) { iCount = reader->readInt(); indices = (const uint16_t*)reader->skip(iCount * sizeof(uint16_t)); } if (flags & DRAW_VERTICES_HAS_XFER) { int mode = reader->readInt(); if (mode < 0 || mode > SkXfermode::kLastMode) { mode = SkXfermode::kModulate_Mode; } xfer = SkXfermode::Make((SkXfermode::Mode)mode); } if (paint) { canvas->drawVertices(vmode, vCount, verts, texs, colors, xfer, indices, iCount, *paint); } } break; case RESTORE: canvas->restore(); break; case ROTATE: canvas->rotate(reader->readScalar()); break; case SAVE: // SKPs with version < 29 also store a SaveFlags param. if (size > 4) { if (reader->validate(8 == size)) { reader->readInt(); } } canvas->save(); break; case SAVE_LAYER_SAVEFLAGS_DEPRECATED: { SkRect storage; const SkRect* boundsPtr = get_rect_ptr(reader, &storage); const SkPaint* paint = fPictureData->getPaint(reader); auto flags = SkCanvas::LegacySaveFlagsToSaveLayerFlags(reader->readInt()); canvas->saveLayer(SkCanvas::SaveLayerRec(boundsPtr, paint, flags)); } break; case SAVE_LAYER_SAVELAYERFLAGS_DEPRECATED_JAN_2016: { SkRect storage; const SkRect* boundsPtr = get_rect_ptr(reader, &storage); const SkPaint* paint = fPictureData->getPaint(reader); canvas->saveLayer(SkCanvas::SaveLayerRec(boundsPtr, paint, reader->readInt())); } break; case SAVE_LAYER_SAVELAYERREC: { SkCanvas::SaveLayerRec rec(nullptr, nullptr, nullptr, 0); const uint32_t flatFlags = reader->readInt(); SkRect bounds; if (flatFlags & SAVELAYERREC_HAS_BOUNDS) { reader->readRect(&bounds); rec.fBounds = &bounds; } if (flatFlags & SAVELAYERREC_HAS_PAINT) { rec.fPaint = fPictureData->getPaint(reader); } if (flatFlags & SAVELAYERREC_HAS_BACKDROP) { const SkPaint* paint = fPictureData->getPaint(reader); rec.fBackdrop = paint->getImageFilter(); } if (flatFlags & SAVELAYERREC_HAS_FLAGS) { rec.fSaveLayerFlags = reader->readInt(); } canvas->saveLayer(rec); } break; case SCALE: { SkScalar sx = reader->readScalar(); SkScalar sy = reader->readScalar(); canvas->scale(sx, sy); } break; case SET_MATRIX: { SkMatrix matrix; reader->readMatrix(&matrix); matrix.postConcat(initialMatrix); canvas->setMatrix(matrix); } break; case SKEW: { SkScalar sx = reader->readScalar(); SkScalar sy = reader->readScalar(); canvas->skew(sx, sy); } break; case TRANSLATE: { SkScalar dx = reader->readScalar(); SkScalar dy = reader->readScalar(); canvas->translate(dx, dy); } break; default: SkASSERTF(false, "Unknown draw type: %d", op); } }
void SkLinearBitmapPipeline::Stage<Base, kSize, Next>::initSink(Args&& ... args) { SkASSERTF(sizeof(Variant) <= sizeof(fSpace), "Size Variant: %d, Space: %d", sizeof(Variant), sizeof(fSpace)); new (&fSpace) Variant(std::forward<Args>(args)...); fIsInitialized = true; };
void SkPicturePlayback::handleOp(SkReader32* reader, DrawType op, uint32_t size, SkCanvas* canvas, const SkMatrix& initialMatrix) { switch (op) { case NOOP: { SkASSERT(size >= 4); reader->skip(size - 4); } break; case CLIP_PATH: { const SkPath& path = fPictureData->getPath(reader); uint32_t packed = reader->readInt(); SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); bool doAA = ClipParams_unpackDoAA(packed); size_t offsetToRestore = reader->readInt(); SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); canvas->clipPath(path, regionOp, doAA); if (canvas->isClipEmpty() && offsetToRestore) { reader->setOffset(offsetToRestore); } } break; case CLIP_REGION: { SkRegion region; reader->readRegion(®ion); uint32_t packed = reader->readInt(); SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); size_t offsetToRestore = reader->readInt(); SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); canvas->clipRegion(region, regionOp); if (canvas->isClipEmpty() && offsetToRestore) { reader->setOffset(offsetToRestore); } } break; case CLIP_RECT: { const SkRect& rect = reader->skipT<SkRect>(); uint32_t packed = reader->readInt(); SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); bool doAA = ClipParams_unpackDoAA(packed); size_t offsetToRestore = reader->readInt(); SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); canvas->clipRect(rect, regionOp, doAA); if (canvas->isClipEmpty() && offsetToRestore) { reader->setOffset(offsetToRestore); } } break; case CLIP_RRECT: { SkRRect rrect; reader->readRRect(&rrect); uint32_t packed = reader->readInt(); SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed); bool doAA = ClipParams_unpackDoAA(packed); size_t offsetToRestore = reader->readInt(); SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset()); canvas->clipRRect(rrect, regionOp, doAA); if (canvas->isClipEmpty() && offsetToRestore) { reader->setOffset(offsetToRestore); } } break; case PUSH_CULL: break; // Deprecated, safe to ignore both push and pop. case POP_CULL: break; case CONCAT: { SkMatrix matrix; reader->readMatrix(&matrix); canvas->concat(matrix); break; } case DRAW_BITMAP: { const SkPaint* paint = fPictureData->getPaint(reader); const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader)); const SkPoint& loc = reader->skipT<SkPoint>(); canvas->drawBitmap(bitmap, loc.fX, loc.fY, paint); } break; case DRAW_BITMAP_RECT_TO_RECT: { const SkPaint* paint = fPictureData->getPaint(reader); const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader)); const SkRect* src = get_rect_ptr(reader); // may be null const SkRect& dst = reader->skipT<SkRect>(); // required SkCanvas::DrawBitmapRectFlags flags; flags = (SkCanvas::DrawBitmapRectFlags) reader->readInt(); canvas->drawBitmapRectToRect(bitmap, src, dst, paint, flags); } break; case DRAW_BITMAP_MATRIX: { const SkPaint* paint = fPictureData->getPaint(reader); const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader)); SkMatrix matrix; reader->readMatrix(&matrix); SkAutoCanvasRestore acr(canvas, true); canvas->concat(matrix); canvas->drawBitmap(bitmap, 0, 0, paint); } break; case DRAW_BITMAP_NINE: { const SkPaint* paint = fPictureData->getPaint(reader); const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader)); const SkIRect& src = reader->skipT<SkIRect>(); const SkRect& dst = reader->skipT<SkRect>(); canvas->drawBitmapNine(bitmap, src, dst, paint); } break; case DRAW_CLEAR: canvas->clear(reader->readInt()); break; case DRAW_DATA: { size_t length = reader->readInt(); canvas->drawData(reader->skip(length), length); // skip handles padding the read out to a multiple of 4 } break; case DRAW_DRRECT: { const SkPaint& paint = *fPictureData->getPaint(reader); SkRRect outer, inner; reader->readRRect(&outer); reader->readRRect(&inner); canvas->drawDRRect(outer, inner, paint); } break; case BEGIN_COMMENT_GROUP: { const char* desc = reader->readString(); canvas->beginCommentGroup(desc); } break; case COMMENT: { const char* kywd = reader->readString(); const char* value = reader->readString(); canvas->addComment(kywd, value); } break; case END_COMMENT_GROUP: { canvas->endCommentGroup(); } break; case DRAW_OVAL: { const SkPaint& paint = *fPictureData->getPaint(reader); canvas->drawOval(reader->skipT<SkRect>(), paint); } break; case DRAW_PAINT: canvas->drawPaint(*fPictureData->getPaint(reader)); break; case DRAW_PATCH: { const SkPaint& paint = *fPictureData->getPaint(reader); const SkPoint* cubics = (const SkPoint*)reader->skip(SkPatchUtils::kNumCtrlPts * sizeof(SkPoint)); uint32_t flag = reader->readInt(); const SkColor* colors = NULL; if (flag & DRAW_VERTICES_HAS_COLORS) { colors = (const SkColor*)reader->skip(SkPatchUtils::kNumCorners * sizeof(SkColor)); } const SkPoint* texCoords = NULL; if (flag & DRAW_VERTICES_HAS_TEXS) { texCoords = (const SkPoint*)reader->skip(SkPatchUtils::kNumCorners * sizeof(SkPoint)); } SkAutoTUnref<SkXfermode> xfer; if (flag & DRAW_VERTICES_HAS_XFER) { int mode = reader->readInt(); if (mode < 0 || mode > SkXfermode::kLastMode) { mode = SkXfermode::kModulate_Mode; } xfer.reset(SkXfermode::Create((SkXfermode::Mode)mode)); } canvas->drawPatch(cubics, colors, texCoords, xfer, paint); } break; case DRAW_PATH: { const SkPaint& paint = *fPictureData->getPaint(reader); canvas->drawPath(fPictureData->getPath(reader), paint); } break; case DRAW_PICTURE: canvas->drawPicture(fPictureData->getPicture(reader)); break; case DRAW_PICTURE_MATRIX_PAINT: { const SkPaint* paint = fPictureData->getPaint(reader); SkMatrix matrix; reader->readMatrix(&matrix); const SkPicture* pic = fPictureData->getPicture(reader); canvas->drawPicture(pic, &matrix, paint); } break; case DRAW_POINTS: { const SkPaint& paint = *fPictureData->getPaint(reader); SkCanvas::PointMode mode = (SkCanvas::PointMode)reader->readInt(); size_t count = reader->readInt(); const SkPoint* pts = (const SkPoint*)reader->skip(sizeof(SkPoint)* count); canvas->drawPoints(mode, count, pts, paint); } break; case DRAW_POS_TEXT: { const SkPaint& paint = *fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); size_t points = reader->readInt(); const SkPoint* pos = (const SkPoint*)reader->skip(points * sizeof(SkPoint)); canvas->drawPosText(text.text(), text.length(), pos, paint); } break; case DRAW_POS_TEXT_TOP_BOTTOM: { const SkPaint& paint = *fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); size_t points = reader->readInt(); const SkPoint* pos = (const SkPoint*)reader->skip(points * sizeof(SkPoint)); const SkScalar top = reader->readScalar(); const SkScalar bottom = reader->readScalar(); if (!canvas->quickRejectY(top, bottom)) { canvas->drawPosText(text.text(), text.length(), pos, paint); } } break; case DRAW_POS_TEXT_H: { const SkPaint& paint = *fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); size_t xCount = reader->readInt(); const SkScalar constY = reader->readScalar(); const SkScalar* xpos = (const SkScalar*)reader->skip(xCount * sizeof(SkScalar)); canvas->drawPosTextH(text.text(), text.length(), xpos, constY, paint); } break; case DRAW_POS_TEXT_H_TOP_BOTTOM: { const SkPaint& paint = *fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); size_t xCount = reader->readInt(); const SkScalar* xpos = (const SkScalar*)reader->skip((3 + xCount) * sizeof(SkScalar)); const SkScalar top = *xpos++; const SkScalar bottom = *xpos++; const SkScalar constY = *xpos++; if (!canvas->quickRejectY(top, bottom)) { canvas->drawPosTextH(text.text(), text.length(), xpos, constY, paint); } } break; case DRAW_RECT: { const SkPaint& paint = *fPictureData->getPaint(reader); canvas->drawRect(reader->skipT<SkRect>(), paint); } break; case DRAW_RRECT: { const SkPaint& paint = *fPictureData->getPaint(reader); SkRRect rrect; reader->readRRect(&rrect); canvas->drawRRect(rrect, paint); } break; case DRAW_SPRITE: { const SkPaint* paint = fPictureData->getPaint(reader); const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader)); int left = reader->readInt(); int top = reader->readInt(); canvas->drawSprite(bitmap, left, top, paint); } break; case DRAW_TEXT: { const SkPaint& paint = *fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); SkScalar x = reader->readScalar(); SkScalar y = reader->readScalar(); canvas->drawText(text.text(), text.length(), x, y, paint); } break; case DRAW_TEXT_BLOB: { const SkPaint& paint = *fPictureData->getPaint(reader); const SkTextBlob* blob = fPictureData->getTextBlob(reader); SkScalar x = reader->readScalar(); SkScalar y = reader->readScalar(); canvas->drawTextBlob(blob, x, y, paint); } break; case DRAW_TEXT_TOP_BOTTOM: { const SkPaint& paint = *fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); const SkScalar* ptr = (const SkScalar*)reader->skip(4 * sizeof(SkScalar)); // ptr[0] == x // ptr[1] == y // ptr[2] == top // ptr[3] == bottom if (!canvas->quickRejectY(ptr[2], ptr[3])) { canvas->drawText(text.text(), text.length(), ptr[0], ptr[1], paint); } } break; case DRAW_TEXT_ON_PATH: { const SkPaint& paint = *fPictureData->getPaint(reader); TextContainer text; get_text(reader, &text); const SkPath& path = fPictureData->getPath(reader); SkMatrix matrix; reader->readMatrix(&matrix); canvas->drawTextOnPath(text.text(), text.length(), path, &matrix, paint); } break; case DRAW_VERTICES: { SkAutoTUnref<SkXfermode> xfer; const SkPaint& paint = *fPictureData->getPaint(reader); DrawVertexFlags flags = (DrawVertexFlags)reader->readInt(); SkCanvas::VertexMode vmode = (SkCanvas::VertexMode)reader->readInt(); int vCount = reader->readInt(); const SkPoint* verts = (const SkPoint*)reader->skip(vCount * sizeof(SkPoint)); const SkPoint* texs = NULL; const SkColor* colors = NULL; const uint16_t* indices = NULL; int iCount = 0; if (flags & DRAW_VERTICES_HAS_TEXS) { texs = (const SkPoint*)reader->skip(vCount * sizeof(SkPoint)); } if (flags & DRAW_VERTICES_HAS_COLORS) { colors = (const SkColor*)reader->skip(vCount * sizeof(SkColor)); } if (flags & DRAW_VERTICES_HAS_INDICES) { iCount = reader->readInt(); indices = (const uint16_t*)reader->skip(iCount * sizeof(uint16_t)); } if (flags & DRAW_VERTICES_HAS_XFER) { int mode = reader->readInt(); if (mode < 0 || mode > SkXfermode::kLastMode) { mode = SkXfermode::kModulate_Mode; } xfer.reset(SkXfermode::Create((SkXfermode::Mode)mode)); } canvas->drawVertices(vmode, vCount, verts, texs, colors, xfer, indices, iCount, paint); } break; case RESTORE: canvas->restore(); break; case ROTATE: canvas->rotate(reader->readScalar()); break; case SAVE: // SKPs with version < 29 also store a SaveFlags param. if (size > 4) { SkASSERT(8 == size); reader->readInt(); } canvas->save(); break; case SAVE_LAYER: { const SkRect* boundsPtr = get_rect_ptr(reader); const SkPaint* paint = fPictureData->getPaint(reader); canvas->saveLayer(boundsPtr, paint, (SkCanvas::SaveFlags) reader->readInt()); } break; case SCALE: { SkScalar sx = reader->readScalar(); SkScalar sy = reader->readScalar(); canvas->scale(sx, sy); } break; case SET_MATRIX: { SkMatrix matrix; reader->readMatrix(&matrix); matrix.postConcat(initialMatrix); canvas->setMatrix(matrix); } break; case SKEW: { SkScalar sx = reader->readScalar(); SkScalar sy = reader->readScalar(); canvas->skew(sx, sy); } break; case TRANSLATE: { SkScalar dx = reader->readScalar(); SkScalar dy = reader->readScalar(); canvas->translate(dx, dy); } break; default: SkASSERTF(false, "Unknown draw type: %d", op); } }
/** * Copies a FT_Bitmap into an SkMask with the same dimensions. * * Yes, No, Never Requested, Never Produced * * kBW kA8 k3D kARGB32 kLCD16 * FT_PIXEL_MODE_MONO Y Y NR N Y * FT_PIXEL_MODE_GRAY N Y NR N Y * FT_PIXEL_MODE_GRAY2 NP NP NR NP NP * FT_PIXEL_MODE_GRAY4 NP NP NR NP NP * FT_PIXEL_MODE_LCD NP NP NR NP NP * FT_PIXEL_MODE_LCD_V NP NP NR NP NP * FT_PIXEL_MODE_BGRA N N NR Y N * * TODO: All of these N need to be Y or otherwise ruled out. */ static void copyFTBitmap(const FT_Bitmap& srcFTBitmap, SkMask& dstMask) { SkASSERTF(dstMask.fBounds.width() == static_cast<int>(srcFTBitmap.width), "dstMask.fBounds.width() = %d\n" "static_cast<int>(srcFTBitmap.width) = %d", dstMask.fBounds.width(), static_cast<int>(srcFTBitmap.width) ); SkASSERTF(dstMask.fBounds.height() == static_cast<int>(srcFTBitmap.rows), "dstMask.fBounds.height() = %d\n" "static_cast<int>(srcFTBitmap.rows) = %d", dstMask.fBounds.height(), static_cast<int>(srcFTBitmap.rows) ); const uint8_t* src = reinterpret_cast<const uint8_t*>(srcFTBitmap.buffer); const FT_Pixel_Mode srcFormat = static_cast<FT_Pixel_Mode>(srcFTBitmap.pixel_mode); // FT_Bitmap::pitch is an int and allowed to be negative. const int srcPitch = srcFTBitmap.pitch; const size_t srcRowBytes = SkTAbs(srcPitch); uint8_t* dst = dstMask.fImage; const SkMask::Format dstFormat = static_cast<SkMask::Format>(dstMask.fFormat); const size_t dstRowBytes = dstMask.fRowBytes; const size_t width = srcFTBitmap.width; const size_t height = srcFTBitmap.rows; if (SkMask::kLCD16_Format == dstFormat) { copyFT2LCD16<false>(srcFTBitmap, dstMask, false, nullptr, nullptr, nullptr); return; } if ((FT_PIXEL_MODE_MONO == srcFormat && SkMask::kBW_Format == dstFormat) || (FT_PIXEL_MODE_GRAY == srcFormat && SkMask::kA8_Format == dstFormat)) { size_t commonRowBytes = SkTMin(srcRowBytes, dstRowBytes); for (size_t y = height; y --> 0;) { memcpy(dst, src, commonRowBytes); src += srcPitch; dst += dstRowBytes; } } else if (FT_PIXEL_MODE_MONO == srcFormat && SkMask::kA8_Format == dstFormat) { for (size_t y = height; y --> 0;) { uint8_t byte = 0; int bits = 0; const uint8_t* src_row = src; uint8_t* dst_row = dst; for (size_t x = width; x --> 0;) { if (0 == bits) { byte = *src_row++; bits = 8; } *dst_row++ = byte & 0x80 ? 0xff : 0x00; bits--; byte <<= 1; } src += srcPitch; dst += dstRowBytes; } } else if (FT_PIXEL_MODE_BGRA == srcFormat && SkMask::kARGB32_Format == dstFormat) { // FT_PIXEL_MODE_BGRA is pre-multiplied. for (size_t y = height; y --> 0;) { const uint8_t* src_row = src; SkPMColor* dst_row = reinterpret_cast<SkPMColor*>(dst); for (size_t x = 0; x < width; ++x) { uint8_t b = *src_row++; uint8_t g = *src_row++; uint8_t r = *src_row++; uint8_t a = *src_row++; *dst_row++ = SkPackARGB32(a, r, g, b); #ifdef SK_SHOW_TEXT_BLIT_COVERAGE *(dst_row-1) = SkFourByteInterp256(*(dst_row-1), SK_ColorWHITE, 0x40); #endif } src += srcPitch; dst += dstRowBytes; } } else { SkDEBUGF(("FT_Pixel_Mode %d, SkMask::Format %d\n", srcFormat, dstFormat)); SkDEBUGFAIL("unsupported combination of FT_Pixel_Mode and SkMask::Format"); } }