// Creates a bitmap and a matching image. static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) { SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); auto surface(sk_tool_utils::makeSurface(origCanvas, info)); SkCanvas* canvas = surface->getCanvas(); canvas->clear(SK_ColorTRANSPARENT); SkScalar wScalar = SkIntToScalar(w); SkScalar hScalar = SkIntToScalar(h); SkPoint pt = { wScalar / 2, hScalar / 2 }; SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorCYAN, SK_ColorRED}; SkScalar pos[] = {0, SK_Scalar1 / 6, 2 * SK_Scalar1 / 6, 3 * SK_Scalar1 / 6, 4 * SK_Scalar1 / 6, 5 * SK_Scalar1 / 6, SK_Scalar1}; SkPaint paint; SkRect rect = SkRect::MakeWH(wScalar, hScalar); SkMatrix mat = SkMatrix::I(); for (int i = 0; i < 4; ++i) { paint.setShader(SkGradientShader::MakeRadial( pt, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kRepeat_TileMode, 0, &mat)); canvas->drawRect(rect, paint); rect.inset(wScalar / 8, hScalar / 8); mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); } auto image = surface->makeImageSnapshot(); SkBitmap tempBM; image->asLegacyBitmap(&tempBM); // Let backends know we won't change this, so they don't have to deep copy it defensively. tempBM.setImmutable(); *resultBM = tempBM; return image; }
static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) { SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); SkAutoTUnref<SkSurface> surface(origCanvas->newSurface(info)); if (nullptr == surface) { // picture canvas will return null, so fall-back to raster surface.reset(SkSurface::NewRaster(info)); } SkCanvas* canvas = surface->getCanvas(); canvas->clear(SK_ColorTRANSPARENT); SkScalar wScalar = SkIntToScalar(w); SkScalar hScalar = SkIntToScalar(h); SkPoint pt = { wScalar / 2, hScalar / 2 }; SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorCYAN, SK_ColorRED}; SkScalar pos[] = {0, SK_Scalar1 / 6, 2 * SK_Scalar1 / 6, 3 * SK_Scalar1 / 6, 4 * SK_Scalar1 / 6, 5 * SK_Scalar1 / 6, SK_Scalar1}; SkPaint paint; SkRect rect = SkRect::MakeWH(wScalar, hScalar); SkMatrix mat = SkMatrix::I(); for (int i = 0; i < 4; ++i) { paint.setShader(SkGradientShader::MakeRadial( pt, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kRepeat_TileMode, 0, &mat)); canvas->drawRect(rect, paint); rect.inset(wScalar / 8, hScalar / 8); mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); } auto image = surface->makeImageSnapshot(); SkBitmap tempBM; #if SK_SUPPORT_GPU if (GrTexture* texture = as_IB(image)->peekTexture()) { GrWrapTextureInBitmap(texture, image->width(), image->height(), image->isOpaque(), &tempBM); } else #endif { image->asLegacyBitmap(&tempBM, SkImage::kRO_LegacyBitmapMode); } // Let backends know we won't change this, so they don't have to deep copy it defensively. tempBM.setImmutable(); *resultBM = tempBM; return image; }