blob: 215f66733b5e88d8003661ea4b41324b6ad42f6e [file] [log] [blame]
joshualitt4dcbe432016-02-25 10:50:28 -08001/*
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
8#include "Request.h"
9
joshualitte0449cf2016-03-09 10:07:02 -080010#include "SkPictureRecorder.h"
brianosman312aa6a2016-04-19 12:47:54 -070011#include "SkPM4fPriv.h"
brianosman78312952016-04-19 10:16:53 -070012#include "picture_utils.h"
Hal Canarydb683012016-11-23 08:55:18 -070013#include "sk_tool_utils.h"
joshualitt4dcbe432016-02-25 10:50:28 -080014
bsalomon3724e572016-03-30 18:56:19 -070015using namespace sk_gpu_test;
16
joshualittae47aee2016-03-10 13:29:36 -080017static int kDefaultWidth = 1920;
18static int kDefaultHeight = 1080;
jcgregorio9a7acdc2016-06-30 07:54:14 -070019static int kMaxWidth = 8192;
20static int kMaxHeight = 8192;
joshualittae47aee2016-03-10 13:29:36 -080021
joshualitt4dcbe432016-02-25 10:50:28 -080022
joshualittee5348b2016-02-26 08:36:25 -080023Request::Request(SkString rootUrl)
24 : fUploadContext(nullptr)
25 , fUrlDataManager(rootUrl)
brianosman78312952016-04-19 10:16:53 -070026 , fGPUEnabled(false)
Ben Wagnerc03e1c52016-10-17 15:20:02 -040027 , fOverdraw(false)
brianosman78312952016-04-19 10:16:53 -070028 , fColorMode(0) {
joshualittee5348b2016-02-26 08:36:25 -080029 // create surface
30 GrContextOptions grContextOpts;
joshualitt40836102016-03-11 11:45:53 -080031 fContextFactory = new GrContextFactory(grContextOpts);
joshualitt40836102016-03-11 11:45:53 -080032}
33
34Request::~Request() {
joshualitt40836102016-03-11 11:45:53 -080035 if (fContextFactory) {
36 delete fContextFactory;
37 }
joshualittee5348b2016-02-26 08:36:25 -080038}
39
bungeman38d909e2016-08-02 14:40:46 -070040sk_sp<SkData> Request::writeCanvasToPng(SkCanvas* canvas) {
joshualitt4dcbe432016-02-25 10:50:28 -080041 // capture pixels
Brian Osmand9ea8162018-08-08 17:03:39 -040042 SkBitmap bmp;
43 bmp.allocPixels(canvas->imageInfo());
44 SkAssertResult(canvas->readPixels(bmp, 0, 0));
brianosman78312952016-04-19 10:16:53 -070045
msaretta5cf4f42016-06-30 10:06:51 -070046 // write to an opaque png (black background)
joshualitt4dcbe432016-02-25 10:50:28 -080047 SkDynamicMemoryWStream buffer;
Brian Osmand9ea8162018-08-08 17:03:39 -040048 SkDrawCommand::WritePNG(bmp, buffer);
reed42943c82016-09-12 12:01:44 -070049 return buffer.detachAsData();
joshualitt4dcbe432016-02-25 10:50:28 -080050}
51
52SkCanvas* Request::getCanvas() {
53 GrContextFactory* factory = fContextFactory;
Brian Salomon6405e712017-03-20 08:54:16 -040054 GLTestContext* gl = factory->getContextInfo(GrContextFactory::kGL_ContextType,
55 GrContextFactory::ContextOverrides::kNone).glContext();
56 if (!gl) {
57 gl = factory->getContextInfo(GrContextFactory::kGLES_ContextType,
58 GrContextFactory::ContextOverrides::kNone).glContext();
59 }
ethannicholas2ec06c92016-06-13 10:20:52 -070060 if (gl) {
61 gl->makeCurrent();
62 }
joshualitt4dcbe432016-02-25 10:50:28 -080063 SkASSERT(fDebugCanvas);
joshualitte0449cf2016-03-09 10:07:02 -080064
65 // create the appropriate surface if necessary
66 if (!fSurface) {
67 this->enableGPU(fGPUEnabled);
68 }
joshualitt4dcbe432016-02-25 10:50:28 -080069 SkCanvas* target = fSurface->getCanvas();
70 return target;
71}
72
bungeman38d909e2016-08-02 14:40:46 -070073sk_sp<SkData> Request::drawToPng(int n, int m) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040074 //fDebugCanvas->setOverdrawViz(true);
Brian Osmand9ea8162018-08-08 17:03:39 -040075 fDebugCanvas->drawTo(this->getCanvas(), n, m);
Ben Wagnerc03e1c52016-10-17 15:20:02 -040076 //fDebugCanvas->setOverdrawViz(false);
joshualitt4dcbe432016-02-25 10:50:28 -080077 return writeCanvasToPng(this->getCanvas());
78}
79
bungeman38d909e2016-08-02 14:40:46 -070080sk_sp<SkData> Request::writeOutSkp() {
joshualitte0449cf2016-03-09 10:07:02 -080081 // Playback into picture recorder
joshualittae47aee2016-03-10 13:29:36 -080082 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -080083 SkPictureRecorder recorder;
brianosman82996b82016-04-20 10:52:54 -070084 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()),
85 SkIntToScalar(bounds.height()));
joshualitte0449cf2016-03-09 10:07:02 -080086
87 fDebugCanvas->draw(canvas);
88
Mike Reedef038482017-12-16 08:41:28 -050089 return recorder.finishRecordingAsPicture()->serialize();
joshualitte0449cf2016-03-09 10:07:02 -080090}
91
joshualittae47aee2016-03-10 13:29:36 -080092GrContext* Request::getContext() {
Brian Salomon6405e712017-03-20 08:54:16 -040093 GrContext* result = fContextFactory->get(GrContextFactory::kGL_ContextType,
csmartdaltone812d492017-02-21 12:36:05 -070094 GrContextFactory::ContextOverrides::kNone);
ethannicholas2ec06c92016-06-13 10:20:52 -070095 if (!result) {
Brian Salomon6405e712017-03-20 08:54:16 -040096 result = fContextFactory->get(GrContextFactory::kGLES_ContextType,
97 GrContextFactory::ContextOverrides::kNone);
98 }
ethannicholas2ec06c92016-06-13 10:20:52 -070099 return result;
joshualittae47aee2016-03-10 13:29:36 -0800100}
101
102SkIRect Request::getBounds() {
103 SkIRect bounds;
104 if (fPicture) {
105 bounds = fPicture->cullRect().roundOut();
106 if (fGPUEnabled) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400107 int maxRTSize = this->getContext()->maxRenderTargetSize();
joshualittae47aee2016-03-10 13:29:36 -0800108 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
109 SkTMin(bounds.height(), maxRTSize));
110 }
111 } else {
112 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
113 }
114
jcgregorio9a7acdc2016-06-30 07:54:14 -0700115 // We clip to kMaxWidth / kMaxHeight for performance reasons.
joshualittae47aee2016-03-10 13:29:36 -0800116 // TODO make this configurable
jcgregorio9a7acdc2016-06-30 07:54:14 -0700117 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kMaxWidth),
118 SkTMin(bounds.height(), kMaxHeight));
joshualittae47aee2016-03-10 13:29:36 -0800119 return bounds;
120}
121
brianosman78312952016-04-19 10:16:53 -0700122namespace {
123
124struct ColorAndProfile {
125 SkColorType fColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700126 bool fSRGB;
brianosman78312952016-04-19 10:16:53 -0700127};
128
129ColorAndProfile ColorModes[] = {
brianosman3a0dbde2016-07-26 11:36:05 -0700130 { kN32_SkColorType, false },
131 { kN32_SkColorType, true },
132 { kRGBA_F16_SkColorType, true },
brianosman78312952016-04-19 10:16:53 -0700133};
134
135}
136
joshualitt4dcbe432016-02-25 10:50:28 -0800137SkSurface* Request::createCPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800138 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700139 ColorAndProfile cap = ColorModes[fColorMode];
raftias7c602de2016-10-13 10:45:44 -0700140 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500141 ? SkColorSpace::MakeSRGBLinear()
142 : SkColorSpace::MakeSRGB();
brianosman78312952016-04-19 10:16:53 -0700143 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700144 kPremul_SkAlphaType, cap.fSRGB ? colorSpace : nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700145 return SkSurface::MakeRaster(info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800146}
147
148SkSurface* Request::createGPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800149 GrContext* context = this->getContext();
150 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700151 ColorAndProfile cap = ColorModes[fColorMode];
raftias7c602de2016-10-13 10:45:44 -0700152 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500153 ? SkColorSpace::MakeSRGBLinear()
154 : SkColorSpace::MakeSRGB();
brianosman78312952016-04-19 10:16:53 -0700155 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700156 kPremul_SkAlphaType, cap.fSRGB ? colorSpace: nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700157 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800158 return surface;
159}
160
Ben Wagnerc03e1c52016-10-17 15:20:02 -0400161bool Request::setOverdraw(bool enable) {
162 fOverdraw = enable;
163 return true;
164}
165
brianosman78312952016-04-19 10:16:53 -0700166bool Request::setColorMode(int mode) {
167 fColorMode = mode;
168 return enableGPU(fGPUEnabled);
169}
170
halcanary9d524f22016-03-29 09:03:52 -0700171bool Request::enableGPU(bool enable) {
joshualittee5348b2016-02-26 08:36:25 -0800172 if (enable) {
173 SkSurface* surface = this->createGPUSurface();
174 if (surface) {
175 fSurface.reset(surface);
176 fGPUEnabled = true;
joshualitt98bd5b12016-03-11 12:08:15 -0800177
178 // When we switch to GPU, there seems to be some mystery draws in the canvas. So we
179 // draw once to flush the pipe
180 // TODO understand what is actually happening here
brianosman78312952016-04-19 10:16:53 -0700181 if (fDebugCanvas) {
182 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
183 this->getCanvas()->flush();
184 }
joshualitt98bd5b12016-03-11 12:08:15 -0800185
joshualittee5348b2016-02-26 08:36:25 -0800186 return true;
187 }
188 return false;
189 }
190 fSurface.reset(this->createCPUSurface());
191 fGPUEnabled = false;
192 return true;
halcanary9d524f22016-03-29 09:03:52 -0700193}
joshualitt6bc96792016-02-29 05:35:04 -0800194
195bool Request::initPictureFromStream(SkStream* stream) {
196 // parse picture from stream
reedca2622b2016-03-18 07:25:55 -0700197 fPicture = SkPicture::MakeFromStream(stream);
198 if (!fPicture) {
joshualitt6bc96792016-02-29 05:35:04 -0800199 fprintf(stderr, "Could not create picture from stream.\n");
200 return false;
201 }
202
joshualittae47aee2016-03-10 13:29:36 -0800203 // reinitialize canvas with the new picture dimensions
204 this->enableGPU(fGPUEnabled);
205
joshualitt6bc96792016-02-29 05:35:04 -0800206 // pour picture into debug canvas
joshualittae47aee2016-03-10 13:29:36 -0800207 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800208 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800209 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800210
211 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
212 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800213 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800214 return true;
joshualittee5348b2016-02-26 08:36:25 -0800215}
216
bungeman38d909e2016-08-02 14:40:46 -0700217sk_sp<SkData> Request::getJsonOps(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800218 SkCanvas* canvas = this->getCanvas();
219 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
220 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
Brian Salomonf09492b2016-12-21 15:40:26 -0500221 root["drawGpuOpBounds"] = Json::Value(fDebugCanvas->getDrawGpuOpBounds());
brianosman78312952016-04-19 10:16:53 -0700222 root["colorMode"] = Json::Value(fColorMode);
joshualitt1e5884b2016-02-26 08:22:49 -0800223 SkDynamicMemoryWStream stream;
224 stream.writeText(Json::FastWriter().write(root).c_str());
225
reed42943c82016-09-12 12:01:44 -0700226 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800227}
228
Brian Salomon144a5c52016-12-20 16:48:59 -0500229sk_sp<SkData> Request::getJsonOpList(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800230 SkCanvas* canvas = this->getCanvas();
231 SkASSERT(fGPUEnabled);
232
Brian Salomon144a5c52016-12-20 16:48:59 -0500233 Json::Value result = fDebugCanvas->toJSONOpList(n, canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800234
235 SkDynamicMemoryWStream stream;
joshualittae47aee2016-03-10 13:29:36 -0800236 stream.writeText(Json::FastWriter().write(result).c_str());
joshualitt1e5884b2016-02-26 08:22:49 -0800237
reed42943c82016-09-12 12:01:44 -0700238 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800239}
joshualittee5348b2016-02-26 08:36:25 -0800240
bungeman38d909e2016-08-02 14:40:46 -0700241sk_sp<SkData> Request::getJsonInfo(int n) {
joshualittee5348b2016-02-26 08:36:25 -0800242 // drawTo
Hal Canary1b612a82016-11-03 16:26:13 -0400243 sk_sp<SkSurface> surface(this->createCPUSurface());
joshualittee5348b2016-02-26 08:36:25 -0800244 SkCanvas* canvas = surface->getCanvas();
245
246 // TODO this is really slow and we should cache the matrix and clip
247 fDebugCanvas->drawTo(canvas, n);
248
249 // make some json
250 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
251 SkIRect clip = fDebugCanvas->getCurrentClip();
252 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800253 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
254 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800255
256 std::string json = Json::FastWriter().write(info);
257
258 // We don't want the null terminator so strlen is correct
bungeman38d909e2016-08-02 14:40:46 -0700259 return SkData::MakeWithCopy(json.c_str(), strlen(json.c_str()));
joshualittee5348b2016-02-26 08:36:25 -0800260}
joshualitte0449cf2016-03-09 10:07:02 -0800261
262SkColor Request::getPixel(int x, int y) {
Brian Osmand9ea8162018-08-08 17:03:39 -0400263 SkBitmap bmp;
264 bmp.allocPixels(this->getCanvas()->imageInfo().makeWH(1, 1));
265 SkAssertResult(this->getCanvas()->readPixels(bmp, x, y));
266 return bmp.getColor(0, 0);
joshualitte0449cf2016-03-09 10:07:02 -0800267}