blob: 0784222909c07f50af695aeaf9a9cefef1c24cfc [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
8#include "gm.h"
9
10#include "SkPaint.h"
11#include "SkPicture.h"
12#include "SkPictureRecorder.h"
13#include "SkShader.h"
14#include "SkSurface.h"
15
16class PictureShaderCacheGM : public skiagm::GM {
17public:
18 PictureShaderCacheGM(SkScalar tileSize)
19 : fTileSize(tileSize) {
20 }
21
22 protected:
23 void drawTile(SkCanvas* canvas) {
24 SkPaint paint;
25 paint.setColor(SK_ColorGREEN);
26 paint.setStyle(SkPaint::kFill_Style);
27 paint.setAntiAlias(true);
28
29 canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint);
30 canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2,
31 fTileSize / 2, fTileSize / 2), paint);
32
33 paint.setColor(SK_ColorRED);
34 canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3,
35 fTileSize / 2, fTileSize * 2 / 3, paint);
36 canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2,
37 fTileSize * 2 / 3, fTileSize / 2, paint);
38 }
39
40 void onOnceBeforeDraw() override {
41 SkPictureRecorder recorder;
42 SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, nullptr, 0);
43 this->drawTile(pictureCanvas);
44 fPicture = recorder.finishRecordingAsPicture();
45 }
46
47 SkString onShortName() override {
48 return SkString("pictureshadercache");
49 }
50
51 SkISize onISize() override {
52 return SkISize::Make(100, 100);
53 }
54
55 void onDraw(SkCanvas* canvas) override {
56 SkPaint paint;
57 paint.setShader(SkShader::MakePictureShader(fPicture, SkShader::kRepeat_TileMode,
58 SkShader::kRepeat_TileMode, nullptr,
59 nullptr));
60
61 {
62 // Render in a funny color space that converts green to yellow.
63 SkMatrix44 greenToYellow(SkMatrix44::kIdentity_Constructor);
64 greenToYellow.setFloat(0, 1, 1.0f);
65 sk_sp<SkColorSpace> gty = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
66 greenToYellow);
67 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100, std::move(gty));
68 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
69 surface->getCanvas()->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint);
70 }
71
72 // When we draw to the canvas, we should see green because we should *not* reuse the
73 // cached picture shader.
74 canvas->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint);
75 }
76
77private:
78 SkScalar fTileSize;
79 sk_sp<SkPicture> fPicture;
80 SkBitmap fBitmap;
81
82 typedef GM INHERITED;
83};
84
85DEF_GM(return new PictureShaderCacheGM(100);)