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 | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 12 | #include "SkJSONCanvas.h" |
| 13 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 14 | const int Request::kImageWidth = 1920; |
| 15 | const int Request::kImageHeight = 1080; |
| 16 | |
| 17 | static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t length) { |
| 18 | SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); |
| 19 | out->write(data, length); |
| 20 | } |
| 21 | |
| 22 | static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 height, SkWStream& out) { |
| 23 | png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 24 | SkASSERT(png != nullptr); |
| 25 | png_infop info_ptr = png_create_info_struct(png); |
| 26 | SkASSERT(info_ptr != nullptr); |
| 27 | if (setjmp(png_jmpbuf(png))) { |
| 28 | SkFAIL("png encode error"); |
| 29 | } |
| 30 | png_set_IHDR(png, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, |
| 31 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 32 | png_set_compression_level(png, 1); |
| 33 | png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*)); |
| 34 | png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 3); |
| 35 | for (png_size_t y = 0; y < height; ++y) { |
| 36 | const png_bytep src = rgba + y * width * 4; |
| 37 | rows[y] = pixels + y * width * 3; |
| 38 | // convert from RGBA to RGB |
| 39 | for (png_size_t x = 0; x < width; ++x) { |
| 40 | rows[y][x * 3] = src[x * 4]; |
| 41 | rows[y][x * 3 + 1] = src[x * 4 + 1]; |
| 42 | rows[y][x * 3 + 2] = src[x * 4 + 2]; |
| 43 | } |
| 44 | } |
| 45 | png_set_filter(png, 0, PNG_NO_FILTERS); |
| 46 | png_set_rows(png, info_ptr, &rows[0]); |
| 47 | png_set_write_fn(png, &out, write_png_callback, NULL); |
| 48 | png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); |
| 49 | png_destroy_write_struct(&png, NULL); |
| 50 | sk_free(rows); |
| 51 | } |
| 52 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 53 | Request::Request(SkString rootUrl) |
| 54 | : fUploadContext(nullptr) |
| 55 | , fUrlDataManager(rootUrl) |
| 56 | , fGPUEnabled(false) { |
| 57 | // create surface |
| 58 | GrContextOptions grContextOpts; |
| 59 | fContextFactory.reset(new GrContextFactory(grContextOpts)); |
| 60 | fSurface.reset(this->createCPUSurface()); |
| 61 | } |
| 62 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 63 | SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { |
| 64 | SkBitmap* bmp = new SkBitmap(); |
| 65 | SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kRGBA_8888_SkColorType, |
| 66 | kOpaque_SkAlphaType); |
| 67 | bmp->setInfo(info); |
| 68 | if (!canvas->readPixels(bmp, 0, 0)) { |
| 69 | fprintf(stderr, "Can't read pixels\n"); |
| 70 | return nullptr; |
| 71 | } |
| 72 | return bmp; |
| 73 | } |
| 74 | |
| 75 | SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
| 76 | // capture pixels |
| 77 | SkAutoTDelete<SkBitmap> bmp(getBitmapFromCanvas(canvas)); |
| 78 | SkASSERT(bmp); |
| 79 | |
| 80 | // write to png |
| 81 | SkDynamicMemoryWStream buffer; |
| 82 | write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), buffer); |
| 83 | return buffer.copyToData(); |
| 84 | } |
| 85 | |
| 86 | SkCanvas* Request::getCanvas() { |
| 87 | GrContextFactory* factory = fContextFactory; |
| 88 | SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType, |
| 89 | GrContextFactory::kNone_GLContextOptions).fGLContext; |
| 90 | gl->makeCurrent(); |
| 91 | SkASSERT(fDebugCanvas); |
| 92 | SkCanvas* target = fSurface->getCanvas(); |
| 93 | return target; |
| 94 | } |
| 95 | |
| 96 | void Request::drawToCanvas(int n) { |
| 97 | SkCanvas* target = this->getCanvas(); |
| 98 | fDebugCanvas->drawTo(target, n); |
| 99 | } |
| 100 | |
| 101 | SkData* Request::drawToPng(int n) { |
| 102 | this->drawToCanvas(n); |
| 103 | return writeCanvasToPng(this->getCanvas()); |
| 104 | } |
| 105 | |
| 106 | SkSurface* Request::createCPUSurface() { |
| 107 | SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kN32_SkColorType, |
| 108 | kPremul_SkAlphaType); |
| 109 | return SkSurface::NewRaster(info); |
| 110 | } |
| 111 | |
| 112 | SkSurface* Request::createGPUSurface() { |
| 113 | GrContext* context = fContextFactory->get(GrContextFactory::kNative_GLContextType, |
| 114 | GrContextFactory::kNone_GLContextOptions); |
| 115 | int maxRTSize = context->caps()->maxRenderTargetSize(); |
| 116 | SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize), |
| 117 | SkTMin(kImageHeight, maxRTSize), |
| 118 | kN32_SkColorType, kPremul_SkAlphaType); |
| 119 | uint32_t flags = 0; |
| 120 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 121 | SkSurface* surface = SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info, 0, |
| 122 | &props); |
| 123 | return surface; |
| 124 | } |
| 125 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 126 | bool Request::enableGPU(bool enable) { |
| 127 | if (enable) { |
| 128 | SkSurface* surface = this->createGPUSurface(); |
| 129 | if (surface) { |
| 130 | fSurface.reset(surface); |
| 131 | fGPUEnabled = true; |
| 132 | return true; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | fSurface.reset(this->createCPUSurface()); |
| 137 | fGPUEnabled = false; |
| 138 | return true; |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | bool Request::initPictureFromStream(SkStream* stream) { |
| 142 | // parse picture from stream |
| 143 | fPicture.reset(SkPicture::CreateFromStream(stream)); |
| 144 | if (!fPicture.get()) { |
| 145 | fprintf(stderr, "Could not create picture from stream.\n"); |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | // pour picture into debug canvas |
| 150 | fDebugCanvas.reset(new SkDebugCanvas(kImageWidth, Request::kImageHeight)); |
| 151 | fDebugCanvas->drawPicture(fPicture); |
| 152 | return true; |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 153 | } |
| 154 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame^] | 155 | GrAuditTrail* Request::getAuditTrail(SkCanvas* canvas) { |
| 156 | GrAuditTrail* at = nullptr; |
| 157 | #if SK_SUPPORT_GPU |
| 158 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
| 159 | if (rt) { |
| 160 | GrContext* ctx = rt->getContext(); |
| 161 | if (ctx) { |
| 162 | at = ctx->getAuditTrail(); |
| 163 | } |
| 164 | } |
| 165 | #endif |
| 166 | return at; |
| 167 | } |
| 168 | |
| 169 | void Request::cleanupAuditTrail(SkCanvas* canvas) { |
| 170 | GrAuditTrail* at = this->getAuditTrail(canvas); |
| 171 | if (at) { |
| 172 | GrAuditTrail::AutoEnable ae(at); |
| 173 | at->fullReset(); |
| 174 | } |
| 175 | } |
| 176 | |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 177 | SkData* Request::getJsonOps(int n) { |
| 178 | SkCanvas* canvas = this->getCanvas(); |
| 179 | Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas); |
| 180 | root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu"); |
| 181 | SkDynamicMemoryWStream stream; |
| 182 | stream.writeText(Json::FastWriter().write(root).c_str()); |
| 183 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame^] | 184 | this->cleanupAuditTrail(canvas); |
| 185 | |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 186 | return stream.copyToData(); |
| 187 | } |
| 188 | |
| 189 | SkData* Request::getJsonBatchList(int n) { |
| 190 | SkCanvas* canvas = this->getCanvas(); |
| 191 | SkASSERT(fGPUEnabled); |
| 192 | |
| 193 | // TODO if this is inefficient we could add a method to GrAuditTrail which takes |
| 194 | // a Json::Value and is only compiled in this file |
| 195 | Json::Value parsedFromString; |
| 196 | #if SK_SUPPORT_GPU |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame^] | 197 | GrAuditTrail* at = this->getAuditTrail(canvas); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 198 | GrAuditTrail::AutoManageBatchList enable(at); |
| 199 | |
| 200 | fDebugCanvas->drawTo(canvas, n); |
| 201 | |
| 202 | Json::Reader reader; |
| 203 | SkDEBUGCODE(bool parsingSuccessful = )reader.parse(at->toJson(true).c_str(), |
| 204 | parsedFromString); |
| 205 | SkASSERT(parsingSuccessful); |
| 206 | #endif |
| 207 | |
| 208 | SkDynamicMemoryWStream stream; |
| 209 | stream.writeText(Json::FastWriter().write(parsedFromString).c_str()); |
| 210 | |
| 211 | return stream.copyToData(); |
| 212 | } |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 213 | |
| 214 | SkData* Request::getJsonInfo(int n) { |
| 215 | // drawTo |
| 216 | SkAutoTUnref<SkSurface> surface(this->createCPUSurface()); |
| 217 | SkCanvas* canvas = surface->getCanvas(); |
| 218 | |
| 219 | // TODO this is really slow and we should cache the matrix and clip |
| 220 | fDebugCanvas->drawTo(canvas, n); |
| 221 | |
| 222 | // make some json |
| 223 | SkMatrix vm = fDebugCanvas->getCurrentMatrix(); |
| 224 | SkIRect clip = fDebugCanvas->getCurrentClip(); |
| 225 | Json::Value info(Json::objectValue); |
| 226 | info["ViewMatrix"] = SkJSONCanvas::MakeMatrix(vm); |
| 227 | info["ClipRect"] = SkJSONCanvas::MakeIRect(clip); |
| 228 | |
| 229 | std::string json = Json::FastWriter().write(info); |
| 230 | |
| 231 | // We don't want the null terminator so strlen is correct |
| 232 | return SkData::NewWithCopy(json.c_str(), strlen(json.c_str())); |
| 233 | } |