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 | |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 10 | #include "SkPictureRecorder.h" |
| 11 | #include "SkPixelSerializer.h" |
brianosman | 312aa6a | 2016-04-19 12:47:54 -0700 | [diff] [blame] | 12 | #include "SkPM4fPriv.h" |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 13 | #include "picture_utils.h" |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 14 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 15 | using namespace sk_gpu_test; |
| 16 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 17 | static int kDefaultWidth = 1920; |
| 18 | static int kDefaultHeight = 1080; |
| 19 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 20 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 21 | Request::Request(SkString rootUrl) |
| 22 | : fUploadContext(nullptr) |
| 23 | , fUrlDataManager(rootUrl) |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 24 | , fGPUEnabled(false) |
| 25 | , fColorMode(0) { |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 26 | // create surface |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 27 | #if SK_SUPPORT_GPU |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 28 | GrContextOptions grContextOpts; |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 29 | fContextFactory = new GrContextFactory(grContextOpts); |
| 30 | #else |
| 31 | fContextFactory = nullptr; |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | Request::~Request() { |
| 36 | #if SK_SUPPORT_GPU |
| 37 | if (fContextFactory) { |
| 38 | delete fContextFactory; |
| 39 | } |
| 40 | #endif |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 41 | } |
| 42 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 43 | SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { |
| 44 | SkBitmap* bmp = new SkBitmap(); |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 45 | bmp->setInfo(canvas->imageInfo()); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 46 | if (!canvas->readPixels(bmp, 0, 0)) { |
| 47 | fprintf(stderr, "Can't read pixels\n"); |
| 48 | return nullptr; |
| 49 | } |
| 50 | return bmp; |
| 51 | } |
| 52 | |
| 53 | SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
| 54 | // capture pixels |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 55 | SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 56 | SkASSERT(bmp); |
| 57 | |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 58 | // Convert to format suitable for PNG output |
| 59 | sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bmp); |
| 60 | SkASSERT(encodedBitmap.get()); |
| 61 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 62 | // write to png |
| 63 | SkDynamicMemoryWStream buffer; |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 64 | SkDrawCommand::WritePNG((const png_bytep) encodedBitmap->writable_data(), |
| 65 | bmp->width(), bmp->height(), |
ethannicholas | f67531f | 2016-03-21 10:19:39 -0700 | [diff] [blame] | 66 | buffer); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 67 | return buffer.copyToData(); |
| 68 | } |
| 69 | |
| 70 | SkCanvas* Request::getCanvas() { |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 71 | #if SK_SUPPORT_GPU |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 72 | GrContextFactory* factory = fContextFactory; |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 73 | GLTestContext* gl = factory->getContextInfo(GrContextFactory::kNativeGL_ContextType, |
ethannicholas | 2ec06c9 | 2016-06-13 10:20:52 -0700 | [diff] [blame] | 74 | GrContextFactory::kNone_ContextOptions).glContext(); |
| 75 | if (!gl) { |
| 76 | gl = factory->getContextInfo(GrContextFactory::kMESA_ContextType, |
| 77 | GrContextFactory::kNone_ContextOptions).glContext(); |
| 78 | } |
| 79 | if (gl) { |
| 80 | gl->makeCurrent(); |
| 81 | } |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 82 | #endif |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 83 | SkASSERT(fDebugCanvas); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 84 | |
| 85 | // create the appropriate surface if necessary |
| 86 | if (!fSurface) { |
| 87 | this->enableGPU(fGPUEnabled); |
| 88 | } |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 89 | SkCanvas* target = fSurface->getCanvas(); |
| 90 | return target; |
| 91 | } |
| 92 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 93 | void Request::drawToCanvas(int n, int m) { |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 94 | SkCanvas* target = this->getCanvas(); |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 95 | fDebugCanvas->drawTo(target, n, m); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 96 | } |
| 97 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 98 | SkData* Request::drawToPng(int n, int m) { |
| 99 | this->drawToCanvas(n, m); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 100 | return writeCanvasToPng(this->getCanvas()); |
| 101 | } |
| 102 | |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 103 | SkData* Request::writeOutSkp() { |
| 104 | // Playback into picture recorder |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 105 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 106 | SkPictureRecorder recorder; |
brianosman | 82996b8 | 2016-04-20 10:52:54 -0700 | [diff] [blame] | 107 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()), |
| 108 | SkIntToScalar(bounds.height())); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 109 | |
| 110 | fDebugCanvas->draw(canvas); |
| 111 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 112 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 113 | |
| 114 | SkDynamicMemoryWStream outStream; |
| 115 | |
| 116 | SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer()); |
| 117 | picture->serialize(&outStream, serializer); |
| 118 | |
| 119 | return outStream.copyToData(); |
| 120 | } |
| 121 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 122 | GrContext* Request::getContext() { |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 123 | #if SK_SUPPORT_GPU |
ethannicholas | 2ec06c9 | 2016-06-13 10:20:52 -0700 | [diff] [blame] | 124 | GrContext* result = fContextFactory->get(GrContextFactory::kNativeGL_ContextType, |
| 125 | GrContextFactory::kNone_ContextOptions); |
| 126 | if (!result) { |
| 127 | result = fContextFactory->get(GrContextFactory::kMESA_ContextType, |
| 128 | GrContextFactory::kNone_ContextOptions); |
| 129 | } |
| 130 | return result; |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 131 | #else |
ethannicholas | 2ec06c9 | 2016-06-13 10:20:52 -0700 | [diff] [blame] | 132 | return nullptr; |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 133 | #endif |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | SkIRect Request::getBounds() { |
| 137 | SkIRect bounds; |
| 138 | if (fPicture) { |
| 139 | bounds = fPicture->cullRect().roundOut(); |
| 140 | if (fGPUEnabled) { |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 141 | #if SK_SUPPORT_GPU |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 142 | int maxRTSize = this->getContext()->caps()->maxRenderTargetSize(); |
| 143 | bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize), |
| 144 | SkTMin(bounds.height(), maxRTSize)); |
joshualitt | 4083610 | 2016-03-11 11:45:53 -0800 | [diff] [blame] | 145 | #endif |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 146 | } |
| 147 | } else { |
| 148 | bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight); |
| 149 | } |
| 150 | |
| 151 | // We clip to kDefaultWidth / kDefaultHeight for performance reasons |
| 152 | // TODO make this configurable |
| 153 | bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth), |
| 154 | SkTMin(bounds.height(), kDefaultHeight)); |
| 155 | return bounds; |
| 156 | } |
| 157 | |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 158 | namespace { |
| 159 | |
| 160 | struct ColorAndProfile { |
| 161 | SkColorType fColorType; |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame^] | 162 | bool fSRGB; |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 163 | bool fGammaCorrect; |
| 164 | }; |
| 165 | |
| 166 | ColorAndProfile ColorModes[] = { |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame^] | 167 | { kN32_SkColorType, false, false }, |
| 168 | { kN32_SkColorType, true, true }, |
| 169 | { kRGBA_F16_SkColorType, false, true }, |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | } |
| 173 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 174 | SkSurface* Request::createCPUSurface() { |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 175 | SkIRect bounds = this->getBounds(); |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 176 | ColorAndProfile cap = ColorModes[fColorMode]; |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame^] | 177 | auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 178 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame^] | 179 | kPremul_SkAlphaType, cap.fSRGB ? srgbColorSpace : nullptr); |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 180 | uint32_t flags = cap.fGammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0; |
| 181 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 182 | return SkSurface::MakeRaster(info, &props).release(); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | SkSurface* Request::createGPUSurface() { |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 186 | GrContext* context = this->getContext(); |
| 187 | SkIRect bounds = this->getBounds(); |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 188 | ColorAndProfile cap = ColorModes[fColorMode]; |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame^] | 189 | auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 190 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame^] | 191 | kPremul_SkAlphaType, cap.fSRGB ? srgbColorSpace : nullptr); |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 192 | uint32_t flags = cap.fGammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0; |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 193 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 194 | SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, |
| 195 | &props).release(); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame] | 196 | return surface; |
| 197 | } |
| 198 | |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 199 | bool Request::setColorMode(int mode) { |
| 200 | fColorMode = mode; |
| 201 | return enableGPU(fGPUEnabled); |
| 202 | } |
| 203 | |
brianosman | 312aa6a | 2016-04-19 12:47:54 -0700 | [diff] [blame] | 204 | bool Request::setSRGBMode(bool enable) { |
| 205 | gTreatSkColorAsSRGB = enable; |
| 206 | return true; |
| 207 | } |
| 208 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 209 | bool Request::enableGPU(bool enable) { |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 210 | if (enable) { |
| 211 | SkSurface* surface = this->createGPUSurface(); |
| 212 | if (surface) { |
| 213 | fSurface.reset(surface); |
| 214 | fGPUEnabled = true; |
joshualitt | 98bd5b1 | 2016-03-11 12:08:15 -0800 | [diff] [blame] | 215 | |
| 216 | // When we switch to GPU, there seems to be some mystery draws in the canvas. So we |
| 217 | // draw once to flush the pipe |
| 218 | // TODO understand what is actually happening here |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 219 | if (fDebugCanvas) { |
| 220 | fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
| 221 | this->getCanvas()->flush(); |
| 222 | } |
joshualitt | 98bd5b1 | 2016-03-11 12:08:15 -0800 | [diff] [blame] | 223 | |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 224 | return true; |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | fSurface.reset(this->createCPUSurface()); |
| 229 | fGPUEnabled = false; |
| 230 | return true; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 231 | } |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 232 | |
| 233 | bool Request::initPictureFromStream(SkStream* stream) { |
| 234 | // parse picture from stream |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 235 | fPicture = SkPicture::MakeFromStream(stream); |
| 236 | if (!fPicture) { |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 237 | fprintf(stderr, "Could not create picture from stream.\n"); |
| 238 | return false; |
| 239 | } |
| 240 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 241 | // reinitialize canvas with the new picture dimensions |
| 242 | this->enableGPU(fGPUEnabled); |
| 243 | |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 244 | // pour picture into debug canvas |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 245 | SkIRect bounds = this->getBounds(); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 246 | fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height())); |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 247 | fDebugCanvas->drawPicture(fPicture); |
joshualitt | 3a9be69 | 2016-02-29 11:38:11 -0800 | [diff] [blame] | 248 | |
| 249 | // for some reason we need to 'flush' the debug canvas by drawing all of the ops |
| 250 | fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 251 | this->getCanvas()->flush(); |
joshualitt | 6bc9679 | 2016-02-29 05:35:04 -0800 | [diff] [blame] | 252 | return true; |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 253 | } |
| 254 | |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 255 | SkData* Request::getJsonOps(int n) { |
| 256 | SkCanvas* canvas = this->getCanvas(); |
| 257 | Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas); |
| 258 | root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu"); |
joshualitt | 5d5207a | 2016-02-29 12:46:04 -0800 | [diff] [blame] | 259 | root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds()); |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 260 | root["colorMode"] = Json::Value(fColorMode); |
brianosman | 312aa6a | 2016-04-19 12:47:54 -0700 | [diff] [blame] | 261 | root["srgbMode"] = Json::Value(gTreatSkColorAsSRGB); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 262 | SkDynamicMemoryWStream stream; |
| 263 | stream.writeText(Json::FastWriter().write(root).c_str()); |
| 264 | |
| 265 | return stream.copyToData(); |
| 266 | } |
| 267 | |
| 268 | SkData* Request::getJsonBatchList(int n) { |
| 269 | SkCanvas* canvas = this->getCanvas(); |
| 270 | SkASSERT(fGPUEnabled); |
| 271 | |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 272 | Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 273 | |
| 274 | SkDynamicMemoryWStream stream; |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 275 | stream.writeText(Json::FastWriter().write(result).c_str()); |
joshualitt | 1e5884b | 2016-02-26 08:22:49 -0800 | [diff] [blame] | 276 | |
| 277 | return stream.copyToData(); |
| 278 | } |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 279 | |
| 280 | SkData* Request::getJsonInfo(int n) { |
| 281 | // drawTo |
| 282 | SkAutoTUnref<SkSurface> surface(this->createCPUSurface()); |
| 283 | SkCanvas* canvas = surface->getCanvas(); |
| 284 | |
| 285 | // TODO this is really slow and we should cache the matrix and clip |
| 286 | fDebugCanvas->drawTo(canvas, n); |
| 287 | |
| 288 | // make some json |
| 289 | SkMatrix vm = fDebugCanvas->getCurrentMatrix(); |
| 290 | SkIRect clip = fDebugCanvas->getCurrentClip(); |
| 291 | Json::Value info(Json::objectValue); |
joshualitt | bd72413 | 2016-03-03 11:39:38 -0800 | [diff] [blame] | 292 | info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm); |
| 293 | info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip); |
joshualitt | ee5348b | 2016-02-26 08:36:25 -0800 | [diff] [blame] | 294 | |
| 295 | std::string json = Json::FastWriter().write(info); |
| 296 | |
| 297 | // We don't want the null terminator so strlen is correct |
| 298 | return SkData::NewWithCopy(json.c_str(), strlen(json.c_str())); |
| 299 | } |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 300 | |
| 301 | SkColor Request::getPixel(int x, int y) { |
| 302 | SkCanvas* canvas = this->getCanvas(); |
| 303 | canvas->flush(); |
| 304 | SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); |
| 305 | SkASSERT(bitmap); |
brianosman | 7831295 | 2016-04-19 10:16:53 -0700 | [diff] [blame] | 306 | |
| 307 | // Convert to format suitable for inspection |
| 308 | sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap); |
| 309 | SkASSERT(encodedBitmap.get()); |
| 310 | |
| 311 | const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) * 4); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 312 | SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); |
joshualitt | e0449cf | 2016-03-09 10:07:02 -0800 | [diff] [blame] | 313 | return result; |
| 314 | } |