blob: 61f79f138908bfee6a0262c49cf3a56e95eb4f77 [file] [log] [blame]
joshualitt4dcbe432016-02-25 10:50:28 -08001/*
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
joshualitte0449cf2016-03-09 10:07:02 -080010#include "SkPictureRecorder.h"
brianosman312aa6a2016-04-19 12:47:54 -070011#include "SkPM4fPriv.h"
brianosman78312952016-04-19 10:16:53 -070012#include "picture_utils.h"
Hal Canarydb683012016-11-23 08:55:18 -070013#include "sk_tool_utils.h"
joshualitt4dcbe432016-02-25 10:50:28 -080014
bsalomon3724e572016-03-30 18:56:19 -070015using namespace sk_gpu_test;
16
joshualittae47aee2016-03-10 13:29:36 -080017static int kDefaultWidth = 1920;
18static int kDefaultHeight = 1080;
jcgregorio9a7acdc2016-06-30 07:54:14 -070019static int kMaxWidth = 8192;
20static int kMaxHeight = 8192;
joshualittae47aee2016-03-10 13:29:36 -080021
joshualitt4dcbe432016-02-25 10:50:28 -080022
joshualittee5348b2016-02-26 08:36:25 -080023Request::Request(SkString rootUrl)
24 : fUploadContext(nullptr)
25 , fUrlDataManager(rootUrl)
brianosman78312952016-04-19 10:16:53 -070026 , fGPUEnabled(false)
Ben Wagnerc03e1c52016-10-17 15:20:02 -040027 , fOverdraw(false)
brianosman78312952016-04-19 10:16:53 -070028 , fColorMode(0) {
joshualittee5348b2016-02-26 08:36:25 -080029 // create surface
30 GrContextOptions grContextOpts;
joshualitt40836102016-03-11 11:45:53 -080031 fContextFactory = new GrContextFactory(grContextOpts);
joshualitt40836102016-03-11 11:45:53 -080032}
33
34Request::~Request() {
joshualitt40836102016-03-11 11:45:53 -080035 if (fContextFactory) {
36 delete fContextFactory;
37 }
joshualittee5348b2016-02-26 08:36:25 -080038}
39
joshualitt4dcbe432016-02-25 10:50:28 -080040SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
41 SkBitmap* bmp = new SkBitmap();
Mike Reed12e946b2017-04-17 10:53:29 -040042 if (!bmp->tryAllocPixels(canvas->imageInfo()) || !canvas->readPixels(*bmp, 0, 0)) {
joshualitt4dcbe432016-02-25 10:50:28 -080043 fprintf(stderr, "Can't read pixels\n");
Mike Reed12e946b2017-04-17 10:53:29 -040044 delete bmp;
joshualitt4dcbe432016-02-25 10:50:28 -080045 return nullptr;
46 }
47 return bmp;
48}
49
bungeman38d909e2016-08-02 14:40:46 -070050sk_sp<SkData> Request::writeCanvasToPng(SkCanvas* canvas) {
joshualitt4dcbe432016-02-25 10:50:28 -080051 // capture pixels
Ben Wagner145dbcd2016-11-03 14:40:50 -040052 std::unique_ptr<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
joshualitt4dcbe432016-02-25 10:50:28 -080053 SkASSERT(bmp);
54
brianosman78312952016-04-19 10:16:53 -070055 // Convert to format suitable for PNG output
56 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bmp);
57 SkASSERT(encodedBitmap.get());
58
msaretta5cf4f42016-06-30 10:06:51 -070059 // write to an opaque png (black background)
joshualitt4dcbe432016-02-25 10:50:28 -080060 SkDynamicMemoryWStream buffer;
halcanarya73d76a2016-10-17 13:19:02 -070061 SkDrawCommand::WritePNG(encodedBitmap->bytes(), bmp->width(), bmp->height(),
msaretta5cf4f42016-06-30 10:06:51 -070062 buffer, true);
reed42943c82016-09-12 12:01:44 -070063 return buffer.detachAsData();
joshualitt4dcbe432016-02-25 10:50:28 -080064}
65
66SkCanvas* Request::getCanvas() {
67 GrContextFactory* factory = fContextFactory;
Brian Salomon6405e712017-03-20 08:54:16 -040068 GLTestContext* gl = factory->getContextInfo(GrContextFactory::kGL_ContextType,
69 GrContextFactory::ContextOverrides::kNone).glContext();
70 if (!gl) {
71 gl = factory->getContextInfo(GrContextFactory::kGLES_ContextType,
72 GrContextFactory::ContextOverrides::kNone).glContext();
73 }
ethannicholas2ec06c92016-06-13 10:20:52 -070074 if (gl) {
75 gl->makeCurrent();
76 }
joshualitt4dcbe432016-02-25 10:50:28 -080077 SkASSERT(fDebugCanvas);
joshualitte0449cf2016-03-09 10:07:02 -080078
79 // create the appropriate surface if necessary
80 if (!fSurface) {
81 this->enableGPU(fGPUEnabled);
82 }
joshualitt4dcbe432016-02-25 10:50:28 -080083 SkCanvas* target = fSurface->getCanvas();
84 return target;
85}
86
joshualitt46b301d2016-03-02 08:32:37 -080087void Request::drawToCanvas(int n, int m) {
joshualitt4dcbe432016-02-25 10:50:28 -080088 SkCanvas* target = this->getCanvas();
joshualitt46b301d2016-03-02 08:32:37 -080089 fDebugCanvas->drawTo(target, n, m);
joshualitt4dcbe432016-02-25 10:50:28 -080090}
91
bungeman38d909e2016-08-02 14:40:46 -070092sk_sp<SkData> Request::drawToPng(int n, int m) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040093 //fDebugCanvas->setOverdrawViz(true);
joshualitt46b301d2016-03-02 08:32:37 -080094 this->drawToCanvas(n, m);
Ben Wagnerc03e1c52016-10-17 15:20:02 -040095 //fDebugCanvas->setOverdrawViz(false);
joshualitt4dcbe432016-02-25 10:50:28 -080096 return writeCanvasToPng(this->getCanvas());
97}
98
bungeman38d909e2016-08-02 14:40:46 -070099sk_sp<SkData> Request::writeOutSkp() {
joshualitte0449cf2016-03-09 10:07:02 -0800100 // Playback into picture recorder
joshualittae47aee2016-03-10 13:29:36 -0800101 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800102 SkPictureRecorder recorder;
brianosman82996b82016-04-20 10:52:54 -0700103 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()),
104 SkIntToScalar(bounds.height()));
joshualitte0449cf2016-03-09 10:07:02 -0800105
106 fDebugCanvas->draw(canvas);
107
Mike Reedef038482017-12-16 08:41:28 -0500108 return recorder.finishRecordingAsPicture()->serialize();
joshualitte0449cf2016-03-09 10:07:02 -0800109}
110
joshualittae47aee2016-03-10 13:29:36 -0800111GrContext* Request::getContext() {
Brian Salomon6405e712017-03-20 08:54:16 -0400112 GrContext* result = fContextFactory->get(GrContextFactory::kGL_ContextType,
csmartdaltone812d492017-02-21 12:36:05 -0700113 GrContextFactory::ContextOverrides::kNone);
ethannicholas2ec06c92016-06-13 10:20:52 -0700114 if (!result) {
Brian Salomon6405e712017-03-20 08:54:16 -0400115 result = fContextFactory->get(GrContextFactory::kGLES_ContextType,
116 GrContextFactory::ContextOverrides::kNone);
117 }
ethannicholas2ec06c92016-06-13 10:20:52 -0700118 return result;
joshualittae47aee2016-03-10 13:29:36 -0800119}
120
121SkIRect Request::getBounds() {
122 SkIRect bounds;
123 if (fPicture) {
124 bounds = fPicture->cullRect().roundOut();
125 if (fGPUEnabled) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400126 int maxRTSize = this->getContext()->maxRenderTargetSize();
joshualittae47aee2016-03-10 13:29:36 -0800127 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
128 SkTMin(bounds.height(), maxRTSize));
129 }
130 } else {
131 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
132 }
133
jcgregorio9a7acdc2016-06-30 07:54:14 -0700134 // We clip to kMaxWidth / kMaxHeight for performance reasons.
joshualittae47aee2016-03-10 13:29:36 -0800135 // TODO make this configurable
jcgregorio9a7acdc2016-06-30 07:54:14 -0700136 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kMaxWidth),
137 SkTMin(bounds.height(), kMaxHeight));
joshualittae47aee2016-03-10 13:29:36 -0800138 return bounds;
139}
140
brianosman78312952016-04-19 10:16:53 -0700141namespace {
142
143struct ColorAndProfile {
144 SkColorType fColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700145 bool fSRGB;
brianosman78312952016-04-19 10:16:53 -0700146};
147
148ColorAndProfile ColorModes[] = {
brianosman3a0dbde2016-07-26 11:36:05 -0700149 { kN32_SkColorType, false },
150 { kN32_SkColorType, true },
151 { kRGBA_F16_SkColorType, true },
brianosman78312952016-04-19 10:16:53 -0700152};
153
154}
155
joshualitt4dcbe432016-02-25 10:50:28 -0800156SkSurface* Request::createCPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800157 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700158 ColorAndProfile cap = ColorModes[fColorMode];
raftias7c602de2016-10-13 10:45:44 -0700159 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500160 ? SkColorSpace::MakeSRGBLinear()
161 : SkColorSpace::MakeSRGB();
brianosman78312952016-04-19 10:16:53 -0700162 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700163 kPremul_SkAlphaType, cap.fSRGB ? colorSpace : nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700164 return SkSurface::MakeRaster(info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800165}
166
167SkSurface* Request::createGPUSurface() {
joshualittae47aee2016-03-10 13:29:36 -0800168 GrContext* context = this->getContext();
169 SkIRect bounds = this->getBounds();
brianosman78312952016-04-19 10:16:53 -0700170 ColorAndProfile cap = ColorModes[fColorMode];
raftias7c602de2016-10-13 10:45:44 -0700171 auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500172 ? SkColorSpace::MakeSRGBLinear()
173 : SkColorSpace::MakeSRGB();
brianosman78312952016-04-19 10:16:53 -0700174 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
brianosman0e22eb82016-08-30 07:07:59 -0700175 kPremul_SkAlphaType, cap.fSRGB ? colorSpace: nullptr);
brianosman3a0dbde2016-07-26 11:36:05 -0700176 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info).release();
joshualitt4dcbe432016-02-25 10:50:28 -0800177 return surface;
178}
179
Ben Wagnerc03e1c52016-10-17 15:20:02 -0400180bool Request::setOverdraw(bool enable) {
181 fOverdraw = enable;
182 return true;
183}
184
brianosman78312952016-04-19 10:16:53 -0700185bool Request::setColorMode(int mode) {
186 fColorMode = mode;
187 return enableGPU(fGPUEnabled);
188}
189
halcanary9d524f22016-03-29 09:03:52 -0700190bool Request::enableGPU(bool enable) {
joshualittee5348b2016-02-26 08:36:25 -0800191 if (enable) {
192 SkSurface* surface = this->createGPUSurface();
193 if (surface) {
194 fSurface.reset(surface);
195 fGPUEnabled = true;
joshualitt98bd5b12016-03-11 12:08:15 -0800196
197 // When we switch to GPU, there seems to be some mystery draws in the canvas. So we
198 // draw once to flush the pipe
199 // TODO understand what is actually happening here
brianosman78312952016-04-19 10:16:53 -0700200 if (fDebugCanvas) {
201 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
202 this->getCanvas()->flush();
203 }
joshualitt98bd5b12016-03-11 12:08:15 -0800204
joshualittee5348b2016-02-26 08:36:25 -0800205 return true;
206 }
207 return false;
208 }
209 fSurface.reset(this->createCPUSurface());
210 fGPUEnabled = false;
211 return true;
halcanary9d524f22016-03-29 09:03:52 -0700212}
joshualitt6bc96792016-02-29 05:35:04 -0800213
214bool Request::initPictureFromStream(SkStream* stream) {
215 // parse picture from stream
reedca2622b2016-03-18 07:25:55 -0700216 fPicture = SkPicture::MakeFromStream(stream);
217 if (!fPicture) {
joshualitt6bc96792016-02-29 05:35:04 -0800218 fprintf(stderr, "Could not create picture from stream.\n");
219 return false;
220 }
221
joshualittae47aee2016-03-10 13:29:36 -0800222 // reinitialize canvas with the new picture dimensions
223 this->enableGPU(fGPUEnabled);
224
joshualitt6bc96792016-02-29 05:35:04 -0800225 // pour picture into debug canvas
joshualittae47aee2016-03-10 13:29:36 -0800226 SkIRect bounds = this->getBounds();
joshualitte0449cf2016-03-09 10:07:02 -0800227 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height()));
joshualitt6bc96792016-02-29 05:35:04 -0800228 fDebugCanvas->drawPicture(fPicture);
joshualitt3a9be692016-02-29 11:38:11 -0800229
230 // for some reason we need to 'flush' the debug canvas by drawing all of the ops
231 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
joshualittb0666ad2016-03-08 10:43:41 -0800232 this->getCanvas()->flush();
joshualitt6bc96792016-02-29 05:35:04 -0800233 return true;
joshualittee5348b2016-02-26 08:36:25 -0800234}
235
bungeman38d909e2016-08-02 14:40:46 -0700236sk_sp<SkData> Request::getJsonOps(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800237 SkCanvas* canvas = this->getCanvas();
238 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
239 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
Brian Salomonf09492b2016-12-21 15:40:26 -0500240 root["drawGpuOpBounds"] = Json::Value(fDebugCanvas->getDrawGpuOpBounds());
brianosman78312952016-04-19 10:16:53 -0700241 root["colorMode"] = Json::Value(fColorMode);
joshualitt1e5884b2016-02-26 08:22:49 -0800242 SkDynamicMemoryWStream stream;
243 stream.writeText(Json::FastWriter().write(root).c_str());
244
reed42943c82016-09-12 12:01:44 -0700245 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800246}
247
Brian Salomon144a5c52016-12-20 16:48:59 -0500248sk_sp<SkData> Request::getJsonOpList(int n) {
joshualitt1e5884b2016-02-26 08:22:49 -0800249 SkCanvas* canvas = this->getCanvas();
250 SkASSERT(fGPUEnabled);
251
Brian Salomon144a5c52016-12-20 16:48:59 -0500252 Json::Value result = fDebugCanvas->toJSONOpList(n, canvas);
joshualitt1e5884b2016-02-26 08:22:49 -0800253
254 SkDynamicMemoryWStream stream;
joshualittae47aee2016-03-10 13:29:36 -0800255 stream.writeText(Json::FastWriter().write(result).c_str());
joshualitt1e5884b2016-02-26 08:22:49 -0800256
reed42943c82016-09-12 12:01:44 -0700257 return stream.detachAsData();
joshualitt1e5884b2016-02-26 08:22:49 -0800258}
joshualittee5348b2016-02-26 08:36:25 -0800259
bungeman38d909e2016-08-02 14:40:46 -0700260sk_sp<SkData> Request::getJsonInfo(int n) {
joshualittee5348b2016-02-26 08:36:25 -0800261 // drawTo
Hal Canary1b612a82016-11-03 16:26:13 -0400262 sk_sp<SkSurface> surface(this->createCPUSurface());
joshualittee5348b2016-02-26 08:36:25 -0800263 SkCanvas* canvas = surface->getCanvas();
264
265 // TODO this is really slow and we should cache the matrix and clip
266 fDebugCanvas->drawTo(canvas, n);
267
268 // make some json
269 SkMatrix vm = fDebugCanvas->getCurrentMatrix();
270 SkIRect clip = fDebugCanvas->getCurrentClip();
271 Json::Value info(Json::objectValue);
joshualittbd724132016-03-03 11:39:38 -0800272 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
273 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
joshualittee5348b2016-02-26 08:36:25 -0800274
275 std::string json = Json::FastWriter().write(info);
276
277 // We don't want the null terminator so strlen is correct
bungeman38d909e2016-08-02 14:40:46 -0700278 return SkData::MakeWithCopy(json.c_str(), strlen(json.c_str()));
joshualittee5348b2016-02-26 08:36:25 -0800279}
joshualitte0449cf2016-03-09 10:07:02 -0800280
281SkColor Request::getPixel(int x, int y) {
282 SkCanvas* canvas = this->getCanvas();
283 canvas->flush();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400284 std::unique_ptr<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
joshualitte0449cf2016-03-09 10:07:02 -0800285 SkASSERT(bitmap);
brianosman78312952016-04-19 10:16:53 -0700286
287 // Convert to format suitable for inspection
288 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap);
reed42943c82016-09-12 12:01:44 -0700289 SkASSERT(encodedBitmap);
brianosman78312952016-04-19 10:16:53 -0700290
291 const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) * 4);
joshualitte0449cf2016-03-09 10:07:02 -0800292 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
joshualitte0449cf2016-03-09 10:07:02 -0800293 return result;
294}