reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkColorSpace.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkData.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkEncodedImageFormat.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 16 | #include "include/core/SkImageInfo.h" |
| 17 | #include "include/core/SkMatrix.h" |
| 18 | #include "include/core/SkPaint.h" |
| 19 | #include "include/core/SkPicture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/core/SkPictureRecorder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/core/SkRect.h" |
| 22 | #include "include/core/SkRefCnt.h" |
| 23 | #include "include/core/SkShader.h" |
| 24 | #include "include/core/SkSize.h" |
| 25 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include "include/core/SkTileMode.h" |
| 28 | #include "include/core/SkTypes.h" |
| 29 | |
| 30 | #include <utility> |
| 31 | |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 32 | static void draw_something(SkCanvas* canvas, const SkRect& bounds) { |
| 33 | SkPaint paint; |
| 34 | paint.setAntiAlias(true); |
| 35 | paint.setColor(SK_ColorRED); |
| 36 | paint.setStyle(SkPaint::kStroke_Style); |
| 37 | paint.setStrokeWidth(10); |
| 38 | canvas->drawRect(bounds, paint); |
| 39 | paint.setStyle(SkPaint::kFill_Style); |
| 40 | paint.setColor(SK_ColorBLUE); |
| 41 | canvas->drawOval(bounds, paint); |
| 42 | } |
| 43 | |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 44 | typedef sk_sp<SkImage> (*ImageMakerProc)(GrRecordingContext*, SkPicture*, const SkImageInfo&); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 45 | |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 46 | static sk_sp<SkImage> make_raster(GrRecordingContext*, |
| 47 | SkPicture* pic, |
| 48 | const SkImageInfo& info) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 49 | auto surface(SkSurface::MakeRaster(info)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 50 | surface->getCanvas()->clear(0); |
| 51 | surface->getCanvas()->drawPicture(pic); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 52 | return surface->makeImageSnapshot(); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 55 | static sk_sp<SkImage> make_texture(GrRecordingContext* ctx, |
| 56 | SkPicture* pic, |
| 57 | const SkImageInfo& info) { |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 58 | if (!ctx) { |
| 59 | return nullptr; |
| 60 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 61 | auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info)); |
scroggo | a1b2833 | 2016-05-12 06:19:37 -0700 | [diff] [blame] | 62 | if (!surface) { |
| 63 | return nullptr; |
| 64 | } |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 65 | surface->getCanvas()->clear(0); |
| 66 | surface->getCanvas()->drawPicture(pic); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 67 | return surface->makeImageSnapshot(); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 70 | static sk_sp<SkImage> make_pict_gen(GrRecordingContext*, |
| 71 | SkPicture* pic, |
| 72 | const SkImageInfo& info) { |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 73 | return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 74 | SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 75 | SkColorSpace::MakeSRGB()); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 78 | static sk_sp<SkImage> make_encode_gen(GrRecordingContext* ctx, |
| 79 | SkPicture* pic, |
| 80 | const SkImageInfo& info) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 81 | sk_sp<SkImage> src(make_raster(ctx, pic, info)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 82 | if (!src) { |
| 83 | return nullptr; |
| 84 | } |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 85 | sk_sp<SkData> encoded = src->encodeToData(SkEncodedImageFormat::kPNG, 100); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 86 | if (!encoded) { |
| 87 | return nullptr; |
| 88 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 89 | return SkImage::MakeFromEncoded(std::move(encoded)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | const ImageMakerProc gProcs[] = { |
| 93 | make_raster, |
| 94 | make_texture, |
| 95 | make_pict_gen, |
| 96 | make_encode_gen, |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * Exercise drawing pictures inside an image, showing that the image version is pixelated |
| 101 | * (correctly) when it is inside an image. |
| 102 | */ |
| 103 | class ImageShaderGM : public skiagm::GM { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 104 | sk_sp<SkPicture> fPicture; |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 105 | |
| 106 | public: |
| 107 | ImageShaderGM() {} |
| 108 | |
| 109 | protected: |
| 110 | SkString onShortName() override { |
| 111 | return SkString("image-shader"); |
| 112 | } |
| 113 | |
| 114 | SkISize onISize() override { |
| 115 | return SkISize::Make(850, 450); |
| 116 | } |
| 117 | |
| 118 | void onOnceBeforeDraw() override { |
| 119 | const SkRect bounds = SkRect::MakeWH(100, 100); |
| 120 | SkPictureRecorder recorder; |
| 121 | draw_something(recorder.beginRecording(bounds), bounds); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 122 | fPicture = recorder.finishRecordingAsPicture(); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void testImage(SkCanvas* canvas, SkImage* image) { |
| 126 | SkAutoCanvasRestore acr(canvas, true); |
| 127 | |
| 128 | canvas->drawImage(image, 0, 0); |
| 129 | canvas->translate(0, 120); |
| 130 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 131 | const SkTileMode tile = SkTileMode::kRepeat; |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 132 | const SkMatrix localM = SkMatrix::Translate(-50, -50); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 133 | SkPaint paint; |
Mike Reed | b612b6c | 2020-12-08 21:58:35 -0500 | [diff] [blame] | 134 | paint.setShader(image->makeShader(tile, tile, SkSamplingOptions(), &localM)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 135 | paint.setAntiAlias(true); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 136 | canvas->drawCircle(50, 50, 50, paint); |
| 137 | } |
| 138 | |
| 139 | void onDraw(SkCanvas* canvas) override { |
| 140 | canvas->translate(20, 20); |
| 141 | |
| 142 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 143 | |
| 144 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) { |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 145 | sk_sp<SkImage> image(gProcs[i](canvas->recordingContext(), fPicture.get(), info)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 146 | if (image) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 147 | this->testImage(canvas, image.get()); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 148 | } |
| 149 | canvas->translate(120, 0); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 154 | using INHERITED = skiagm::GM; |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 155 | }; |
| 156 | DEF_GM( return new ImageShaderGM; ) |
Mike Reed | 198e6ea | 2020-12-07 12:29:18 -0500 | [diff] [blame] | 157 | |
| 158 | ////////////////////////////////////////////////////////////////////////////////////////////// |
| 159 | |
| 160 | #include "tools/ToolUtils.h" |
| 161 | |
| 162 | static sk_sp<SkImage> make_checker_img(int w, int h, SkColor c0, SkColor c1, int size) { |
| 163 | SkBitmap bm = ToolUtils::create_checkerboard_bitmap(w, h, c0, c1, size); |
| 164 | bm.setImmutable(); |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 165 | return bm.asImage(); |
Mike Reed | 198e6ea | 2020-12-07 12:29:18 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | DEF_SIMPLE_GM(drawimage_sampling, canvas, 500, 500) { |
| 169 | constexpr int N = 256; |
| 170 | constexpr float kScale = 1.0f/6; |
| 171 | const SkRect dst = {0, 0, kScale*N, kScale*N}; |
| 172 | |
| 173 | auto img = make_checker_img(N, N, SK_ColorBLACK, SK_ColorWHITE, 7)->withDefaultMipmaps(); |
| 174 | const SkRect src = SkRect::MakeIWH(img->width(), img->height()); |
| 175 | |
Mike Reed | 2ac6ce8 | 2021-01-15 12:26:22 -0500 | [diff] [blame] | 176 | SkMatrix mx = SkMatrix::RectToRect(src, dst); |
Mike Reed | 198e6ea | 2020-12-07 12:29:18 -0500 | [diff] [blame] | 177 | |
| 178 | SkPaint paint; |
| 179 | |
| 180 | for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) { |
| 181 | for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) { |
| 182 | SkSamplingOptions sampling(fm, mm); |
| 183 | |
| 184 | canvas->save(); |
| 185 | |
| 186 | canvas->save(); |
| 187 | canvas->concat(mx); |
| 188 | canvas->drawImage(img.get(), 0, 0, sampling); |
| 189 | canvas->restore(); |
| 190 | |
| 191 | canvas->translate(dst.width() + 4, 0); |
| 192 | |
| 193 | paint.setShader(img->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, sampling, &mx)); |
| 194 | canvas->drawRect(dst, paint); |
| 195 | |
| 196 | canvas->translate(dst.width() + 4, 0); |
| 197 | |
Mike Reed | 4f23dec | 2020-12-30 14:22:42 +0000 | [diff] [blame] | 198 | canvas->drawImageRect(img.get(), src, dst, sampling, nullptr, |
| 199 | SkCanvas::kFast_SrcRectConstraint); |
Mike Reed | 198e6ea | 2020-12-07 12:29:18 -0500 | [diff] [blame] | 200 | canvas->restore(); |
| 201 | |
| 202 | canvas->translate(0, dst.height() + 8); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | } |