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 |
| 59 | GrContextOptions grContextOpts; |
| 60 | fContextFactory.reset(new GrContextFactory(grContextOpts)); |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 61 | } |
| 62 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 63 | SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { |
| 64 | SkBitmap* bmp = new SkBitmap(); |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 65 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 66 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
| 67 | kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 68 | 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 | |
| 76 | SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
| 77 | // capture pixels |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 78 | SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 79 | 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 | |
| 87 | SkCanvas* 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); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 93 | |
| 94 | // create the appropriate surface if necessary |
| 95 | if (!fSurface) { |
| 96 | this->enableGPU(fGPUEnabled); |
| 97 | } |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 98 | SkCanvas* target = fSurface->getCanvas(); |
| 99 | return target; |
| 100 | } |
| 101 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 102 | void Request::drawToCanvas(int n, int m) { |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 103 | SkCanvas* target = this->getCanvas(); |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 104 | fDebugCanvas->drawTo(target, n, m); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 105 | } |
| 106 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 107 | SkData* Request::drawToPng(int n, int m) { |
| 108 | this->drawToCanvas(n, m); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 109 | return writeCanvasToPng(this->getCanvas()); |
| 110 | } |
| 111 | |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 112 | SkData* Request::writeOutSkp() { |
| 113 | // Playback into picture recorder |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 114 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 115 | 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 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 130 | GrContext* Request::getContext() { |
| 131 | return fContextFactory->get(GrContextFactory::kNative_GLContextType, |
| 132 | GrContextFactory::kNone_GLContextOptions); |
| 133 | } |
| 134 | |
| 135 | SkIRect 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 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 155 | SkSurface* Request::createCPUSurface() { |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 156 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 157 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), kN32_SkColorType, |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 158 | kPremul_SkAlphaType); |
| 159 | return SkSurface::NewRaster(info); |
| 160 | } |
| 161 | |
| 162 | SkSurface* Request::createGPUSurface() { |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 163 | GrContext* context = this->getContext(); |
| 164 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 165 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 166 | 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 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 174 | bool 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; |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | bool 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 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 197 | // reinitialize canvas with the new picture dimensions |
| 198 | this->enableGPU(fGPUEnabled); |
| 199 | |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 200 | // pour picture into debug canvas |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 201 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 202 | fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height())); |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 203 | fDebugCanvas->drawPicture(fPicture); |
joshualitt | 3a9be69 | 2016-02-29 11:38:11 -0800 | [diff] [blame] | 204 | |
| 205 | // for some reason we need to 'flush' the debug canvas by drawing all of the ops |
| 206 | fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 207 | this->getCanvas()->flush(); |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 208 | return true; |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 209 | } |
| 210 | |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 211 | SkData* 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"); |
joshualitt | 5d5207a | 2016-02-29 12:46:04 -0800 | [diff] [blame] | 215 | root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds()); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 216 | SkDynamicMemoryWStream stream; |
| 217 | stream.writeText(Json::FastWriter().write(root).c_str()); |
| 218 | |
| 219 | return stream.copyToData(); |
| 220 | } |
| 221 | |
| 222 | SkData* Request::getJsonBatchList(int n) { |
| 223 | SkCanvas* canvas = this->getCanvas(); |
| 224 | SkASSERT(fGPUEnabled); |
| 225 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 226 | Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 227 | |
| 228 | SkDynamicMemoryWStream stream; |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 229 | stream.writeText(Json::FastWriter().write(result).c_str()); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 230 | |
| 231 | return stream.copyToData(); |
| 232 | } |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 233 | |
| 234 | SkData* 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); |
joshualitt | bd72413 | 2016-03-03 11:39:38 -0800 | [diff] [blame] | 246 | info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm); |
| 247 | info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip); |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 248 | |
| 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 | } |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 254 | |
| 255 | SkColor 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 | |