Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkColorSpace.h" |
| 13 | #include "include/core/SkImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkPicture.h" |
| 16 | #include "include/core/SkPictureRecorder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkRect.h" |
| 18 | #include "include/core/SkRefCnt.h" |
| 19 | #include "include/core/SkScalar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/core/SkShader.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/core/SkSize.h" |
| 22 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include "include/core/SkTileMode.h" |
| 25 | #include "include/third_party/skcms/skcms.h" |
| 26 | |
| 27 | #include <utility> |
Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 28 | |
| 29 | class PictureShaderCacheGM : public skiagm::GM { |
| 30 | public: |
| 31 | PictureShaderCacheGM(SkScalar tileSize) |
| 32 | : fTileSize(tileSize) { |
| 33 | } |
| 34 | |
| 35 | protected: |
| 36 | void drawTile(SkCanvas* canvas) { |
| 37 | SkPaint paint; |
| 38 | paint.setColor(SK_ColorGREEN); |
| 39 | paint.setStyle(SkPaint::kFill_Style); |
| 40 | paint.setAntiAlias(true); |
| 41 | |
| 42 | canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint); |
| 43 | canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2, |
| 44 | fTileSize / 2, fTileSize / 2), paint); |
| 45 | |
| 46 | paint.setColor(SK_ColorRED); |
| 47 | canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3, |
| 48 | fTileSize / 2, fTileSize * 2 / 3, paint); |
| 49 | canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2, |
| 50 | fTileSize * 2 / 3, fTileSize / 2, paint); |
| 51 | } |
| 52 | |
| 53 | void onOnceBeforeDraw() override { |
| 54 | SkPictureRecorder recorder; |
Mike Reed | 457c6dd | 2020-08-21 13:42:01 -0400 | [diff] [blame] | 55 | SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize); |
Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 56 | this->drawTile(pictureCanvas); |
| 57 | fPicture = recorder.finishRecordingAsPicture(); |
| 58 | } |
| 59 | |
| 60 | SkString onShortName() override { |
| 61 | return SkString("pictureshadercache"); |
| 62 | } |
| 63 | |
| 64 | SkISize onISize() override { |
| 65 | return SkISize::Make(100, 100); |
| 66 | } |
| 67 | |
| 68 | void onDraw(SkCanvas* canvas) override { |
| 69 | SkPaint paint; |
Mike Reed | 10a5ff2 | 2021-03-18 17:18:58 -0400 | [diff] [blame] | 70 | paint.setShader(fPicture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 71 | SkFilterMode::kNearest)); |
Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 72 | |
| 73 | { |
| 74 | // Render in a funny color space that converts green to yellow. |
Brian Osman | 82ebe04 | 2019-01-04 17:03:00 -0500 | [diff] [blame] | 75 | skcms_Matrix3x3 greenToYellow = {{ |
| 76 | { 1, 1, 0 }, |
| 77 | { 0, 1, 0 }, |
| 78 | { 0, 0, 1 }, |
| 79 | }}; |
| 80 | sk_sp<SkColorSpace> gty = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, |
Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 81 | greenToYellow); |
| 82 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100, std::move(gty)); |
| 83 | sk_sp<SkSurface> surface(SkSurface::MakeRaster(info)); |
| 84 | surface->getCanvas()->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint); |
| 85 | } |
| 86 | |
| 87 | // When we draw to the canvas, we should see green because we should *not* reuse the |
| 88 | // cached picture shader. |
| 89 | canvas->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | SkScalar fTileSize; |
| 94 | sk_sp<SkPicture> fPicture; |
| 95 | SkBitmap fBitmap; |
| 96 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 97 | using INHERITED = GM; |
Matt Sarett | e71db44 | 2017-04-21 15:06:51 -0400 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | DEF_GM(return new PictureShaderCacheGM(100);) |