blob: 97719e2bd2ad7905d2bd96dbbea05f00d01014e5 [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,
bsalomon8b7451a2016-05-11 06:33:06 -070074 GrContextFactory::kNone_ContextOptions).glContext();
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;
brianosman82996b82016-04-20 10:52:54 -0700101 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()),
102 SkIntToScalar(bounds.height()));
joshualitte0449cf2016-03-09 10:07:02 -0800103
104 fDebugCanvas->draw(canvas);
105
reedca2622b2016-03-18 07:25:55 -0700106 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
joshualitte0449cf2016-03-09 10:07:02 -0800107
108 SkDynamicMemoryWStream outStream;
109
110 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
111 picture->serialize(&outStream, serializer);
112
113 return outStream.copyToData();
114}
115
joshualittae47aee2016-03-10 13:29:36 -0800116GrContext* Request::getContext() {
joshualitt40836102016-03-11 11:45:53 -0800117#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700118 return fContextFactory->get(GrContextFactory::kNativeGL_ContextType,
119 GrContextFactory::kNone_ContextOptions);
joshualitt40836102016-03-11 11:45:53 -0800120#else
121 return nullptr;
122#endif
joshualittae47aee2016-03-10 13:29:36 -0800123}
124
125SkIRect Request::getBounds() {
126 SkIRect bounds;
127 if (fPicture) {
128 bounds = fPicture->cullRect().roundOut();
129 if (fGPUEnabled) {
joshualitt40836102016-03-11 11:45:53 -0800130#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800131 int maxRTSize = this->getContext()->caps()->maxRenderTargetSize();
132 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
133 SkTMin(bounds.height(), maxRTSize));
joshualitt40836102016-03-11 11:45:53 -0800134#endif
joshualittae47aee2016-03-10 13:29:36 -0800135 }
136 } else {
137 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
138 }
139
140 // We clip to kDefaultWidth / kDefaultHeight for performance reasons
141 // TODO make this configurable
142 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth),
143 SkTMin(bounds.height(), kDefaultHeight));
144 return bounds;
145}
146
brianosman78312952016-04-19 10:16:53 -0700147namespace {
148
149struct ColorAndProfile {
150 SkColorType fColorType;
151 SkColorProfileType fProfileType;
152 bool fGammaCorrect;
153};
154
155ColorAndProfile ColorModes[] = {
156 { kN32_SkColorType, kLinear_SkColorProfileType, false },
157 { kN32_SkColorType, kSRGB_SkColorProfileType, true },
158 { kRGBA_F16_SkColorType, kLinear_SkColorProfileType, true },
159};
160
161}
162
joshualitt4dcbe432016-02-25 10:50:28 -0800163SkSurface* Request::createCPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800164 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700165 ColorAndProfile cap = ColorModes[fColorMode];
166 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
167 kPremul_SkAlphaType, cap.fProfileType);
168 uint32_t flags = cap.fGammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0;
169 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
170 return SkSurface::MakeRaster(info, &props).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800171}
172
173SkSurface* Request::createGPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800174 GrContext* context = this->getContext();
175 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700176 ColorAndProfile cap = ColorModes[fColorMode];
177 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
178 kPremul_SkAlphaType, cap.fProfileType);
179 uint32_t flags = cap.fGammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0;
joshualitt4dcbe432016-02-25 10:50:28 -0800180 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700181 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
182 &props).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800183 return surface;
184}
185
brianosman78312952016-04-19 10:16:53 -0700186bool Request::setColorMode(int mode) {
187 fColorMode = mode;
188 return enableGPU(fGPUEnabled);
189}
190
brianosman312aa6a2016-04-19 12:47:54 -0700191bool Request::setSRGBMode(bool enable) {
192 gTreatSkColorAsSRGB = enable;
193 return true;
194}
195
halcanary9d524f22016-03-29 09:03:52 -0700196bool Request::enableGPU(bool enable) {
joshualittee5348b2016-02-26 08:36:25 -0800197 if (enable) {
198 SkSurface* surface = this->createGPUSurface();
199 if (surface) {
200 fSurface.reset(surface);
201 fGPUEnabled = true;
joshualitt98bd5b12016-03-11 12:08:15 -0800202
203 // When we switch to GPU, there seems to be some mystery draws in the canvas. So we
204 // draw once to flush the pipe
205 // TODO understand what is actually happening here
brianosman78312952016-04-19 10:16:53 -0700206 if (fDebugCanvas) {
207 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
208 this->getCanvas()->flush();
209 }
joshualitt98bd5b12016-03-11 12:08:15 -0800210
joshualittee5348b2016-02-26 08:36:25 -0800211 return true;
212 }
213 return false;
214 }
215 fSurface.reset(this->createCPUSurface());
216 fGPUEnabled = false;
217 return true;
halcanary9d524f22016-03-29 09:03:52 -0700218}
joshualitt6bc96792016-02-29 05:35:04 -0800219
220bool Request::initPictureFromStream(SkStream* stream) {
221 // parse picture from stream
reedca2622b2016-03-18 07:25:55 -0700222 fPicture = SkPicture::MakeFromStream(stream);
223 if (!fPicture) {
joshualitt6bc96792016-02-29 05:35:04 -0800224 fprintf(stderr, "Could not create picture from stream.\n");
225 return false;
226 }
227
joshualittae47aee2016-03-10 13:29:36 -0800228 // reinitialize canvas with the new picture dimensions
229 this->enableGPU(fGPUEnabled);
230
joshualitt6bc96792016-02-29 05:35:04 -0800231 // pour picture into debug canvas
joshualittae47aee2016-03-10 13:29:36 -0800232 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800233 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800234 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800235
236 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
237 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800238 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800239 return true;
joshualittee5348b2016-02-26 08:36:25 -0800240}
241
joshualitt1e5884b2016-02-26 08:22:49 -0800242SkData* Request::getJsonOps(int n) {
243 SkCanvas* canvas = this->getCanvas();
244 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
245 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
joshualitt5d5207a2016-02-29 12:46:04 -0800246 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds());
brianosman78312952016-04-19 10:16:53 -0700247 root["colorMode"] = Json::Value(fColorMode);
brianosman312aa6a2016-04-19 12:47:54 -0700248 root["srgbMode"] = Json::Value(gTreatSkColorAsSRGB);
joshualitt1e5884b2016-02-26 08:22:49 -0800249 SkDynamicMemoryWStream stream;
250 stream.writeText(Json::FastWriter().write(root).c_str());
251
252 return stream.copyToData();
253}
254
255SkData* Request::getJsonBatchList(int n) {
256 SkCanvas* canvas = this->getCanvas();
257 SkASSERT(fGPUEnabled);
258
joshualittae47aee2016-03-10 13:29:36 -0800259 Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800260
261 SkDynamicMemoryWStream stream;
joshualittae47aee2016-03-10 13:29:36 -0800262 stream.writeText(Json::FastWriter().write(result).c_str());
joshualitt1e5884b2016-02-26 08:22:49 -0800263
264 return stream.copyToData();
265}
joshualittee5348b2016-02-26 08:36:25 -0800266
267SkData* Request::getJsonInfo(int n) {
268 // drawTo
269 SkAutoTUnref<SkSurface> surface(this->createCPUSurface());
270 SkCanvas* canvas = surface->getCanvas();
271
272 // TODO this is really slow and we should cache the matrix and clip
273 fDebugCanvas->drawTo(canvas, n);
274
275 // make some json
276 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
277 SkIRect clip = fDebugCanvas->getCurrentClip();
278 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800279 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
280 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800281
282 std::string json = Json::FastWriter().write(info);
283
284 // We don't want the null terminator so strlen is correct
285 return SkData::NewWithCopy(json.c_str(), strlen(json.c_str()));
286}
joshualitte0449cf2016-03-09 10:07:02 -0800287
288SkColor Request::getPixel(int x, int y) {
289 SkCanvas* canvas = this->getCanvas();
290 canvas->flush();
291 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
292 SkASSERT(bitmap);
brianosman78312952016-04-19 10:16:53 -0700293
294 // Convert to format suitable for inspection
295 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap);
296 SkASSERT(encodedBitmap.get());
297
298 const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) * 4);
joshualitte0449cf2016-03-09 10:07:02 -0800299 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
joshualitte0449cf2016-03-09 10:07:02 -0800300 return result;
301}