blob: b560a0c6d908c202c5e253676f72c63b2afd849f [file] [log] [blame]
Matt Sarette71db442017-04-21 15:06:51 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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 Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPaint.h"
15#include "include/core/SkPicture.h"
16#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkSize.h"
22#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "include/core/SkTileMode.h"
25#include "include/third_party/skcms/skcms.h"
26
27#include <utility>
Matt Sarette71db442017-04-21 15:06:51 -040028
29class PictureShaderCacheGM : public skiagm::GM {
30public:
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 Reed457c6dd2020-08-21 13:42:01 -040055 SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize);
Matt Sarette71db442017-04-21 15:06:51 -040056 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 Reed10a5ff22021-03-18 17:18:58 -040070 paint.setShader(fPicture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
71 SkFilterMode::kNearest));
Matt Sarette71db442017-04-21 15:06:51 -040072
73 {
74 // Render in a funny color space that converts green to yellow.
Brian Osman82ebe042019-01-04 17:03:00 -050075 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 Sarette71db442017-04-21 15:06:51 -040081 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
92private:
93 SkScalar fTileSize;
94 sk_sp<SkPicture> fPicture;
95 SkBitmap fBitmap;
96
John Stiles7571f9e2020-09-02 22:42:33 -040097 using INHERITED = GM;
Matt Sarette71db442017-04-21 15:06:51 -040098};
99
100DEF_GM(return new PictureShaderCacheGM(100);)