2016-02-25 18:50:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Request.h"
|
|
|
|
|
2019-01-28 18:41:19 +00:00
|
|
|
#include "SkJSONWriter.h"
|
2016-03-09 18:07:02 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2019-03-20 16:12:10 +00:00
|
|
|
#include "ToolUtils.h"
|
2016-02-25 18:50:28 +00:00
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
using namespace sk_gpu_test;
|
|
|
|
|
2016-03-10 21:29:36 +00:00
|
|
|
static int kDefaultWidth = 1920;
|
|
|
|
static int kDefaultHeight = 1080;
|
2016-06-30 14:54:14 +00:00
|
|
|
static int kMaxWidth = 8192;
|
|
|
|
static int kMaxHeight = 8192;
|
2016-03-10 21:29:36 +00:00
|
|
|
|
2016-02-25 18:50:28 +00:00
|
|
|
|
2016-02-26 16:36:25 +00:00
|
|
|
Request::Request(SkString rootUrl)
|
|
|
|
: fUploadContext(nullptr)
|
|
|
|
, fUrlDataManager(rootUrl)
|
2016-04-19 17:16:53 +00:00
|
|
|
, fGPUEnabled(false)
|
2016-10-17 19:20:02 +00:00
|
|
|
, fOverdraw(false)
|
2016-04-19 17:16:53 +00:00
|
|
|
, fColorMode(0) {
|
2016-02-26 16:36:25 +00:00
|
|
|
// create surface
|
|
|
|
GrContextOptions grContextOpts;
|
2016-03-11 19:45:53 +00:00
|
|
|
fContextFactory = new GrContextFactory(grContextOpts);
|
|
|
|
}
|
|
|
|
|
|
|
|
Request::~Request() {
|
|
|
|
if (fContextFactory) {
|
|
|
|
delete fContextFactory;
|
|
|
|
}
|
2016-02-26 16:36:25 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> Request::writeCanvasToPng(SkCanvas* canvas) {
|
2016-02-25 18:50:28 +00:00
|
|
|
// capture pixels
|
2018-08-08 21:03:39 +00:00
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocPixels(canvas->imageInfo());
|
|
|
|
SkAssertResult(canvas->readPixels(bmp, 0, 0));
|
2016-04-19 17:16:53 +00:00
|
|
|
|
2016-06-30 17:06:51 +00:00
|
|
|
// write to an opaque png (black background)
|
2016-02-25 18:50:28 +00:00
|
|
|
SkDynamicMemoryWStream buffer;
|
2019-03-20 16:59:00 +00:00
|
|
|
DrawCommand::WritePNG(bmp, buffer);
|
2016-09-12 19:01:44 +00:00
|
|
|
return buffer.detachAsData();
|
2016-02-25 18:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* Request::getCanvas() {
|
|
|
|
GrContextFactory* factory = fContextFactory;
|
2017-03-20 12:54:16 +00:00
|
|
|
GLTestContext* gl = factory->getContextInfo(GrContextFactory::kGL_ContextType,
|
|
|
|
GrContextFactory::ContextOverrides::kNone).glContext();
|
|
|
|
if (!gl) {
|
|
|
|
gl = factory->getContextInfo(GrContextFactory::kGLES_ContextType,
|
|
|
|
GrContextFactory::ContextOverrides::kNone).glContext();
|
|
|
|
}
|
2016-06-13 17:20:52 +00:00
|
|
|
if (gl) {
|
|
|
|
gl->makeCurrent();
|
|
|
|
}
|
2016-02-25 18:50:28 +00:00
|
|
|
SkASSERT(fDebugCanvas);
|
2016-03-09 18:07:02 +00:00
|
|
|
|
|
|
|
// create the appropriate surface if necessary
|
|
|
|
if (!fSurface) {
|
|
|
|
this->enableGPU(fGPUEnabled);
|
|
|
|
}
|
2016-02-25 18:50:28 +00:00
|
|
|
SkCanvas* target = fSurface->getCanvas();
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> Request::drawToPng(int n, int m) {
|
2016-10-17 19:20:02 +00:00
|
|
|
//fDebugCanvas->setOverdrawViz(true);
|
2018-08-08 21:03:39 +00:00
|
|
|
fDebugCanvas->drawTo(this->getCanvas(), n, m);
|
2016-10-17 19:20:02 +00:00
|
|
|
//fDebugCanvas->setOverdrawViz(false);
|
2016-02-25 18:50:28 +00:00
|
|
|
return writeCanvasToPng(this->getCanvas());
|
|
|
|
}
|
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> Request::writeOutSkp() {
|
2016-03-09 18:07:02 +00:00
|
|
|
// Playback into picture recorder
|
2016-03-10 21:29:36 +00:00
|
|
|
SkIRect bounds = this->getBounds();
|
2016-03-09 18:07:02 +00:00
|
|
|
SkPictureRecorder recorder;
|
2016-04-20 17:52:54 +00:00
|
|
|
SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()),
|
|
|
|
SkIntToScalar(bounds.height()));
|
2016-03-09 18:07:02 +00:00
|
|
|
|
|
|
|
fDebugCanvas->draw(canvas);
|
|
|
|
|
2017-12-16 13:41:28 +00:00
|
|
|
return recorder.finishRecordingAsPicture()->serialize();
|
2016-03-09 18:07:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:29:36 +00:00
|
|
|
GrContext* Request::getContext() {
|
2017-03-20 12:54:16 +00:00
|
|
|
GrContext* result = fContextFactory->get(GrContextFactory::kGL_ContextType,
|
2017-02-21 19:36:05 +00:00
|
|
|
GrContextFactory::ContextOverrides::kNone);
|
2016-06-13 17:20:52 +00:00
|
|
|
if (!result) {
|
2017-03-20 12:54:16 +00:00
|
|
|
result = fContextFactory->get(GrContextFactory::kGLES_ContextType,
|
|
|
|
GrContextFactory::ContextOverrides::kNone);
|
|
|
|
}
|
2016-06-13 17:20:52 +00:00
|
|
|
return result;
|
2016-03-10 21:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkIRect Request::getBounds() {
|
|
|
|
SkIRect bounds;
|
|
|
|
if (fPicture) {
|
|
|
|
bounds = fPicture->cullRect().roundOut();
|
|
|
|
if (fGPUEnabled) {
|
2018-05-11 14:14:21 +00:00
|
|
|
int maxRTSize = this->getContext()->maxRenderTargetSize();
|
2016-03-10 21:29:36 +00:00
|
|
|
bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
|
|
|
|
SkTMin(bounds.height(), maxRTSize));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight);
|
|
|
|
}
|
|
|
|
|
2016-06-30 14:54:14 +00:00
|
|
|
// We clip to kMaxWidth / kMaxHeight for performance reasons.
|
2016-03-10 21:29:36 +00:00
|
|
|
// TODO make this configurable
|
2016-06-30 14:54:14 +00:00
|
|
|
bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kMaxWidth),
|
|
|
|
SkTMin(bounds.height(), kMaxHeight));
|
2016-03-10 21:29:36 +00:00
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
|
2016-04-19 17:16:53 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct ColorAndProfile {
|
|
|
|
SkColorType fColorType;
|
2016-06-16 20:03:24 +00:00
|
|
|
bool fSRGB;
|
2016-04-19 17:16:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ColorAndProfile ColorModes[] = {
|
2016-07-26 18:36:05 +00:00
|
|
|
{ kN32_SkColorType, false },
|
|
|
|
{ kN32_SkColorType, true },
|
|
|
|
{ kRGBA_F16_SkColorType, true },
|
2016-04-19 17:16:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:50:28 +00:00
|
|
|
SkSurface* Request::createCPUSurface() {
|
2016-03-10 21:29:36 +00:00
|
|
|
SkIRect bounds = this->getBounds();
|
2016-04-19 17:16:53 +00:00
|
|
|
ColorAndProfile cap = ColorModes[fColorMode];
|
2016-10-13 17:45:44 +00:00
|
|
|
auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
|
2017-02-07 18:56:11 +00:00
|
|
|
? SkColorSpace::MakeSRGBLinear()
|
|
|
|
: SkColorSpace::MakeSRGB();
|
2016-04-19 17:16:53 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
|
2016-08-30 14:07:59 +00:00
|
|
|
kPremul_SkAlphaType, cap.fSRGB ? colorSpace : nullptr);
|
2016-07-26 18:36:05 +00:00
|
|
|
return SkSurface::MakeRaster(info).release();
|
2016-02-25 18:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkSurface* Request::createGPUSurface() {
|
2016-03-10 21:29:36 +00:00
|
|
|
GrContext* context = this->getContext();
|
|
|
|
SkIRect bounds = this->getBounds();
|
2016-04-19 17:16:53 +00:00
|
|
|
ColorAndProfile cap = ColorModes[fColorMode];
|
2016-10-13 17:45:44 +00:00
|
|
|
auto colorSpace = kRGBA_F16_SkColorType == cap.fColorType
|
2017-02-07 18:56:11 +00:00
|
|
|
? SkColorSpace::MakeSRGBLinear()
|
|
|
|
: SkColorSpace::MakeSRGB();
|
2016-04-19 17:16:53 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fColorType,
|
2016-08-30 14:07:59 +00:00
|
|
|
kPremul_SkAlphaType, cap.fSRGB ? colorSpace: nullptr);
|
2016-07-26 18:36:05 +00:00
|
|
|
SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info).release();
|
2016-02-25 18:50:28 +00:00
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2016-10-17 19:20:02 +00:00
|
|
|
bool Request::setOverdraw(bool enable) {
|
|
|
|
fOverdraw = enable;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-19 17:16:53 +00:00
|
|
|
bool Request::setColorMode(int mode) {
|
|
|
|
fColorMode = mode;
|
|
|
|
return enableGPU(fGPUEnabled);
|
|
|
|
}
|
|
|
|
|
2016-03-29 16:03:52 +00:00
|
|
|
bool Request::enableGPU(bool enable) {
|
2016-02-26 16:36:25 +00:00
|
|
|
if (enable) {
|
|
|
|
SkSurface* surface = this->createGPUSurface();
|
|
|
|
if (surface) {
|
|
|
|
fSurface.reset(surface);
|
|
|
|
fGPUEnabled = true;
|
2016-03-11 20:08:15 +00:00
|
|
|
|
|
|
|
// When we switch to GPU, there seems to be some mystery draws in the canvas. So we
|
|
|
|
// draw once to flush the pipe
|
|
|
|
// TODO understand what is actually happening here
|
2016-04-19 17:16:53 +00:00
|
|
|
if (fDebugCanvas) {
|
|
|
|
fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
|
|
|
|
this->getCanvas()->flush();
|
|
|
|
}
|
2016-03-11 20:08:15 +00:00
|
|
|
|
2016-02-26 16:36:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fSurface.reset(this->createCPUSurface());
|
|
|
|
fGPUEnabled = false;
|
|
|
|
return true;
|
2016-03-29 16:03:52 +00:00
|
|
|
}
|
2016-02-29 13:35:04 +00:00
|
|
|
|
|
|
|
bool Request::initPictureFromStream(SkStream* stream) {
|
|
|
|
// parse picture from stream
|
2016-03-18 14:25:55 +00:00
|
|
|
fPicture = SkPicture::MakeFromStream(stream);
|
|
|
|
if (!fPicture) {
|
2016-02-29 13:35:04 +00:00
|
|
|
fprintf(stderr, "Could not create picture from stream.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-10 21:29:36 +00:00
|
|
|
// reinitialize canvas with the new picture dimensions
|
|
|
|
this->enableGPU(fGPUEnabled);
|
|
|
|
|
2016-02-29 13:35:04 +00:00
|
|
|
// pour picture into debug canvas
|
2016-03-10 21:29:36 +00:00
|
|
|
SkIRect bounds = this->getBounds();
|
2019-03-20 16:59:00 +00:00
|
|
|
fDebugCanvas.reset(new DebugCanvas(bounds.width(), bounds.height()));
|
2016-02-29 13:35:04 +00:00
|
|
|
fDebugCanvas->drawPicture(fPicture);
|
2016-02-29 19:38:11 +00:00
|
|
|
|
|
|
|
// for some reason we need to 'flush' the debug canvas by drawing all of the ops
|
|
|
|
fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
|
2016-03-08 18:43:41 +00:00
|
|
|
this->getCanvas()->flush();
|
2016-02-29 13:35:04 +00:00
|
|
|
return true;
|
2016-02-26 16:36:25 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> Request::getJsonOps(int n) {
|
2016-02-26 16:22:49 +00:00
|
|
|
SkCanvas* canvas = this->getCanvas();
|
|
|
|
SkDynamicMemoryWStream stream;
|
2019-01-28 18:41:19 +00:00
|
|
|
SkJSONWriter writer(&stream, SkJSONWriter::Mode::kFast);
|
|
|
|
writer.beginObject(); // root
|
2016-02-26 16:22:49 +00:00
|
|
|
|
2019-01-28 18:41:19 +00:00
|
|
|
writer.appendString("mode", fGPUEnabled ? "gpu" : "cpu");
|
|
|
|
writer.appendBool("drawGpuOpBounds", fDebugCanvas->getDrawGpuOpBounds());
|
|
|
|
writer.appendS32("colorMode", fColorMode);
|
|
|
|
fDebugCanvas->toJSON(writer, fUrlDataManager, n, canvas);
|
|
|
|
|
|
|
|
writer.endObject(); // root
|
|
|
|
writer.flush();
|
2016-09-12 19:01:44 +00:00
|
|
|
return stream.detachAsData();
|
2016-02-26 16:22:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:48:59 +00:00
|
|
|
sk_sp<SkData> Request::getJsonOpList(int n) {
|
2016-02-26 16:22:49 +00:00
|
|
|
SkCanvas* canvas = this->getCanvas();
|
|
|
|
SkASSERT(fGPUEnabled);
|
|
|
|
SkDynamicMemoryWStream stream;
|
2019-01-28 18:41:19 +00:00
|
|
|
SkJSONWriter writer(&stream, SkJSONWriter::Mode::kFast);
|
|
|
|
|
|
|
|
fDebugCanvas->toJSONOpList(writer, n, canvas);
|
2016-02-26 16:22:49 +00:00
|
|
|
|
2019-01-28 18:41:19 +00:00
|
|
|
writer.flush();
|
2016-09-12 19:01:44 +00:00
|
|
|
return stream.detachAsData();
|
2016-02-26 16:22:49 +00:00
|
|
|
}
|
2016-02-26 16:36:25 +00:00
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> Request::getJsonInfo(int n) {
|
2016-02-26 16:36:25 +00:00
|
|
|
// drawTo
|
2016-11-03 20:26:13 +00:00
|
|
|
sk_sp<SkSurface> surface(this->createCPUSurface());
|
2016-02-26 16:36:25 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
// TODO this is really slow and we should cache the matrix and clip
|
|
|
|
fDebugCanvas->drawTo(canvas, n);
|
|
|
|
|
|
|
|
// make some json
|
2019-01-28 18:41:19 +00:00
|
|
|
SkDynamicMemoryWStream stream;
|
|
|
|
SkJSONWriter writer(&stream, SkJSONWriter::Mode::kFast);
|
|
|
|
|
2016-02-26 16:36:25 +00:00
|
|
|
SkMatrix vm = fDebugCanvas->getCurrentMatrix();
|
|
|
|
SkIRect clip = fDebugCanvas->getCurrentClip();
|
|
|
|
|
2019-01-28 18:41:19 +00:00
|
|
|
writer.beginObject(); // root
|
2019-03-20 16:59:00 +00:00
|
|
|
writer.appendName("ViewMatrix");
|
|
|
|
DrawCommand::MakeJsonMatrix(writer, vm);
|
|
|
|
writer.appendName("ClipRect");
|
|
|
|
DrawCommand::MakeJsonIRect(writer, clip);
|
2019-01-28 18:41:19 +00:00
|
|
|
writer.endObject(); // root
|
2016-02-26 16:36:25 +00:00
|
|
|
|
2019-01-28 18:41:19 +00:00
|
|
|
// TODO: Old code explicitly avoided the null terminator in the returned data. Important?
|
|
|
|
writer.flush();
|
|
|
|
return stream.detachAsData();
|
2016-02-26 16:36:25 +00:00
|
|
|
}
|
2016-03-09 18:07:02 +00:00
|
|
|
|
|
|
|
SkColor Request::getPixel(int x, int y) {
|
2018-08-08 21:03:39 +00:00
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocPixels(this->getCanvas()->imageInfo().makeWH(1, 1));
|
|
|
|
SkAssertResult(this->getCanvas()->readPixels(bmp, x, y));
|
|
|
|
return bmp.getColor(0, 0);
|
2016-03-09 18:07:02 +00:00
|
|
|
}
|