| 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 #include "SkJSONCanvas.h" | 
|  | 13 | 
| 12 const int Request::kImageWidth = 1920; | 14 const int Request::kImageWidth = 1920; | 
| 13 const int Request::kImageHeight = 1080; | 15 const int Request::kImageHeight = 1080; | 
| 14 | 16 | 
| 15 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l
     ength) { | 17 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); | 18     SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); | 
| 17     out->write(data, length); | 19     out->write(data, length); | 
| 18 } | 20 } | 
| 19 | 21 | 
| 20 static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 heigh
     t, SkWStream& out) { | 22 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); | 23     png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
      NULL); | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
| 41         } | 43         } | 
| 42     } | 44     } | 
| 43     png_set_filter(png, 0, PNG_NO_FILTERS); | 45     png_set_filter(png, 0, PNG_NO_FILTERS); | 
| 44     png_set_rows(png, info_ptr, &rows[0]); | 46     png_set_rows(png, info_ptr, &rows[0]); | 
| 45     png_set_write_fn(png, &out, write_png_callback, NULL); | 47     png_set_write_fn(png, &out, write_png_callback, NULL); | 
| 46     png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); | 48     png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); | 
| 47     png_destroy_write_struct(&png, NULL); | 49     png_destroy_write_struct(&png, NULL); | 
| 48     sk_free(rows); | 50     sk_free(rows); | 
| 49 } | 51 } | 
| 50 | 52 | 
|  | 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 | 
| 51 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { | 63 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { | 
| 52     SkBitmap* bmp = new SkBitmap(); | 64     SkBitmap* bmp = new SkBitmap(); | 
| 53     SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kRGBA_8888_S
     kColorType, | 65     SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kRGBA_8888_S
     kColorType, | 
| 54                 kOpaque_SkAlphaType); | 66                 kOpaque_SkAlphaType); | 
| 55     bmp->setInfo(info); | 67     bmp->setInfo(info); | 
| 56     if (!canvas->readPixels(bmp, 0, 0)) { | 68     if (!canvas->readPixels(bmp, 0, 0)) { | 
| 57         fprintf(stderr, "Can't read pixels\n"); | 69         fprintf(stderr, "Can't read pixels\n"); | 
| 58         return nullptr; | 70         return nullptr; | 
| 59     } | 71     } | 
| 60     return bmp; | 72     return bmp; | 
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 104     SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize), | 116     SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize), | 
| 105                                          SkTMin(kImageHeight, maxRTSize), | 117                                          SkTMin(kImageHeight, maxRTSize), | 
| 106                                          kN32_SkColorType, kPremul_SkAlphaType); | 118                                          kN32_SkColorType, kPremul_SkAlphaType); | 
| 107     uint32_t flags = 0; | 119     uint32_t flags = 0; | 
| 108     SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); | 120     SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); | 
| 109     SkSurface* surface = SkSurface::NewRenderTarget(context, SkBudgeted::kNo, in
     fo, 0, | 121     SkSurface* surface = SkSurface::NewRenderTarget(context, SkBudgeted::kNo, in
     fo, 0, | 
| 110                                                     &props); | 122                                                     &props); | 
| 111     return surface; | 123     return surface; | 
| 112 } | 124 } | 
| 113 | 125 | 
|  | 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; | 
|  | 139 } | 
|  | 140 | 
| 114 SkData* Request::getJsonOps(int n) { | 141 SkData* Request::getJsonOps(int n) { | 
| 115     SkCanvas* canvas = this->getCanvas(); | 142     SkCanvas* canvas = this->getCanvas(); | 
| 116     Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas); | 143     Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas); | 
| 117     root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu"); | 144     root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu"); | 
| 118     SkDynamicMemoryWStream stream; | 145     SkDynamicMemoryWStream stream; | 
| 119     stream.writeText(Json::FastWriter().write(root).c_str()); | 146     stream.writeText(Json::FastWriter().write(root).c_str()); | 
| 120 | 147 | 
| 121     return stream.copyToData(); | 148     return stream.copyToData(); | 
| 122 } | 149 } | 
| 123 | 150 | 
| (...skipping 18 matching lines...) Expand all  Loading... | 
| 142     SkDEBUGCODE(bool parsingSuccessful = )reader.parse(at->toJson(true).c_str(), | 169     SkDEBUGCODE(bool parsingSuccessful = )reader.parse(at->toJson(true).c_str(), | 
| 143                                                        parsedFromString); | 170                                                        parsedFromString); | 
| 144     SkASSERT(parsingSuccessful); | 171     SkASSERT(parsingSuccessful); | 
| 145 #endif | 172 #endif | 
| 146 | 173 | 
| 147     SkDynamicMemoryWStream stream; | 174     SkDynamicMemoryWStream stream; | 
| 148     stream.writeText(Json::FastWriter().write(parsedFromString).c_str()); | 175     stream.writeText(Json::FastWriter().write(parsedFromString).c_str()); | 
| 149 | 176 | 
| 150     return stream.copyToData(); | 177     return stream.copyToData(); | 
| 151 } | 178 } | 
|  | 179 | 
|  | 180 SkData* Request::getJsonInfo(int n) { | 
|  | 181     // drawTo | 
|  | 182     SkAutoTUnref<SkSurface> surface(this->createCPUSurface()); | 
|  | 183     SkCanvas* canvas = surface->getCanvas(); | 
|  | 184 | 
|  | 185     // TODO this is really slow and we should cache the matrix and clip | 
|  | 186     fDebugCanvas->drawTo(canvas, n); | 
|  | 187 | 
|  | 188     // make some json | 
|  | 189     SkMatrix vm = fDebugCanvas->getCurrentMatrix(); | 
|  | 190     SkIRect clip = fDebugCanvas->getCurrentClip(); | 
|  | 191     Json::Value info(Json::objectValue); | 
|  | 192     info["ViewMatrix"] = SkJSONCanvas::MakeMatrix(vm); | 
|  | 193     info["ClipRect"] = SkJSONCanvas::MakeIRect(clip); | 
|  | 194 | 
|  | 195     std::string json = Json::FastWriter().write(info); | 
|  | 196 | 
|  | 197     // We don't want the null terminator so strlen is correct | 
|  | 198     return SkData::NewWithCopy(json.c_str(), strlen(json.c_str())); | 
|  | 199 } | 
| OLD | NEW | 
|---|