blob: 2d956c90f72d4a819635ab3794125b8f0e0a79a3 [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
15static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t length) {
16 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr);
17 out->write(data, length);
18}
19
20static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 height, SkWStream& out) {
21 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
22 SkASSERT(png != nullptr);
23 png_infop info_ptr = png_create_info_struct(png);
24 SkASSERT(info_ptr != nullptr);
25 if (setjmp(png_jmpbuf(png))) {
26 SkFAIL("png encode error");
27 }
28 png_set_IHDR(png, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
29 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
30 png_set_compression_level(png, 1);
31 png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*));
32 png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 3);
33 for (png_size_t y = 0; y < height; ++y) {
34 const png_bytep src = rgba + y * width * 4;
35 rows[y] = pixels + y * width * 3;
36 // convert from RGBA to RGB
37 for (png_size_t x = 0; x < width; ++x) {
38 rows[y][x * 3] = src[x * 4];
39 rows[y][x * 3 + 1] = src[x * 4 + 1];
40 rows[y][x * 3 + 2] = src[x * 4 + 2];
41 }
42 }
43 png_set_filter(png, 0, PNG_NO_FILTERS);
44 png_set_rows(png, info_ptr, &rows[0]);
45 png_set_write_fn(png, &out, write_png_callback, NULL);
46 png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
47 png_destroy_write_struct(&png, NULL);
48 sk_free(rows);
49}
50
joshualittee5348b2016-02-26 08:36:25 -080051Request::Request(SkString rootUrl)
52 : fUploadContext(nullptr)
53 , fUrlDataManager(rootUrl)
54 , fGPUEnabled(false) {
55 // create surface
56 GrContextOptions grContextOpts;
57 fContextFactory.reset(new GrContextFactory(grContextOpts));
joshualittee5348b2016-02-26 08:36:25 -080058}
59
joshualitt4dcbe432016-02-25 10:50:28 -080060SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
61 SkBitmap* bmp = new SkBitmap();
joshualitte0449cf2016-03-09 10:07:02 -080062 SkIRect bounds = fPicture->cullRect().roundOut();
63 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
64 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
joshualitt4dcbe432016-02-25 10:50:28 -080065 bmp->setInfo(info);
66 if (!canvas->readPixels(bmp, 0, 0)) {
67 fprintf(stderr, "Can't read pixels\n");
68 return nullptr;
69 }
70 return bmp;
71}
72
73SkData* Request::writeCanvasToPng(SkCanvas* canvas) {
74 // capture pixels
joshualitte0449cf2016-03-09 10:07:02 -080075 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
joshualitt4dcbe432016-02-25 10:50:28 -080076 SkASSERT(bmp);
77
78 // write to png
79 SkDynamicMemoryWStream buffer;
80 write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), buffer);
81 return buffer.copyToData();
82}
83
84SkCanvas* Request::getCanvas() {
85 GrContextFactory* factory = fContextFactory;
86 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType,
87 GrContextFactory::kNone_GLContextOptions).fGLContext;
88 gl->makeCurrent();
89 SkASSERT(fDebugCanvas);
joshualitte0449cf2016-03-09 10:07:02 -080090
91 // create the appropriate surface if necessary
92 if (!fSurface) {
93 this->enableGPU(fGPUEnabled);
94 }
joshualitt4dcbe432016-02-25 10:50:28 -080095 SkCanvas* target = fSurface->getCanvas();
96 return target;
97}
98
joshualitt46b301d2016-03-02 08:32:37 -080099void Request::drawToCanvas(int n, int m) {
joshualitt4dcbe432016-02-25 10:50:28 -0800100 SkCanvas* target = this->getCanvas();
joshualitt46b301d2016-03-02 08:32:37 -0800101 fDebugCanvas->drawTo(target, n, m);
joshualitt4dcbe432016-02-25 10:50:28 -0800102}
103
joshualitt46b301d2016-03-02 08:32:37 -0800104SkData* Request::drawToPng(int n, int m) {
105 this->drawToCanvas(n, m);
joshualitt4dcbe432016-02-25 10:50:28 -0800106 return writeCanvasToPng(this->getCanvas());
107}
108
joshualitte0449cf2016-03-09 10:07:02 -0800109SkData* Request::writeOutSkp() {
110 // Playback into picture recorder
111 SkIRect bounds = fPicture->cullRect().roundOut();
112 SkPictureRecorder recorder;
113 SkCanvas* canvas = recorder.beginRecording(bounds.width(), bounds.height());
114
115 fDebugCanvas->draw(canvas);
116
117 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
118
119 SkDynamicMemoryWStream outStream;
120
121 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
122 picture->serialize(&outStream, serializer);
123
124 return outStream.copyToData();
125}
126
joshualitt4dcbe432016-02-25 10:50:28 -0800127SkSurface* Request::createCPUSurface() {
joshualitte0449cf2016-03-09 10:07:02 -0800128 SkIRect bounds = fPicture->cullRect().roundOut();
129 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), kN32_SkColorType,
joshualitt4dcbe432016-02-25 10:50:28 -0800130 kPremul_SkAlphaType);
131 return SkSurface::NewRaster(info);
132}
133
134SkSurface* Request::createGPUSurface() {
135 GrContext* context = fContextFactory->get(GrContextFactory::kNative_GLContextType,
136 GrContextFactory::kNone_GLContextOptions);
joshualitte0449cf2016-03-09 10:07:02 -0800137 SkIRect bounds = fPicture->cullRect().roundOut();
138 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
joshualitt4dcbe432016-02-25 10:50:28 -0800139 kN32_SkColorType, kPremul_SkAlphaType);
140 uint32_t flags = 0;
141 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
142 SkSurface* surface = SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info, 0,
143 &props);
144 return surface;
145}
146
joshualittee5348b2016-02-26 08:36:25 -0800147bool Request::enableGPU(bool enable) {
148 if (enable) {
149 SkSurface* surface = this->createGPUSurface();
150 if (surface) {
151 fSurface.reset(surface);
152 fGPUEnabled = true;
153 return true;
154 }
155 return false;
156 }
157 fSurface.reset(this->createCPUSurface());
158 fGPUEnabled = false;
159 return true;
joshualitt6bc96792016-02-29 05:35:04 -0800160}
161
162bool Request::initPictureFromStream(SkStream* stream) {
163 // parse picture from stream
164 fPicture.reset(SkPicture::CreateFromStream(stream));
165 if (!fPicture.get()) {
166 fprintf(stderr, "Could not create picture from stream.\n");
167 return false;
168 }
169
170 // pour picture into debug canvas
joshualitte0449cf2016-03-09 10:07:02 -0800171 SkIRect bounds = fPicture->cullRect().roundOut();
172 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800173 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800174
175 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
176 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800177 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800178 return true;
joshualittee5348b2016-02-26 08:36:25 -0800179}
180
joshualittb95c7722016-02-29 07:44:02 -0800181GrAuditTrail* Request::getAuditTrail(SkCanvas* canvas) {
182 GrAuditTrail* at = nullptr;
183#if SK_SUPPORT_GPU
184 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
185 if (rt) {
186 GrContext* ctx = rt->getContext();
187 if (ctx) {
188 at = ctx->getAuditTrail();
189 }
190 }
191#endif
192 return at;
193}
194
195void Request::cleanupAuditTrail(SkCanvas* canvas) {
196 GrAuditTrail* at = this->getAuditTrail(canvas);
197 if (at) {
198 GrAuditTrail::AutoEnable ae(at);
199 at->fullReset();
200 }
201}
202
joshualitt1e5884b2016-02-26 08:22:49 -0800203SkData* Request::getJsonOps(int n) {
204 SkCanvas* canvas = this->getCanvas();
205 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
206 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
joshualitt5d5207a2016-02-29 12:46:04 -0800207 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds());
joshualitt1e5884b2016-02-26 08:22:49 -0800208 SkDynamicMemoryWStream stream;
209 stream.writeText(Json::FastWriter().write(root).c_str());
210
joshualittb95c7722016-02-29 07:44:02 -0800211 this->cleanupAuditTrail(canvas);
212
joshualitt1e5884b2016-02-26 08:22:49 -0800213 return stream.copyToData();
214}
215
216SkData* Request::getJsonBatchList(int n) {
217 SkCanvas* canvas = this->getCanvas();
218 SkASSERT(fGPUEnabled);
219
220 // TODO if this is inefficient we could add a method to GrAuditTrail which takes
221 // a Json::Value and is only compiled in this file
222 Json::Value parsedFromString;
223#if SK_SUPPORT_GPU
joshualitt3a9be692016-02-29 11:38:11 -0800224 // we use the toJSON method on debug canvas, but then just ignore the results and pull
225 // the information we care about from the audit trail
226 fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
227
joshualittb95c7722016-02-29 07:44:02 -0800228 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800229 GrAuditTrail::AutoManageBatchList enable(at);
joshualitt1e5884b2016-02-26 08:22:49 -0800230 Json::Reader reader;
joshualitt3a9be692016-02-29 11:38:11 -0800231 SkDEBUGCODE(bool parsingSuccessful = )reader.parse(at->toJson().c_str(),
joshualitt1e5884b2016-02-26 08:22:49 -0800232 parsedFromString);
233 SkASSERT(parsingSuccessful);
234#endif
235
236 SkDynamicMemoryWStream stream;
237 stream.writeText(Json::FastWriter().write(parsedFromString).c_str());
238
239 return stream.copyToData();
240}
joshualittee5348b2016-02-26 08:36:25 -0800241
242SkData* Request::getJsonInfo(int n) {
243 // drawTo
244 SkAutoTUnref<SkSurface> surface(this->createCPUSurface());
245 SkCanvas* canvas = surface->getCanvas();
246
247 // TODO this is really slow and we should cache the matrix and clip
248 fDebugCanvas->drawTo(canvas, n);
249
250 // make some json
251 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
252 SkIRect clip = fDebugCanvas->getCurrentClip();
253 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800254 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
255 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800256
257 std::string json = Json::FastWriter().write(info);
258
259 // We don't want the null terminator so strlen is correct
260 return SkData::NewWithCopy(json.c_str(), strlen(json.c_str()));
261}
joshualitte0449cf2016-03-09 10:07:02 -0800262
263SkColor Request::getPixel(int x, int y) {
264 SkCanvas* canvas = this->getCanvas();
265 canvas->flush();
266 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
267 SkASSERT(bitmap);
268 bitmap->lockPixels();
269 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x) * 4;
270 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
271 bitmap->unlockPixels();
272 return result;
273}
274