blob: dc040f86c95cba5b562fc1943a13f308033b721f [file] [log] [blame]
Mike Klein7ac04832017-03-25 11:29:41 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "ok.h"
9#include "gm.h"
Florin Malitaab244f02017-05-03 19:16:58 +000010#include "SkData.h"
Mike Klein7ac04832017-03-25 11:29:41 -040011#include "SkOSFile.h"
12#include "SkPicture.h"
13#include <vector>
14
15struct GMStream : Stream {
16 const skiagm::GMRegistry* registry = skiagm::GMRegistry::Head();
17
18 static std::unique_ptr<Stream> Create(Options) {
19 GMStream stream;
20 return move_unique(stream);
21 }
22
23 struct GMSrc : Src {
24 skiagm::GM* (*factory)(void*);
25 std::unique_ptr<skiagm::GM> gm;
26
Mike Klein0222e702017-03-25 15:53:14 -040027 void init() {
28 if (gm) { return; }
Mike Klein7ac04832017-03-25 11:29:41 -040029 gm.reset(factory(nullptr));
Mike Klein0222e702017-03-25 15:53:14 -040030 }
31
32 std::string name() override {
33 this->init();
Mike Klein7ac04832017-03-25 11:29:41 -040034 return gm->getName();
35 }
36
37 SkISize size() override {
Mike Klein0222e702017-03-25 15:53:14 -040038 this->init();
Mike Klein7ac04832017-03-25 11:29:41 -040039 return gm->getISize();
40 }
41
Mike Kleine15a7b52017-03-29 12:41:13 -040042 Status draw(SkCanvas* canvas) override {
Mike Klein0222e702017-03-25 15:53:14 -040043 this->init();
Mike Klein7ac04832017-03-25 11:29:41 -040044 canvas->clear(0xffffffff);
Mike Klein7ac04832017-03-25 11:29:41 -040045 gm->draw(canvas);
Mike Kleine15a7b52017-03-29 12:41:13 -040046 return Status::OK;
Mike Klein7ac04832017-03-25 11:29:41 -040047 }
48 };
49
50 std::unique_ptr<Src> next() override {
51 if (!registry) {
52 return nullptr;
53 }
54 GMSrc src;
55 src.factory = registry->factory();
56 registry = registry->next();
57 return move_unique(src);
58 }
59};
Mike Kleine15a7b52017-03-29 12:41:13 -040060static Register gm{"gm", "draw GMs linked into this binary", GMStream::Create};
Mike Klein7ac04832017-03-25 11:29:41 -040061
62struct SKPStream : Stream {
63 std::string dir;
64 std::vector<std::string> skps;
65
66 static std::unique_ptr<Stream> Create(Options options) {
67 SKPStream stream;
68 stream.dir = options("dir", "skps");
69 SkOSFile::Iter it{stream.dir.c_str(), ".skp"};
70 for (SkString path; it.next(&path); ) {
71 stream.skps.push_back(path.c_str());
72 }
73 return move_unique(stream);
74 }
75
76 struct SKPSrc : Src {
77 std::string dir, path;
78 sk_sp<SkPicture> pic;
79
Mike Klein0222e702017-03-25 15:53:14 -040080 void init() {
81 if (pic) { return; }
82 auto skp = SkData::MakeFromFileName((dir+"/"+path).c_str());
83 pic = SkPicture::MakeFromData(skp.get());
84 }
85
Mike Klein7ac04832017-03-25 11:29:41 -040086 std::string name() override {
87 return path;
88 }
89
90 SkISize size() override {
Mike Klein0222e702017-03-25 15:53:14 -040091 this->init();
Mike Klein7ac04832017-03-25 11:29:41 -040092 return pic->cullRect().roundOut().size();
93 }
94
Mike Kleine15a7b52017-03-29 12:41:13 -040095 Status draw(SkCanvas* canvas) override {
Mike Klein0222e702017-03-25 15:53:14 -040096 this->init();
Mike Klein7ac04832017-03-25 11:29:41 -040097 canvas->clear(0xffffffff);
98 pic->playback(canvas);
Mike Kleine15a7b52017-03-29 12:41:13 -040099 return Status::OK;
Mike Klein7ac04832017-03-25 11:29:41 -0400100 }
101 };
102
103 std::unique_ptr<Src> next() override {
104 if (skps.empty()) {
105 return nullptr;
106 }
107 SKPSrc src;
108 src.dir = dir;
109 src.path = skps.back();
110 skps.pop_back();
111 return move_unique(src);
112 }
113};
Mike Kleine15a7b52017-03-29 12:41:13 -0400114static Register skp{"skp", "draw SKPs from dir=skps", SKPStream::Create};