| 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" | |
| 11 | |
| 12 #include "SkPictureRecorder.h" | 10 #include "SkPictureRecorder.h" |
| 13 #include "SkPixelSerializer.h" | 11 #include "SkPixelSerializer.h" |
| 14 | 12 |
| 15 static int kDefaultWidth = 1920; | 13 static int kDefaultWidth = 1920; |
| 16 static int kDefaultHeight = 1080; | 14 static int kDefaultHeight = 1080; |
| 17 | 15 |
| 18 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l
ength) { | |
| 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 heigh
t, 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_INTERL
ACE_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 | 16 |
| 54 Request::Request(SkString rootUrl) | 17 Request::Request(SkString rootUrl) |
| 55 : fUploadContext(nullptr) | 18 : fUploadContext(nullptr) |
| 56 , fUrlDataManager(rootUrl) | 19 , fUrlDataManager(rootUrl) |
| 57 , fGPUEnabled(false) { | 20 , fGPUEnabled(false) { |
| 58 // create surface | 21 // create surface |
| 59 #if SK_SUPPORT_GPU | 22 #if SK_SUPPORT_GPU |
| 60 GrContextOptions grContextOpts; | 23 GrContextOptions grContextOpts; |
| 61 fContextFactory = new GrContextFactory(grContextOpts); | 24 fContextFactory = new GrContextFactory(grContextOpts); |
| 62 #else | 25 #else |
| (...skipping 22 matching lines...) Expand all Loading... |
| 85 return bmp; | 48 return bmp; |
| 86 } | 49 } |
| 87 | 50 |
| 88 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { | 51 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
| 89 // capture pixels | 52 // capture pixels |
| 90 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); | 53 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
| 91 SkASSERT(bmp); | 54 SkASSERT(bmp); |
| 92 | 55 |
| 93 // write to png | 56 // write to png |
| 94 SkDynamicMemoryWStream buffer; | 57 SkDynamicMemoryWStream buffer; |
| 95 write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), b
uffer); | 58 SkDrawCommand::WritePNG((const png_bytep) bmp->getPixels(), bmp->width(), bm
p->height(), |
| 59 buffer); |
| 96 return buffer.copyToData(); | 60 return buffer.copyToData(); |
| 97 } | 61 } |
| 98 | 62 |
| 99 SkCanvas* Request::getCanvas() { | 63 SkCanvas* Request::getCanvas() { |
| 100 #if SK_SUPPORT_GPU | 64 #if SK_SUPPORT_GPU |
| 101 GrContextFactory* factory = fContextFactory; | 65 GrContextFactory* factory = fContextFactory; |
| 102 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex
tType, | 66 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex
tType, |
| 103 GrContextFactory::kNone_GLContextO
ptions).fGLContext; | 67 GrContextFactory::kNone_GLContextO
ptions).fGLContext; |
| 104 gl->makeCurrent(); | 68 gl->makeCurrent(); |
| 105 #endif | 69 #endif |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 canvas->flush(); | 248 canvas->flush(); |
| 285 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); | 249 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); |
| 286 SkASSERT(bitmap); | 250 SkASSERT(bitmap); |
| 287 bitmap->lockPixels(); | 251 bitmap->lockPixels(); |
| 288 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x
) * 4; | 252 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x
) * 4; |
| 289 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); | 253 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); |
| 290 bitmap->unlockPixels(); | 254 bitmap->unlockPixels(); |
| 291 return result; | 255 return result; |
| 292 } | 256 } |
| 293 | 257 |
| OLD | NEW |