skia2/tools/ok_srcs.cpp
Mike Klein 0222e709a3 ok: refactor Src/Dst interactions
This makes everything a lot more like DM, for the same reason:
it's the best way to make Vias work.

Instead of exposing a canvas, Dsts take a Src to draw.  Vias still are
Dsts that wrap Dsts.  They do their internal work in draw() then pass a
proxy Src encapsulating that work to the next Dst's draw().

A little refactoring in ok.cpp allows arbitrary chains of Vias.

I removed the guarantee that Src methods are called in strict order.
It's easy enough to make each Src initialize itself as needed.

I moved the .png encoding back to ok.cpp.  It seemed weird for Dsts to
have to think about files and paths.  One day Dst will want a data()
method for non-image output (.pdf, .skp), and then we'll want ok.cpp to
be the one to coordinate what to write where.

Change-Id: Id4a3674b2d05aef2b5f10e0077df0a8407c07b61
Reviewed-on: https://skia-review.googlesource.com/10175
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Mike Klein <mtklein@chromium.org>
2017-03-25 20:43:01 +00:00

115 lines
2.7 KiB
C++

/*
* 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"
#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;
void init() {
if (gm) { return; }
gm.reset(factory(nullptr));
}
std::string name() override {
this->init();
return gm->getName();
}
SkISize size() override {
this->init();
return gm->getISize();
}
bool draw(SkCanvas* canvas) override {
this->init();
canvas->clear(0xffffffff);
canvas->concat(gm->getInitialTransform());
gm->draw(canvas);
return true;
}
};
std::unique_ptr<Src> next() override {
if (!registry) {
return nullptr;
}
GMSrc src;
src.factory = registry->factory();
registry = registry->next();
return move_unique(src);
}
};
static Register gm{"gm", GMStream::Create};
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;
void init() {
if (pic) { return; }
auto skp = SkData::MakeFromFileName((dir+"/"+path).c_str());
pic = SkPicture::MakeFromData(skp.get());
}
std::string name() override {
return path;
}
SkISize size() override {
this->init();
return pic->cullRect().roundOut().size();
}
bool draw(SkCanvas* canvas) override {
this->init();
canvas->clear(0xffffffff);
pic->playback(canvas);
return true;
}
};
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);
}
};
static Register skp{"skp", SKPStream::Create};