Flutter Engine
The Flutter Engine
pictureshadercache.cpp
Go to the documentation of this file.
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"
17#include "include/core/SkRect.h"
21#include "include/core/SkSize.h"
25#include "modules/skcms/skcms.h"
26
27#include <utility>
28
30public:
32 : fTileSize(tileSize) {
33 }
34
35 protected:
36 void drawTile(SkCanvas* canvas) {
38 paint.setColor(SK_ColorGREEN);
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;
55 SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize);
56 this->drawTile(pictureCanvas);
57 fPicture = recorder.finishRecordingAsPicture();
58 }
59
60 SkString getName() const override { return SkString("pictureshadercache"); }
61
62 SkISize getISize() override { return SkISize::Make(100, 100); }
63
64 void onDraw(SkCanvas* canvas) override {
68
69 {
70 // Render in a funny color space that converts green to yellow.
71 skcms_Matrix3x3 greenToYellow = {{
72 { 1, 1, 0 },
73 { 0, 1, 0 },
74 { 0, 0, 1 },
75 }};
77 greenToYellow);
78 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100, std::move(gty));
80 surface->getCanvas()->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint);
81 }
82
83 // When we draw to the canvas, we should see green because we should *not* reuse the
84 // cached picture shader.
85 canvas->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint);
86 }
87
88private:
89 SkScalar fTileSize;
90 sk_sp<SkPicture> fPicture;
91 SkBitmap fBitmap;
92
93 using INHERITED = GM;
94};
95
96DEF_GM(return new PictureShaderCacheGM(100);)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
void drawTile(SkCanvas *canvas)
void onDraw(SkCanvas *canvas) override
SkString getName() const override
PictureShaderCacheGM(SkScalar tileSize)
void onOnceBeforeDraw() override
SkISize getISize() override
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
static sk_sp< SkColorSpace > MakeRGB(const skcms_TransferFunction &transferFn, const skcms_Matrix3x3 &toXYZ)
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, SkFilterMode mode, const SkMatrix *localMatrix, const SkRect *tileRect) const
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
static constexpr skcms_TransferFunction kSRGB
Definition: SkColorSpace.h:45
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609