blob: 7cbc9ef212d9a80a99be06c90352ec8fb25c28a3 [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;
19
joshualitt4dcbe432016-02-25 10:50:28 -080020
joshualittee5348b2016-02-26 08:36:25 -080021Request::Request(SkString rootUrl)
22 : fUploadContext(nullptr)
23 , fUrlDataManager(rootUrl)
brianosman78312952016-04-19 10:16:53 -070024 , fGPUEnabled(false)
25 , fColorMode(0) {
joshualittee5348b2016-02-26 08:36:25 -080026 // create surface
joshualitt40836102016-03-11 11:45:53 -080027#if SK_SUPPORT_GPU
joshualittee5348b2016-02-26 08:36:25 -080028 GrContextOptions grContextOpts;
joshualitt40836102016-03-11 11:45:53 -080029 fContextFactory = new GrContextFactory(grContextOpts);
30#else
31 fContextFactory = nullptr;
32#endif
33}
34
35Request::~Request() {
36#if SK_SUPPORT_GPU
37 if (fContextFactory) {
38 delete fContextFactory;
39 }
40#endif
joshualittee5348b2016-02-26 08:36:25 -080041}
42
joshualitt4dcbe432016-02-25 10:50:28 -080043SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
44 SkBitmap* bmp = new SkBitmap();
brianosman78312952016-04-19 10:16:53 -070045 bmp->setInfo(canvas->imageInfo());
joshualitt4dcbe432016-02-25 10:50:28 -080046 if (!canvas->readPixels(bmp, 0, 0)) {
47 fprintf(stderr, "Can't read pixels\n");
48 return nullptr;
49 }
50 return bmp;
51}
52
53SkData* Request::writeCanvasToPng(SkCanvas* canvas) {
54 // capture pixels
joshualitte0449cf2016-03-09 10:07:02 -080055 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
joshualitt4dcbe432016-02-25 10:50:28 -080056 SkASSERT(bmp);
57
brianosman78312952016-04-19 10:16:53 -070058 // Convert to format suitable for PNG output
59 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bmp);
60 SkASSERT(encodedBitmap.get());
61
joshualitt4dcbe432016-02-25 10:50:28 -080062 // write to png
63 SkDynamicMemoryWStream buffer;
brianosman78312952016-04-19 10:16:53 -070064 SkDrawCommand::WritePNG((const png_bytep) encodedBitmap->writable_data(),
65 bmp->width(), bmp->height(),
ethannicholasf67531f2016-03-21 10:19:39 -070066 buffer);
joshualitt4dcbe432016-02-25 10:50:28 -080067 return buffer.copyToData();
68}
69
70SkCanvas* Request::getCanvas() {
joshualitt40836102016-03-11 11:45:53 -080071#if SK_SUPPORT_GPU
joshualitt4dcbe432016-02-25 10:50:28 -080072 GrContextFactory* factory = fContextFactory;
bsalomon85b4b532016-04-05 11:06:27 -070073 GLTestContext* gl = factory->getContextInfo(GrContextFactory::kNativeGL_ContextType,
74 GrContextFactory::kNone_ContextOptions).fGLContext;
joshualitt4dcbe432016-02-25 10:50:28 -080075 gl->makeCurrent();
joshualitt40836102016-03-11 11:45:53 -080076#endif
joshualitt4dcbe432016-02-25 10:50:28 -080077 SkASSERT(fDebugCanvas);
joshualitte0449cf2016-03-09 10:07:02 -080078
79 // create the appropriate surface if necessary
80 if (!fSurface) {
81 this->enableGPU(fGPUEnabled);
82 }
joshualitt4dcbe432016-02-25 10:50:28 -080083 SkCanvas* target = fSurface->getCanvas();
84 return target;
85}
86
joshualitt46b301d2016-03-02 08:32:37 -080087void Request::drawToCanvas(int n, int m) {
joshualitt4dcbe432016-02-25 10:50:28 -080088 SkCanvas* target = this->getCanvas();
joshualitt46b301d2016-03-02 08:32:37 -080089 fDebugCanvas->drawTo(target, n, m);
joshualitt4dcbe432016-02-25 10:50:28 -080090}
91
joshualitt46b301d2016-03-02 08:32:37 -080092SkData* Request::drawToPng(int n, int m) {
93 this->drawToCanvas(n, m);
joshualitt4dcbe432016-02-25 10:50:28 -080094 return writeCanvasToPng(this->getCanvas());
95}
96
joshualitte0449cf2016-03-09 10:07:02 -080097SkData* Request::writeOutSkp() {
98 // Playback into picture recorder
joshualittae47aee2016-03-10 13:29:36 -080099 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800100 SkPictureRecorder recorder;
101 SkCanvas* canvas = recorder.beginRecording(bounds.width(), bounds.height());
102
103 fDebugCanvas->draw(canvas);
104
reedca2622b2016-03-18 07:25:55 -0700105 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
joshualitte0449cf2016-03-09 10:07:02 -0800106
107 SkDynamicMemoryWStream outStream;
108
109 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
110 picture->serialize(&outStream, serializer);
111
112 return outStream.copyToData();
113}
114
joshualittae47aee2016-03-10 13:29:36 -0800115GrContext* Request::getContext() {
joshualitt40836102016-03-11 11:45:53 -0800116#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700117 return fContextFactory->get(GrContextFactory::kNativeGL_ContextType,
118 GrContextFactory::kNone_ContextOptions);
joshualitt40836102016-03-11 11:45:53 -0800119#else
120 return nullptr;
121#endif
joshualittae47aee2016-03-10 13:29:36 -0800122}
123
124SkIRect Request::getBounds() {
125 SkIRect bounds;
126 if (fPicture) {
127 bounds = fPicture->cullRect().roundOut();
128 if (fGPUEnabled) {
joshualitt40836102016-03-11 11:45:53 -0800129#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800130 int maxRTSize = this->getContext()->caps()->maxRenderTargetSize();
131 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
132 SkTMin(bounds.height(), maxRTSize));
joshualitt40836102016-03-11 11:45:53 -0800133#endif
joshualittae47aee2016-03-10 13:29:36 -0800134 }
135 } else {
136 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
137 }
138
139 // We clip to kDefaultWidth / kDefaultHeight for performance reasons
140 // TODO make this configurable
141 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth),
142 SkTMin(bounds.height(), kDefaultHeight));
143 return bounds;
144}
145
brianosman78312952016-04-19 10:16:53 -0700146namespace {
147
148struct ColorAndProfile {
149 SkColorType fColorType;
150 SkColorProfileType fProfileType;
151 bool fGammaCorrect;
152};
153
154ColorAndProfile ColorModes[] = {
155 { kN32_SkColorType, kLinear_SkColorProfileType, false },
156 { kN32_SkColorType, kSRGB_SkColorProfileType, true },
157 { kRGBA_F16_SkColorType, kLinear_SkColorProfileType, true },
158};
159
160}
161
joshualitt4dcbe432016-02-25 10:50:28 -0800162SkSurface* Request::createCPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800163 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700164 ColorAndProfile cap = ColorModes[fColorMode];
165 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
166 kPremul_SkAlphaType, cap.fProfileType);
167 uint32_t flags = cap.fGammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0;
168 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
169 return SkSurface::MakeRaster(info, &props).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800170}
171
172SkSurface* Request::createGPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800173 GrContext* context = this->getContext();
174 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700175 ColorAndProfile cap = ColorModes[fColorMode];
176 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
177 kPremul_SkAlphaType, cap.fProfileType);
178 uint32_t flags = cap.fGammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0;
joshualitt4dcbe432016-02-25 10:50:28 -0800179 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700180 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
181 &props).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800182 return surface;
183}
184
brianosman78312952016-04-19 10:16:53 -0700185bool Request::setColorMode(int mode) {
186 fColorMode = mode;
187 return enableGPU(fGPUEnabled);
188}
189
brianosman312aa6a2016-04-19 12:47:54 -0700190bool Request::setSRGBMode(bool enable) {
191 gTreatSkColorAsSRGB = enable;
192 return true;
193}
194
halcanary9d524f22016-03-29 09:03:52 -0700195bool Request::enableGPU(bool enable) {
joshualittee5348b2016-02-26 08:36:25 -0800196 if (enable) {
197 SkSurface* surface = this->createGPUSurface();
198 if (surface) {
199 fSurface.reset(surface);
200 fGPUEnabled = true;
joshualitt98bd5b12016-03-11 12:08:15 -0800201
202 // When we switch to GPU, there seems to be some mystery draws in the canvas. So we
203 // draw once to flush the pipe
204 // TODO understand what is actually happening here
brianosman78312952016-04-19 10:16:53 -0700205 if (fDebugCanvas) {
206 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
207 this->getCanvas()->flush();
208 }
joshualitt98bd5b12016-03-11 12:08:15 -0800209
joshualittee5348b2016-02-26 08:36:25 -0800210 return true;
211 }
212 return false;
213 }
214 fSurface.reset(this->createCPUSurface());
215 fGPUEnabled = false;
216 return true;
halcanary9d524f22016-03-29 09:03:52 -0700217}
joshualitt6bc96792016-02-29 05:35:04 -0800218
219bool Request::initPictureFromStream(SkStream* stream) {
220 // parse picture from stream
reedca2622b2016-03-18 07:25:55 -0700221 fPicture = SkPicture::MakeFromStream(stream);
222 if (!fPicture) {
joshualitt6bc96792016-02-29 05:35:04 -0800223 fprintf(stderr, "Could not create picture from stream.\n");
224 return false;
225 }
226
joshualittae47aee2016-03-10 13:29:36 -0800227 // reinitialize canvas with the new picture dimensions
228 this->enableGPU(fGPUEnabled);
229
joshualitt6bc96792016-02-29 05:35:04 -0800230 // pour picture into debug canvas
joshualittae47aee2016-03-10 13:29:36 -0800231 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800232 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800233 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800234
235 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
236 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800237 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800238 return true;
joshualittee5348b2016-02-26 08:36:25 -0800239}
240
joshualitt1e5884b2016-02-26 08:22:49 -0800241SkData* Request::getJsonOps(int n) {
242 SkCanvas* canvas = this->getCanvas();
243 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
244 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
joshualitt5d5207a2016-02-29 12:46:04 -0800245 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds());
brianosman78312952016-04-19 10:16:53 -0700246 root["colorMode"] = Json::Value(fColorMode);
brianosman312aa6a2016-04-19 12:47:54 -0700247 root["srgbMode"] = Json::Value(gTreatSkColorAsSRGB);
joshualitt1e5884b2016-02-26 08:22:49 -0800248 SkDynamicMemoryWStream stream;
249 stream.writeText(Json::FastWriter().write(root).c_str());
250
251 return stream.copyToData();
252}
253
254SkData* Request::getJsonBatchList(int n) {
255 SkCanvas* canvas = this->getCanvas();
256 SkASSERT(fGPUEnabled);
257
joshualittae47aee2016-03-10 13:29:36 -0800258 Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800259
260 SkDynamicMemoryWStream stream;
joshualittae47aee2016-03-10 13:29:36 -0800261 stream.writeText(Json::FastWriter().write(result).c_str());
joshualitt1e5884b2016-02-26 08:22:49 -0800262
263 return stream.copyToData();
264}
joshualittee5348b2016-02-26 08:36:25 -0800265
266SkData* Request::getJsonInfo(int n) {
267 // drawTo
268 SkAutoTUnref<SkSurface> surface(this->createCPUSurface());
269 SkCanvas* canvas = surface->getCanvas();
270
271 // TODO this is really slow and we should cache the matrix and clip
272 fDebugCanvas->drawTo(canvas, n);
273
274 // make some json
275 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
276 SkIRect clip = fDebugCanvas->getCurrentClip();
277 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800278 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
279 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800280
281 std::string json = Json::FastWriter().write(info);
282
283 // We don't want the null terminator so strlen is correct
284 return SkData::NewWithCopy(json.c_str(), strlen(json.c_str()));
285}
joshualitte0449cf2016-03-09 10:07:02 -0800286
287SkColor Request::getPixel(int x, int y) {
288 SkCanvas* canvas = this->getCanvas();
289 canvas->flush();
290 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
291 SkASSERT(bitmap);
brianosman78312952016-04-19 10:16:53 -0700292
293 // Convert to format suitable for inspection
294 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap);
295 SkASSERT(encodedBitmap.get());
296
297 const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) * 4);
joshualitte0449cf2016-03-09 10:07:02 -0800298 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
joshualitte0449cf2016-03-09 10:07:02 -0800299 return result;
300}