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 | |
| 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 | |
| 15 | static 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 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 27 | typedef sk_sp<SkImage> (*ImageMakerProc)(GrContext*, SkPicture*, const SkImageInfo&); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 28 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 29 | static sk_sp<SkImage> make_raster(GrContext*, SkPicture* pic, const SkImageInfo& info) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 30 | auto surface(SkSurface::MakeRaster(info)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 31 | surface->getCanvas()->clear(0); |
| 32 | surface->getCanvas()->drawPicture(pic); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 33 | return surface->makeImageSnapshot(); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 34 | } |
| 35 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 36 | static sk_sp<SkImage> make_texture(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) { |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 37 | if (!ctx) { |
| 38 | return nullptr; |
| 39 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 40 | auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info)); |
scroggo | a1b2833 | 2016-05-12 06:19:37 -0700 | [diff] [blame] | 41 | if (!surface) { |
| 42 | return nullptr; |
| 43 | } |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 44 | surface->getCanvas()->clear(0); |
| 45 | surface->getCanvas()->drawPicture(pic); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 46 | return surface->makeImageSnapshot(); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 47 | } |
| 48 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 49 | static sk_sp<SkImage> make_pict_gen(GrContext*, SkPicture* pic, const SkImageInfo& info) { |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 50 | return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 51 | SkImage::BitDepth::kU8, |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 52 | SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 53 | } |
| 54 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 55 | static sk_sp<SkImage> make_encode_gen(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) { |
| 56 | sk_sp<SkImage> src(make_raster(ctx, pic, info)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 57 | if (!src) { |
| 58 | return nullptr; |
| 59 | } |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 60 | sk_sp<SkData> encoded(src->encode(SkEncodedImageFormat::kPNG, 100)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 61 | if (!encoded) { |
| 62 | return nullptr; |
| 63 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 64 | return SkImage::MakeFromEncoded(std::move(encoded)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | const 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 | */ |
| 78 | class ImageShaderGM : public skiagm::GM { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 79 | sk_sp<SkPicture> fPicture; |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 80 | |
| 81 | public: |
| 82 | ImageShaderGM() {} |
| 83 | |
| 84 | protected: |
| 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); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 97 | fPicture = recorder.finishRecordingAsPicture(); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 98 | } |
| 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); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 108 | SkPaint paint; |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 109 | paint.setShader(image->makeShader(tile, tile, &localM)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 110 | paint.setAntiAlias(true); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 111 | 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) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 120 | sk_sp<SkImage> image(gProcs[i](canvas->getGrContext(), fPicture.get(), info)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 121 | if (image) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 122 | this->testImage(canvas, image.get()); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 123 | } |
| 124 | canvas->translate(120, 0); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | typedef skiagm::GM INHERITED; |
| 130 | }; |
| 131 | DEF_GM( return new ImageShaderGM; ) |