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