Flutter Engine
The Flutter Engine
Request.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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
9
10#include <memory>
11
18#include "tools/ToolUtils.h"
20
21using namespace sk_gpu_test;
22
23static int kDefaultWidth = 1920;
24static int kDefaultHeight = 1080;
25static int kMaxWidth = 8192;
26static int kMaxHeight = 8192;
27
28
30 : fUploadContext(nullptr)
31 , fUrlDataManager(rootUrl)
32 , fGPUEnabled(false)
33 , fOverdraw(false)
34 , fColorMode(0) {
35 // create surface
37 fContextFactory = new GrContextFactory(grContextOpts);
38}
39
41 if (fContextFactory) {
42 delete fContextFactory;
43 }
44}
45
46sk_sp<SkData> Request::writeCanvasToPng(SkCanvas* canvas) {
47 // capture pixels
48 SkBitmap bmp;
49 bmp.allocPixels(canvas->imageInfo());
50 SkAssertResult(canvas->readPixels(bmp, 0, 0));
51
52 // write to an opaque png (black background)
55 return buffer.detachAsData();
56}
57
59#ifdef SK_GL
60 GrContextFactory* factory = fContextFactory;
63 if (!gl) {
66 }
67 if (gl) {
68 gl->makeCurrent();
69 }
70#endif
71 SkASSERT(fDebugCanvas);
72
73 // create the appropriate surface if necessary
74 if (!fSurface) {
75 this->enableGPU(fGPUEnabled);
76 }
77 SkCanvas* target = fSurface->getCanvas();
78 return target;
79}
80
82 //fDebugCanvas->setOverdrawViz(true);
83 auto* canvas = this->getCanvas();
85 fDebugCanvas->drawTo(canvas, n, m);
86 //fDebugCanvas->setOverdrawViz(false);
87 return writeCanvasToPng(this->getCanvas());
88}
89
91 // Playback into picture recorder
92 SkIRect bounds = this->getBounds();
93 SkPictureRecorder recorder;
94 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()),
95 SkIntToScalar(bounds.height()));
96
97 fDebugCanvas->draw(canvas);
98
99 return recorder.finishRecordingAsPicture()->serialize();
100}
101
102GrDirectContext* Request::directContext() {
103 auto result = fContextFactory->get(skgpu::ContextType::kGL,
105 if (!result) {
106 result = fContextFactory->get(skgpu::ContextType::kGLES,
108 }
109 return result;
110}
111
112SkIRect Request::getBounds() {
114 if (fPicture) {
115 bounds = fPicture->cullRect().roundOut();
116 if (fGPUEnabled) {
117 int maxRTSize = this->directContext()->maxRenderTargetSize();
118 bounds = SkIRect::MakeWH(std::min(bounds.width(), maxRTSize),
119 std::min(bounds.height(), maxRTSize));
120 }
121 } else {
123 }
124
125 // We clip to kMaxWidth / kMaxHeight for performance reasons.
126 // TODO make this configurable
128 std::min(bounds.height(), kMaxHeight));
129 return bounds;
130}
131
132namespace {
133
134struct ColorAndProfile {
136 bool fSRGB;
137};
138
139ColorAndProfile ColorModes[] = {
140 { kN32_SkColorType, false },
141 { kN32_SkColorType, true },
142 { kRGBA_F16_SkColorType, true },
143};
144
145} // namespace
146
147SkSurface* Request::createCPUSurface() {
148 SkIRect bounds = this->getBounds();
149 ColorAndProfile cap = ColorModes[fColorMode];
150 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
152 : SkColorSpace::MakeSRGB();
154 cap.fSRGB ? colorSpace : nullptr);
156}
157
158SkSurface* Request::createGPUSurface() {
159 auto context = this->directContext();
160 SkIRect bounds = this->getBounds();
161 ColorAndProfile cap = ColorModes[fColorMode];
162 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
164 : SkColorSpace::MakeSRGB();
166 cap.fSRGB ? colorSpace : nullptr);
168 return surface;
169}
170
171bool Request::setOverdraw(bool enable) {
172 fOverdraw = enable;
173 return true;
174}
175
177 fColorMode = mode;
178 return enableGPU(fGPUEnabled);
179}
180
181bool Request::enableGPU(bool enable) {
182 if (enable) {
183 SkSurface* surface = this->createGPUSurface();
184 if (surface) {
185 fSurface.reset(surface);
186 fGPUEnabled = true;
187
188 // When we switch to GPU, there seems to be some mystery draws in the canvas. So we
189 // draw once to flush the pipe
190 // TODO understand what is actually happening here
191 if (fDebugCanvas) {
192 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
193 this->directContext()->flush(fSurface.get());
194 }
195
196 return true;
197 }
198 return false;
199 }
200 fSurface.reset(this->createCPUSurface());
201 fGPUEnabled = false;
202 return true;
203}
204
206 // parse picture from stream
208 if (!fPicture) {
209 fprintf(stderr, "Could not create picture from stream.\n");
210 return false;
211 }
212
213 // reinitialize canvas with the new picture dimensions
214 this->enableGPU(fGPUEnabled);
215
216 // pour picture into debug canvas
217 SkIRect bounds = this->getBounds();
218 fDebugCanvas = std::make_unique<DebugCanvas>(bounds.width(), bounds.height());
219 fDebugCanvas->drawPicture(fPicture);
220
221 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
222 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
223 this->directContext()->flush(fSurface.get());
224 return true;
225}
226
228 SkCanvas* canvas = this->getCanvas();
231 writer.beginObject(); // root
232
233 writer.appendCString("mode", fGPUEnabled ? "gpu" : "cpu");
234 writer.appendBool("drawGpuOpBounds", fDebugCanvas->getDrawGpuOpBounds());
235 writer.appendS32("colorMode", fColorMode);
236 fDebugCanvas->toJSON(writer, fUrlDataManager, canvas);
237
238 writer.endObject(); // root
239 writer.flush();
240 return stream.detachAsData();
241}
242
244 SkCanvas* canvas = this->getCanvas();
245 SkASSERT(fGPUEnabled);
248
249 fDebugCanvas->toJSONOpsTask(writer, canvas);
250
251 writer.flush();
252 return stream.detachAsData();
253}
254
256 // drawTo
257 sk_sp<SkSurface> surface(this->createCPUSurface());
258 SkCanvas* canvas = surface->getCanvas();
259
260 // TODO this is really slow and we should cache the matrix and clip
261 fDebugCanvas->drawTo(canvas, n);
262
263 // make some json
266
267 SkM44 vm = fDebugCanvas->getCurrentMatrix();
268 SkIRect clip = fDebugCanvas->getCurrentClip();
269
270 writer.beginObject(); // root
271 writer.appendName("ViewMatrix");
273 writer.appendName("ClipRect");
275 writer.endObject(); // root
276
277 // TODO: Old code explicitly avoided the null terminator in the returned data. Important?
278 writer.flush();
279 return stream.detachAsData();
280}
281
283 SkBitmap bmp;
284 bmp.allocPixels(this->getCanvas()->imageInfo().makeWH(1, 1));
285 SkAssertResult(this->getCanvas()->readPixels(bmp, x, y));
286 return bmp.getColor(0, 0);
287}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
SkColorType fColorType
static int kDefaultWidth
Definition: Request.cpp:23
static int kMaxHeight
Definition: Request.cpp:26
static int kDefaultHeight
Definition: Request.cpp:24
static int kMaxWidth
Definition: Request.cpp:25
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static void MakeJsonMatrix44(SkJSONWriter &, const SkM44 &)
static void MakeJsonIRect(SkJSONWriter &, const SkIRect &)
static void WritePNG(const SkBitmap &bitmap, SkWStream &out)
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
SkColor getColor(int x, int y) const
Definition: SkBitmap.h:874
void clear(SkColor color)
Definition: SkCanvas.h:1199
SkImageInfo imageInfo() const
Definition: SkCanvas.cpp:1206
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY)
Definition: SkCanvas.cpp:382
static sk_sp< SkColorSpace > MakeSRGBLinear()
void appendS32(int32_t value)
Definition: SkJSONWriter.h:237
void beginObject(const char *name=nullptr, bool multiline=true)
Definition: SkJSONWriter.h:114
void endObject()
Definition: SkJSONWriter.h:126
void appendBool(bool value)
Definition: SkJSONWriter.h:229
void appendName(const char *name)
Definition: SkJSONWriter.h:90
void appendCString(const char *value)
Definition: SkJSONWriter.h:224
void flush()
Definition: SkJSONWriter.h:78
Definition: SkM44.h:150
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
sk_sp< SkData > serialize(const SkSerialProcs *procs=nullptr) const
Definition: SkPicture.cpp:249
static sk_sp< SkPicture > MakeFromStream(SkStream *stream, const SkDeserialProcs *procs=nullptr)
Definition: SkPicture.cpp:147
ContextInfo getContextInfo(ContextType type, ContextOverrides=ContextOverrides::kNone)
T * release()
Definition: SkRefCnt.h:324
VkSurfaceKHR surface
Definition: main.cc:49
GAsyncResult * result
uint32_t * target
static float min(float r, float g, float b)
Definition: hsl.cpp:48
double y
double x
Optional< SkRect > bounds
Definition: SkRecords.h:189
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
@ kNone
Definition: layer.h:53
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
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 mode
Definition: switches.h:228
gl
Definition: malisc.py:41
@ kGLES
OpenGL context.
GrContextOptions grContextOpts
Definition: nanobench.cpp:107
bool setOverdraw(bool enable)
Definition: Request.cpp:171
SkColor getPixel(int x, int y)
Definition: Request.cpp:282
sk_sp< SkData > getJsonInfo(int n)
Definition: Request.cpp:255
sk_sp< SkData > drawToPng(int n, int m=-1)
Definition: Request.cpp:81
SkCanvas * getCanvas()
Definition: Request.cpp:58
bool enableGPU(bool enable)
Definition: Request.cpp:181
~Request()
Definition: Request.cpp:40
sk_sp< SkData > writeOutSkp()
Definition: Request.cpp:90
sk_sp< SkData > getJsonOpsTask()
Definition: Request.cpp:243
bool initPictureFromStream(SkStream *)
Definition: Request.cpp:205
sk_sp< SkData > getJsonOps()
Definition: Request.cpp:227
Request(SkString rootUrl)
Definition: Request.cpp:29
bool setColorMode(int mode)
Definition: Request.cpp:176
Definition: SkRect.h:32
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)