blob: 5e55786f87808dee439afd01b6cd42f5797ffb52 [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));
scroggoa1b28332016-05-12 06:19:37 -070041 if (!surface) {
42 return nullptr;
43 }
reed499abab2015-08-20 12:30:20 -070044 surface->getCanvas()->clear(0);
45 surface->getCanvas()->drawPicture(pic);
reed9ce9d672016-03-17 10:51:11 -070046 return surface->makeImageSnapshot();
reed499abab2015-08-20 12:30:20 -070047}
48
reed9ce9d672016-03-17 10:51:11 -070049static sk_sp<SkImage> make_pict_gen(GrContext*, SkPicture* pic, const SkImageInfo& info) {
Brian Osman138ea972016-12-16 11:55:18 -050050 return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -050051 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -050052 SkColorSpace::MakeSRGB());
reed499abab2015-08-20 12:30:20 -070053}
54
reed9ce9d672016-03-17 10:51:11 -070055static sk_sp<SkImage> make_encode_gen(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
56 sk_sp<SkImage> src(make_raster(ctx, pic, info));
reed499abab2015-08-20 12:30:20 -070057 if (!src) {
58 return nullptr;
59 }
Mike Reed6409f842017-07-11 16:03:13 -040060 sk_sp<SkData> encoded = src->encodeToData(SkEncodedImageFormat::kPNG, 100);
reed499abab2015-08-20 12:30:20 -070061 if (!encoded) {
62 return nullptr;
63 }
reed9ce9d672016-03-17 10:51:11 -070064 return SkImage::MakeFromEncoded(std::move(encoded));
reed499abab2015-08-20 12:30:20 -070065}
66
67const ImageMakerProc gProcs[] = {
68 make_raster,
69 make_texture,
70 make_pict_gen,
71 make_encode_gen,
72};
73
74/*
75 * Exercise drawing pictures inside an image, showing that the image version is pixelated
76 * (correctly) when it is inside an image.
77 */
78class ImageShaderGM : public skiagm::GM {
reedca2622b2016-03-18 07:25:55 -070079 sk_sp<SkPicture> fPicture;
reed499abab2015-08-20 12:30:20 -070080
81public:
82 ImageShaderGM() {}
83
84protected:
85 SkString onShortName() override {
86 return SkString("image-shader");
87 }
88
89 SkISize onISize() override {
90 return SkISize::Make(850, 450);
91 }
92
93 void onOnceBeforeDraw() override {
94 const SkRect bounds = SkRect::MakeWH(100, 100);
95 SkPictureRecorder recorder;
96 draw_something(recorder.beginRecording(bounds), bounds);
reedca2622b2016-03-18 07:25:55 -070097 fPicture = recorder.finishRecordingAsPicture();
reed499abab2015-08-20 12:30:20 -070098 }
99
100 void testImage(SkCanvas* canvas, SkImage* image) {
101 SkAutoCanvasRestore acr(canvas, true);
102
103 canvas->drawImage(image, 0, 0);
104 canvas->translate(0, 120);
105
106 const SkShader::TileMode tile = SkShader::kRepeat_TileMode;
107 const SkMatrix localM = SkMatrix::MakeTrans(-50, -50);
reed499abab2015-08-20 12:30:20 -0700108 SkPaint paint;
reed5671c5b2016-03-09 14:47:34 -0800109 paint.setShader(image->makeShader(tile, tile, &localM));
reed499abab2015-08-20 12:30:20 -0700110 paint.setAntiAlias(true);
reed499abab2015-08-20 12:30:20 -0700111 canvas->drawCircle(50, 50, 50, paint);
112 }
113
114 void onDraw(SkCanvas* canvas) override {
115 canvas->translate(20, 20);
116
117 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
118
119 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
reedca2622b2016-03-18 07:25:55 -0700120 sk_sp<SkImage> image(gProcs[i](canvas->getGrContext(), fPicture.get(), info));
reed499abab2015-08-20 12:30:20 -0700121 if (image) {
reed9ce9d672016-03-17 10:51:11 -0700122 this->testImage(canvas, image.get());
reed499abab2015-08-20 12:30:20 -0700123 }
124 canvas->translate(120, 0);
125 }
126 }
127
128private:
129 typedef skiagm::GM INHERITED;
130};
131DEF_GM( return new ImageShaderGM; )