Flutter Engine
The Flutter Engine
graphite_replay.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google LLC
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
13#include "tools/DecodeUtils.h"
14#include "tools/GpuToolUtils.h"
15#include "tools/Resources.h"
16#include "tools/ToolUtils.h"
17
18#if defined(SK_GRAPHITE)
25#endif
26
27namespace skiagm {
28
29class GraphiteReplayGM : public GM {
30public:
31 GraphiteReplayGM() = default;
32
33protected:
34 void onOnceBeforeDraw() override {
36 fImage = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
37 }
38
39 SkString getName() const override { return SkString("graphite-replay"); }
40
41 SkISize getISize() override { return SkISize::Make(kTileWidth * 3, kTileHeight * 2); }
42
43 bool onAnimate(double nanos) override {
44 fStartX = kTileWidth * (1.0f + sinf(nanos * 1e-9)) * 0.5f;
45 return true;
46 }
47
48 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
49#if defined(SK_GRAPHITE)
50 skgpu::graphite::Recorder* recorder = canvas->recorder();
51 if (recorder) {
52 this->drawGraphite(canvas, recorder);
53 return DrawResult::kOk;
54 }
55#endif
56 return this->drawNonGraphite(canvas, errorMsg);
57 }
58
59private:
60 static constexpr int kImageSize = 128;
61 static constexpr int kPadding = 2;
62 static constexpr int kPaddedImageSize = kImageSize + kPadding * 2;
63 static constexpr int kTileWidth = kPaddedImageSize * 2;
64 static constexpr int kTileHeight = kPaddedImageSize * 2;
65
66 float fStartX = 0.0f;
67
68 sk_sp<SkImage> fImage;
69
70 void drawContent(SkCanvas* canvas, int y) {
71 SkPaint gradientPaint;
72 constexpr SkPoint points[2] = {{0.0f, 0.0f}, {kImageSize, kImageSize}};
76
77 // Draw image.
78 canvas->drawImage(fImage, kPadding, kPadding + y);
79
80 // Draw gradient.
81 canvas->save();
82 canvas->translate(kPaddedImageSize + kPadding, kPadding + y);
83 canvas->drawRect(SkRect::MakeXYWH(0, 0, kImageSize, kImageSize), gradientPaint);
84 canvas->restore();
85 }
86
87 void drawTile(SkCanvas* canvas) {
88 // Clip off the right 1/4 of the tile, after clearing.
89 canvas->clear(SkColors::kRed);
90 canvas->clipIRect(SkIRect::MakeWH(3 * kTileWidth / 4, kTileHeight));
91
92 // Draw content directly.
93 drawContent(canvas, 0);
94
95 // Draw content to a saved layer.
96 SkPaint pAlpha;
97 pAlpha.setAlphaf(0.5f);
98 canvas->saveLayer(nullptr, &pAlpha);
99 drawContent(canvas, kPaddedImageSize);
100 canvas->restore();
101 }
102
103#if defined(SK_GRAPHITE)
104 void drawGraphite(SkCanvas* canvas, skgpu::graphite::Recorder* canvasRecorder) {
105 SkImageInfo tileImageInfo =
106 canvas->imageInfo().makeDimensions(SkISize::Make(kTileWidth, kTileHeight));
107 skgpu::graphite::TextureInfo textureInfo =
108 static_cast<skgpu::graphite::Surface*>(canvas->getSurface())
109 ->backingTextureProxy()
110 ->textureInfo();
111
112 skgpu::graphite::Context* context = canvasRecorder->priv().context();
113 std::unique_ptr<skgpu::graphite::Recorder> recorder =
114 context->makeRecorder(ToolUtils::CreateTestingRecorderOptions());
115 SkCanvas* recordingCanvas = recorder->makeDeferredCanvas(tileImageInfo, textureInfo);
116 this->drawTile(recordingCanvas);
117 std::unique_ptr<skgpu::graphite::Recording> recording = recorder->snap();
118
119 // Flush the initial clear added by MakeGraphite.
120 std::unique_ptr<skgpu::graphite::Recording> canvasRecording = canvasRecorder->snap();
121 context->insertRecording({canvasRecording.get()});
122
123 for (int y = 0; y < 2; ++y) {
124 for (int x = 0; x < 2; ++x) {
125 context->insertRecording(
126 {recording.get(),
127 canvas->getSurface(),
128 {x * kTileWidth + SkScalarRoundToInt(fStartX), y * kTileHeight}});
129 }
130 }
131 }
132#endif
133
134 DrawResult drawNonGraphite(SkCanvas* canvas, SkString* errorMsg) {
135 SkImageInfo tileImageInfo =
136 canvas->imageInfo().makeDimensions(SkISize::Make(kTileWidth, kTileHeight));
137
138 sk_sp<SkSurface> imageSurface = canvas->makeSurface(tileImageInfo);
139 if (!imageSurface) {
140 *errorMsg = "Cannot create new SkSurface.";
141 return DrawResult::kSkip;
142 }
143
144 SkCanvas* imageCanvas = imageSurface->getCanvas();
145 this->drawTile(imageCanvas);
146 sk_sp<SkImage> image = imageSurface->makeImageSnapshot();
147
148 for (int y = 0; y < 2; ++y) {
149 for (int x = 0; x < 2; ++x) {
150 canvas->drawImage(image, x * kTileWidth + fStartX, y * kTileHeight);
151 }
152 }
153 return DrawResult::kOk;
154 }
155};
156
157DEF_GM(return new GraphiteReplayGM;)
158
159} // namespace skiagm
static const int points[]
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
SkSurface * getSurface() const
Definition: SkCanvas.cpp:365
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
sk_sp< SkSurface > makeSurface(const SkImageInfo &info, const SkSurfaceProps *props=nullptr)
Definition: SkCanvas.cpp:1195
void clipIRect(const SkIRect &irect, SkClipOp op=SkClipOp::kIntersect)
Definition: SkCanvas.h:991
virtual skgpu::graphite::Recorder * recorder() const
Definition: SkCanvas.cpp:1641
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
SkImageInfo imageInfo() const
Definition: SkCanvas.cpp:1206
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
void setShader(sk_sp< SkShader > shader)
void setAlphaf(float a)
Definition: SkPaint.cpp:130
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
std::unique_ptr< Recorder > makeRecorder(const RecorderOptions &={})
Definition: Context.cpp:132
bool insertRecording(const InsertRecordingInfo &)
Definition: Context.cpp:156
std::unique_ptr< Recording > snap()
Definition: Recorder.cpp:159
Definition: gm.h:110
skiagm::DrawResult DrawResult
Definition: gm.h:112
void setBGColor(SkColor)
Definition: gm.cpp:159
void onOnceBeforeDraw() override
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
SkISize getISize() override
SkString getName() const override
bool onAnimate(double nanos) override
double y
double x
constexpr SkColor4f kRed
Definition: SkColor.h:440
sk_sp< const SkImage > image
Definition: SkRecords.h:269
PODArray< SkColor > colors
Definition: SkRecords.h:276
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DrawResult
Definition: gm.h:104
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
SkImageInfo makeDimensions(SkISize newSize) const
Definition: SkImageInfo.h:454
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659