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