blob: 7c2b893ef2f6460d4f42c5b69b94baddacd3c226 [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"
11#include "SkPixelSerializer.h"
brianosman312aa6a2016-04-19 12:47:54 -070012#include "SkPM4fPriv.h"
brianosman78312952016-04-19 10:16:53 -070013#include "picture_utils.h"
Hal Canarydb683012016-11-23 08:55:18 -070014#include "sk_tool_utils.h"
joshualitt4dcbe432016-02-25 10:50:28 -080015
bsalomon3724e572016-03-30 18:56:19 -070016using namespace sk_gpu_test;
17
joshualittae47aee2016-03-10 13:29:36 -080018static int kDefaultWidth = 1920;
19static int kDefaultHeight = 1080;
jcgregorio9a7acdc2016-06-30 07:54:14 -070020static int kMaxWidth = 8192;
21static int kMaxHeight = 8192;
joshualittae47aee2016-03-10 13:29:36 -080022
joshualitt4dcbe432016-02-25 10:50:28 -080023
joshualittee5348b2016-02-26 08:36:25 -080024Request::Request(SkString rootUrl)
25 : fUploadContext(nullptr)
26 , fUrlDataManager(rootUrl)
brianosman78312952016-04-19 10:16:53 -070027 , fGPUEnabled(false)
Ben Wagnerc03e1c52016-10-17 15:20:02 -040028 , fOverdraw(false)
brianosman78312952016-04-19 10:16:53 -070029 , fColorMode(0) {
joshualittee5348b2016-02-26 08:36:25 -080030 // create surface
joshualitt40836102016-03-11 11:45:53 -080031#if SK_SUPPORT_GPU
joshualittee5348b2016-02-26 08:36:25 -080032 GrContextOptions grContextOpts;
joshualitt40836102016-03-11 11:45:53 -080033 fContextFactory = new GrContextFactory(grContextOpts);
34#else
35 fContextFactory = nullptr;
36#endif
37}
38
39Request::~Request() {
40#if SK_SUPPORT_GPU
41 if (fContextFactory) {
42 delete fContextFactory;
43 }
44#endif
joshualittee5348b2016-02-26 08:36:25 -080045}
46
joshualitt4dcbe432016-02-25 10:50:28 -080047SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
48 SkBitmap* bmp = new SkBitmap();
brianosman78312952016-04-19 10:16:53 -070049 bmp->setInfo(canvas->imageInfo());
joshualitt4dcbe432016-02-25 10:50:28 -080050 if (!canvas->readPixels(bmp, 0, 0)) {
51 fprintf(stderr, "Can't read pixels\n");
52 return nullptr;
53 }
54 return bmp;
55}
56
bungeman38d909e2016-08-02 14:40:46 -070057sk_sp<SkData> Request::writeCanvasToPng(SkCanvas* canvas) {
joshualitt4dcbe432016-02-25 10:50:28 -080058 // capture pixels
Ben Wagner145dbcd2016-11-03 14:40:50 -040059 std::unique_ptr<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
joshualitt4dcbe432016-02-25 10:50:28 -080060 SkASSERT(bmp);
61
brianosman78312952016-04-19 10:16:53 -070062 // Convert to format suitable for PNG output
63 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bmp);
64 SkASSERT(encodedBitmap.get());
65
msaretta5cf4f42016-06-30 10:06:51 -070066 // write to an opaque png (black background)
joshualitt4dcbe432016-02-25 10:50:28 -080067 SkDynamicMemoryWStream buffer;
halcanarya73d76a2016-10-17 13:19:02 -070068 SkDrawCommand::WritePNG(encodedBitmap->bytes(), bmp->width(), bmp->height(),
msaretta5cf4f42016-06-30 10:06:51 -070069 buffer, true);
reed42943c82016-09-12 12:01:44 -070070 return buffer.detachAsData();
joshualitt4dcbe432016-02-25 10:50:28 -080071}
72
73SkCanvas* Request::getCanvas() {
joshualitt40836102016-03-11 11:45:53 -080074#if SK_SUPPORT_GPU
joshualitt4dcbe432016-02-25 10:50:28 -080075 GrContextFactory* factory = fContextFactory;
Brian Salomon6405e712017-03-20 08:54:16 -040076 GLTestContext* gl = factory->getContextInfo(GrContextFactory::kGL_ContextType,
77 GrContextFactory::ContextOverrides::kNone).glContext();
78 if (!gl) {
79 gl = factory->getContextInfo(GrContextFactory::kGLES_ContextType,
80 GrContextFactory::ContextOverrides::kNone).glContext();
81 }
ethannicholas2ec06c92016-06-13 10:20:52 -070082 if (!gl) {
83 gl = factory->getContextInfo(GrContextFactory::kMESA_ContextType,
csmartdaltone812d492017-02-21 12:36:05 -070084 GrContextFactory::ContextOverrides::kNone).glContext();
ethannicholas2ec06c92016-06-13 10:20:52 -070085 }
86 if (gl) {
87 gl->makeCurrent();
88 }
joshualitt40836102016-03-11 11:45:53 -080089#endif
joshualitt4dcbe432016-02-25 10:50:28 -080090 SkASSERT(fDebugCanvas);
joshualitte0449cf2016-03-09 10:07:02 -080091
92 // create the appropriate surface if necessary
93 if (!fSurface) {
94 this->enableGPU(fGPUEnabled);
95 }
joshualitt4dcbe432016-02-25 10:50:28 -080096 SkCanvas* target = fSurface->getCanvas();
97 return target;
98}
99
joshualitt46b301d2016-03-02 08:32:37 -0800100void Request::drawToCanvas(int n, int m) {
joshualitt4dcbe432016-02-25 10:50:28 -0800101 SkCanvas* target = this->getCanvas();
joshualitt46b301d2016-03-02 08:32:37 -0800102 fDebugCanvas->drawTo(target, n, m);
joshualitt4dcbe432016-02-25 10:50:28 -0800103}
104
bungeman38d909e2016-08-02 14:40:46 -0700105sk_sp<SkData> Request::drawToPng(int n, int m) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -0400106 //fDebugCanvas->setOverdrawViz(true);
joshualitt46b301d2016-03-02 08:32:37 -0800107 this->drawToCanvas(n, m);
Ben Wagnerc03e1c52016-10-17 15:20:02 -0400108 //fDebugCanvas->setOverdrawViz(false);
joshualitt4dcbe432016-02-25 10:50:28 -0800109 return writeCanvasToPng(this->getCanvas());
110}
111
bungeman38d909e2016-08-02 14:40:46 -0700112sk_sp<SkData> Request::writeOutSkp() {
joshualitte0449cf2016-03-09 10:07:02 -0800113 // Playback into picture recorder
joshualittae47aee2016-03-10 13:29:36 -0800114 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800115 SkPictureRecorder recorder;
brianosman82996b82016-04-20 10:52:54 -0700116 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()),
117 SkIntToScalar(bounds.height()));
joshualitte0449cf2016-03-09 10:07:02 -0800118
119 fDebugCanvas->draw(canvas);
120
reedca2622b2016-03-18 07:25:55 -0700121 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
joshualitte0449cf2016-03-09 10:07:02 -0800122
123 SkDynamicMemoryWStream outStream;
124
Hal Canarydb683012016-11-23 08:55:18 -0700125 sk_sp<SkPixelSerializer> serializer = sk_tool_utils::MakePixelSerializer();
Hal Canary1b612a82016-11-03 16:26:13 -0400126 picture->serialize(&outStream, serializer.get());
joshualitte0449cf2016-03-09 10:07:02 -0800127
reed42943c82016-09-12 12:01:44 -0700128 return outStream.detachAsData();
joshualitte0449cf2016-03-09 10:07:02 -0800129}
130
joshualittae47aee2016-03-10 13:29:36 -0800131GrContext* Request::getContext() {
joshualitt40836102016-03-11 11:45:53 -0800132#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400133 GrContext* result = fContextFactory->get(GrContextFactory::kGL_ContextType,
csmartdaltone812d492017-02-21 12:36:05 -0700134 GrContextFactory::ContextOverrides::kNone);
ethannicholas2ec06c92016-06-13 10:20:52 -0700135 if (!result) {
Brian Salomon6405e712017-03-20 08:54:16 -0400136 result = fContextFactory->get(GrContextFactory::kGLES_ContextType,
137 GrContextFactory::ContextOverrides::kNone);
138 }
139 if (!result) {
ethannicholas2ec06c92016-06-13 10:20:52 -0700140 result = fContextFactory->get(GrContextFactory::kMESA_ContextType,
csmartdaltone812d492017-02-21 12:36:05 -0700141 GrContextFactory::ContextOverrides::kNone);
jcgregorio9a7acdc2016-06-30 07:54:14 -0700142 }
ethannicholas2ec06c92016-06-13 10:20:52 -0700143 return result;
joshualitt40836102016-03-11 11:45:53 -0800144#else
ethannicholas2ec06c92016-06-13 10:20:52 -0700145 return nullptr;
joshualitt40836102016-03-11 11:45:53 -0800146#endif
joshualittae47aee2016-03-10 13:29:36 -0800147}
148
149SkIRect Request::getBounds() {
150 SkIRect bounds;
151 if (fPicture) {
152 bounds = fPicture->cullRect().roundOut();
153 if (fGPUEnabled) {
joshualitt40836102016-03-11 11:45:53 -0800154#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800155 int maxRTSize = this->getContext()->caps()->maxRenderTargetSize();
156 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
157 SkTMin(bounds.height(), maxRTSize));
joshualitt40836102016-03-11 11:45:53 -0800158#endif
joshualittae47aee2016-03-10 13:29:36 -0800159 }
160 } else {
161 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
162 }
163
jcgregorio9a7acdc2016-06-30 07:54:14 -0700164 // We clip to kMaxWidth / kMaxHeight for performance reasons.
joshualittae47aee2016-03-10 13:29:36 -0800165 // TODO make this configurable
jcgregorio9a7acdc2016-06-30 07:54:14 -0700166 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kMaxWidth),
167 SkTMin(bounds.height(), kMaxHeight));
joshualittae47aee2016-03-10 13:29:36 -0800168 return bounds;
169}
170
brianosman78312952016-04-19 10:16:53 -0700171namespace {
172
173struct ColorAndProfile {
174 SkColorType fColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700175 bool fSRGB;
brianosman78312952016-04-19 10:16:53 -0700176};
177
178ColorAndProfile ColorModes[] = {
brianosman3a0dbde2016-07-26 11:36:05 -0700179 { kN32_SkColorType, false },
180 { kN32_SkColorType, true },
181 { kRGBA_F16_SkColorType, true },
brianosman78312952016-04-19 10:16:53 -0700182};
183
184}
185
joshualitt4dcbe432016-02-25 10:50:28 -0800186SkSurface* Request::createCPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800187 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700188 ColorAndProfile cap = ColorModes[fColorMode];
raftias7c602de2016-10-13 10:45:44 -0700189 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500190 ? SkColorSpace::MakeSRGBLinear()
191 : SkColorSpace::MakeSRGB();
brianosman78312952016-04-19 10:16:53 -0700192 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700193 kPremul_SkAlphaType, cap.fSRGB ? colorSpace : nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700194 return SkSurface::MakeRaster(info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800195}
196
197SkSurface* Request::createGPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800198 GrContext* context = this->getContext();
199 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700200 ColorAndProfile cap = ColorModes[fColorMode];
raftias7c602de2016-10-13 10:45:44 -0700201 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500202 ? SkColorSpace::MakeSRGBLinear()
203 : SkColorSpace::MakeSRGB();
brianosman78312952016-04-19 10:16:53 -0700204 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700205 kPremul_SkAlphaType, cap.fSRGB ? colorSpace: nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700206 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800207 return surface;
208}
209
Ben Wagnerc03e1c52016-10-17 15:20:02 -0400210bool Request::setOverdraw(bool enable) {
211 fOverdraw = enable;
212 return true;
213}
214
brianosman78312952016-04-19 10:16:53 -0700215bool Request::setColorMode(int mode) {
216 fColorMode = mode;
217 return enableGPU(fGPUEnabled);
218}
219
halcanary9d524f22016-03-29 09:03:52 -0700220bool Request::enableGPU(bool enable) {
joshualittee5348b2016-02-26 08:36:25 -0800221 if (enable) {
222 SkSurface* surface = this->createGPUSurface();
223 if (surface) {
224 fSurface.reset(surface);
225 fGPUEnabled = true;
joshualitt98bd5b12016-03-11 12:08:15 -0800226
227 // When we switch to GPU, there seems to be some mystery draws in the canvas. So we
228 // draw once to flush the pipe
229 // TODO understand what is actually happening here
brianosman78312952016-04-19 10:16:53 -0700230 if (fDebugCanvas) {
231 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
232 this->getCanvas()->flush();
233 }
joshualitt98bd5b12016-03-11 12:08:15 -0800234
joshualittee5348b2016-02-26 08:36:25 -0800235 return true;
236 }
237 return false;
238 }
239 fSurface.reset(this->createCPUSurface());
240 fGPUEnabled = false;
241 return true;
halcanary9d524f22016-03-29 09:03:52 -0700242}
joshualitt6bc96792016-02-29 05:35:04 -0800243
244bool Request::initPictureFromStream(SkStream* stream) {
245 // parse picture from stream
reedca2622b2016-03-18 07:25:55 -0700246 fPicture = SkPicture::MakeFromStream(stream);
247 if (!fPicture) {
joshualitt6bc96792016-02-29 05:35:04 -0800248 fprintf(stderr, "Could not create picture from stream.\n");
249 return false;
250 }
251
joshualittae47aee2016-03-10 13:29:36 -0800252 // reinitialize canvas with the new picture dimensions
253 this->enableGPU(fGPUEnabled);
254
joshualitt6bc96792016-02-29 05:35:04 -0800255 // pour picture into debug canvas
joshualittae47aee2016-03-10 13:29:36 -0800256 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800257 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800258 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800259
260 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
261 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800262 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800263 return true;
joshualittee5348b2016-02-26 08:36:25 -0800264}
265
bungeman38d909e2016-08-02 14:40:46 -0700266sk_sp<SkData> Request::getJsonOps(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800267 SkCanvas* canvas = this->getCanvas();
268 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
269 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
Brian Salomonf09492b2016-12-21 15:40:26 -0500270 root["drawGpuOpBounds"] = Json::Value(fDebugCanvas->getDrawGpuOpBounds());
brianosman78312952016-04-19 10:16:53 -0700271 root["colorMode"] = Json::Value(fColorMode);
joshualitt1e5884b2016-02-26 08:22:49 -0800272 SkDynamicMemoryWStream stream;
273 stream.writeText(Json::FastWriter().write(root).c_str());
274
reed42943c82016-09-12 12:01:44 -0700275 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800276}
277
Brian Salomon144a5c52016-12-20 16:48:59 -0500278sk_sp<SkData> Request::getJsonOpList(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800279 SkCanvas* canvas = this->getCanvas();
280 SkASSERT(fGPUEnabled);
281
Brian Salomon144a5c52016-12-20 16:48:59 -0500282 Json::Value result = fDebugCanvas->toJSONOpList(n, canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800283
284 SkDynamicMemoryWStream stream;
joshualittae47aee2016-03-10 13:29:36 -0800285 stream.writeText(Json::FastWriter().write(result).c_str());
joshualitt1e5884b2016-02-26 08:22:49 -0800286
reed42943c82016-09-12 12:01:44 -0700287 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800288}
joshualittee5348b2016-02-26 08:36:25 -0800289
bungeman38d909e2016-08-02 14:40:46 -0700290sk_sp<SkData> Request::getJsonInfo(int n) {
joshualittee5348b2016-02-26 08:36:25 -0800291 // drawTo
Hal Canary1b612a82016-11-03 16:26:13 -0400292 sk_sp<SkSurface> surface(this->createCPUSurface());
joshualittee5348b2016-02-26 08:36:25 -0800293 SkCanvas* canvas = surface->getCanvas();
294
295 // TODO this is really slow and we should cache the matrix and clip
296 fDebugCanvas->drawTo(canvas, n);
297
298 // make some json
299 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
300 SkIRect clip = fDebugCanvas->getCurrentClip();
301 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800302 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
303 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800304
305 std::string json = Json::FastWriter().write(info);
306
307 // We don't want the null terminator so strlen is correct
bungeman38d909e2016-08-02 14:40:46 -0700308 return SkData::MakeWithCopy(json.c_str(), strlen(json.c_str()));
joshualittee5348b2016-02-26 08:36:25 -0800309}
joshualitte0449cf2016-03-09 10:07:02 -0800310
311SkColor Request::getPixel(int x, int y) {
312 SkCanvas* canvas = this->getCanvas();
313 canvas->flush();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400314 std::unique_ptr<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
joshualitte0449cf2016-03-09 10:07:02 -0800315 SkASSERT(bitmap);
brianosman78312952016-04-19 10:16:53 -0700316
317 // Convert to format suitable for inspection
318 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap);
reed42943c82016-09-12 12:01:44 -0700319 SkASSERT(encodedBitmap);
brianosman78312952016-04-19 10:16:53 -0700320
321 const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) * 4);
joshualitte0449cf2016-03-09 10:07:02 -0800322 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
joshualitte0449cf2016-03-09 10:07:02 -0800323 return result;
324}