Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Request.h" | 8 #include "Request.h" |
| 9 | 9 |
| 10 #include "png.h" | 10 #include "png.h" |
| 11 | 11 |
| 12 const int Request::kImageWidth = 1920; | 12 #include "SkPictureRecorder.h" |
| 13 const int Request::kImageHeight = 1080; | 13 #include "SkPixelSerializer.h" |
| 14 | 14 |
| 15 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l ength) { | 15 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l ength) { |
| 16 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); | 16 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); |
| 17 out->write(data, length); | 17 out->write(data, length); |
| 18 } | 18 } |
| 19 | 19 |
| 20 static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 heigh t, SkWStream& out) { | 20 static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 heigh t, SkWStream& out) { |
| 21 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | 21 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 22 SkASSERT(png != nullptr); | 22 SkASSERT(png != nullptr); |
| 23 png_infop info_ptr = png_create_info_struct(png); | 23 png_infop info_ptr = png_create_info_struct(png); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 48 sk_free(rows); | 48 sk_free(rows); |
| 49 } | 49 } |
| 50 | 50 |
| 51 Request::Request(SkString rootUrl) | 51 Request::Request(SkString rootUrl) |
| 52 : fUploadContext(nullptr) | 52 : fUploadContext(nullptr) |
| 53 , fUrlDataManager(rootUrl) | 53 , fUrlDataManager(rootUrl) |
| 54 , fGPUEnabled(false) { | 54 , fGPUEnabled(false) { |
| 55 // create surface | 55 // create surface |
| 56 GrContextOptions grContextOpts; | 56 GrContextOptions grContextOpts; |
| 57 fContextFactory.reset(new GrContextFactory(grContextOpts)); | 57 fContextFactory.reset(new GrContextFactory(grContextOpts)); |
| 58 fSurface.reset(this->createCPUSurface()); | |
| 59 } | 58 } |
| 60 | 59 |
| 61 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { | 60 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { |
| 62 SkBitmap* bmp = new SkBitmap(); | 61 SkBitmap* bmp = new SkBitmap(); |
| 63 SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kRGBA_8888_S kColorType, | 62 SkIRect bounds = fPicture->cullRect().roundOut(); |
| 64 kOpaque_SkAlphaType); | 63 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
| 64 kRGBA_8888_SkColorType, kOpaque_SkAlpha Type); | |
| 65 bmp->setInfo(info); | 65 bmp->setInfo(info); |
| 66 if (!canvas->readPixels(bmp, 0, 0)) { | 66 if (!canvas->readPixels(bmp, 0, 0)) { |
| 67 fprintf(stderr, "Can't read pixels\n"); | 67 fprintf(stderr, "Can't read pixels\n"); |
| 68 return nullptr; | 68 return nullptr; |
| 69 } | 69 } |
| 70 return bmp; | 70 return bmp; |
| 71 } | 71 } |
| 72 | 72 |
| 73 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { | 73 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
| 74 // capture pixels | 74 // capture pixels |
| 75 SkAutoTDelete<SkBitmap> bmp(getBitmapFromCanvas(canvas)); | 75 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
| 76 SkASSERT(bmp); | 76 SkASSERT(bmp); |
| 77 | 77 |
| 78 // write to png | 78 // write to png |
| 79 SkDynamicMemoryWStream buffer; | 79 SkDynamicMemoryWStream buffer; |
| 80 write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), b uffer); | 80 write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), b uffer); |
| 81 return buffer.copyToData(); | 81 return buffer.copyToData(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 SkCanvas* Request::getCanvas() { | 84 SkCanvas* Request::getCanvas() { |
| 85 GrContextFactory* factory = fContextFactory; | 85 GrContextFactory* factory = fContextFactory; |
| 86 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex tType, | 86 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex tType, |
| 87 GrContextFactory::kNone_GLContextO ptions).fGLContext; | 87 GrContextFactory::kNone_GLContextO ptions).fGLContext; |
| 88 gl->makeCurrent(); | 88 gl->makeCurrent(); |
| 89 SkASSERT(fDebugCanvas); | 89 SkASSERT(fDebugCanvas); |
| 90 | |
| 91 // create the appropriate surface if necessary | |
| 92 if (!fSurface) { | |
| 93 this->enableGPU(fGPUEnabled); | |
| 94 } | |
| 90 SkCanvas* target = fSurface->getCanvas(); | 95 SkCanvas* target = fSurface->getCanvas(); |
| 91 return target; | 96 return target; |
| 92 } | 97 } |
| 93 | 98 |
| 94 void Request::drawToCanvas(int n, int m) { | 99 void Request::drawToCanvas(int n, int m) { |
| 95 SkCanvas* target = this->getCanvas(); | 100 SkCanvas* target = this->getCanvas(); |
| 96 fDebugCanvas->drawTo(target, n, m); | 101 fDebugCanvas->drawTo(target, n, m); |
| 97 } | 102 } |
| 98 | 103 |
| 99 SkData* Request::drawToPng(int n, int m) { | 104 SkData* Request::drawToPng(int n, int m) { |
| 100 this->drawToCanvas(n, m); | 105 this->drawToCanvas(n, m); |
| 101 return writeCanvasToPng(this->getCanvas()); | 106 return writeCanvasToPng(this->getCanvas()); |
| 102 } | 107 } |
| 103 | 108 |
| 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::CreatePixelSerial izer()); | |
| 122 picture->serialize(&outStream, serializer); | |
| 123 | |
| 124 return outStream.copyToData(); | |
| 125 } | |
| 126 | |
| 104 SkSurface* Request::createCPUSurface() { | 127 SkSurface* Request::createCPUSurface() { |
| 105 SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kN32_SkColor Type, | 128 SkIRect bounds = fPicture->cullRect().roundOut(); |
| 129 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), kN32_S kColorType, | |
| 106 kPremul_SkAlphaType); | 130 kPremul_SkAlphaType); |
| 107 return SkSurface::NewRaster(info); | 131 return SkSurface::NewRaster(info); |
| 108 } | 132 } |
| 109 | 133 |
| 110 SkSurface* Request::createGPUSurface() { | 134 SkSurface* Request::createGPUSurface() { |
| 111 GrContext* context = fContextFactory->get(GrContextFactory::kNative_GLContex tType, | 135 GrContext* context = fContextFactory->get(GrContextFactory::kNative_GLContex tType, |
| 112 GrContextFactory::kNone_GLContextO ptions); | 136 GrContextFactory::kNone_GLContextO ptions); |
| 113 int maxRTSize = context->caps()->maxRenderTargetSize(); | 137 SkIRect bounds = fPicture->cullRect().roundOut(); |
| 114 SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize), | 138 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
| 115 SkTMin(kImageHeight, maxRTSize), | |
| 116 kN32_SkColorType, kPremul_SkAlphaType); | 139 kN32_SkColorType, kPremul_SkAlphaType); |
| 117 uint32_t flags = 0; | 140 uint32_t flags = 0; |
| 118 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); | 141 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 119 SkSurface* surface = SkSurface::NewRenderTarget(context, SkBudgeted::kNo, in fo, 0, | 142 SkSurface* surface = SkSurface::NewRenderTarget(context, SkBudgeted::kNo, in fo, 0, |
| 120 &props); | 143 &props); |
| 121 return surface; | 144 return surface; |
| 122 } | 145 } |
| 123 | 146 |
| 124 bool Request::enableGPU(bool enable) { | 147 bool Request::enableGPU(bool enable) { |
| 125 if (enable) { | 148 if (enable) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 138 | 161 |
| 139 bool Request::initPictureFromStream(SkStream* stream) { | 162 bool Request::initPictureFromStream(SkStream* stream) { |
| 140 // parse picture from stream | 163 // parse picture from stream |
| 141 fPicture.reset(SkPicture::CreateFromStream(stream)); | 164 fPicture.reset(SkPicture::CreateFromStream(stream)); |
| 142 if (!fPicture.get()) { | 165 if (!fPicture.get()) { |
| 143 fprintf(stderr, "Could not create picture from stream.\n"); | 166 fprintf(stderr, "Could not create picture from stream.\n"); |
| 144 return false; | 167 return false; |
| 145 } | 168 } |
| 146 | 169 |
| 147 // pour picture into debug canvas | 170 // pour picture into debug canvas |
| 148 fDebugCanvas.reset(new SkDebugCanvas(kImageWidth, Request::kImageHeight)); | 171 SkIRect bounds = fPicture->cullRect().roundOut(); |
| 172 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height())); | |
| 149 fDebugCanvas->drawPicture(fPicture); | 173 fDebugCanvas->drawPicture(fPicture); |
| 150 | 174 |
| 151 // for some reason we need to 'flush' the debug canvas by drawing all of the ops | 175 // for some reason we need to 'flush' the debug canvas by drawing all of the ops |
| 152 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); | 176 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
| 153 this->getCanvas()->flush(); | 177 this->getCanvas()->flush(); |
| 154 return true; | 178 return true; |
| 155 } | 179 } |
| 156 | 180 |
| 157 GrAuditTrail* Request::getAuditTrail(SkCanvas* canvas) { | 181 GrAuditTrail* Request::getAuditTrail(SkCanvas* canvas) { |
| 158 GrAuditTrail* at = nullptr; | 182 GrAuditTrail* at = nullptr; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 SkIRect clip = fDebugCanvas->getCurrentClip(); | 252 SkIRect clip = fDebugCanvas->getCurrentClip(); |
| 229 Json::Value info(Json::objectValue); | 253 Json::Value info(Json::objectValue); |
| 230 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm); | 254 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm); |
| 231 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip); | 255 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip); |
| 232 | 256 |
| 233 std::string json = Json::FastWriter().write(info); | 257 std::string json = Json::FastWriter().write(info); |
| 234 | 258 |
| 235 // We don't want the null terminator so strlen is correct | 259 // We don't want the null terminator so strlen is correct |
| 236 return SkData::NewWithCopy(json.c_str(), strlen(json.c_str())); | 260 return SkData::NewWithCopy(json.c_str(), strlen(json.c_str())); |
| 237 } | 261 } |
| 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; | |
|
joshualitt
2016/03/09 15:34:47
I changed this to bitmap->width(), is that correct
ethannicholas
2016/03/09 16:32:36
Yep.
| |
| 270 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); | |
| 271 bitmap->unlockPixels(); | |
| 272 return result; | |
| 273 } | |
| 274 | |
| OLD | NEW |