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