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"
|
|
|
|
|
2016-03-09 18:07:02 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
|
|
|
#include "SkPixelSerializer.h"
|
2016-04-19 19:47:54 +00:00
|
|
|
#include "SkPM4fPriv.h"
|
2016-04-19 17:16:53 +00:00
|
|
|
#include "picture_utils.h"
|
2016-11-23 15:55:18 +00:00
|
|
|
#include "sk_tool_utils.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
|
2016-03-11 19:45:53 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2016-02-26 16:36:25 +00:00
|
|
|
GrContextOptions grContextOpts;
|
2016-03-11 19:45:53 +00:00
|
|
|
fContextFactory = new GrContextFactory(grContextOpts);
|
|
|
|
#else
|
|
|
|
fContextFactory = nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Request::~Request() {
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (fContextFactory) {
|
|
|
|
delete fContextFactory;
|
|
|
|
}
|
|
|
|
#endif
|
2016-02-26 16:36:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 18:50:28 +00:00
|
|
|
SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
|
|
|
|
SkBitmap* bmp = new SkBitmap();
|
2016-04-19 17:16:53 +00:00
|
|
|
bmp->setInfo(canvas->imageInfo());
|
2016-02-25 18:50:28 +00:00
|
|
|
if (!canvas->readPixels(bmp, 0, 0)) {
|
|
|
|
fprintf(stderr, "Can't read pixels\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
|
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
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
|
2016-02-25 18:50:28 +00:00
|
|
|
SkASSERT(bmp);
|
|
|
|
|
2016-04-19 17:16:53 +00:00
|
|
|
// Convert to format suitable for PNG output
|
|
|
|
sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bmp);
|
|
|
|
SkASSERT(encodedBitmap.get());
|
|
|
|
|
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;
|
2016-10-17 20:19:02 +00:00
|
|
|
SkDrawCommand::WritePNG(encodedBitmap->bytes(), bmp->width(), bmp->height(),
|
2016-06-30 17:06:51 +00:00
|
|
|
buffer, true);
|
2016-09-12 19:01:44 +00:00
|
|
|
return buffer.detachAsData();
|
2016-02-25 18:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* Request::getCanvas() {
|
2016-03-11 19:45:53 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2016-02-25 18:50:28 +00:00
|
|
|
GrContextFactory* factory = fContextFactory;
|
2016-04-05 18:06:27 +00:00
|
|
|
GLTestContext* gl = factory->getContextInfo(GrContextFactory::kNativeGL_ContextType,
|
2016-09-13 17:41:49 +00:00
|
|
|
GrContextFactory::ContextOptions::kNone).glContext();
|
2016-06-13 17:20:52 +00:00
|
|
|
if (!gl) {
|
|
|
|
gl = factory->getContextInfo(GrContextFactory::kMESA_ContextType,
|
2016-09-13 17:41:49 +00:00
|
|
|
GrContextFactory::ContextOptions::kNone).glContext();
|
2016-06-13 17:20:52 +00:00
|
|
|
}
|
|
|
|
if (gl) {
|
|
|
|
gl->makeCurrent();
|
|
|
|
}
|
2016-03-11 19:45:53 +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-03-02 16:32:37 +00:00
|
|
|
void Request::drawToCanvas(int n, int m) {
|
2016-02-25 18:50:28 +00:00
|
|
|
SkCanvas* target = this->getCanvas();
|
2016-03-02 16:32:37 +00:00
|
|
|
fDebugCanvas->drawTo(target, n, m);
|
2016-02-25 18:50:28 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2016-03-02 16:32:37 +00:00
|
|
|
this->drawToCanvas(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);
|
|
|
|
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
|
2016-03-09 18:07:02 +00:00
|
|
|
|
|
|
|
SkDynamicMemoryWStream outStream;
|
|
|
|
|
2016-11-23 15:55:18 +00:00
|
|
|
sk_sp<SkPixelSerializer> serializer = sk_tool_utils::MakePixelSerializer();
|
2016-11-03 20:26:13 +00:00
|
|
|
picture->serialize(&outStream, serializer.get());
|
2016-03-09 18:07:02 +00:00
|
|
|
|
2016-09-12 19:01:44 +00:00
|
|
|
return outStream.detachAsData();
|
2016-03-09 18:07:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:29:36 +00:00
|
|
|
GrContext* Request::getContext() {
|
2016-03-11 19:45:53 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2016-06-13 17:20:52 +00:00
|
|
|
GrContext* result = fContextFactory->get(GrContextFactory::kNativeGL_ContextType,
|
2016-09-13 17:41:49 +00:00
|
|
|
GrContextFactory::ContextOptions::kNone);
|
2016-06-13 17:20:52 +00:00
|
|
|
if (!result) {
|
|
|
|
result = fContextFactory->get(GrContextFactory::kMESA_ContextType,
|
2016-09-13 17:41:49 +00:00
|
|
|
GrContextFactory::ContextOptions::kNone);
|
2016-06-30 14:54:14 +00:00
|
|
|
}
|
2016-06-13 17:20:52 +00:00
|
|
|
return result;
|
2016-03-11 19:45:53 +00:00
|
|
|
#else
|
2016-06-13 17:20:52 +00:00
|
|
|
return nullptr;
|
2016-03-11 19:45:53 +00:00
|
|
|
#endif
|
2016-03-10 21:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkIRect Request::getBounds() {
|
|
|
|
SkIRect bounds;
|
|
|
|
if (fPicture) {
|
|
|
|
bounds = fPicture->cullRect().roundOut();
|
|
|
|
if (fGPUEnabled) {
|
2016-03-11 19:45:53 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2016-03-10 21:29:36 +00:00
|
|
|
int maxRTSize = this->getContext()->caps()->maxRenderTargetSize();
|
|
|
|
bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize),
|
|
|
|
SkTMin(bounds.height(), maxRTSize));
|
2016-03-11 19:45:53 +00:00
|
|
|
#endif
|
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
|
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
|
2016-10-24 13:24:02 +00:00
|
|
|
? SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named)
|
|
|
|
: SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
|
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
|
2016-10-24 13:24:02 +00:00
|
|
|
? SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named)
|
|
|
|
: SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
|
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();
|
2016-03-09 18:07:02 +00:00
|
|
|
fDebugCanvas.reset(new SkDebugCanvas(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();
|
|
|
|
Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
|
|
|
|
root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu");
|
2016-02-29 20:46:04 +00:00
|
|
|
root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds());
|
2016-04-19 17:16:53 +00:00
|
|
|
root["colorMode"] = Json::Value(fColorMode);
|
2016-02-26 16:22:49 +00:00
|
|
|
SkDynamicMemoryWStream stream;
|
|
|
|
stream.writeText(Json::FastWriter().write(root).c_str());
|
|
|
|
|
2016-09-12 19:01:44 +00:00
|
|
|
return stream.detachAsData();
|
2016-02-26 16:22:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> Request::getJsonBatchList(int n) {
|
2016-02-26 16:22:49 +00:00
|
|
|
SkCanvas* canvas = this->getCanvas();
|
|
|
|
SkASSERT(fGPUEnabled);
|
|
|
|
|
2016-03-10 21:29:36 +00:00
|
|
|
Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas);
|
2016-02-26 16:22:49 +00:00
|
|
|
|
|
|
|
SkDynamicMemoryWStream stream;
|
2016-03-10 21:29:36 +00:00
|
|
|
stream.writeText(Json::FastWriter().write(result).c_str());
|
2016-02-26 16:22:49 +00:00
|
|
|
|
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
|
|
|
|
SkMatrix vm = fDebugCanvas->getCurrentMatrix();
|
|
|
|
SkIRect clip = fDebugCanvas->getCurrentClip();
|
|
|
|
Json::Value info(Json::objectValue);
|
2016-03-03 19:39:38 +00:00
|
|
|
info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm);
|
|
|
|
info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip);
|
2016-02-26 16:36:25 +00:00
|
|
|
|
|
|
|
std::string json = Json::FastWriter().write(info);
|
|
|
|
|
|
|
|
// We don't want the null terminator so strlen is correct
|
2016-08-02 21:40:46 +00:00
|
|
|
return SkData::MakeWithCopy(json.c_str(), strlen(json.c_str()));
|
2016-02-26 16:36:25 +00:00
|
|
|
}
|
2016-03-09 18:07:02 +00:00
|
|
|
|
|
|
|
SkColor Request::getPixel(int x, int y) {
|
|
|
|
SkCanvas* canvas = this->getCanvas();
|
|
|
|
canvas->flush();
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
|
2016-03-09 18:07:02 +00:00
|
|
|
SkASSERT(bitmap);
|
2016-04-19 17:16:53 +00:00
|
|
|
|
|
|
|
// Convert to format suitable for inspection
|
|
|
|
sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap);
|
2016-09-12 19:01:44 +00:00
|
|
|
SkASSERT(encodedBitmap);
|
2016-04-19 17:16:53 +00:00
|
|
|
|
|
|
|
const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) * 4);
|
2016-03-09 18:07:02 +00:00
|
|
|
SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
|
|
|
|
return result;
|
|
|
|
}
|