Index: gm/image.cpp |
diff --git a/gm/image.cpp b/gm/image.cpp |
index 60c1f1141862c36db176df9bba313b911012986b..e79968a18f95b58cfa6e38e98b054eea26d1bd89 100644 |
--- a/gm/image.cpp |
+++ b/gm/image.cpp |
@@ -5,6 +5,7 @@ |
* found in the LICENSE file. |
*/ |
+#include <functional> |
#include "gm.h" |
#include "SkData.h" |
#include "SkCanvas.h" |
@@ -429,3 +430,86 @@ private: |
typedef skiagm::GM INHERITED; |
}; |
DEF_GM( return new ScaleGeneratorGM; ) |
+ |
+#if SK_SUPPORT_GPU |
+#include "GrContextFactory.h" |
+#endif |
+ |
+DEF_SIMPLE_GM(new_texture_image, canvas, 225, 60) { |
+ GrContext* context = nullptr; |
+#if SK_SUPPORT_GPU |
+ context = canvas->getGrContext(); |
+ GrContextFactory factory; |
+#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 const int kSize = 50; |
+ SkBitmap bmp; |
+ bmp.allocN32Pixels(kSize, kSize); |
+ SkCanvas bmpCanvas(bmp); |
+ render_image(&bmpCanvas); |
+ |
+ std::function<SkImage*()> imageFactories[] = { |
+ // Create sw raster image. |
+ [bmp] { |
+ return SkImage::NewFromBitmap(bmp); |
+ }, |
+ // Create encoded image. |
+ [bmp] { |
+ SkAutoTUnref<SkData> src( |
+ SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100)); |
+ return SkImage::NewFromEncoded(src); |
+ }, |
+ // Create a picture image. |
+ [render_image] { |
+ SkPictureRecorder recorder; |
+ SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
+ render_image(canvas); |
+ SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
+ return SkImage::NewFromPicture(picture, SkISize::Make(kSize, kSize), nullptr, nullptr); |
+ }, |
+ // Create a texture image |
+ [context, render_image]() -> SkImage* { |
+ SkAutoTUnref<SkSurface> surface( |
+ SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, |
+ SkImageInfo::MakeN32Premul(kSize, kSize))); |
+ if (!surface) { |
+ return nullptr; |
+ } |
+ render_image(surface->getCanvas()); |
+ return surface->newImageSnapshot(); |
+ } |
+ }; |
+ |
+ static const SkScalar kPad = 5.f; |
+ canvas->translate(kPad, kPad); |
+ for (auto factory : imageFactories) { |
+ SkAutoTUnref<SkImage> image(factory()); |
+ if (!image) { |
+ continue; |
+ } |
+ if (context) { |
+ SkAutoTUnref<SkImage> texImage(image->newTextureImage(context)); |
+ if (texImage) { |
+ canvas->drawImage(texImage, 0, 0); |
+ } |
+ } |
+ canvas->translate(image->width() + kPad, 0); |
+ } |
+} |