Bring back SkImage::makeTextureImage
Ensures that an image is GPU backed on the passed-in GrContxt. The new
version requires a destination color space (intended usage of the image),
so we can make a proper decision about decoded format.
This reverts commit d263413a2a92cafe3fd3b051c67d00206c9a0e4d.
BUG=skia:
Change-Id: Ibccddbafc301779559592045ed5a5fa9264e7432
Reviewed-on: https://skia-review.googlesource.com/8116
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/gm/image.cpp b/gm/image.cpp
index 595a5ef..1a4b712 100644
--- a/gm/image.cpp
+++ b/gm/image.cpp
@@ -427,3 +427,84 @@
typedef skiagm::GM INHERITED;
};
DEF_GM( return new ScaleGeneratorGM; )
+
+DEF_SIMPLE_GM(new_texture_image, canvas, 225, 60) {
+ GrContext* context = nullptr;
+#if SK_SUPPORT_GPU
+ context = canvas->getGrContext();
+#endif
+ if (!context) {
+ skiagm::GM::DrawGpuOnlyMessage(canvas);
+ return;
+ }
+
+ auto render_image = [](SkCanvas* canvas) {
+ canvas->clear(SK_ColorBLUE);
+ SkPaint paint;
+ paint.setColor(SK_ColorRED);
+ canvas->drawRect(SkRect::MakeXYWH(10.f,10.f,10.f,10.f), paint);
+ paint.setColor(SK_ColorGREEN);
+ canvas->drawRect(SkRect::MakeXYWH(30.f,10.f,10.f,10.f), paint);
+ paint.setColor(SK_ColorYELLOW);
+ canvas->drawRect(SkRect::MakeXYWH(10.f,30.f,10.f,10.f), paint);
+ paint.setColor(SK_ColorCYAN);
+ canvas->drawRect(SkRect::MakeXYWH(30.f,30.f,10.f,10.f), paint);
+ };
+
+ static constexpr int kSize = 50;
+ SkBitmap bmp;
+ bmp.allocPixels(SkImageInfo::MakeS32(kSize, kSize, kPremul_SkAlphaType));
+ SkCanvas bmpCanvas(bmp);
+ render_image(&bmpCanvas);
+
+ std::function<sk_sp<SkImage>()> imageFactories[] = {
+ // Create sw raster image.
+ [bmp] {
+ return SkImage::MakeFromBitmap(bmp);
+ },
+ // Create encoded image.
+ [bmp] {
+ sk_sp<SkData> src(
+ sk_tool_utils::EncodeImageToData(bmp, SkEncodedImageFormat::kPNG, 100));
+ return SkImage::MakeFromEncoded(std::move(src));
+ },
+ // Create a picture image.
+ [render_image] {
+ SkPictureRecorder recorder;
+ SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkIntToScalar(kSize));
+ render_image(canvas);
+ sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
+ return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
+ SkISize::Make(kSize, kSize), nullptr, nullptr,
+ SkImage::BitDepth::kU8, srgbColorSpace);
+ },
+ // Create a texture image
+ [context, render_image]() -> sk_sp<SkImage> {
+ auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
+ SkImageInfo::MakeS32(kSize, kSize,
+ kPremul_SkAlphaType)));
+ if (!surface) {
+ return nullptr;
+ }
+ render_image(surface->getCanvas());
+ return surface->makeImageSnapshot();
+ }
+ };
+
+ constexpr SkScalar kPad = 5.f;
+ canvas->translate(kPad, kPad);
+ for (auto factory : imageFactories) {
+ auto image(factory());
+ if (!image) {
+ continue;
+ }
+ if (context) {
+ sk_sp<SkImage> texImage(image->makeTextureImage(context,
+ canvas->imageInfo().colorSpace()));
+ if (texImage) {
+ canvas->drawImage(texImage, 0, 0);
+ }
+ }
+ canvas->translate(image->width() + kPad, 0);
+ }
+}