blob: e0aad15aad705d80f311fe6cc99af080113af3aa [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
10#include "png.h"
11
joshualitte0449cf2016-03-09 10:07:02 -080012#include "SkPictureRecorder.h"
13#include "SkPixelSerializer.h"
joshualitt4dcbe432016-02-25 10:50:28 -080014
joshualittae47aee2016-03-10 13:29:36 -080015static int kDefaultWidth = 1920;
16static int kDefaultHeight = 1080;
17
joshualitt4dcbe432016-02-25 10:50:28 -080018static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t length) {
19 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr);
20 out->write(data, length);
21}
22
23static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 height, SkWStream& out) {
24 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
25 SkASSERT(png != nullptr);
26 png_infop info_ptr = png_create_info_struct(png);
27 SkASSERT(info_ptr != nullptr);
28 if (setjmp(png_jmpbuf(png))) {
29 SkFAIL("png encode error");
30 }
31 png_set_IHDR(png, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
32 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
33 png_set_compression_level(png, 1);
34 png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*));
35 png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 3);
36 for (png_size_t y = 0; y < height; ++y) {
37 const png_bytep src = rgba + y * width * 4;
38 rows[y] = pixels + y * width * 3;
39 // convert from RGBA to RGB
40 for (png_size_t x = 0; x < width; ++x) {
41 rows[y][x * 3] = src[x * 4];
42 rows[y][x * 3 + 1] = src[x * 4 + 1];
43 rows[y][x * 3 + 2] = src[x * 4 + 2];
44 }
45 }
46 png_set_filter(png, 0, PNG_NO_FILTERS);
47 png_set_rows(png, info_ptr, &rows[0]);
48 png_set_write_fn(png, &out, write_png_callback, NULL);
49 png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
50 png_destroy_write_struct(&png, NULL);
51 sk_free(rows);
52}
53
joshualittee5348b2016-02-26 08:36:25 -080054Request::Request(SkString rootUrl)
55 : fUploadContext(nullptr)
56 , fUrlDataManager(rootUrl)
57 , fGPUEnabled(false) {
58 // create surface
joshualitt40836102016-03-11 11:45:53 -080059#if SK_SUPPORT_GPU
joshualittee5348b2016-02-26 08:36:25 -080060 GrContextOptions grContextOpts;
joshualitt40836102016-03-11 11:45:53 -080061 fContextFactory = new GrContextFactory(grContextOpts);
62#else
63 fContextFactory = nullptr;
64#endif
65}
66
67Request::~Request() {
68#if SK_SUPPORT_GPU
69 if (fContextFactory) {
70 delete fContextFactory;
71 }
72#endif
joshualittee5348b2016-02-26 08:36:25 -080073}
74
joshualitt4dcbe432016-02-25 10:50:28 -080075SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
76 SkBitmap* bmp = new SkBitmap();
joshualittae47aee2016-03-10 13:29:36 -080077 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -080078 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
79 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
joshualitt4dcbe432016-02-25 10:50:28 -080080 bmp->setInfo(info);
81 if (!canvas->readPixels(bmp, 0, 0)) {
82 fprintf(stderr, "Can't read pixels\n");
83 return nullptr;
84 }
85 return bmp;
86}
87
88SkData* Request::writeCanvasToPng(SkCanvas* canvas) {
89 // capture pixels
joshualitte0449cf2016-03-09 10:07:02 -080090 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
joshualitt4dcbe432016-02-25 10:50:28 -080091 SkASSERT(bmp);
92
93 // write to png
94 SkDynamicMemoryWStream buffer;
95 write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), buffer);
96 return buffer.copyToData();
97}
98
99SkCanvas* Request::getCanvas() {
joshualitt40836102016-03-11 11:45:53 -0800100#if SK_SUPPORT_GPU
joshualitt4dcbe432016-02-25 10:50:28 -0800101 GrContextFactory* factory = fContextFactory;
102 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType,
103 GrContextFactory::kNone_GLContextOptions).fGLContext;
104 gl->makeCurrent();
joshualitt40836102016-03-11 11:45:53 -0800105#endif
joshualitt4dcbe432016-02-25 10:50:28 -0800106 SkASSERT(fDebugCanvas);
joshualitte0449cf2016-03-09 10:07:02 -0800107
108 // create the appropriate surface if necessary
109 if (!fSurface) {
110 this->enableGPU(fGPUEnabled);
111 }
joshualitt4dcbe432016-02-25 10:50:28 -0800112 SkCanvas* target = fSurface->getCanvas();
113 return target;
114}
115
joshualitt46b301d2016-03-02 08:32:37 -0800116void Request::drawToCanvas(int n, int m) {
joshualitt4dcbe432016-02-25 10:50:28 -0800117 SkCanvas* target = this->getCanvas();
joshualitt46b301d2016-03-02 08:32:37 -0800118 fDebugCanvas->drawTo(target, n, m);
joshualitt4dcbe432016-02-25 10:50:28 -0800119}
120
joshualitt46b301d2016-03-02 08:32:37 -0800121SkData* Request::drawToPng(int n, int m) {
122 this->drawToCanvas(n, m);
joshualitt4dcbe432016-02-25 10:50:28 -0800123 return writeCanvasToPng(this->getCanvas());
124}
125
joshualitte0449cf2016-03-09 10:07:02 -0800126SkData* Request::writeOutSkp() {
127 // Playback into picture recorder
joshualittae47aee2016-03-10 13:29:36 -0800128 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800129 SkPictureRecorder recorder;
130 SkCanvas* canvas = recorder.beginRecording(bounds.width(), bounds.height());
131
132 fDebugCanvas->draw(canvas);
133
134 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
135
136 SkDynamicMemoryWStream outStream;
137
138 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
139 picture->serialize(&outStream, serializer);
140
141 return outStream.copyToData();
142}
143
joshualittae47aee2016-03-10 13:29:36 -0800144GrContext* Request::getContext() {
joshualitt40836102016-03-11 11:45:53 -0800145#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800146 return fContextFactory->get(GrContextFactory::kNative_GLContextType,
147 GrContextFactory::kNone_GLContextOptions);
joshualitt40836102016-03-11 11:45:53 -0800148#else
149 return nullptr;
150#endif
joshualittae47aee2016-03-10 13:29:36 -0800151}
152
153SkIRect Request::getBounds() {
154 SkIRect bounds;
155 if (fPicture) {
156 bounds = fPicture->cullRect().roundOut();
157 if (fGPUEnabled) {
joshualitt40836102016-03-11 11:45:53 -0800158#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800159 int maxRTSize = this->getContext()->caps()->maxRenderTargetSize();
160 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
161 SkTMin(bounds.height(), maxRTSize));
joshualitt40836102016-03-11 11:45:53 -0800162#endif
joshualittae47aee2016-03-10 13:29:36 -0800163 }
164 } else {
165 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
166 }
167
168 // We clip to kDefaultWidth / kDefaultHeight for performance reasons
169 // TODO make this configurable
170 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth),
171 SkTMin(bounds.height(), kDefaultHeight));
172 return bounds;
173}
174
joshualitt4dcbe432016-02-25 10:50:28 -0800175SkSurface* Request::createCPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800176 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800177 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), kN32_SkColorType,
joshualitt4dcbe432016-02-25 10:50:28 -0800178 kPremul_SkAlphaType);
179 return SkSurface::NewRaster(info);
180}
181
182SkSurface* Request::createGPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800183 GrContext* context = this->getContext();
184 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800185 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
joshualitt4dcbe432016-02-25 10:50:28 -0800186 kN32_SkColorType, kPremul_SkAlphaType);
187 uint32_t flags = 0;
188 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
189 SkSurface* surface = SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info, 0,
190 &props);
191 return surface;
192}
193
joshualittee5348b2016-02-26 08:36:25 -0800194bool Request::enableGPU(bool enable) {
195 if (enable) {
196 SkSurface* surface = this->createGPUSurface();
197 if (surface) {
198 fSurface.reset(surface);
199 fGPUEnabled = true;
joshualitt98bd5b12016-03-11 12:08:15 -0800200
201 // When we switch to GPU, there seems to be some mystery draws in the canvas. So we
202 // draw once to flush the pipe
203 // TODO understand what is actually happening here
204 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
205 this->getCanvas()->flush();
206
joshualittee5348b2016-02-26 08:36:25 -0800207 return true;
208 }
209 return false;
210 }
211 fSurface.reset(this->createCPUSurface());
212 fGPUEnabled = false;
213 return true;
joshualitt6bc96792016-02-29 05:35:04 -0800214}
215
216bool Request::initPictureFromStream(SkStream* stream) {
217 // parse picture from stream
218 fPicture.reset(SkPicture::CreateFromStream(stream));
219 if (!fPicture.get()) {
220 fprintf(stderr, "Could not create picture from stream.\n");
221 return false;
222 }
223
joshualittae47aee2016-03-10 13:29:36 -0800224 // reinitialize canvas with the new picture dimensions
225 this->enableGPU(fGPUEnabled);
226
joshualitt6bc96792016-02-29 05:35:04 -0800227 // pour picture into debug canvas
joshualittae47aee2016-03-10 13:29:36 -0800228 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800229 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800230 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800231
232 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
233 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800234 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800235 return true;
joshualittee5348b2016-02-26 08:36:25 -0800236}
237
joshualitt1e5884b2016-02-26 08:22:49 -0800238SkData* Request::getJsonOps(int n) {
239 SkCanvas* canvas = this->getCanvas();
240 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
241 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
joshualitt5d5207a2016-02-29 12:46:04 -0800242 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds());
joshualitt1e5884b2016-02-26 08:22:49 -0800243 SkDynamicMemoryWStream stream;
244 stream.writeText(Json::FastWriter().write(root).c_str());
245
246 return stream.copyToData();
247}
248
249SkData* Request::getJsonBatchList(int n) {
250 SkCanvas* canvas = this->getCanvas();
251 SkASSERT(fGPUEnabled);
252
joshualittae47aee2016-03-10 13:29:36 -0800253 Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800254
255 SkDynamicMemoryWStream stream;
joshualittae47aee2016-03-10 13:29:36 -0800256 stream.writeText(Json::FastWriter().write(result).c_str());
joshualitt1e5884b2016-02-26 08:22:49 -0800257
258 return stream.copyToData();
259}
joshualittee5348b2016-02-26 08:36:25 -0800260
261SkData* Request::getJsonInfo(int n) {
262 // drawTo
263 SkAutoTUnref<SkSurface> surface(this->createCPUSurface());
264 SkCanvas* canvas = surface->getCanvas();
265
266 // TODO this is really slow and we should cache the matrix and clip
267 fDebugCanvas->drawTo(canvas, n);
268
269 // make some json
270 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
271 SkIRect clip = fDebugCanvas->getCurrentClip();
272 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800273 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
274 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800275
276 std::string json = Json::FastWriter().write(info);
277
278 // We don't want the null terminator so strlen is correct
279 return SkData::NewWithCopy(json.c_str(), strlen(json.c_str()));
280}
joshualitte0449cf2016-03-09 10:07:02 -0800281
282SkColor Request::getPixel(int x, int y) {
283 SkCanvas* canvas = this->getCanvas();
284 canvas->flush();
285 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
286 SkASSERT(bitmap);
287 bitmap->lockPixels();
288 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x) * 4;
289 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
290 bitmap->unlockPixels();
291 return result;
292}
293