blob: 487da35424d4018d16d2a1f94a115a33455d9361 [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"
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
joshualitt40836102016-03-11 11:45:53 -080030#if SK_SUPPORT_GPU
joshualittee5348b2016-02-26 08:36:25 -080031 GrContextOptions grContextOpts;
joshualitt40836102016-03-11 11:45:53 -080032 fContextFactory = new GrContextFactory(grContextOpts);
33#else
34 fContextFactory = nullptr;
35#endif
36}
37
38Request::~Request() {
39#if SK_SUPPORT_GPU
40 if (fContextFactory) {
41 delete fContextFactory;
42 }
43#endif
joshualittee5348b2016-02-26 08:36:25 -080044}
45
joshualitt4dcbe432016-02-25 10:50:28 -080046SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
47 SkBitmap* bmp = new SkBitmap();
brianosman78312952016-04-19 10:16:53 -070048 bmp->setInfo(canvas->imageInfo());
joshualitt4dcbe432016-02-25 10:50:28 -080049 if (!canvas->readPixels(bmp, 0, 0)) {
50 fprintf(stderr, "Can't read pixels\n");
51 return nullptr;
52 }
53 return bmp;
54}
55
bungeman38d909e2016-08-02 14:40:46 -070056sk_sp<SkData> Request::writeCanvasToPng(SkCanvas* canvas) {
joshualitt4dcbe432016-02-25 10:50:28 -080057 // capture pixels
joshualitte0449cf2016-03-09 10:07:02 -080058 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
joshualitt4dcbe432016-02-25 10:50:28 -080059 SkASSERT(bmp);
60
brianosman78312952016-04-19 10:16:53 -070061 // Convert to format suitable for PNG output
62 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bmp);
63 SkASSERT(encodedBitmap.get());
64
msaretta5cf4f42016-06-30 10:06:51 -070065 // write to an opaque png (black background)
joshualitt4dcbe432016-02-25 10:50:28 -080066 SkDynamicMemoryWStream buffer;
halcanarya73d76a2016-10-17 13:19:02 -070067 SkDrawCommand::WritePNG(encodedBitmap->bytes(), bmp->width(), bmp->height(),
msaretta5cf4f42016-06-30 10:06:51 -070068 buffer, true);
reed42943c82016-09-12 12:01:44 -070069 return buffer.detachAsData();
joshualitt4dcbe432016-02-25 10:50:28 -080070}
71
72SkCanvas* Request::getCanvas() {
joshualitt40836102016-03-11 11:45:53 -080073#if SK_SUPPORT_GPU
joshualitt4dcbe432016-02-25 10:50:28 -080074 GrContextFactory* factory = fContextFactory;
bsalomon85b4b532016-04-05 11:06:27 -070075 GLTestContext* gl = factory->getContextInfo(GrContextFactory::kNativeGL_ContextType,
csmartdalton6270e552016-09-13 10:41:49 -070076 GrContextFactory::ContextOptions::kNone).glContext();
ethannicholas2ec06c92016-06-13 10:20:52 -070077 if (!gl) {
78 gl = factory->getContextInfo(GrContextFactory::kMESA_ContextType,
csmartdalton6270e552016-09-13 10:41:49 -070079 GrContextFactory::ContextOptions::kNone).glContext();
ethannicholas2ec06c92016-06-13 10:20:52 -070080 }
81 if (gl) {
82 gl->makeCurrent();
83 }
joshualitt40836102016-03-11 11:45:53 -080084#endif
joshualitt4dcbe432016-02-25 10:50:28 -080085 SkASSERT(fDebugCanvas);
joshualitte0449cf2016-03-09 10:07:02 -080086
87 // create the appropriate surface if necessary
88 if (!fSurface) {
89 this->enableGPU(fGPUEnabled);
90 }
joshualitt4dcbe432016-02-25 10:50:28 -080091 SkCanvas* target = fSurface->getCanvas();
92 return target;
93}
94
joshualitt46b301d2016-03-02 08:32:37 -080095void Request::drawToCanvas(int n, int m) {
joshualitt4dcbe432016-02-25 10:50:28 -080096 SkCanvas* target = this->getCanvas();
joshualitt46b301d2016-03-02 08:32:37 -080097 fDebugCanvas->drawTo(target, n, m);
joshualitt4dcbe432016-02-25 10:50:28 -080098}
99
bungeman38d909e2016-08-02 14:40:46 -0700100sk_sp<SkData> Request::drawToPng(int n, int m) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -0400101 //fDebugCanvas->setOverdrawViz(true);
joshualitt46b301d2016-03-02 08:32:37 -0800102 this->drawToCanvas(n, m);
Ben Wagnerc03e1c52016-10-17 15:20:02 -0400103 //fDebugCanvas->setOverdrawViz(false);
joshualitt4dcbe432016-02-25 10:50:28 -0800104 return writeCanvasToPng(this->getCanvas());
105}
106
bungeman38d909e2016-08-02 14:40:46 -0700107sk_sp<SkData> Request::writeOutSkp() {
joshualitte0449cf2016-03-09 10:07:02 -0800108 // Playback into picture recorder
joshualittae47aee2016-03-10 13:29:36 -0800109 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800110 SkPictureRecorder recorder;
brianosman82996b82016-04-20 10:52:54 -0700111 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()),
112 SkIntToScalar(bounds.height()));
joshualitte0449cf2016-03-09 10:07:02 -0800113
114 fDebugCanvas->draw(canvas);
115
reedca2622b2016-03-18 07:25:55 -0700116 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
joshualitte0449cf2016-03-09 10:07:02 -0800117
118 SkDynamicMemoryWStream outStream;
119
120 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
121 picture->serialize(&outStream, serializer);
122
reed42943c82016-09-12 12:01:44 -0700123 return outStream.detachAsData();
joshualitte0449cf2016-03-09 10:07:02 -0800124}
125
joshualittae47aee2016-03-10 13:29:36 -0800126GrContext* Request::getContext() {
joshualitt40836102016-03-11 11:45:53 -0800127#if SK_SUPPORT_GPU
ethannicholas2ec06c92016-06-13 10:20:52 -0700128 GrContext* result = fContextFactory->get(GrContextFactory::kNativeGL_ContextType,
csmartdalton6270e552016-09-13 10:41:49 -0700129 GrContextFactory::ContextOptions::kNone);
ethannicholas2ec06c92016-06-13 10:20:52 -0700130 if (!result) {
131 result = fContextFactory->get(GrContextFactory::kMESA_ContextType,
csmartdalton6270e552016-09-13 10:41:49 -0700132 GrContextFactory::ContextOptions::kNone);
jcgregorio9a7acdc2016-06-30 07:54:14 -0700133 }
ethannicholas2ec06c92016-06-13 10:20:52 -0700134 return result;
joshualitt40836102016-03-11 11:45:53 -0800135#else
ethannicholas2ec06c92016-06-13 10:20:52 -0700136 return nullptr;
joshualitt40836102016-03-11 11:45:53 -0800137#endif
joshualittae47aee2016-03-10 13:29:36 -0800138}
139
140SkIRect Request::getBounds() {
141 SkIRect bounds;
142 if (fPicture) {
143 bounds = fPicture->cullRect().roundOut();
144 if (fGPUEnabled) {
joshualitt40836102016-03-11 11:45:53 -0800145#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800146 int maxRTSize = this->getContext()->caps()->maxRenderTargetSize();
147 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
148 SkTMin(bounds.height(), maxRTSize));
joshualitt40836102016-03-11 11:45:53 -0800149#endif
joshualittae47aee2016-03-10 13:29:36 -0800150 }
151 } else {
152 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
153 }
154
jcgregorio9a7acdc2016-06-30 07:54:14 -0700155 // We clip to kMaxWidth / kMaxHeight for performance reasons.
joshualittae47aee2016-03-10 13:29:36 -0800156 // TODO make this configurable
jcgregorio9a7acdc2016-06-30 07:54:14 -0700157 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kMaxWidth),
158 SkTMin(bounds.height(), kMaxHeight));
joshualittae47aee2016-03-10 13:29:36 -0800159 return bounds;
160}
161
brianosman78312952016-04-19 10:16:53 -0700162namespace {
163
164struct ColorAndProfile {
165 SkColorType fColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700166 bool fSRGB;
brianosman78312952016-04-19 10:16:53 -0700167};
168
169ColorAndProfile ColorModes[] = {
brianosman3a0dbde2016-07-26 11:36:05 -0700170 { kN32_SkColorType, false },
171 { kN32_SkColorType, true },
172 { kRGBA_F16_SkColorType, true },
brianosman78312952016-04-19 10:16:53 -0700173};
174
175}
176
joshualitt4dcbe432016-02-25 10:50:28 -0800177SkSurface* Request::createCPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800178 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700179 ColorAndProfile cap = ColorModes[fColorMode];
raftias7c602de2016-10-13 10:45:44 -0700180 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
181 ? SkColorSpace::NewNamed(SkColorSpace::kSRGBLinear_Named)
182 : SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
brianosman78312952016-04-19 10:16:53 -0700183 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700184 kPremul_SkAlphaType, cap.fSRGB ? colorSpace : nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700185 return SkSurface::MakeRaster(info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800186}
187
188SkSurface* Request::createGPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800189 GrContext* context = this->getContext();
190 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700191 ColorAndProfile cap = ColorModes[fColorMode];
raftias7c602de2016-10-13 10:45:44 -0700192 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
193 ? SkColorSpace::NewNamed(SkColorSpace::kSRGBLinear_Named)
194 : SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
brianosman78312952016-04-19 10:16:53 -0700195 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700196 kPremul_SkAlphaType, cap.fSRGB ? colorSpace: nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700197 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800198 return surface;
199}
200
Ben Wagnerc03e1c52016-10-17 15:20:02 -0400201bool Request::setOverdraw(bool enable) {
202 fOverdraw = enable;
203 return true;
204}
205
brianosman78312952016-04-19 10:16:53 -0700206bool Request::setColorMode(int mode) {
207 fColorMode = mode;
208 return enableGPU(fGPUEnabled);
209}
210
halcanary9d524f22016-03-29 09:03:52 -0700211bool Request::enableGPU(bool enable) {
joshualittee5348b2016-02-26 08:36:25 -0800212 if (enable) {
213 SkSurface* surface = this->createGPUSurface();
214 if (surface) {
215 fSurface.reset(surface);
216 fGPUEnabled = true;
joshualitt98bd5b12016-03-11 12:08:15 -0800217
218 // When we switch to GPU, there seems to be some mystery draws in the canvas. So we
219 // draw once to flush the pipe
220 // TODO understand what is actually happening here
brianosman78312952016-04-19 10:16:53 -0700221 if (fDebugCanvas) {
222 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
223 this->getCanvas()->flush();
224 }
joshualitt98bd5b12016-03-11 12:08:15 -0800225
joshualittee5348b2016-02-26 08:36:25 -0800226 return true;
227 }
228 return false;
229 }
230 fSurface.reset(this->createCPUSurface());
231 fGPUEnabled = false;
232 return true;
halcanary9d524f22016-03-29 09:03:52 -0700233}
joshualitt6bc96792016-02-29 05:35:04 -0800234
235bool Request::initPictureFromStream(SkStream* stream) {
236 // parse picture from stream
reedca2622b2016-03-18 07:25:55 -0700237 fPicture = SkPicture::MakeFromStream(stream);
238 if (!fPicture) {
joshualitt6bc96792016-02-29 05:35:04 -0800239 fprintf(stderr, "Could not create picture from stream.\n");
240 return false;
241 }
242
joshualittae47aee2016-03-10 13:29:36 -0800243 // reinitialize canvas with the new picture dimensions
244 this->enableGPU(fGPUEnabled);
245
joshualitt6bc96792016-02-29 05:35:04 -0800246 // pour picture into debug canvas
joshualittae47aee2016-03-10 13:29:36 -0800247 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800248 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800249 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800250
251 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
252 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800253 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800254 return true;
joshualittee5348b2016-02-26 08:36:25 -0800255}
256
bungeman38d909e2016-08-02 14:40:46 -0700257sk_sp<SkData> Request::getJsonOps(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800258 SkCanvas* canvas = this->getCanvas();
259 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
260 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
joshualitt5d5207a2016-02-29 12:46:04 -0800261 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds());
brianosman78312952016-04-19 10:16:53 -0700262 root["colorMode"] = Json::Value(fColorMode);
joshualitt1e5884b2016-02-26 08:22:49 -0800263 SkDynamicMemoryWStream stream;
264 stream.writeText(Json::FastWriter().write(root).c_str());
265
reed42943c82016-09-12 12:01:44 -0700266 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800267}
268
bungeman38d909e2016-08-02 14:40:46 -0700269sk_sp<SkData> Request::getJsonBatchList(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800270 SkCanvas* canvas = this->getCanvas();
271 SkASSERT(fGPUEnabled);
272
joshualittae47aee2016-03-10 13:29:36 -0800273 Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800274
275 SkDynamicMemoryWStream stream;
joshualittae47aee2016-03-10 13:29:36 -0800276 stream.writeText(Json::FastWriter().write(result).c_str());
joshualitt1e5884b2016-02-26 08:22:49 -0800277
reed42943c82016-09-12 12:01:44 -0700278 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800279}
joshualittee5348b2016-02-26 08:36:25 -0800280
bungeman38d909e2016-08-02 14:40:46 -0700281sk_sp<SkData> Request::getJsonInfo(int n) {
joshualittee5348b2016-02-26 08:36:25 -0800282 // drawTo
283 SkAutoTUnref<SkSurface> surface(this->createCPUSurface());
284 SkCanvas* canvas = surface->getCanvas();
285
286 // TODO this is really slow and we should cache the matrix and clip
287 fDebugCanvas->drawTo(canvas, n);
288
289 // make some json
290 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
291 SkIRect clip = fDebugCanvas->getCurrentClip();
292 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800293 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
294 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800295
296 std::string json = Json::FastWriter().write(info);
297
298 // We don't want the null terminator so strlen is correct
bungeman38d909e2016-08-02 14:40:46 -0700299 return SkData::MakeWithCopy(json.c_str(), strlen(json.c_str()));
joshualittee5348b2016-02-26 08:36:25 -0800300}
joshualitte0449cf2016-03-09 10:07:02 -0800301
302SkColor Request::getPixel(int x, int y) {
303 SkCanvas* canvas = this->getCanvas();
304 canvas->flush();
305 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
306 SkASSERT(bitmap);
brianosman78312952016-04-19 10:16:53 -0700307
308 // Convert to format suitable for inspection
309 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap);
reed42943c82016-09-12 12:01:44 -0700310 SkASSERT(encodedBitmap);
brianosman78312952016-04-19 10:16:53 -0700311
312 const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) * 4);
joshualitte0449cf2016-03-09 10:07:02 -0800313 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
joshualitte0449cf2016-03-09 10:07:02 -0800314 return result;
315}