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 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 15 | static int kDefaultWidth = 1920; |
| 16 | static int kDefaultHeight = 1080; |
| 17 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 18 | static 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 | |
| 23 | static 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 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 54 | Request::Request(SkString rootUrl) |
| 55 | : fUploadContext(nullptr) |
| 56 | , fUrlDataManager(rootUrl) |
| 57 | , fGPUEnabled(false) { |
| 58 | // create surface |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 59 | #if SK_SUPPORT_GPU |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 60 | GrContextOptions grContextOpts; |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 61 | fContextFactory = new GrContextFactory(grContextOpts); |
| 62 | #else |
| 63 | fContextFactory = nullptr; |
| 64 | #endif |
| 65 | } |
| 66 | |
| 67 | Request::~Request() { |
| 68 | #if SK_SUPPORT_GPU |
| 69 | if (fContextFactory) { |
| 70 | delete fContextFactory; |
| 71 | } |
| 72 | #endif |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 73 | } |
| 74 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 75 | SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { |
| 76 | SkBitmap* bmp = new SkBitmap(); |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 77 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 78 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
| 79 | kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 80 | 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 | |
| 88 | SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
| 89 | // capture pixels |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 90 | SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 91 | 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 | |
| 99 | SkCanvas* Request::getCanvas() { |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 100 | #if SK_SUPPORT_GPU |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 101 | GrContextFactory* factory = fContextFactory; |
| 102 | SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType, |
| 103 | GrContextFactory::kNone_GLContextOptions).fGLContext; |
| 104 | gl->makeCurrent(); |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 105 | #endif |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 106 | SkASSERT(fDebugCanvas); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 107 | |
| 108 | // create the appropriate surface if necessary |
| 109 | if (!fSurface) { |
| 110 | this->enableGPU(fGPUEnabled); |
| 111 | } |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 112 | SkCanvas* target = fSurface->getCanvas(); |
| 113 | return target; |
| 114 | } |
| 115 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 116 | void Request::drawToCanvas(int n, int m) { |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 117 | SkCanvas* target = this->getCanvas(); |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 118 | fDebugCanvas->drawTo(target, n, m); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 119 | } |
| 120 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 121 | SkData* Request::drawToPng(int n, int m) { |
| 122 | this->drawToCanvas(n, m); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 123 | return writeCanvasToPng(this->getCanvas()); |
| 124 | } |
| 125 | |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 126 | SkData* Request::writeOutSkp() { |
| 127 | // Playback into picture recorder |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 128 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 129 | 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 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 144 | GrContext* Request::getContext() { |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 145 | #if SK_SUPPORT_GPU |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 146 | return fContextFactory->get(GrContextFactory::kNative_GLContextType, |
| 147 | GrContextFactory::kNone_GLContextOptions); |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 148 | #else |
| 149 | return nullptr; |
| 150 | #endif |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | SkIRect Request::getBounds() { |
| 154 | SkIRect bounds; |
| 155 | if (fPicture) { |
| 156 | bounds = fPicture->cullRect().roundOut(); |
| 157 | if (fGPUEnabled) { |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 158 | #if SK_SUPPORT_GPU |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 159 | int maxRTSize = this->getContext()->caps()->maxRenderTargetSize(); |
| 160 | bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize), |
| 161 | SkTMin(bounds.height(), maxRTSize)); |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 162 | #endif |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 163 | } |
| 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 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 175 | SkSurface* Request::createCPUSurface() { |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 176 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 177 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), kN32_SkColorType, |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 178 | kPremul_SkAlphaType); |
| 179 | return SkSurface::NewRaster(info); |
| 180 | } |
| 181 | |
| 182 | SkSurface* Request::createGPUSurface() { |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 183 | GrContext* context = this->getContext(); |
| 184 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 185 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 186 | 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 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 194 | bool Request::enableGPU(bool enable) { |
| 195 | if (enable) { |
| 196 | SkSurface* surface = this->createGPUSurface(); |
| 197 | if (surface) { |
| 198 | fSurface.reset(surface); |
| 199 | fGPUEnabled = true; |
joshualitt | 98bd5b1 | 2016-03-11 12:08:15 -0800 | [diff] [blame^] | 200 | |
| 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 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | fSurface.reset(this->createCPUSurface()); |
| 212 | fGPUEnabled = false; |
| 213 | return true; |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | bool 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 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 224 | // reinitialize canvas with the new picture dimensions |
| 225 | this->enableGPU(fGPUEnabled); |
| 226 | |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 227 | // pour picture into debug canvas |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 228 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 229 | fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height())); |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 230 | fDebugCanvas->drawPicture(fPicture); |
joshualitt | 3a9be69 | 2016-02-29 11:38:11 -0800 | [diff] [blame] | 231 | |
| 232 | // for some reason we need to 'flush' the debug canvas by drawing all of the ops |
| 233 | fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 234 | this->getCanvas()->flush(); |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 235 | return true; |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 236 | } |
| 237 | |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 238 | SkData* 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"); |
joshualitt | 5d5207a | 2016-02-29 12:46:04 -0800 | [diff] [blame] | 242 | root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds()); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 243 | SkDynamicMemoryWStream stream; |
| 244 | stream.writeText(Json::FastWriter().write(root).c_str()); |
| 245 | |
| 246 | return stream.copyToData(); |
| 247 | } |
| 248 | |
| 249 | SkData* Request::getJsonBatchList(int n) { |
| 250 | SkCanvas* canvas = this->getCanvas(); |
| 251 | SkASSERT(fGPUEnabled); |
| 252 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 253 | Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 254 | |
| 255 | SkDynamicMemoryWStream stream; |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 256 | stream.writeText(Json::FastWriter().write(result).c_str()); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 257 | |
| 258 | return stream.copyToData(); |
| 259 | } |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 260 | |
| 261 | SkData* 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); |
joshualitt | bd72413 | 2016-03-03 11:39:38 -0800 | [diff] [blame] | 273 | info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm); |
| 274 | info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip); |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 275 | |
| 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 | } |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 281 | |
| 282 | SkColor 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 | |