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-02-25 18:50:28 +00:00
|
|
|
|
2016-03-10 21:29:36 +00:00
|
|
|
static int kDefaultWidth = 1920;
|
|
|
|
static int kDefaultHeight = 1080;
|
|
|
|
|
2016-02-25 18:50:28 +00:00
|
|
|
|
2016-02-26 16:36:25 +00:00
|
|
|
Request::Request(SkString rootUrl)
|
|
|
|
: fUploadContext(nullptr)
|
|
|
|
, fUrlDataManager(rootUrl)
|
|
|
|
, fGPUEnabled(false) {
|
|
|
|
// 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-03-10 21:29:36 +00:00
|
|
|
SkIRect bounds = this->getBounds();
|
2016-03-09 18:07:02 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
|
|
|
|
kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
|
2016-02-25 18:50:28 +00:00
|
|
|
bmp->setInfo(info);
|
|
|
|
if (!canvas->readPixels(bmp, 0, 0)) {
|
|
|
|
fprintf(stderr, "Can't read pixels\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkData* Request::writeCanvasToPng(SkCanvas* canvas) {
|
|
|
|
// capture pixels
|
2016-03-09 18:07:02 +00:00
|
|
|
SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas));
|
2016-02-25 18:50:28 +00:00
|
|
|
SkASSERT(bmp);
|
|
|
|
|
|
|
|
// write to png
|
|
|
|
SkDynamicMemoryWStream buffer;
|
2016-03-21 17:19:39 +00:00
|
|
|
SkDrawCommand::WritePNG((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(),
|
|
|
|
buffer);
|
2016-02-25 18:50:28 +00:00
|
|
|
return buffer.copyToData();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType,
|
|
|
|
GrContextFactory::kNone_GLContextOptions).fGLContext;
|
|
|
|
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-03-02 16:32:37 +00:00
|
|
|
SkData* Request::drawToPng(int n, int m) {
|
|
|
|
this->drawToCanvas(n, m);
|
2016-02-25 18:50:28 +00:00
|
|
|
return writeCanvasToPng(this->getCanvas());
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:07:02 +00:00
|
|
|
SkData* Request::writeOutSkp() {
|
|
|
|
// 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;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(bounds.width(), bounds.height());
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
|
|
|
|
picture->serialize(&outStream, serializer);
|
|
|
|
|
|
|
|
return outStream.copyToData();
|
|
|
|
}
|
|
|
|
|
2016-03-10 21:29:36 +00:00
|
|
|
GrContext* Request::getContext() {
|
2016-03-11 19:45:53 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2016-03-10 21:29:36 +00:00
|
|
|
return fContextFactory->get(GrContextFactory::kNative_GLContextType,
|
|
|
|
GrContextFactory::kNone_GLContextOptions);
|
2016-03-11 19:45:53 +00:00
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We clip to kDefaultWidth / kDefaultHeight for performance reasons
|
|
|
|
// TODO make this configurable
|
|
|
|
bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth),
|
|
|
|
SkTMin(bounds.height(), kDefaultHeight));
|
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:50:28 +00:00
|
|
|
SkSurface* Request::createCPUSurface() {
|
2016-03-10 21:29:36 +00:00
|
|
|
SkIRect bounds = this->getBounds();
|
2016-03-09 18:07:02 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), kN32_SkColorType,
|
2016-02-25 18:50:28 +00:00
|
|
|
kPremul_SkAlphaType);
|
2016-03-24 01:59:25 +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-03-09 18:07:02 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
|
2016-02-25 18:50:28 +00:00
|
|
|
kN32_SkColorType, kPremul_SkAlphaType);
|
|
|
|
uint32_t flags = 0;
|
|
|
|
SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
|
2016-03-24 01:59:25 +00:00
|
|
|
SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
|
|
|
|
&props).release();
|
2016-02-25 18:50:28 +00:00
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2016-02-26 16:36:25 +00:00
|
|
|
bool Request::enableGPU(bool enable) {
|
|
|
|
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
|
|
|
|
fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp());
|
|
|
|
this->getCanvas()->flush();
|
|
|
|
|
2016-02-26 16:36:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fSurface.reset(this->createCPUSurface());
|
|
|
|
fGPUEnabled = false;
|
|
|
|
return true;
|
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-02-26 16:22:49 +00:00
|
|
|
SkData* Request::getJsonOps(int n) {
|
|
|
|
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-02-26 16:22:49 +00:00
|
|
|
SkDynamicMemoryWStream stream;
|
|
|
|
stream.writeText(Json::FastWriter().write(root).c_str());
|
|
|
|
|
|
|
|
return stream.copyToData();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkData* Request::getJsonBatchList(int n) {
|
|
|
|
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
|
|
|
|
|
|
|
return stream.copyToData();
|
|
|
|
}
|
2016-02-26 16:36:25 +00:00
|
|
|
|
|
|
|
SkData* Request::getJsonInfo(int n) {
|
|
|
|
// drawTo
|
|
|
|
SkAutoTUnref<SkSurface> surface(this->createCPUSurface());
|
|
|
|
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
|
|
|
|
return SkData::NewWithCopy(json.c_str(), strlen(json.c_str()));
|
|
|
|
}
|
2016-03-09 18:07:02 +00:00
|
|
|
|
|
|
|
SkColor Request::getPixel(int x, int y) {
|
|
|
|
SkCanvas* canvas = this->getCanvas();
|
|
|
|
canvas->flush();
|
|
|
|
SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas));
|
|
|
|
SkASSERT(bitmap);
|
|
|
|
bitmap->lockPixels();
|
|
|
|
uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x) * 4;
|
|
|
|
SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
|
|
|
|
bitmap->unlockPixels();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|