blob: 5fed7d1344bec0c39bb7c9c7a8d8db9ba2bc5e85 [file] [log] [blame]
reed499abab2015-08-20 12:30:20 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkData.h"
11#include "SkImage.h"
12#include "SkPictureRecorder.h"
13#include "SkSurface.h"
14
15static void draw_something(SkCanvas* canvas, const SkRect& bounds) {
16 SkPaint paint;
17 paint.setAntiAlias(true);
18 paint.setColor(SK_ColorRED);
19 paint.setStyle(SkPaint::kStroke_Style);
20 paint.setStrokeWidth(10);
21 canvas->drawRect(bounds, paint);
22 paint.setStyle(SkPaint::kFill_Style);
23 paint.setColor(SK_ColorBLUE);
24 canvas->drawOval(bounds, paint);
25}
26
reed9ce9d672016-03-17 10:51:11 -070027typedef sk_sp<SkImage> (*ImageMakerProc)(GrContext*, SkPicture*, const SkImageInfo&);
reed499abab2015-08-20 12:30:20 -070028
reed9ce9d672016-03-17 10:51:11 -070029static sk_sp<SkImage> make_raster(GrContext*, SkPicture* pic, const SkImageInfo& info) {
reede8f30622016-03-23 18:59:25 -070030 auto surface(SkSurface::MakeRaster(info));
reed499abab2015-08-20 12:30:20 -070031 surface->getCanvas()->clear(0);
32 surface->getCanvas()->drawPicture(pic);
reed9ce9d672016-03-17 10:51:11 -070033 return surface->makeImageSnapshot();
reed499abab2015-08-20 12:30:20 -070034}
35
reed9ce9d672016-03-17 10:51:11 -070036static sk_sp<SkImage> make_texture(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
reed499abab2015-08-20 12:30:20 -070037 if (!ctx) {
38 return nullptr;
39 }
reede8f30622016-03-23 18:59:25 -070040 auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
reed499abab2015-08-20 12:30:20 -070041 surface->getCanvas()->clear(0);
42 surface->getCanvas()->drawPicture(pic);
reed9ce9d672016-03-17 10:51:11 -070043 return surface->makeImageSnapshot();
reed499abab2015-08-20 12:30:20 -070044}
45
reed9ce9d672016-03-17 10:51:11 -070046static sk_sp<SkImage> make_pict_gen(GrContext*, SkPicture* pic, const SkImageInfo& info) {
47 return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr);
reed499abab2015-08-20 12:30:20 -070048}
49
reed9ce9d672016-03-17 10:51:11 -070050static sk_sp<SkImage> make_encode_gen(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
51 sk_sp<SkImage> src(make_raster(ctx, pic, info));
reed499abab2015-08-20 12:30:20 -070052 if (!src) {
53 return nullptr;
54 }
reed9ce9d672016-03-17 10:51:11 -070055 sk_sp<SkData> encoded(src->encode(SkImageEncoder::kPNG_Type, 100));
reed499abab2015-08-20 12:30:20 -070056 if (!encoded) {
57 return nullptr;
58 }
reed9ce9d672016-03-17 10:51:11 -070059 return SkImage::MakeFromEncoded(std::move(encoded));
reed499abab2015-08-20 12:30:20 -070060}
61
62const ImageMakerProc gProcs[] = {
63 make_raster,
64 make_texture,
65 make_pict_gen,
66 make_encode_gen,
67};
68
69/*
70 * Exercise drawing pictures inside an image, showing that the image version is pixelated
71 * (correctly) when it is inside an image.
72 */
73class ImageShaderGM : public skiagm::GM {
reedca2622b2016-03-18 07:25:55 -070074 sk_sp<SkPicture> fPicture;
reed499abab2015-08-20 12:30:20 -070075
76public:
77 ImageShaderGM() {}
78
79protected:
80 SkString onShortName() override {
81 return SkString("image-shader");
82 }
83
84 SkISize onISize() override {
85 return SkISize::Make(850, 450);
86 }
87
88 void onOnceBeforeDraw() override {
89 const SkRect bounds = SkRect::MakeWH(100, 100);
90 SkPictureRecorder recorder;
91 draw_something(recorder.beginRecording(bounds), bounds);
reedca2622b2016-03-18 07:25:55 -070092 fPicture = recorder.finishRecordingAsPicture();
reed499abab2015-08-20 12:30:20 -070093 }
94
95 void testImage(SkCanvas* canvas, SkImage* image) {
96 SkAutoCanvasRestore acr(canvas, true);
97
98 canvas->drawImage(image, 0, 0);
99 canvas->translate(0, 120);
100
101 const SkShader::TileMode tile = SkShader::kRepeat_TileMode;
102 const SkMatrix localM = SkMatrix::MakeTrans(-50, -50);
reed499abab2015-08-20 12:30:20 -0700103 SkPaint paint;
reed5671c5b2016-03-09 14:47:34 -0800104 paint.setShader(image->makeShader(tile, tile, &localM));
reed499abab2015-08-20 12:30:20 -0700105 paint.setAntiAlias(true);
reed499abab2015-08-20 12:30:20 -0700106 canvas->drawCircle(50, 50, 50, paint);
107 }
108
109 void onDraw(SkCanvas* canvas) override {
110 canvas->translate(20, 20);
111
112 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
113
114 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
reedca2622b2016-03-18 07:25:55 -0700115 sk_sp<SkImage> image(gProcs[i](canvas->getGrContext(), fPicture.get(), info));
reed499abab2015-08-20 12:30:20 -0700116 if (image) {
reed9ce9d672016-03-17 10:51:11 -0700117 this->testImage(canvas, image.get());
reed499abab2015-08-20 12:30:20 -0700118 }
119 canvas->translate(120, 0);
120 }
121 }
122
123private:
124 typedef skiagm::GM INHERITED;
125};
126DEF_GM( return new ImageShaderGM; )
127