void drawHere(SkCanvas* canvas, SkFilterQuality filter, SkScalar dx, SkScalar dy) { SkCanvas* origCanvas = canvas; SkAutoCanvasRestore acr(canvas, true); SkISize size = SkISize::Make(fImage->width(), fImage->height()); SkAutoTUnref<SkSurface> surface; if (fShowFatBits) { // scale up so we don't clip rotations SkImageInfo info = SkImageInfo::MakeN32(fImage->width() * 2, fImage->height() * 2, kOpaque_SkAlphaType); surface.reset(make_surface(canvas, info)); canvas = surface->getCanvas(); canvas->drawColor(SK_ColorWHITE); size.set(info.width(), info.height()); } else { canvas->translate(SkScalarHalf(fCell.width() - fImage->width()), SkScalarHalf(fCell.height() - fImage->height())); } this->drawTheImage(canvas, size, filter, dx, dy); if (surface) { SkAutoTUnref<SkImage> orig(surface->newImageSnapshot()); SkAutoTUnref<SkImage> zoomed(zoom_up(orig)); origCanvas->drawImage(zoomed, SkScalarHalf(fCell.width() - zoomed->width()), SkScalarHalf(fCell.height() - zoomed->height())); } }
int checkOval(int* flatCount, int* buldgeCount) { int flatc = 0; int buldgec = 0; const SkScalar rad = SkScalarHalf(fSize.width()); SkScalar cx = SkScalarHalf(fSize.width()); SkScalar cy = SkScalarHalf(fSize.height()); for (int y = 0; y < kILimit; y++) { for (int x = 0; x < kILimit; x++) { // measure from pixel centers SkScalar px = SkIntToScalar(x) + SK_ScalarHalf; SkScalar py = SkIntToScalar(y) + SK_ScalarHalf; SkPMColor* ptr = fBitmap.getAddr32(x, y); SkScalar dist = SkPoint::Length(px - cx, py - cy); if (dist <= rad && !*ptr) { flatc++; *ptr = fInsideColor; } else if (dist > rad && *ptr) { buldgec++; *ptr = fOutsideColor; } } } if (flatCount) *flatCount = flatc; if (buldgeCount) *buldgeCount = buldgec; return flatc + buldgec; }
SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) const { SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0); SkMatrix m; m.setConcat(matrix, this->getLocalMatrix()); if (localM) { m.preConcat(*localM); } // Use a rotation-invariant scale SkPoint scale; if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { // Decomposition failed, use an approximation. scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()), SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY())); } SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() * fPicture->height()); SkISize tileSize = scaledSize.toRound(); if (tileSize.isEmpty()) { return NULL; } // The actual scale, compensating for rounding. SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->width(), SkIntToScalar(tileSize.height()) / fPicture->height()); SkAutoMutexAcquire ama(fCachedBitmapShaderMutex); if (!fCachedBitmapShader || tileScale != fCachedTileScale) { SkBitmap bm; if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { return NULL; } bm.eraseColor(SK_ColorTRANSPARENT); SkCanvas canvas(bm); canvas.scale(tileScale.width(), tileScale.height()); canvas.drawPicture(fPicture); fCachedTileScale = tileScale; SkMatrix shaderMatrix = this->getLocalMatrix(); shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix)); } // Increment the ref counter inside the mutex to ensure the returned pointer is still valid. // Otherwise, the pointer may have been overwritten on a different thread before the object's // ref count was incremented. fCachedBitmapShader.get()->ref(); return fCachedBitmapShader; }
/* * Modulo internal errors, this should always succeed *if* the matrix is downscaling * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscaling) */ bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmapProvider& provider) { SkASSERT(fQuality <= kMedium_SkFilterQuality); if (fQuality != kMedium_SkFilterQuality) { return false; } // Our default return state is to downgrade the request to Low, w/ or w/o setting fBitmap // to a valid bitmap. fQuality = kLow_SkFilterQuality; SkSize invScaleSize; if (!fInvMatrix.decomposeScale(&invScaleSize, nullptr)) { return false; } // Use the largest (non-inverse) scale, to ensure anisotropic consistency. SkASSERT(invScaleSize.width() >= 0 && invScaleSize.height() >= 0); const SkScalar invScale = SkTMin(invScaleSize.width(), invScaleSize.height()); if (invScale > SK_Scalar1) { fCurrMip.reset(SkMipMapCache::FindAndRef(provider.makeCacheDesc())); if (nullptr == fCurrMip.get()) { SkBitmap orig; if (!provider.asBitmap(&orig)) { return false; } fCurrMip.reset(SkMipMapCache::AddAndRef(orig)); if (nullptr == fCurrMip.get()) { return false; } } // diagnostic for a crasher... if (nullptr == fCurrMip->data()) { sk_throw(); } SkScalar levelScale = SkScalarInvert(invScale); SkMipMap::Level level; if (fCurrMip->extractLevel(levelScale, &level)) { const SkSize& invScaleFixup = level.fScale; fInvMatrix.postScale(invScaleFixup.width(), invScaleFixup.height()); // todo: if we could wrap the fCurrMip in a pixelref, then we could just install // that here, and not need to explicitly track it ourselves. return fResultBitmap.installPixels(level.fPixmap); } else { // failed to extract, so release the mipmap fCurrMip.reset(nullptr); } } return false; }
static bool check_decompScale(const SkMatrix& matrix) { SkSize scale; SkMatrix remaining; if (!matrix.decomposeScale(&scale, &remaining)) { return false; } if (scale.width() <= 0 || scale.height() <= 0) { return false; } remaining.preScale(scale.width(), scale.height()); return nearly_equal(matrix, remaining); }
/* * Modulo internal errors, this should always succeed *if* the matrix is downscaling * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscaling) */ bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmapProvider& provider) { SkASSERT(fQuality <= kMedium_SkFilterQuality); if (fQuality != kMedium_SkFilterQuality) { return false; } // Our default return state is to downgrade the request to Low, w/ or w/o setting fBitmap // to a valid bitmap. fQuality = kLow_SkFilterQuality; SkSize invScaleSize; if (!fInvMatrix.decomposeScale(&invScaleSize, nullptr)) { return false; } SkDestinationSurfaceColorMode colorMode = provider.dstColorSpace() ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware : SkDestinationSurfaceColorMode::kLegacy; if (invScaleSize.width() > SK_Scalar1 || invScaleSize.height() > SK_Scalar1) { fCurrMip.reset(SkMipMapCache::FindAndRef(provider.makeCacheDesc(), colorMode)); if (nullptr == fCurrMip.get()) { SkBitmap orig; if (!provider.asBitmap(&orig)) { return false; } fCurrMip.reset(SkMipMapCache::AddAndRef(orig, colorMode)); if (nullptr == fCurrMip.get()) { return false; } } // diagnostic for a crasher... SkASSERT_RELEASE(fCurrMip->data()); const SkSize scale = SkSize::Make(SkScalarInvert(invScaleSize.width()), SkScalarInvert(invScaleSize.height())); SkMipMap::Level level; if (fCurrMip->extractLevel(scale, &level)) { const SkSize& invScaleFixup = level.fScale; fInvMatrix.postScale(invScaleFixup.width(), invScaleFixup.height()); // todo: if we could wrap the fCurrMip in a pixelref, then we could just install // that here, and not need to explicitly track it ourselves. return fResultBitmap.installPixels(level.fPixmap); } else { // failed to extract, so release the mipmap fCurrMip.reset(nullptr); } } return false; }
void draw(SkCanvas* canvas, const SkRect& rect, const SkSize& deviceSize, SkPaint::FilterLevel filterLevel, SkImageFilter* input = NULL) { SkRect dstRect; canvas->getTotalMatrix().mapRect(&dstRect, rect); canvas->save(); SkScalar deviceScaleX = SkScalarDiv(deviceSize.width(), dstRect.width()); SkScalar deviceScaleY = SkScalarDiv(deviceSize.height(), dstRect.height()); canvas->translate(rect.x(), rect.y()); canvas->scale(deviceScaleX, deviceScaleY); canvas->translate(-rect.x(), -rect.y()); SkMatrix matrix; matrix.setScale(SkScalarInvert(deviceScaleX), SkScalarInvert(deviceScaleY)); SkAutoTUnref<SkImageFilter> imageFilter( SkMatrixImageFilter::Create(matrix, filterLevel, input)); SkPaint filteredPaint; filteredPaint.setImageFilter(imageFilter.get()); canvas->saveLayer(&rect, &filteredPaint); SkPaint paint; paint.setColor(0xFF00FF00); SkRect ovalRect = rect; ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4)); canvas->drawOval(ovalRect, paint); canvas->restore(); // for saveLayer canvas->restore(); }
static SkVector map_sigma(const SkSize& localSigma, const SkMatrix& ctm) { SkVector sigma = SkVector::Make(localSigma.width(), localSigma.height()); ctm.mapVectors(&sigma, 1); sigma.fX = SkMinScalar(SkScalarAbs(sigma.fX), MAX_SIGMA); sigma.fY = SkMinScalar(SkScalarAbs(sigma.fY), MAX_SIGMA); return sigma; }
bool SkBitmapController::State::processHighRequest(const SkBitmapProvider& provider) { if (fQuality != kHigh_SkFilterQuality) { return false; } fQuality = kMedium_SkFilterQuality; SkScalar invScaleX = fInvMatrix.getScaleX(); SkScalar invScaleY = fInvMatrix.getScaleY(); if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) { SkSize scale; if (!fInvMatrix.decomposeScale(&scale)) { return false; } invScaleX = scale.width(); invScaleY = scale.height(); } invScaleX = SkScalarAbs(invScaleX); invScaleY = SkScalarAbs(invScaleY); if (invScaleX >= 1 - SK_ScalarNearlyZero || invScaleY >= 1 - SK_ScalarNearlyZero) { // we're down-scaling so abort HQ return false; } // Confirmed that we can use HQ (w/ rasterpipeline) fQuality = kHigh_SkFilterQuality; (void)provider.asBitmap(&fResultBitmap); return true; }
/* * High quality is implemented by performing up-right scale-only filtering and then * using bilerp for any remaining transformations. */ bool SkDefaultBitmapControllerState::processHQRequest(const SkBitmap& origBitmap) { if (fQuality != kHigh_SkFilterQuality) { return false; } // Our default return state is to downgrade the request to Medium, w/ or w/o setting fBitmap // to a valid bitmap. If we succeed, we will set this to Low instead. fQuality = kMedium_SkFilterQuality; if (kN32_SkColorType != origBitmap.colorType() || !cache_size_okay(origBitmap, fInvMatrix) || fInvMatrix.hasPerspective()) { return false; // can't handle the reqeust } SkScalar invScaleX = fInvMatrix.getScaleX(); SkScalar invScaleY = fInvMatrix.getScaleY(); if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) { SkSize scale; if (!fInvMatrix.decomposeScale(&scale)) { return false; } invScaleX = scale.width(); invScaleY = scale.height(); } if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1)) { return false; // no need for HQ } SkScalar trueDestWidth = origBitmap.width() / invScaleX; SkScalar trueDestHeight = origBitmap.height() / invScaleY; SkScalar roundedDestWidth = SkScalarRoundToScalar(trueDestWidth); SkScalar roundedDestHeight = SkScalarRoundToScalar(trueDestHeight); if (!SkBitmapCache::Find(origBitmap, roundedDestWidth, roundedDestHeight, &fResultBitmap)) { SkAutoPixmapUnlock src; if (!origBitmap.requestLock(&src)) { return false; } if (!SkBitmapScaler::Resize(&fResultBitmap, src.pixmap(), SkBitmapScaler::RESIZE_BEST, roundedDestWidth, roundedDestHeight, SkResourceCache::GetAllocator())) { return false; // we failed to create fScaledBitmap } SkASSERT(fResultBitmap.getPixels()); fResultBitmap.setImmutable(); SkBitmapCache::Add(origBitmap, roundedDestWidth, roundedDestHeight, fResultBitmap); } SkASSERT(fResultBitmap.getPixels()); fInvMatrix.postScale(roundedDestWidth / origBitmap.width(), roundedDestHeight / origBitmap.height()); fQuality = kLow_SkFilterQuality; return true; }
bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const { if (nullptr == fLevels) { return false; } SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0); #ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE // Use the smallest scale to match the GPU impl. const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height()); #else // Ideally we'd pick the smaller scale, to match Ganesh. But ignoring one of the // scales can produce some atrocious results, so for now we use the geometric mean. // (https://bugs.chromium.org/p/skia/issues/detail?id=4863) const SkScalar scale = SkScalarSqrt(scaleSize.width() * scaleSize.height()); #endif if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) { return false; } SkScalar L = -SkScalarLog2(scale); if (!SkScalarIsFinite(L)) { return false; } SkASSERT(L >= 0); int level = SkScalarFloorToInt(L); SkASSERT(level >= 0); if (level <= 0) { return false; } if (level > fCount) { level = fCount; } if (levelPtr) { *levelPtr = fLevels[level - 1]; // need to augment with our colorspace levelPtr->fPixmap.setColorSpace(fCS); } return true; }
void drawBorders(SkCanvas* canvas) { SkPaint p; p.setStyle(SkPaint::kStroke_Style); p.setColor(SK_ColorBLUE); SkRect r = SkRect::MakeWH(fCell.width() * 2, fCell.height() * 2); r.inset(SK_ScalarHalf, SK_ScalarHalf); canvas->drawRect(r, p); canvas->drawLine(r.left(), r.centerY(), r.right(), r.centerY(), p); canvas->drawLine(r.centerX(), r.top(), r.centerX(), r.bottom(), p); }
void onDrawContent(SkCanvas* canvas) override { fCell.set(this->height() / 2, this->height() / 2); SkScalar trans[2]; fTrans.timeToValues(fCurrTime, trans); for (int y = 0; y < 2; ++y) { for (int x = 0; x < 2; ++x) { int index = y * 2 + x; SkAutoCanvasRestore acr(canvas, true); canvas->translate(fCell.width() * x, fCell.height() * y); SkRect r = SkRect::MakeWH(fCell.width(), fCell.height()); r.inset(4, 4); canvas->clipRect(r); this->drawHere(canvas, SkFilterQuality(index), trans[0], trans[1]); } } this->drawBorders(canvas); const SkScalar textX = fCell.width() * 2 + 30; SkPaint paint; paint.setAntiAlias(true); paint.setTextSize(36); SkString str; str.appendScalar(fScale); canvas->drawText(str.c_str(), str.size(), textX, 100, paint); str.reset(); str.appendScalar(fAngle); canvas->drawText(str.c_str(), str.size(), textX, 150, paint); str.reset(); str.appendScalar(trans[0]); canvas->drawText(str.c_str(), str.size(), textX, 200, paint); str.reset(); str.appendScalar(trans[1]); canvas->drawText(str.c_str(), str.size(), textX, 250, paint); }
/* * Modulo internal errors, this should always succeed *if* the matrix is downscaling * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscaling) */ bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmap& origBitmap) { SkASSERT(fQuality <= kMedium_SkFilterQuality); if (fQuality != kMedium_SkFilterQuality) { return false; } // Our default return state is to downgrade the request to Low, w/ or w/o setting fBitmap // to a valid bitmap. fQuality = kLow_SkFilterQuality; SkSize invScaleSize; if (!fInvMatrix.decomposeScale(&invScaleSize, NULL)) { return false; } SkScalar invScale = SkScalarSqrt(invScaleSize.width() * invScaleSize.height()); if (invScale > SK_Scalar1) { fCurrMip.reset(SkMipMapCache::FindAndRef(origBitmap)); if (NULL == fCurrMip.get()) { fCurrMip.reset(SkMipMapCache::AddAndRef(origBitmap)); if (NULL == fCurrMip.get()) { return false; } } // diagnostic for a crasher... if (NULL == fCurrMip->data()) { sk_throw(); } SkScalar levelScale = SkScalarInvert(invScale); SkMipMap::Level level; if (fCurrMip->extractLevel(levelScale, &level)) { SkScalar invScaleFixup = level.fScale; fInvMatrix.postScale(invScaleFixup, invScaleFixup); const SkImageInfo info = origBitmap.info().makeWH(level.fWidth, level.fHeight); // todo: if we could wrap the fCurrMip in a pixelref, then we could just install // that here, and not need to explicitly track it ourselves. return fResultBitmap.installPixels(info, level.fPixels, level.fRowBytes); } else { // failed to extract, so release the mipmap fCurrMip.reset(NULL); } } return false; }
// Here is an example of using Skia’s PDF backend (SkPDF) via the SkDocument // and SkCanvas APIs. void WritePDF(SkWStream* outputStream, const char* documentTitle, void (*writePage)(SkCanvas*, int page), int numberOfPages, SkSize pageSize) { SkPDF::Metadata metadata; metadata.fTitle = documentTitle; metadata.fCreator = "Example WritePDF() Function"; metadata.fCreation = {0, 2019, 1, 4, 31, 12, 34, 56}; metadata.fModified = {0, 2019, 1, 4, 31, 12, 34, 56}; auto pdfDocument = SkPDF::MakeDocument(outputStream, metadata); SkASSERT(pdfDocument); for (int page = 0; page < numberOfPages; ++page) { SkCanvas* pageCanvas = pdfDocument->beginPage(pageSize.width(), pageSize.height()); writePage(pageCanvas, page); pdfDocument->endPage(); } pdfDocument->close(); }
bool SkMultiPictureDocumentRead(SkStreamSeekable* stream, SkDocumentPage* dstArray, int dstArrayCount, const SkDeserialProcs* procs) { if (!SkMultiPictureDocumentReadPageSizes(stream, dstArray, dstArrayCount)) { return false; } SkSize joined = {0.0f, 0.0f}; for (int i = 0; i < dstArrayCount; ++i) { joined = SkSize{SkTMax(joined.width(), dstArray[i].fSize.width()), SkTMax(joined.height(), dstArray[i].fSize.height())}; } auto picture = SkPicture::MakeFromStream(stream, procs); PagerCanvas canvas(joined.toCeil(), dstArray, dstArrayCount); // Must call playback(), not drawPicture() to reach // PagerCanvas::onDrawAnnotation(). picture->playback(&canvas); if (canvas.fIndex != dstArrayCount) { SkDEBUGF("Malformed SkMultiPictureDocument\n"); } return true; }
static void TestPath(skiatest::Reporter* reporter) { { SkSize size; size.fWidth = 3.4f; size.width(); size = SkSize::Make(3,4); SkISize isize = SkISize::Make(3,4); } SkTSize<SkScalar>::Make(3,4); SkPath p, p2; SkRect bounds, bounds2; REPORTER_ASSERT(reporter, p.isEmpty()); REPORTER_ASSERT(reporter, !p.isConvex()); REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType); REPORTER_ASSERT(reporter, !p.isInverseFillType()); REPORTER_ASSERT(reporter, p == p2); REPORTER_ASSERT(reporter, !(p != p2)); REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); bounds.set(0, 0, SK_Scalar1, SK_Scalar1); p.setIsConvex(false); p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); check_convex_bounds(reporter, p, bounds); p.reset(); p.setIsConvex(false); p.addOval(bounds); check_convex_bounds(reporter, p, bounds); p.reset(); p.setIsConvex(false); p.addRect(bounds); check_convex_bounds(reporter, p, bounds); REPORTER_ASSERT(reporter, p != p2); REPORTER_ASSERT(reporter, !(p == p2)); // does getPoints return the right result REPORTER_ASSERT(reporter, p.getPoints(NULL, 5) == 4); SkPoint pts[4]; int count = p.getPoints(pts, 4); REPORTER_ASSERT(reporter, count == 4); bounds2.set(pts, 4); REPORTER_ASSERT(reporter, bounds == bounds2); bounds.offset(SK_Scalar1*3, SK_Scalar1*4); p.offset(SK_Scalar1*3, SK_Scalar1*4); REPORTER_ASSERT(reporter, bounds == p.getBounds()); #if 0 // isRect needs to be implemented REPORTER_ASSERT(reporter, p.isRect(NULL)); bounds.setEmpty(); REPORTER_ASSERT(reporter, p.isRect(&bounds2)); REPORTER_ASSERT(reporter, bounds == bounds2); // now force p to not be a rect bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2); p.addRect(bounds); REPORTER_ASSERT(reporter, !p.isRect(NULL)); #endif SkPoint pt; p.moveTo(SK_Scalar1, 0); p.getLastPt(&pt); REPORTER_ASSERT(reporter, pt.fX == SK_Scalar1); }
static SkISize SkSizeToISize(const SkSize& size) { return SkISize::Make(SkScalarRoundToInt(size.width()), SkScalarRoundToInt(size.height())); }
/* * High quality is implemented by performing up-right scale-only filtering and then * using bilerp for any remaining transformations. */ bool SkDefaultBitmapControllerState::processHQRequest(const SkBitmapProvider& provider) { if (fQuality != kHigh_SkFilterQuality) { return false; } // Our default return state is to downgrade the request to Medium, w/ or w/o setting fBitmap // to a valid bitmap. If we succeed, we will set this to Low instead. fQuality = kMedium_SkFilterQuality; if (kN32_SkColorType != provider.info().colorType() || !cache_size_okay(provider, fInvMatrix) || fInvMatrix.hasPerspective()) { return false; // can't handle the reqeust } SkScalar invScaleX = fInvMatrix.getScaleX(); SkScalar invScaleY = fInvMatrix.getScaleY(); if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) { SkSize scale; if (!fInvMatrix.decomposeScale(&scale)) { return false; } invScaleX = scale.width(); invScaleY = scale.height(); } if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1)) { return false; // no need for HQ } #ifndef SK_SUPPORT_LEGACY_HQ_DOWNSAMPLING if (invScaleX > 1 || invScaleY > 1) { return false; // only use HQ when upsampling } #endif const int dstW = SkScalarRoundToScalar(provider.width() / invScaleX); const int dstH = SkScalarRoundToScalar(provider.height() / invScaleY); const SkBitmapCacheDesc desc = provider.makeCacheDesc(dstW, dstH); if (!SkBitmapCache::FindWH(desc, &fResultBitmap)) { SkBitmap orig; if (!provider.asBitmap(&orig)) { return false; } SkAutoPixmapUnlock src; if (!orig.requestLock(&src)) { return false; } if (!SkBitmapScaler::Resize(&fResultBitmap, src.pixmap(), kHQ_RESIZE_METHOD, dstW, dstH, SkResourceCache::GetAllocator())) { return false; // we failed to create fScaledBitmap } SkASSERT(fResultBitmap.getPixels()); fResultBitmap.setImmutable(); if (!provider.isVolatile()) { if (SkBitmapCache::AddWH(desc, fResultBitmap)) { provider.notifyAddedToCache(); } } } SkASSERT(fResultBitmap.getPixels()); fInvMatrix.postScale(SkIntToScalar(dstW) / provider.width(), SkIntToScalar(dstH) / provider.height()); fQuality = kLow_SkFilterQuality; return true; }
SkPDFShader::State::State(const SkShader& shader, const SkMatrix& canvasTransform, const SkIRect& bbox, SkScalar rasterScale) : fCanvasTransform(canvasTransform), fBBox(bbox), fPixelGeneration(0) { fInfo.fColorCount = 0; fInfo.fColors = NULL; fInfo.fColorOffsets = NULL; fShaderTransform = shader.getLocalMatrix(); fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; fType = shader.asAGradient(&fInfo); if (fType == SkShader::kNone_GradientType) { SkShader::BitmapType bitmapType; SkMatrix matrix; bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes); if (bitmapType != SkShader::kDefault_BitmapType) { // Generic fallback for unsupported shaders: // * allocate a bbox-sized bitmap // * shade the whole area // * use the result as a bitmap shader // bbox is in device space. While that's exactly what we want for sizing our bitmap, // we need to map it into shader space for adjustments (to match // SkPDFImageShader::Create's behavior). SkRect shaderRect = SkRect::Make(bbox); if (!inverse_transform_bbox(canvasTransform, &shaderRect)) { fImage.reset(); return; } // Clamp the bitmap size to about 1M pixels static const SkScalar kMaxBitmapArea = 1024 * 1024; SkScalar bitmapArea = rasterScale * bbox.width() * rasterScale * bbox.height(); if (bitmapArea > kMaxBitmapArea) { rasterScale *= SkScalarSqrt(SkScalarDiv(kMaxBitmapArea, bitmapArea)); } SkISize size = SkISize::Make(SkScalarRoundToInt(rasterScale * bbox.width()), SkScalarRoundToInt(rasterScale * bbox.height())); SkSize scale = SkSize::Make(SkIntToScalar(size.width()) / shaderRect.width(), SkIntToScalar(size.height()) / shaderRect.height()); fImage.allocN32Pixels(size.width(), size.height()); fImage.eraseColor(SK_ColorTRANSPARENT); SkPaint p; p.setShader(const_cast<SkShader*>(&shader)); SkCanvas canvas(fImage); canvas.scale(scale.width(), scale.height()); canvas.translate(-shaderRect.x(), -shaderRect.y()); canvas.drawPaint(p); fShaderTransform.setTranslate(shaderRect.x(), shaderRect.y()); fShaderTransform.preScale(1 / scale.width(), 1 / scale.height()); } else { SkASSERT(matrix.isIdentity()); } fPixelGeneration = fImage.getGenerationID(); } else { AllocateGradientInfoStorage(); shader.asAGradient(&fInfo); } }
void TestPath(skiatest::Reporter* reporter) { { SkSize size; size.fWidth = 3.4f; size.width(); size = SkSize::Make(3,4); SkISize isize = SkISize::Make(3,4); } SkTSize<SkScalar>::Make(3,4); SkPath p, p2; SkRect bounds, bounds2; REPORTER_ASSERT(reporter, p.isEmpty()); REPORTER_ASSERT(reporter, 0 == p.countPoints()); REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks()); REPORTER_ASSERT(reporter, p.isConvex()); REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType); REPORTER_ASSERT(reporter, !p.isInverseFillType()); REPORTER_ASSERT(reporter, p == p2); REPORTER_ASSERT(reporter, !(p != p2)); REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); bounds.set(0, 0, SK_Scalar1, SK_Scalar1); p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); check_convex_bounds(reporter, p, bounds); // we have quads or cubics REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask); REPORTER_ASSERT(reporter, !p.isEmpty()); p.reset(); REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks()); REPORTER_ASSERT(reporter, p.isEmpty()); p.addOval(bounds); check_convex_bounds(reporter, p, bounds); REPORTER_ASSERT(reporter, !p.isEmpty()); p.reset(); p.addRect(bounds); check_convex_bounds(reporter, p, bounds); // we have only lines REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks()); REPORTER_ASSERT(reporter, !p.isEmpty()); REPORTER_ASSERT(reporter, p != p2); REPORTER_ASSERT(reporter, !(p == p2)); // does getPoints return the right result REPORTER_ASSERT(reporter, p.getPoints(NULL, 5) == 4); SkPoint pts[4]; int count = p.getPoints(pts, 4); REPORTER_ASSERT(reporter, count == 4); bounds2.set(pts, 4); REPORTER_ASSERT(reporter, bounds == bounds2); bounds.offset(SK_Scalar1*3, SK_Scalar1*4); p.offset(SK_Scalar1*3, SK_Scalar1*4); REPORTER_ASSERT(reporter, bounds == p.getBounds()); REPORTER_ASSERT(reporter, p.isRect(NULL)); bounds2.setEmpty(); REPORTER_ASSERT(reporter, p.isRect(&bounds2)); REPORTER_ASSERT(reporter, bounds == bounds2); // now force p to not be a rect bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2); p.addRect(bounds); REPORTER_ASSERT(reporter, !p.isRect(NULL)); test_isRect(reporter); SkPoint pt; p.moveTo(SK_Scalar1, 0); p.getLastPt(&pt); REPORTER_ASSERT(reporter, pt.fX == SK_Scalar1); REPORTER_ASSERT(reporter, !p.isEmpty()); test_zero_length_paths(reporter); test_convexity(reporter); test_convexity2(reporter); test_close(reporter); p.reset(); p.moveTo(0, 0); p.quadTo(100, 100, 200, 200); REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks()); REPORTER_ASSERT(reporter, !p.isEmpty()); p.cubicTo(100, 100, 200, 200, 300, 300); REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks()); REPORTER_ASSERT(reporter, !p.isEmpty()); p.reset(); p.moveTo(0, 0); p.cubicTo(100, 100, 200, 200, 300, 300); REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks()); REPORTER_ASSERT(reporter, !p.isEmpty()); test_flattening(reporter); test_transform(reporter); test_bounds(reporter); }
int main() { const DrawOptions options = GetDrawOptions(); if (options.source) { sk_sp<SkData> data(SkData::NewFromFileName(options.source)); if (!data) { perror(options.source); return 1; } else { image = SkImage::MakeFromEncoded(std::move(data)); if (!image) { perror("Unable to decode the source image."); return 1; } SkAssertResult(image->asLegacyBitmap( &source, SkImage::kRO_LegacyBitmapMode)); } } sk_sp<SkData> rasterData, gpuData, pdfData, skpData; if (options.raster) { auto rasterSurface = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(options.size)); srand(0); draw(rasterSurface->getCanvas()); rasterData.reset(encode_snapshot(rasterSurface)); } if (options.gpu) { auto grContext = create_grcontext(); if (!grContext) { fputs("Unable to get GrContext.\n", stderr); } else { auto surface = SkSurface::MakeRenderTarget( grContext.get(), SkBudgeted::kNo, SkImageInfo::MakeN32Premul(options.size)); if (!surface) { fputs("Unable to get render surface.\n", stderr); exit(1); } srand(0); draw(surface->getCanvas()); gpuData.reset(encode_snapshot(surface)); } } if (options.pdf) { SkDynamicMemoryWStream pdfStream; sk_sp<SkDocument> document(SkDocument::MakePDF(&pdfStream)); srand(0); draw(document->beginPage(options.size.width(), options.size.height())); document->close(); pdfData.reset(pdfStream.copyToData()); } if (options.skp) { SkSize size; size = options.size; SkPictureRecorder recorder; srand(0); draw(recorder.beginRecording(size.width(), size.height())); auto picture = recorder.finishRecordingAsPicture(); SkDynamicMemoryWStream skpStream; picture->serialize(&skpStream); skpData.reset(skpStream.copyToData()); } printf("{\n"); dump_output(rasterData, "Raster", !gpuData && !pdfData && !skpData); dump_output(gpuData, "Gpu", !pdfData && !skpData); dump_output(pdfData, "Pdf", !skpData); dump_output(skpData, "Skp"); printf("}\n"); return 0; }
void writeSize(FILE* file, int indentLevel, const char* str, SkSize size) { writeIndent(file, indentLevel); fprintf(file, "%s = { w = %.3f; h = %.3f; };\n", str, size.width(), size.height()); }