2018-01-09 17:34:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-01-09 17:34:11 +00:00
|
|
|
#include <chrono>
|
2018-01-09 17:34:11 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <iostream>
|
2018-03-21 20:23:49 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2018-01-09 17:34:11 +00:00
|
|
|
#include <sys/types.h>
|
2018-02-07 22:47:59 +00:00
|
|
|
#include <sys/uio.h>
|
2018-01-09 17:34:11 +00:00
|
|
|
#include <sys/wait.h>
|
2018-03-21 20:23:49 +00:00
|
|
|
#include <thread>
|
2018-01-09 17:34:11 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "src/core/SkRemoteGlyphCache.h"
|
|
|
|
#include "src/core/SkScalerContext.h"
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-03-21 20:23:49 +00:00
|
|
|
static std::string gSkpName;
|
2018-02-02 17:54:55 +00:00
|
|
|
static bool gUseGpu = true;
|
|
|
|
static bool gPurgeFontCaches = true;
|
|
|
|
static bool gUseProcess = true;
|
|
|
|
|
2018-05-02 17:29:37 +00:00
|
|
|
class ServerDiscardableManager : public SkStrikeServer::DiscardableHandleManager {
|
|
|
|
public:
|
|
|
|
ServerDiscardableManager() = default;
|
|
|
|
~ServerDiscardableManager() override = default;
|
|
|
|
|
|
|
|
SkDiscardableHandleId createHandle() override { return ++nextHandleId; }
|
|
|
|
bool lockHandle(SkDiscardableHandleId handleId) override {
|
|
|
|
return handleId > lastPurgedHandleId;
|
|
|
|
}
|
|
|
|
void purgeAll() { lastPurgedHandleId = nextHandleId; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkDiscardableHandleId nextHandleId = 0u;
|
|
|
|
SkDiscardableHandleId lastPurgedHandleId = 0u;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ClientDiscardableManager : public SkStrikeClient::DiscardableHandleManager {
|
|
|
|
public:
|
|
|
|
class ScopedPurgeCache {
|
|
|
|
public:
|
|
|
|
ScopedPurgeCache(ClientDiscardableManager* manager) : fManager(manager) {
|
|
|
|
if (fManager) fManager->allowPurging = true;
|
|
|
|
}
|
|
|
|
~ScopedPurgeCache() {
|
|
|
|
if (fManager) fManager->allowPurging = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ClientDiscardableManager* fManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientDiscardableManager() = default;
|
|
|
|
~ClientDiscardableManager() override = default;
|
|
|
|
|
|
|
|
bool deleteHandle(SkDiscardableHandleId) override { return allowPurging; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool allowPurging = false;
|
|
|
|
};
|
|
|
|
|
2018-04-05 20:57:25 +00:00
|
|
|
static bool write_SkData(int fd, const SkData& data) {
|
|
|
|
size_t size = data.size();
|
|
|
|
ssize_t bytesWritten = ::write(fd, &size, sizeof(size));
|
|
|
|
if (bytesWritten < 0) {
|
|
|
|
err(1,"Failed write %zu", size);
|
|
|
|
return false;
|
2018-02-07 22:47:59 +00:00
|
|
|
}
|
2018-04-05 20:57:25 +00:00
|
|
|
|
|
|
|
bytesWritten = ::write(fd, data.data(), data.size());
|
|
|
|
if (bytesWritten < 0) {
|
|
|
|
err(1,"Failed write %zu", size);
|
|
|
|
return false;
|
2018-02-07 22:47:59 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 20:57:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkData> read_SkData(int fd) {
|
|
|
|
size_t size;
|
|
|
|
ssize_t readSize = ::read(fd, &size, sizeof(size));
|
|
|
|
if (readSize <= 0) {
|
2018-04-08 03:56:42 +00:00
|
|
|
if (readSize < 0) {
|
2018-04-05 20:57:25 +00:00
|
|
|
err(1, "Failed read %zu", size);
|
2018-04-05 20:57:25 +00:00
|
|
|
}
|
2018-04-05 20:57:25 +00:00
|
|
|
return nullptr;
|
2018-04-05 20:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 20:57:25 +00:00
|
|
|
auto out = SkData::MakeUninitialized(size);
|
|
|
|
auto data = (uint8_t*)out->data();
|
|
|
|
|
|
|
|
size_t totalRead = 0;
|
|
|
|
while (totalRead < size) {
|
|
|
|
ssize_t sizeRead;
|
|
|
|
sizeRead = ::read(fd, &data[totalRead], size - totalRead);
|
|
|
|
if (sizeRead <= 0) {
|
|
|
|
if (readSize < 0) {
|
|
|
|
err(1, "Failed read %zu", size);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
totalRead += sizeRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-03-27 18:40:53 +00:00
|
|
|
class Timer {
|
|
|
|
public:
|
|
|
|
void start() {
|
|
|
|
fStart = std::chrono::high_resolution_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop() {
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
|
|
fElapsedSeconds += end - fStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
double elapsedSeconds() {
|
|
|
|
return fElapsedSeconds.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
decltype(std::chrono::high_resolution_clock::now()) fStart;
|
|
|
|
std::chrono::duration<double> fElapsedSeconds{0.0};
|
|
|
|
};
|
|
|
|
|
2019-02-19 20:46:38 +00:00
|
|
|
static bool push_font_data(const SkPicture& pic, SkStrikeServer* strikeServer,
|
|
|
|
sk_sp<SkColorSpace> colorSpace, int writeFd) {
|
2018-05-02 17:29:37 +00:00
|
|
|
const SkIRect bounds = pic.cullRect().round();
|
|
|
|
const SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
|
2019-02-19 20:46:38 +00:00
|
|
|
SkTextBlobCacheDiffCanvas filter(bounds.width(), bounds.height(), props,
|
2019-08-16 02:45:43 +00:00
|
|
|
strikeServer, std::move(colorSpace), true);
|
2018-05-02 13:54:11 +00:00
|
|
|
pic.playback(&filter);
|
2018-05-02 17:29:37 +00:00
|
|
|
|
|
|
|
std::vector<uint8_t> fontData;
|
|
|
|
strikeServer->writeStrikeData(&fontData);
|
|
|
|
auto data = SkData::MakeWithoutCopy(fontData.data(), fontData.size());
|
|
|
|
return write_SkData(writeFd, *data);
|
2018-02-07 22:47:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 17:29:37 +00:00
|
|
|
static void final_draw(std::string outFilename, SkData* picData, SkStrikeClient* client,
|
|
|
|
ClientDiscardableManager* discardableManager, int readFd, int writeFd) {
|
|
|
|
SkDeserialProcs procs;
|
|
|
|
auto decode = [](const void* data, size_t length, void* ctx) -> sk_sp<SkTypeface> {
|
|
|
|
return reinterpret_cast<SkStrikeClient*>(ctx)->deserializeTypeface(data, length);
|
|
|
|
};
|
|
|
|
procs.fTypefaceProc = decode;
|
|
|
|
procs.fTypefaceCtx = client;
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-05-02 17:29:37 +00:00
|
|
|
auto pic = SkPicture::MakeFromData(picData, &procs);
|
2018-01-09 17:34:11 +00:00
|
|
|
|
|
|
|
auto cullRect = pic->cullRect();
|
|
|
|
auto r = cullRect.round();
|
|
|
|
|
2018-01-09 17:34:11 +00:00
|
|
|
auto s = SkSurface::MakeRasterN32Premul(r.width(), r.height());
|
2018-01-09 17:34:11 +00:00
|
|
|
auto c = s->getCanvas();
|
2018-05-02 17:29:37 +00:00
|
|
|
auto picUnderTest = SkPicture::MakeFromData(picData, &procs);
|
2018-02-07 22:47:59 +00:00
|
|
|
|
2018-03-27 18:40:53 +00:00
|
|
|
Timer drawTime;
|
2018-05-02 17:29:37 +00:00
|
|
|
auto randomData = SkData::MakeUninitialized(1u);
|
2018-03-27 18:40:53 +00:00
|
|
|
for (int i = 0; i < 100; i++) {
|
2018-02-02 17:54:55 +00:00
|
|
|
if (gPurgeFontCaches) {
|
2018-05-02 17:29:37 +00:00
|
|
|
ClientDiscardableManager::ScopedPurgeCache purge(discardableManager);
|
2018-02-02 17:54:55 +00:00
|
|
|
SkGraphics::PurgeFontCache();
|
2018-05-02 17:29:37 +00:00
|
|
|
SkASSERT(SkGraphics::GetFontCacheUsed() == 0u);
|
2018-02-02 17:54:55 +00:00
|
|
|
}
|
2018-05-02 17:29:37 +00:00
|
|
|
|
2018-03-27 18:40:53 +00:00
|
|
|
drawTime.start();
|
2018-03-21 20:23:49 +00:00
|
|
|
if (client != nullptr) {
|
2018-05-02 17:29:37 +00:00
|
|
|
// Kick the renderer to send us the fonts.
|
|
|
|
write_SkData(writeFd, *randomData);
|
|
|
|
auto fontData = read_SkData(readFd);
|
|
|
|
if (fontData && !fontData->isEmpty()) {
|
|
|
|
if (!client->readStrikeData(fontData->data(), fontData->size()))
|
|
|
|
SK_ABORT("Bad serialization");
|
|
|
|
}
|
2018-02-07 22:47:59 +00:00
|
|
|
}
|
2018-01-09 17:34:11 +00:00
|
|
|
c->drawPicture(picUnderTest);
|
2018-03-27 18:40:53 +00:00
|
|
|
drawTime.stop();
|
2018-02-02 17:54:55 +00:00
|
|
|
}
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-02-02 17:54:55 +00:00
|
|
|
std::cout << "useProcess: " << gUseProcess
|
|
|
|
<< " useGPU: " << gUseGpu
|
|
|
|
<< " purgeCache: " << gPurgeFontCaches << std::endl;
|
2018-02-07 22:47:59 +00:00
|
|
|
fprintf(stderr, "%s use GPU %s elapsed time %8.6f s\n", gSkpName.c_str(),
|
2018-03-27 18:40:53 +00:00
|
|
|
gUseGpu ? "true" : "false", drawTime.elapsedSeconds());
|
2018-01-09 17:34:11 +00:00
|
|
|
|
|
|
|
auto i = s->makeImageSnapshot();
|
|
|
|
auto data = i->encodeToData();
|
|
|
|
SkFILEWStream f(outFilename.c_str());
|
|
|
|
f.write(data->data(), data->size());
|
|
|
|
}
|
|
|
|
|
2018-01-09 17:34:11 +00:00
|
|
|
static void gpu(int readFd, int writeFd) {
|
|
|
|
|
2018-03-27 18:40:53 +00:00
|
|
|
if (gUseGpu) {
|
2018-04-05 20:57:25 +00:00
|
|
|
auto picData = read_SkData(readFd);
|
2018-03-27 18:40:53 +00:00
|
|
|
if (picData == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-05-02 17:29:37 +00:00
|
|
|
sk_sp<ClientDiscardableManager> discardableManager = sk_make_sp<ClientDiscardableManager>();
|
|
|
|
SkStrikeClient strikeClient(discardableManager);
|
2018-03-21 20:23:49 +00:00
|
|
|
|
2018-05-02 17:29:37 +00:00
|
|
|
final_draw("test.png", picData.get(), &strikeClient, discardableManager.get(), readFd,
|
|
|
|
writeFd);
|
2018-03-27 18:40:53 +00:00
|
|
|
}
|
2018-03-15 21:34:40 +00:00
|
|
|
|
2018-04-05 20:57:25 +00:00
|
|
|
::close(writeFd);
|
|
|
|
::close(readFd);
|
|
|
|
|
2018-03-21 20:23:49 +00:00
|
|
|
printf("GPU is exiting\n");
|
2018-01-09 17:34:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 21:47:54 +00:00
|
|
|
static int renderer(
|
|
|
|
const std::string& skpName, int readFd, int writeFd)
|
|
|
|
{
|
2018-05-02 17:29:37 +00:00
|
|
|
ServerDiscardableManager discardableManager;
|
|
|
|
SkStrikeServer server(&discardableManager);
|
2018-04-05 20:57:25 +00:00
|
|
|
auto closeAll = [readFd, writeFd]() {
|
|
|
|
::close(writeFd);
|
|
|
|
::close(readFd);
|
|
|
|
};
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-02-07 22:47:59 +00:00
|
|
|
auto skpData = SkData::MakeFromFileName(skpName.c_str());
|
|
|
|
std::cout << "skp stream is " << skpData->size() << " bytes long " << std::endl;
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-02-02 17:54:55 +00:00
|
|
|
sk_sp<SkData> stream;
|
|
|
|
if (gUseGpu) {
|
2018-02-07 22:47:59 +00:00
|
|
|
auto pic = SkPicture::MakeFromData(skpData.get());
|
2019-02-19 20:46:38 +00:00
|
|
|
auto colorSpace = SkColorSpace::MakeSRGB();
|
2018-05-02 17:29:37 +00:00
|
|
|
SkSerialProcs procs;
|
|
|
|
auto encode = [](SkTypeface* tf, void* ctx) -> sk_sp<SkData> {
|
|
|
|
return reinterpret_cast<SkStrikeServer*>(ctx)->serializeTypeface(tf);
|
|
|
|
};
|
|
|
|
procs.fTypefaceProc = encode;
|
|
|
|
procs.fTypefaceCtx = &server;
|
|
|
|
|
2018-02-02 17:54:55 +00:00
|
|
|
stream = pic->serialize(&procs);
|
2018-02-07 22:47:59 +00:00
|
|
|
|
2018-04-05 20:57:25 +00:00
|
|
|
if (!write_SkData(writeFd, *stream)) {
|
|
|
|
closeAll();
|
2018-03-21 20:23:49 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-02-07 22:47:59 +00:00
|
|
|
|
2018-04-05 20:57:25 +00:00
|
|
|
while (true) {
|
|
|
|
auto inBuffer = read_SkData(readFd);
|
|
|
|
if (inBuffer == nullptr) {
|
|
|
|
closeAll();
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-02 17:29:37 +00:00
|
|
|
if (gPurgeFontCaches) discardableManager.purgeAll();
|
2019-02-19 20:46:38 +00:00
|
|
|
push_font_data(*pic.get(), &server, colorSpace, writeFd);
|
2018-04-05 20:57:25 +00:00
|
|
|
}
|
2018-03-21 20:23:49 +00:00
|
|
|
} else {
|
|
|
|
stream = skpData;
|
2018-05-02 17:29:37 +00:00
|
|
|
final_draw("test-correct.png", stream.get(), nullptr, nullptr, -1, -1);
|
2018-04-05 20:57:25 +00:00
|
|
|
closeAll();
|
2018-03-21 20:23:49 +00:00
|
|
|
return 0;
|
2018-01-09 17:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2018-02-07 22:47:59 +00:00
|
|
|
std::string skpName = argc > 1 ? std::string{argv[1]} : std::string{"skps/desk_nytimes.skp"};
|
2018-02-02 17:54:55 +00:00
|
|
|
int mode = argc > 2 ? atoi(argv[2]) : -1;
|
2018-01-09 17:34:11 +00:00
|
|
|
printf("skp: %s\n", skpName.c_str());
|
|
|
|
|
2018-02-07 22:47:59 +00:00
|
|
|
gSkpName = skpName;
|
|
|
|
|
2018-03-21 20:23:49 +00:00
|
|
|
enum direction : int {kRead = 0, kWrite = 1};
|
|
|
|
|
|
|
|
|
2018-01-09 17:34:11 +00:00
|
|
|
int render_to_gpu[2],
|
|
|
|
gpu_to_render[2];
|
|
|
|
|
2018-02-02 17:54:55 +00:00
|
|
|
for (int m = 0; m < 8; m++) {
|
|
|
|
int r = pipe(render_to_gpu);
|
|
|
|
if (r < 0) {
|
|
|
|
perror("Can't write picture from render to GPU ");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
r = pipe(gpu_to_render);
|
|
|
|
if (r < 0) {
|
|
|
|
perror("Can't write picture from render to GPU ");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-02-02 17:54:55 +00:00
|
|
|
gPurgeFontCaches = (m & 4) == 4;
|
|
|
|
gUseGpu = (m & 2) == 2;
|
|
|
|
gUseProcess = (m & 1) == 1;
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-02-02 17:54:55 +00:00
|
|
|
if (mode >= 0 && mode < 8 && mode != m) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-09 17:34:11 +00:00
|
|
|
|
2018-02-02 17:54:55 +00:00
|
|
|
if (gUseProcess) {
|
|
|
|
pid_t child = fork();
|
|
|
|
SkGraphics::Init();
|
|
|
|
|
|
|
|
if (child == 0) {
|
2018-03-21 20:23:49 +00:00
|
|
|
close(gpu_to_render[kRead]);
|
|
|
|
close(render_to_gpu[kWrite]);
|
|
|
|
gpu(render_to_gpu[kRead], gpu_to_render[kWrite]);
|
2018-02-02 17:54:55 +00:00
|
|
|
} else {
|
2018-03-21 20:23:49 +00:00
|
|
|
close(render_to_gpu[kRead]);
|
|
|
|
close(gpu_to_render[kWrite]);
|
|
|
|
renderer(skpName, gpu_to_render[kRead], render_to_gpu[kWrite]);
|
2018-02-02 17:54:55 +00:00
|
|
|
waitpid(child, nullptr, 0);
|
|
|
|
}
|
2018-01-26 21:47:54 +00:00
|
|
|
} else {
|
2018-02-02 17:54:55 +00:00
|
|
|
SkGraphics::Init();
|
|
|
|
std::thread(gpu, render_to_gpu[kRead], gpu_to_render[kWrite]).detach();
|
|
|
|
renderer(skpName, gpu_to_render[kRead], render_to_gpu[kWrite]);
|
2018-01-09 17:34:11 +00:00
|
|
|
}
|
2018-01-09 17:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|