2017-03-25 15:29:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ok.h"
|
|
|
|
#include "gm.h"
|
2017-05-03 19:16:58 +00:00
|
|
|
#include "SkData.h"
|
2017-03-25 15:29:41 +00:00
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
struct GMStream : Stream {
|
|
|
|
const skiagm::GMRegistry* registry = skiagm::GMRegistry::Head();
|
|
|
|
|
|
|
|
static std::unique_ptr<Stream> Create(Options) {
|
|
|
|
GMStream stream;
|
|
|
|
return move_unique(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GMSrc : Src {
|
|
|
|
skiagm::GM* (*factory)(void*);
|
|
|
|
std::unique_ptr<skiagm::GM> gm;
|
|
|
|
|
2017-03-25 19:53:14 +00:00
|
|
|
void init() {
|
|
|
|
if (gm) { return; }
|
2017-03-25 15:29:41 +00:00
|
|
|
gm.reset(factory(nullptr));
|
2017-03-25 19:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string name() override {
|
|
|
|
this->init();
|
2017-03-25 15:29:41 +00:00
|
|
|
return gm->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize size() override {
|
2017-03-25 19:53:14 +00:00
|
|
|
this->init();
|
2017-03-25 15:29:41 +00:00
|
|
|
return gm->getISize();
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
Status draw(SkCanvas* canvas) override {
|
2017-03-25 19:53:14 +00:00
|
|
|
this->init();
|
2017-03-25 15:29:41 +00:00
|
|
|
canvas->clear(0xffffffff);
|
|
|
|
canvas->concat(gm->getInitialTransform());
|
|
|
|
gm->draw(canvas);
|
2017-03-29 16:41:13 +00:00
|
|
|
return Status::OK;
|
2017-03-25 15:29:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Src> next() override {
|
|
|
|
if (!registry) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
GMSrc src;
|
|
|
|
src.factory = registry->factory();
|
|
|
|
registry = registry->next();
|
|
|
|
return move_unique(src);
|
|
|
|
}
|
|
|
|
};
|
2017-03-29 16:41:13 +00:00
|
|
|
static Register gm{"gm", "draw GMs linked into this binary", GMStream::Create};
|
2017-03-25 15:29:41 +00:00
|
|
|
|
|
|
|
struct SKPStream : Stream {
|
|
|
|
std::string dir;
|
|
|
|
std::vector<std::string> skps;
|
|
|
|
|
|
|
|
static std::unique_ptr<Stream> Create(Options options) {
|
|
|
|
SKPStream stream;
|
|
|
|
stream.dir = options("dir", "skps");
|
|
|
|
SkOSFile::Iter it{stream.dir.c_str(), ".skp"};
|
|
|
|
for (SkString path; it.next(&path); ) {
|
|
|
|
stream.skps.push_back(path.c_str());
|
|
|
|
}
|
|
|
|
return move_unique(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SKPSrc : Src {
|
|
|
|
std::string dir, path;
|
|
|
|
sk_sp<SkPicture> pic;
|
|
|
|
|
2017-03-25 19:53:14 +00:00
|
|
|
void init() {
|
|
|
|
if (pic) { return; }
|
|
|
|
auto skp = SkData::MakeFromFileName((dir+"/"+path).c_str());
|
|
|
|
pic = SkPicture::MakeFromData(skp.get());
|
|
|
|
}
|
|
|
|
|
2017-03-25 15:29:41 +00:00
|
|
|
std::string name() override {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize size() override {
|
2017-03-25 19:53:14 +00:00
|
|
|
this->init();
|
2017-03-25 15:29:41 +00:00
|
|
|
return pic->cullRect().roundOut().size();
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:41:13 +00:00
|
|
|
Status draw(SkCanvas* canvas) override {
|
2017-03-25 19:53:14 +00:00
|
|
|
this->init();
|
2017-03-25 15:29:41 +00:00
|
|
|
canvas->clear(0xffffffff);
|
|
|
|
pic->playback(canvas);
|
2017-03-29 16:41:13 +00:00
|
|
|
return Status::OK;
|
2017-03-25 15:29:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Src> next() override {
|
|
|
|
if (skps.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
SKPSrc src;
|
|
|
|
src.dir = dir;
|
|
|
|
src.path = skps.back();
|
|
|
|
skps.pop_back();
|
|
|
|
return move_unique(src);
|
|
|
|
}
|
|
|
|
};
|
2017-03-29 16:41:13 +00:00
|
|
|
static Register skp{"skp", "draw SKPs from dir=skps", SKPStream::Create};
|