reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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" |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 11 | #include "SkPath.h" |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 12 | #include "SkPictureRecorder.h" |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 13 | #include "SkSurface.h" |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 14 | |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 15 | static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) { |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 16 | const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 }; |
| 17 | const SkScalar radius = 40; |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 18 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 19 | paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2, |
| 20 | SkShader::kMirror_TileMode)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 21 | paint.setBlendMode(SkBlendMode::kSrc); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 22 | canvas->drawPaint(paint); |
| 23 | } |
| 24 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 25 | static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 26 | auto surface(SkSurface::MakeRasterN32Premul(width, height)); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 27 | draw(surface->getCanvas(), width, height, colors); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 28 | return surface->makeImageSnapshot(); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 29 | } |
| 30 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 31 | static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) { |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 32 | SkPictureRecorder recorder; |
| 33 | draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 34 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 35 | SkISize::Make(width, height), nullptr, nullptr, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 36 | SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 37 | SkColorSpace::MakeSRGB()); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 38 | } |
| 39 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 40 | typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 41 | |
| 42 | static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2], |
| 43 | ImageMakerProc proc) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 44 | sk_sp<SkImage> image(proc(width, height, colors)); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 45 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 46 | SkPaint borderPaint; |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 47 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 48 | borderPaint.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 49 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 50 | SkRect dstRect = SkRect::MakeWH(128.f, 128.f); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 51 | |
| 52 | canvas->save(); |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 53 | canvas->clipRect(dstRect); |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 54 | canvas->drawImage(image, 0, 0, nullptr); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 55 | canvas->restore(); |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 56 | canvas->drawRect(dstRect, borderPaint); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 57 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 58 | dstRect.offset(SkIntToScalar(150), 0); |
| 59 | int hw = width / 2; |
| 60 | int hh = height / 2; |
| 61 | SkIRect subset = SkIRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32); |
| 62 | canvas->drawImageRect(image, subset, dstRect, nullptr); |
| 63 | canvas->drawRect(dstRect, borderPaint); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 64 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 65 | dstRect.offset(SkIntToScalar(150), 0); |
| 66 | canvas->drawImageRect(image, dstRect, nullptr); |
| 67 | canvas->drawRect(dstRect, borderPaint); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | class VeryLargeBitmapGM : public skiagm::GM { |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 71 | ImageMakerProc fProc; |
| 72 | SkString fName; |
| 73 | |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 74 | public: |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 75 | VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) { |
| 76 | fName.printf("verylarge%s", suffix); |
| 77 | } |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 78 | |
| 79 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 80 | SkString onShortName() override { |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 81 | return fName; |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 82 | } |
| 83 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 84 | SkISize onISize() override { |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 85 | return SkISize::Make(500, 600); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 86 | } |
| 87 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | edf00c7 | 2013-10-24 20:55:14 +0000 | [diff] [blame] | 89 | int veryBig = 65*1024; // 64K < size |
| 90 | int big = 33*1024; // 32K < size < 64K |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 91 | // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons. |
bsalomon@google.com | edf00c7 | 2013-10-24 20:55:14 +0000 | [diff] [blame] | 92 | int medium = 5*1024; |
robertphillips@google.com | c3dc12d | 2013-07-15 15:48:48 +0000 | [diff] [blame] | 93 | int small = 150; |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 94 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 95 | SkColor colors[2]; |
| 96 | |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 97 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 98 | colors[0] = SK_ColorRED; |
| 99 | colors[1] = SK_ColorGREEN; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 100 | show_image(canvas, small, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 101 | canvas->translate(0, SkIntToScalar(150)); |
| 102 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 103 | colors[0] = SK_ColorBLUE; |
| 104 | colors[1] = SK_ColorMAGENTA; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 105 | show_image(canvas, big, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 106 | canvas->translate(0, SkIntToScalar(150)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 108 | colors[0] = SK_ColorMAGENTA; |
| 109 | colors[1] = SK_ColorYELLOW; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 110 | show_image(canvas, medium, medium, colors, fProc); |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 111 | canvas->translate(0, SkIntToScalar(150)); |
| 112 | |
| 113 | colors[0] = SK_ColorGREEN; |
| 114 | colors[1] = SK_ColorYELLOW; |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 115 | // This used to be big enough that we didn't draw on CPU, but now we do. |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 116 | show_image(canvas, veryBig, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private: |
| 120 | typedef skiagm::GM INHERITED; |
| 121 | }; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 122 | DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); ) |
| 123 | DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); ) |