blob: ea2d17afb94229015f3c9ae32fc9bb3617809743 [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)
27 , fColorMode(0) {
joshualittee5348b2016-02-26 08:36:25 -080028 // create surface
joshualitt40836102016-03-11 11:45:53 -080029#if SK_SUPPORT_GPU
joshualittee5348b2016-02-26 08:36:25 -080030 GrContextOptions grContextOpts;
joshualitt40836102016-03-11 11:45:53 -080031 fContextFactory = new GrContextFactory(grContextOpts);
32#else
33 fContextFactory = nullptr;
34#endif
35}
36
37Request::~Request() {
38#if SK_SUPPORT_GPU
39 if (fContextFactory) {
40 delete fContextFactory;
41 }
42#endif
joshualittee5348b2016-02-26 08:36:25 -080043}
44
joshualitt4dcbe432016-02-25 10:50:28 -080045SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
46 SkBitmap* bmp = new SkBitmap();
brianosman78312952016-04-19 10:16:53 -070047 bmp->setInfo(canvas->imageInfo());
joshualitt4dcbe432016-02-25 10:50:28 -080048 if (!canvas->readPixels(bmp, 0, 0)) {
49 fprintf(stderr, "Can't read pixels\n");
50 return nullptr;
51 }
52 return bmp;
53}
54
bungeman38d909e2016-08-02 14:40:46 -070055sk_sp<SkData> Request::writeCanvasToPng(SkCanvas* canvas) {
joshualitt4dcbe432016-02-25 10:50:28 -080056 // capture pixels
joshualitte0449cf2016-03-09 10:07:02 -080057 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
joshualitt4dcbe432016-02-25 10:50:28 -080058 SkASSERT(bmp);
59
brianosman78312952016-04-19 10:16:53 -070060 // Convert to format suitable for PNG output
61 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bmp);
62 SkASSERT(encodedBitmap.get());
63
msaretta5cf4f42016-06-30 10:06:51 -070064 // write to an opaque png (black background)
joshualitt4dcbe432016-02-25 10:50:28 -080065 SkDynamicMemoryWStream buffer;
msaretta5cf4f42016-06-30 10:06:51 -070066 SkDrawCommand::WritePNG((const png_bytep) encodedBitmap->bytes(), bmp->width(), bmp->height(),
67 buffer, true);
reed42943c82016-09-12 12:01:44 -070068 return buffer.detachAsData();
joshualitt4dcbe432016-02-25 10:50:28 -080069}
70
71SkCanvas* Request::getCanvas() {
joshualitt40836102016-03-11 11:45:53 -080072#if SK_SUPPORT_GPU
joshualitt4dcbe432016-02-25 10:50:28 -080073 GrContextFactory* factory = fContextFactory;
bsalomon85b4b532016-04-05 11:06:27 -070074 GLTestContext* gl = factory->getContextInfo(GrContextFactory::kNativeGL_ContextType,
ethannicholas2ec06c92016-06-13 10:20:52 -070075 GrContextFactory::kNone_ContextOptions).glContext();
76 if (!gl) {
77 gl = factory->getContextInfo(GrContextFactory::kMESA_ContextType,
78 GrContextFactory::kNone_ContextOptions).glContext();
79 }
80 if (gl) {
81 gl->makeCurrent();
82 }
joshualitt40836102016-03-11 11:45:53 -080083#endif
joshualitt4dcbe432016-02-25 10:50:28 -080084 SkASSERT(fDebugCanvas);
joshualitte0449cf2016-03-09 10:07:02 -080085
86 // create the appropriate surface if necessary
87 if (!fSurface) {
88 this->enableGPU(fGPUEnabled);
89 }
joshualitt4dcbe432016-02-25 10:50:28 -080090 SkCanvas* target = fSurface->getCanvas();
91 return target;
92}
93
joshualitt46b301d2016-03-02 08:32:37 -080094void Request::drawToCanvas(int n, int m) {
joshualitt4dcbe432016-02-25 10:50:28 -080095 SkCanvas* target = this->getCanvas();
joshualitt46b301d2016-03-02 08:32:37 -080096 fDebugCanvas->drawTo(target, n, m);
joshualitt4dcbe432016-02-25 10:50:28 -080097}
98
bungeman38d909e2016-08-02 14:40:46 -070099sk_sp<SkData> Request::drawToPng(int n, int m) {
joshualitt46b301d2016-03-02 08:32:37 -0800100 this->drawToCanvas(n, m);
joshualitt4dcbe432016-02-25 10:50:28 -0800101 return writeCanvasToPng(this->getCanvas());
102}
103
bungeman38d909e2016-08-02 14:40:46 -0700104sk_sp<SkData> Request::writeOutSkp() {
joshualitte0449cf2016-03-09 10:07:02 -0800105 // Playback into picture recorder
joshualittae47aee2016-03-10 13:29:36 -0800106 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800107 SkPictureRecorder recorder;
brianosman82996b82016-04-20 10:52:54 -0700108 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()),
109 SkIntToScalar(bounds.height()));
joshualitte0449cf2016-03-09 10:07:02 -0800110
111 fDebugCanvas->draw(canvas);
112
reedca2622b2016-03-18 07:25:55 -0700113 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
joshualitte0449cf2016-03-09 10:07:02 -0800114
115 SkDynamicMemoryWStream outStream;
116
117 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
118 picture->serialize(&outStream, serializer);
119
reed42943c82016-09-12 12:01:44 -0700120 return outStream.detachAsData();
joshualitte0449cf2016-03-09 10:07:02 -0800121}
122
joshualittae47aee2016-03-10 13:29:36 -0800123GrContext* Request::getContext() {
joshualitt40836102016-03-11 11:45:53 -0800124#if SK_SUPPORT_GPU
ethannicholas2ec06c92016-06-13 10:20:52 -0700125 GrContext* result = fContextFactory->get(GrContextFactory::kNativeGL_ContextType,
126 GrContextFactory::kNone_ContextOptions);
127 if (!result) {
128 result = fContextFactory->get(GrContextFactory::kMESA_ContextType,
129 GrContextFactory::kNone_ContextOptions);
jcgregorio9a7acdc2016-06-30 07:54:14 -0700130 }
ethannicholas2ec06c92016-06-13 10:20:52 -0700131 return result;
joshualitt40836102016-03-11 11:45:53 -0800132#else
ethannicholas2ec06c92016-06-13 10:20:52 -0700133 return nullptr;
joshualitt40836102016-03-11 11:45:53 -0800134#endif
joshualittae47aee2016-03-10 13:29:36 -0800135}
136
137SkIRect Request::getBounds() {
138 SkIRect bounds;
139 if (fPicture) {
140 bounds = fPicture->cullRect().roundOut();
141 if (fGPUEnabled) {
joshualitt40836102016-03-11 11:45:53 -0800142#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800143 int maxRTSize = this->getContext()->caps()->maxRenderTargetSize();
144 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
145 SkTMin(bounds.height(), maxRTSize));
joshualitt40836102016-03-11 11:45:53 -0800146#endif
joshualittae47aee2016-03-10 13:29:36 -0800147 }
148 } else {
149 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
150 }
151
jcgregorio9a7acdc2016-06-30 07:54:14 -0700152 // We clip to kMaxWidth / kMaxHeight for performance reasons.
joshualittae47aee2016-03-10 13:29:36 -0800153 // TODO make this configurable
jcgregorio9a7acdc2016-06-30 07:54:14 -0700154 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kMaxWidth),
155 SkTMin(bounds.height(), kMaxHeight));
joshualittae47aee2016-03-10 13:29:36 -0800156 return bounds;
157}
158
brianosman78312952016-04-19 10:16:53 -0700159namespace {
160
161struct ColorAndProfile {
162 SkColorType fColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700163 bool fSRGB;
brianosman78312952016-04-19 10:16:53 -0700164};
165
166ColorAndProfile ColorModes[] = {
brianosman3a0dbde2016-07-26 11:36:05 -0700167 { kN32_SkColorType, false },
168 { kN32_SkColorType, true },
169 { kRGBA_F16_SkColorType, true },
brianosman78312952016-04-19 10:16:53 -0700170};
171
172}
173
joshualitt4dcbe432016-02-25 10:50:28 -0800174SkSurface* Request::createCPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800175 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700176 ColorAndProfile cap = ColorModes[fColorMode];
brianosman0e22eb82016-08-30 07:07:59 -0700177 auto colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
178 if (kRGBA_F16_SkColorType == cap.fColorType) {
179 colorSpace = colorSpace->makeLinearGamma();
180 }
brianosman78312952016-04-19 10:16:53 -0700181 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700182 kPremul_SkAlphaType, cap.fSRGB ? colorSpace : nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700183 return SkSurface::MakeRaster(info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800184}
185
186SkSurface* Request::createGPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800187 GrContext* context = this->getContext();
188 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700189 ColorAndProfile cap = ColorModes[fColorMode];
brianosman0e22eb82016-08-30 07:07:59 -0700190 auto colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
191 if (kRGBA_F16_SkColorType == cap.fColorType) {
192 colorSpace = colorSpace->makeLinearGamma();
193 }
brianosman78312952016-04-19 10:16:53 -0700194 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700195 kPremul_SkAlphaType, cap.fSRGB ? colorSpace: nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700196 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800197 return surface;
198}
199
brianosman78312952016-04-19 10:16:53 -0700200bool Request::setColorMode(int mode) {
201 fColorMode = mode;
202 return enableGPU(fGPUEnabled);
203}
204
halcanary9d524f22016-03-29 09:03:52 -0700205bool Request::enableGPU(bool enable) {
joshualittee5348b2016-02-26 08:36:25 -0800206 if (enable) {
207 SkSurface* surface = this->createGPUSurface();
208 if (surface) {
209 fSurface.reset(surface);
210 fGPUEnabled = true;
joshualitt98bd5b12016-03-11 12:08:15 -0800211
212 // When we switch to GPU, there seems to be some mystery draws in the canvas. So we
213 // draw once to flush the pipe
214 // TODO understand what is actually happening here
brianosman78312952016-04-19 10:16:53 -0700215 if (fDebugCanvas) {
216 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
217 this->getCanvas()->flush();
218 }
joshualitt98bd5b12016-03-11 12:08:15 -0800219
joshualittee5348b2016-02-26 08:36:25 -0800220 return true;
221 }
222 return false;
223 }
224 fSurface.reset(this->createCPUSurface());
225 fGPUEnabled = false;
226 return true;
halcanary9d524f22016-03-29 09:03:52 -0700227}
joshualitt6bc96792016-02-29 05:35:04 -0800228
229bool Request::initPictureFromStream(SkStream* stream) {
230 // parse picture from stream
reedca2622b2016-03-18 07:25:55 -0700231 fPicture = SkPicture::MakeFromStream(stream);
232 if (!fPicture) {
joshualitt6bc96792016-02-29 05:35:04 -0800233 fprintf(stderr, "Could not create picture from stream.\n");
234 return false;
235 }
236
joshualittae47aee2016-03-10 13:29:36 -0800237 // reinitialize canvas with the new picture dimensions
238 this->enableGPU(fGPUEnabled);
239
joshualitt6bc96792016-02-29 05:35:04 -0800240 // pour picture into debug canvas
joshualittae47aee2016-03-10 13:29:36 -0800241 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800242 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800243 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800244
245 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
246 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800247 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800248 return true;
joshualittee5348b2016-02-26 08:36:25 -0800249}
250
bungeman38d909e2016-08-02 14:40:46 -0700251sk_sp<SkData> Request::getJsonOps(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800252 SkCanvas* canvas = this->getCanvas();
253 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
254 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
joshualitt5d5207a2016-02-29 12:46:04 -0800255 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds());
brianosman78312952016-04-19 10:16:53 -0700256 root["colorMode"] = Json::Value(fColorMode);
joshualitt1e5884b2016-02-26 08:22:49 -0800257 SkDynamicMemoryWStream stream;
258 stream.writeText(Json::FastWriter().write(root).c_str());
259
reed42943c82016-09-12 12:01:44 -0700260 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800261}
262
bungeman38d909e2016-08-02 14:40:46 -0700263sk_sp<SkData> Request::getJsonBatchList(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800264 SkCanvas* canvas = this->getCanvas();
265 SkASSERT(fGPUEnabled);
266
joshualittae47aee2016-03-10 13:29:36 -0800267 Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800268
269 SkDynamicMemoryWStream stream;
joshualittae47aee2016-03-10 13:29:36 -0800270 stream.writeText(Json::FastWriter().write(result).c_str());
joshualitt1e5884b2016-02-26 08:22:49 -0800271
reed42943c82016-09-12 12:01:44 -0700272 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800273}
joshualittee5348b2016-02-26 08:36:25 -0800274
bungeman38d909e2016-08-02 14:40:46 -0700275sk_sp<SkData> Request::getJsonInfo(int n) {
joshualittee5348b2016-02-26 08:36:25 -0800276 // drawTo
277 SkAutoTUnref<SkSurface> surface(this->createCPUSurface());
278 SkCanvas* canvas = surface->getCanvas();
279
280 // TODO this is really slow and we should cache the matrix and clip
281 fDebugCanvas->drawTo(canvas, n);
282
283 // make some json
284 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
285 SkIRect clip = fDebugCanvas->getCurrentClip();
286 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800287 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
288 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800289
290 std::string json = Json::FastWriter().write(info);
291
292 // We don't want the null terminator so strlen is correct
bungeman38d909e2016-08-02 14:40:46 -0700293 return SkData::MakeWithCopy(json.c_str(), strlen(json.c_str()));
joshualittee5348b2016-02-26 08:36:25 -0800294}
joshualitte0449cf2016-03-09 10:07:02 -0800295
296SkColor Request::getPixel(int x, int y) {
297 SkCanvas* canvas = this->getCanvas();
298 canvas->flush();
299 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
300 SkASSERT(bitmap);
brianosman78312952016-04-19 10:16:53 -0700301
302 // Convert to format suitable for inspection
303 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap);
reed42943c82016-09-12 12:01:44 -0700304 SkASSERT(encodedBitmap);
brianosman78312952016-04-19 10:16:53 -0700305
306 const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) * 4);
joshualitte0449cf2016-03-09 10:07:02 -0800307 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
joshualitte0449cf2016-03-09 10:07:02 -0800308 return result;
309}