Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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;
61 GLTestContext* gl = factory->getContextInfo(skgpu::ContextType::kGL,
62 GrContextFactory::ContextOverrides::kNone).glContext();
63 if (!gl) {
64 gl = factory->getContextInfo(skgpu::ContextType::kGLES,
65 GrContextFactory::ContextOverrides::kNone).glContext();
66 }
67 if (gl) {
68 gl->makeCurrent();
69 }
70#endif
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,
104 GrContextFactory::ContextOverrides::kNone);
105 if (!result) {
106 result = fContextFactory->get(skgpu::ContextType::kGLES,
107 GrContextFactory::ContextOverrides::kNone);
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
127 bounds = SkIRect::MakeWH(std::min(bounds.width(), kMaxWidth),
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
176bool Request::setColorMode(int mode) {
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
207 fPicture = SkPicture::MakeFromStream(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
static constexpr int kMaxHeight
static constexpr int kMaxWidth
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 SkAssertResult(cond)
Definition SkAssert.h:123
#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:3824
#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)
GrSemaphoresSubmitted flush(const GrFlushInfo &info)
SK_API int maxRenderTargetSize() const
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
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY)
Definition SkCanvas.cpp:386
static sk_sp< SkColorSpace > MakeSRGBLinear()
void appendS32(int32_t value)
void beginObject(const char *name=nullptr, bool multiline=true)
void appendBool(bool value)
void appendName(const char *name)
void appendCString(const char *value)
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
virtual SkRect cullRect() const =0
static sk_sp< SkPicture > MakeFromStream(SkStream *stream, const SkDeserialProcs *procs=nullptr)
SkCanvas * getCanvas()
Definition SkSurface.cpp:82
ContextInfo getContextInfo(ContextType type, ContextOverrides=ContextOverrides::kNone)
GrDirectContext * get(ContextType type, ContextOverrides overrides=ContextOverrides::kNone)
T * get() const
Definition SkRefCnt.h:303
T * release()
Definition SkRefCnt.h:324
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
VkSurfaceKHR surface
Definition main.cc:49
static const uint8_t buffer[]
GAsyncResult * result
uint32_t * target
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)
GrContextOptions grContextOpts
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
std::unique_ptr< DebugCanvas > fDebugCanvas
Definition Request.h:63
sk_sp< SkData > writeOutSkp()
Definition Request.cpp:90
sk_sp< SkData > getJsonOpsTask()
Definition Request.cpp:243
bool initPictureFromStream(SkStream *)
Definition Request.cpp:205
UrlDataManager fUrlDataManager
Definition Request.h:64
sk_sp< SkData > getJsonOps()
Definition Request.cpp:227
int getLastOp() const
Definition Request.h:46
Request(SkString rootUrl)
Definition Request.cpp:29
bool setColorMode(int mode)
Definition Request.cpp:176
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)
void roundOut(SkIRect *dst) const
Definition SkRect.h:1241