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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlendMode.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" |
| 13 | #include "include/core/SkImage.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkPicture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkPictureRecorder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkPoint.h" |
| 18 | #include "include/core/SkRect.h" |
| 19 | #include "include/core/SkRefCnt.h" |
| 20 | #include "include/core/SkScalar.h" |
| 21 | #include "include/core/SkShader.h" |
| 22 | #include "include/core/SkSize.h" |
| 23 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | #include "include/core/SkTileMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "include/effects/SkGradientShader.h" |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 27 | |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 28 | static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) { |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 29 | const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 }; |
| 30 | const SkScalar radius = 40; |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 31 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 32 | paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 33 | SkTileMode::kMirror)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 34 | paint.setBlendMode(SkBlendMode::kSrc); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 35 | canvas->drawPaint(paint); |
| 36 | } |
| 37 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 38 | 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] | 39 | auto surface(SkSurface::MakeRasterN32Premul(width, height)); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 40 | draw(surface->getCanvas(), width, height, colors); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 41 | return surface->makeImageSnapshot(); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 42 | } |
| 43 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 44 | 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] | 45 | SkPictureRecorder recorder; |
| 46 | draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 47 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 48 | {width, height}, nullptr, nullptr, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 49 | SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 50 | SkColorSpace::MakeSRGB()); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 51 | } |
| 52 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 53 | typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 54 | |
| 55 | static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2], |
| 56 | ImageMakerProc proc) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 57 | sk_sp<SkImage> image(proc(width, height, colors)); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 58 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 59 | SkPaint borderPaint; |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 60 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 61 | borderPaint.setStyle(SkPaint::kStroke_Style); |
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 | SkRect dstRect = SkRect::MakeWH(128.f, 128.f); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 64 | |
| 65 | canvas->save(); |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 66 | canvas->clipRect(dstRect); |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 67 | canvas->drawImage(image, 0, 0); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 68 | canvas->restore(); |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 69 | canvas->drawRect(dstRect, borderPaint); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 70 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 71 | dstRect.offset(SkIntToScalar(150), 0); |
| 72 | int hw = width / 2; |
| 73 | int hh = height / 2; |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 74 | SkRect subset = SkRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32); |
| 75 | canvas->drawImageRect(image, subset, dstRect, SkSamplingOptions(), nullptr, |
| 76 | SkCanvas::kStrict_SrcRectConstraint); |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 77 | canvas->drawRect(dstRect, borderPaint); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 78 | |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 79 | dstRect.offset(SkIntToScalar(150), 0); |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 80 | canvas->drawImageRect(image, dstRect, SkSamplingOptions(), nullptr); |
Brian Salomon | d93f4a4 | 2016-11-14 14:41:58 -0500 | [diff] [blame] | 81 | canvas->drawRect(dstRect, borderPaint); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | class VeryLargeBitmapGM : public skiagm::GM { |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 85 | ImageMakerProc fProc; |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 86 | const char* fName; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 87 | |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 88 | public: |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 89 | VeryLargeBitmapGM(ImageMakerProc proc, const char name[]) : fProc(proc), fName(name) {} |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 90 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 91 | private: |
| 92 | SkString onShortName() override { return SkString(fName); } |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 93 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 94 | SkISize onISize() override { return {500, 600}; } |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 95 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | edf00c7 | 2013-10-24 20:55:14 +0000 | [diff] [blame] | 97 | int veryBig = 65*1024; // 64K < size |
| 98 | int big = 33*1024; // 32K < size < 64K |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 99 | // 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] | 100 | int medium = 5*1024; |
robertphillips@google.com | c3dc12d | 2013-07-15 15:48:48 +0000 | [diff] [blame] | 101 | int small = 150; |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 102 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 103 | SkColor colors[2]; |
| 104 | |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 105 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 106 | colors[0] = SK_ColorRED; |
| 107 | colors[1] = SK_ColorGREEN; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 108 | show_image(canvas, small, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 109 | canvas->translate(0, SkIntToScalar(150)); |
| 110 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 111 | colors[0] = SK_ColorBLUE; |
| 112 | colors[1] = SK_ColorMAGENTA; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 113 | show_image(canvas, big, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 114 | canvas->translate(0, SkIntToScalar(150)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 115 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 116 | colors[0] = SK_ColorMAGENTA; |
| 117 | colors[1] = SK_ColorYELLOW; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 118 | show_image(canvas, medium, medium, colors, fProc); |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 119 | canvas->translate(0, SkIntToScalar(150)); |
| 120 | |
| 121 | colors[0] = SK_ColorGREEN; |
| 122 | colors[1] = SK_ColorYELLOW; |
Mike Klein | dac694d | 2018-12-18 10:13:52 -0500 | [diff] [blame] | 123 | // 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] | 124 | show_image(canvas, veryBig, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 125 | } |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 126 | }; |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 127 | |
| 128 | DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap"); ) |
| 129 | DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image"); ) |