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(), |
| 35 | SkISize::Make(width, height), nullptr, nullptr); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 36 | } |
| 37 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 38 | typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 39 | |
| 40 | static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2], |
| 41 | ImageMakerProc proc) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 42 | sk_sp<SkImage> image(proc(width, height, colors)); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 43 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 44 | SkPaint borderPaint; |
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 | borderPaint.setStyle(SkPaint::kStroke_Style); |
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 | SkRect dstRect = SkRect::MakeWH(128.f, 128.f); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 49 | |
| 50 | canvas->save(); |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 51 | canvas->clipRect(dstRect); |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 52 | canvas->drawImage(image, 0, 0, nullptr); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 53 | canvas->restore(); |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 54 | canvas->drawRect(dstRect, borderPaint); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 55 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 56 | dstRect.offset(SkIntToScalar(150), 0); |
| 57 | int hw = width / 2; |
| 58 | int hh = height / 2; |
| 59 | SkIRect subset = SkIRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32); |
| 60 | canvas->drawImageRect(image, subset, dstRect, nullptr); |
| 61 | canvas->drawRect(dstRect, borderPaint); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 62 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 63 | dstRect.offset(SkIntToScalar(150), 0); |
| 64 | canvas->drawImageRect(image, dstRect, nullptr); |
| 65 | canvas->drawRect(dstRect, borderPaint); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | class VeryLargeBitmapGM : public skiagm::GM { |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 69 | ImageMakerProc fProc; |
| 70 | SkString fName; |
| 71 | |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 72 | public: |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 73 | VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) { |
| 74 | fName.printf("verylarge%s", suffix); |
| 75 | } |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 76 | |
| 77 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | SkString onShortName() override { |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 79 | return fName; |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 80 | } |
| 81 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 82 | SkISize onISize() override { |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 83 | return SkISize::Make(500, 600); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 84 | } |
| 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | edf00c7 | 2013-10-24 20:55:14 +0000 | [diff] [blame] | 87 | int veryBig = 65*1024; // 64K < size |
| 88 | int big = 33*1024; // 32K < size < 64K |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 89 | // 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] | 90 | int medium = 5*1024; |
robertphillips@google.com | c3dc12d | 2013-07-15 15:48:48 +0000 | [diff] [blame] | 91 | int small = 150; |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 92 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 93 | SkColor colors[2]; |
| 94 | |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 95 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 96 | colors[0] = SK_ColorRED; |
| 97 | colors[1] = SK_ColorGREEN; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 98 | show_image(canvas, small, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 99 | canvas->translate(0, SkIntToScalar(150)); |
| 100 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 101 | colors[0] = SK_ColorBLUE; |
| 102 | colors[1] = SK_ColorMAGENTA; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 103 | show_image(canvas, big, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 104 | canvas->translate(0, SkIntToScalar(150)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 106 | colors[0] = SK_ColorMAGENTA; |
| 107 | colors[1] = SK_ColorYELLOW; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 108 | show_image(canvas, medium, medium, colors, fProc); |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 109 | canvas->translate(0, SkIntToScalar(150)); |
| 110 | |
| 111 | colors[0] = SK_ColorGREEN; |
| 112 | colors[1] = SK_ColorYELLOW; |
| 113 | // as of this writing, the raster code will fail to draw the scaled version |
| 114 | // since it has a 64K limit on x,y coordinates... (but gpu should succeed) |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 115 | show_image(canvas, veryBig, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private: |
| 119 | typedef skiagm::GM INHERITED; |
| 120 | }; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 121 | DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); ) |
| 122 | DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); ) |