joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame^] | 12 | #include "SkPictureRecorder.h" |
| 13 | #include "SkPixelSerializer.h" |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 14 | |
| 15 | static 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 | |
| 20 | static 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 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 51 | Request::Request(SkString rootUrl) |
| 52 | : fUploadContext(nullptr) |
| 53 | , fUrlDataManager(rootUrl) |
| 54 | , fGPUEnabled(false) { |
| 55 | // create surface |
| 56 | GrContextOptions grContextOpts; |
| 57 | fContextFactory.reset(new GrContextFactory(grContextOpts)); |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 58 | } |
| 59 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 60 | SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { |
| 61 | SkBitmap* bmp = new SkBitmap(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame^] | 62 | SkIRect bounds = fPicture->cullRect().roundOut(); |
| 63 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
| 64 | kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 65 | 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 | |
| 73 | SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
| 74 | // capture pixels |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame^] | 75 | SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 76 | 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 | |
| 84 | SkCanvas* 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); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame^] | 90 | |
| 91 | // create the appropriate surface if necessary |
| 92 | if (!fSurface) { |
| 93 | this->enableGPU(fGPUEnabled); |
| 94 | } |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 95 | SkCanvas* target = fSurface->getCanvas(); |
| 96 | return target; |
| 97 | } |
| 98 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 99 | void Request::drawToCanvas(int n, int m) { |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 100 | SkCanvas* target = this->getCanvas(); |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 101 | fDebugCanvas->drawTo(target, n, m); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 102 | } |
| 103 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 104 | SkData* Request::drawToPng(int n, int m) { |
| 105 | this->drawToCanvas(n, m); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 106 | return writeCanvasToPng(this->getCanvas()); |
| 107 | } |
| 108 | |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame^] | 109 | SkData* 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 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 127 | SkSurface* Request::createCPUSurface() { |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame^] | 128 | SkIRect bounds = fPicture->cullRect().roundOut(); |
| 129 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), kN32_SkColorType, |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 130 | kPremul_SkAlphaType); |
| 131 | return SkSurface::NewRaster(info); |
| 132 | } |
| 133 | |
| 134 | SkSurface* Request::createGPUSurface() { |
| 135 | GrContext* context = fContextFactory->get(GrContextFactory::kNative_GLContextType, |
| 136 | GrContextFactory::kNone_GLContextOptions); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame^] | 137 | SkIRect bounds = fPicture->cullRect().roundOut(); |
| 138 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 139 | 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 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 147 | bool 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; |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | bool 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 |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame^] | 171 | SkIRect bounds = fPicture->cullRect().roundOut(); |
| 172 | fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height())); |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 173 | fDebugCanvas->drawPicture(fPicture); |
joshualitt | 3a9be69 | 2016-02-29 11:38:11 -0800 | [diff] [blame] | 174 | |
| 175 | // for some reason we need to 'flush' the debug canvas by drawing all of the ops |
| 176 | fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 177 | this->getCanvas()->flush(); |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 178 | return true; |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 179 | } |
| 180 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 181 | GrAuditTrail* 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 | |
| 195 | void Request::cleanupAuditTrail(SkCanvas* canvas) { |
| 196 | GrAuditTrail* at = this->getAuditTrail(canvas); |
| 197 | if (at) { |
| 198 | GrAuditTrail::AutoEnable ae(at); |
| 199 | at->fullReset(); |
| 200 | } |
| 201 | } |
| 202 | |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 203 | SkData* 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"); |
joshualitt | 5d5207a | 2016-02-29 12:46:04 -0800 | [diff] [blame] | 207 | root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds()); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 208 | SkDynamicMemoryWStream stream; |
| 209 | stream.writeText(Json::FastWriter().write(root).c_str()); |
| 210 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 211 | this->cleanupAuditTrail(canvas); |
| 212 | |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 213 | return stream.copyToData(); |
| 214 | } |
| 215 | |
| 216 | SkData* 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 |
joshualitt | 3a9be69 | 2016-02-29 11:38:11 -0800 | [diff] [blame] | 224 | // 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 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 228 | GrAuditTrail* at = this->getAuditTrail(canvas); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 229 | GrAuditTrail::AutoManageBatchList enable(at); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 230 | Json::Reader reader; |
joshualitt | 3a9be69 | 2016-02-29 11:38:11 -0800 | [diff] [blame] | 231 | SkDEBUGCODE(bool parsingSuccessful = )reader.parse(at->toJson().c_str(), |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 232 | 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 | } |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 241 | |
| 242 | SkData* 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); |
joshualitt | bd72413 | 2016-03-03 11:39:38 -0800 | [diff] [blame] | 254 | info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm); |
| 255 | info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip); |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 256 | |
| 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 | } |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame^] | 262 | |
| 263 | SkColor 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 | |