blob: a0580754107667994ee4d12b03a8521a4d7a6ccb [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
59 GrContextOptions grContextOpts;
60 fContextFactory.reset(new GrContextFactory(grContextOpts));
joshualittee5348b2016-02-26 08:36:25 -080061}
62
joshualitt4dcbe432016-02-25 10:50:28 -080063SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
64 SkBitmap* bmp = new SkBitmap();
joshualittae47aee2016-03-10 13:29:36 -080065 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -080066 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
67 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
joshualitt4dcbe432016-02-25 10:50:28 -080068 bmp->setInfo(info);
69 if (!canvas->readPixels(bmp, 0, 0)) {
70 fprintf(stderr, "Can't read pixels\n");
71 return nullptr;
72 }
73 return bmp;
74}
75
76SkData* Request::writeCanvasToPng(SkCanvas* canvas) {
77 // capture pixels
joshualitte0449cf2016-03-09 10:07:02 -080078 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
joshualitt4dcbe432016-02-25 10:50:28 -080079 SkASSERT(bmp);
80
81 // write to png
82 SkDynamicMemoryWStream buffer;
83 write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), buffer);
84 return buffer.copyToData();
85}
86
87SkCanvas* Request::getCanvas() {
88 GrContextFactory* factory = fContextFactory;
89 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType,
90 GrContextFactory::kNone_GLContextOptions).fGLContext;
91 gl->makeCurrent();
92 SkASSERT(fDebugCanvas);
joshualitte0449cf2016-03-09 10:07:02 -080093
94 // create the appropriate surface if necessary
95 if (!fSurface) {
96 this->enableGPU(fGPUEnabled);
97 }
joshualitt4dcbe432016-02-25 10:50:28 -080098 SkCanvas* target = fSurface->getCanvas();
99 return target;
100}
101
joshualitt46b301d2016-03-02 08:32:37 -0800102void Request::drawToCanvas(int n, int m) {
joshualitt4dcbe432016-02-25 10:50:28 -0800103 SkCanvas* target = this->getCanvas();
joshualitt46b301d2016-03-02 08:32:37 -0800104 fDebugCanvas->drawTo(target, n, m);
joshualitt4dcbe432016-02-25 10:50:28 -0800105}
106
joshualitt46b301d2016-03-02 08:32:37 -0800107SkData* Request::drawToPng(int n, int m) {
108 this->drawToCanvas(n, m);
joshualitt4dcbe432016-02-25 10:50:28 -0800109 return writeCanvasToPng(this->getCanvas());
110}
111
joshualitte0449cf2016-03-09 10:07:02 -0800112SkData* Request::writeOutSkp() {
113 // Playback into picture recorder
joshualittae47aee2016-03-10 13:29:36 -0800114 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800115 SkPictureRecorder recorder;
116 SkCanvas* canvas = recorder.beginRecording(bounds.width(), bounds.height());
117
118 fDebugCanvas->draw(canvas);
119
120 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
121
122 SkDynamicMemoryWStream outStream;
123
124 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
125 picture->serialize(&outStream, serializer);
126
127 return outStream.copyToData();
128}
129
joshualittae47aee2016-03-10 13:29:36 -0800130GrContext* Request::getContext() {
131 return fContextFactory->get(GrContextFactory::kNative_GLContextType,
132 GrContextFactory::kNone_GLContextOptions);
133}
134
135SkIRect Request::getBounds() {
136 SkIRect bounds;
137 if (fPicture) {
138 bounds = fPicture->cullRect().roundOut();
139 if (fGPUEnabled) {
140 int maxRTSize = this->getContext()->caps()->maxRenderTargetSize();
141 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
142 SkTMin(bounds.height(), maxRTSize));
143 }
144 } else {
145 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
146 }
147
148 // We clip to kDefaultWidth / kDefaultHeight for performance reasons
149 // TODO make this configurable
150 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth),
151 SkTMin(bounds.height(), kDefaultHeight));
152 return bounds;
153}
154
joshualitt4dcbe432016-02-25 10:50:28 -0800155SkSurface* Request::createCPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800156 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800157 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), kN32_SkColorType,
joshualitt4dcbe432016-02-25 10:50:28 -0800158 kPremul_SkAlphaType);
159 return SkSurface::NewRaster(info);
160}
161
162SkSurface* Request::createGPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800163 GrContext* context = this->getContext();
164 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800165 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
joshualitt4dcbe432016-02-25 10:50:28 -0800166 kN32_SkColorType, kPremul_SkAlphaType);
167 uint32_t flags = 0;
168 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
169 SkSurface* surface = SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info, 0,
170 &props);
171 return surface;
172}
173
joshualittee5348b2016-02-26 08:36:25 -0800174bool Request::enableGPU(bool enable) {
175 if (enable) {
176 SkSurface* surface = this->createGPUSurface();
177 if (surface) {
178 fSurface.reset(surface);
179 fGPUEnabled = true;
180 return true;
181 }
182 return false;
183 }
184 fSurface.reset(this->createCPUSurface());
185 fGPUEnabled = false;
186 return true;
joshualitt6bc96792016-02-29 05:35:04 -0800187}
188
189bool Request::initPictureFromStream(SkStream* stream) {
190 // parse picture from stream
191 fPicture.reset(SkPicture::CreateFromStream(stream));
192 if (!fPicture.get()) {
193 fprintf(stderr, "Could not create picture from stream.\n");
194 return false;
195 }
196
joshualittae47aee2016-03-10 13:29:36 -0800197 // reinitialize canvas with the new picture dimensions
198 this->enableGPU(fGPUEnabled);
199
joshualitt6bc96792016-02-29 05:35:04 -0800200 // pour picture into debug canvas
joshualittae47aee2016-03-10 13:29:36 -0800201 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800202 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800203 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800204
205 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
206 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800207 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800208 return true;
joshualittee5348b2016-02-26 08:36:25 -0800209}
210
joshualitt1e5884b2016-02-26 08:22:49 -0800211SkData* Request::getJsonOps(int n) {
212 SkCanvas* canvas = this->getCanvas();
213 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
214 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
joshualitt5d5207a2016-02-29 12:46:04 -0800215 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds());
joshualitt1e5884b2016-02-26 08:22:49 -0800216 SkDynamicMemoryWStream stream;
217 stream.writeText(Json::FastWriter().write(root).c_str());
218
219 return stream.copyToData();
220}
221
222SkData* Request::getJsonBatchList(int n) {
223 SkCanvas* canvas = this->getCanvas();
224 SkASSERT(fGPUEnabled);
225
joshualittae47aee2016-03-10 13:29:36 -0800226 Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800227
228 SkDynamicMemoryWStream stream;
joshualittae47aee2016-03-10 13:29:36 -0800229 stream.writeText(Json::FastWriter().write(result).c_str());
joshualitt1e5884b2016-02-26 08:22:49 -0800230
231 return stream.copyToData();
232}
joshualittee5348b2016-02-26 08:36:25 -0800233
234SkData* Request::getJsonInfo(int n) {
235 // drawTo
236 SkAutoTUnref<SkSurface> surface(this->createCPUSurface());
237 SkCanvas* canvas = surface->getCanvas();
238
239 // TODO this is really slow and we should cache the matrix and clip
240 fDebugCanvas->drawTo(canvas, n);
241
242 // make some json
243 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
244 SkIRect clip = fDebugCanvas->getCurrentClip();
245 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800246 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
247 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800248
249 std::string json = Json::FastWriter().write(info);
250
251 // We don't want the null terminator so strlen is correct
252 return SkData::NewWithCopy(json.c_str(), strlen(json.c_str()));
253}
joshualitte0449cf2016-03-09 10:07:02 -0800254
255SkColor Request::getPixel(int x, int y) {
256 SkCanvas* canvas = this->getCanvas();
257 canvas->flush();
258 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
259 SkASSERT(bitmap);
260 bitmap->lockPixels();
261 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x) * 4;
262 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
263 bitmap->unlockPixels();
264 return result;
265}
266