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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/skiaserve/Request.h"
|
2016-02-25 18:50:28 +00:00
|
|
|
|
2020-08-03 17:21:46 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPictureRecorder.h"
|
2020-07-01 20:59:17 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/utils/SkJSONWriter.h"
|
|
|
|
#include "tools/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() {
|
2020-03-19 19:54:28 +00:00
|
|
|
#ifdef SK_GL
|
2016-02-25 18:50:28 +00:00
|
|
|
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();
|
|
|
|
}
|
2020-03-19 19:54:28 +00:00
|
|
|
#endif
|
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);
|
2019-12-12 16:05:10 +00:00
|
|
|
auto* canvas = this->getCanvas();
|
|
|
|
canvas->clear(SK_ColorTRANSPARENT);
|
|
|
|
fDebugCanvas->drawTo(canvas, 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
|
|
|
}
|
|
|
|
|
2020-07-01 20:59:17 +00:00
|
|
|
GrDirectContext* Request::directContext() {
|
|
|
|
auto result = fContextFactory->get(GrContextFactory::kGL_ContextType,
|
|
|
|
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) {
|
2020-07-01 20:59:17 +00:00
|
|
|
int maxRTSize = this->directContext()->maxRenderTargetSize();
|
2020-02-07 15:36:46 +00:00
|
|
|
bounds = SkIRect::MakeWH(std::min(bounds.width(), maxRTSize),
|
|
|
|
std::min(bounds.height(), maxRTSize));
|
2016-03-10 21:29:36 +00:00
|
|
|
}
|
|
|
|
} 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
|
2020-02-07 15:36:46 +00:00
|
|
|
bounds = SkIRect::MakeWH(std::min(bounds.width(), kMaxWidth),
|
|
|
|
std::min(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
|
|
|
};
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|
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();
|
2019-09-30 19:12:27 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(bounds.size(), cap.fColorType, 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() {
|
2020-07-01 20:59:17 +00:00
|
|
|
auto context = this->directContext();
|
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();
|
2019-09-30 19:12:27 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(bounds.size(), cap.fColorType, 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());
|
2020-11-20 18:49:37 +00:00
|
|
|
fSurface->flush();
|
2016-04-19 17:16:53 +00:00
|
|
|
}
|
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();
|
2020-08-03 17:21:46 +00:00
|
|
|
fDebugCanvas = std::make_unique<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());
|
2020-11-20 18:49:37 +00:00
|
|
|
fSurface->flush();
|
2016-02-29 13:35:04 +00:00
|
|
|
return true;
|
2016-02-26 16:36:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 18:51:14 +00:00
|
|
|
sk_sp<SkData> Request::getJsonOps() {
|
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);
|
2019-12-13 18:51:14 +00:00
|
|
|
fDebugCanvas->toJSON(writer, fUrlDataManager, canvas);
|
2019-01-28 18:41:19 +00:00
|
|
|
|
|
|
|
writer.endObject(); // root
|
|
|
|
writer.flush();
|
2016-09-12 19:01:44 +00:00
|
|
|
return stream.detachAsData();
|
2016-02-26 16:22:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 18:51:14 +00:00
|
|
|
sk_sp<SkData> Request::getJsonOpsTask() {
|
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);
|
|
|
|
|
2019-12-13 18:51:14 +00:00
|
|
|
fDebugCanvas->toJSONOpsTask(writer, 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);
|
|
|
|
|
2020-12-03 16:21:31 +00:00
|
|
|
SkM44 vm = fDebugCanvas->getCurrentMatrix();
|
2016-02-26 16:36:25 +00:00
|
|
|
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");
|
2020-12-03 16:21:31 +00:00
|
|
|
DrawCommand::MakeJsonMatrix44(writer, vm);
|
2019-03-20 16:59:00 +00:00
|
|
|
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
|
|
|
}
|