• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "gm/gm.h"
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"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPicture.h"
16 #include "include/core/SkPictureRecorder.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkShader.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkSurface.h"
24 #include "include/core/SkTileMode.h"
25 #include "modules/skcms/skcms.h"
26 
27 #include <utility>
28 
29 class PictureShaderCacheGM : public skiagm::GM {
30 public:
PictureShaderCacheGM(SkScalar tileSize)31     PictureShaderCacheGM(SkScalar tileSize)
32         : fTileSize(tileSize) {
33     }
34 
35  protected:
drawTile(SkCanvas * canvas)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 
onOnceBeforeDraw()53     void onOnceBeforeDraw() override {
54         SkPictureRecorder recorder;
55         SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize);
56         this->drawTile(pictureCanvas);
57         fPicture = recorder.finishRecordingAsPicture();
58     }
59 
onShortName()60     SkString onShortName() override {
61         return SkString("pictureshadercache");
62     }
63 
onISize()64     SkISize onISize() override {
65         return SkISize::Make(100, 100);
66     }
67 
onDraw(SkCanvas * canvas)68     void onDraw(SkCanvas* canvas) override {
69         SkPaint paint;
70         paint.setShader(fPicture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
71                                              SkFilterMode::kNearest));
72 
73         {
74             // Render in a funny color space that converts green to yellow.
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,
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 
97     using INHERITED = GM;
98 };
99 
100 DEF_GM(return new PictureShaderCacheGM(100);)
101