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)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 41 | surface->getCanvas()->clear(0); |
| 42 | surface->getCanvas()->drawPicture(pic); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 43 | return surface->makeImageSnapshot(); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 44 | } |
| 45 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 46 | static sk_sp<SkImage> make_pict_gen(GrContext*, SkPicture* pic, const SkImageInfo& info) { |
| 47 | return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 48 | } |
| 49 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 50 | static sk_sp<SkImage> make_encode_gen(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) { |
| 51 | sk_sp<SkImage> src(make_raster(ctx, pic, info)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 52 | if (!src) { |
| 53 | return nullptr; |
| 54 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 55 | sk_sp<SkData> encoded(src->encode(SkImageEncoder::kPNG_Type, 100)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 56 | if (!encoded) { |
| 57 | return nullptr; |
| 58 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 59 | return SkImage::MakeFromEncoded(std::move(encoded)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | const 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 | */ |
| 73 | class ImageShaderGM : public skiagm::GM { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 74 | sk_sp<SkPicture> fPicture; |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 75 | |
| 76 | public: |
| 77 | ImageShaderGM() {} |
| 78 | |
| 79 | protected: |
| 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); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 92 | fPicture = recorder.finishRecordingAsPicture(); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 93 | } |
| 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); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 103 | SkPaint paint; |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 104 | paint.setShader(image->makeShader(tile, tile, &localM)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 105 | paint.setAntiAlias(true); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 106 | 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) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 115 | sk_sp<SkImage> image(gProcs[i](canvas->getGrContext(), fPicture.get(), info)); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 116 | if (image) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 117 | this->testImage(canvas, image.get()); |
reed | 499abab | 2015-08-20 12:30:20 -0700 | [diff] [blame] | 118 | } |
| 119 | canvas->translate(120, 0); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | typedef skiagm::GM INHERITED; |
| 125 | }; |
| 126 | DEF_GM( return new ImageShaderGM; ) |
| 127 | |