• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
10 #include "SkData.h"
11 #include "SkOSFile.h"
12 #include "SkPicture.h"
13 #include <vector>
14 
15 struct GMStream : Stream {
16     const skiagm::GMRegistry* registry = skiagm::GMRegistry::Head();
17 
CreateGMStream18     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 
initGMStream::GMSrc27         void init() {
28             if (gm) { return; }
29             gm.reset(factory(nullptr));
30         }
31 
nameGMStream::GMSrc32         std::string name() override {
33             this->init();
34             return gm->getName();
35         }
36 
sizeGMStream::GMSrc37         SkISize size() override {
38             this->init();
39             return gm->getISize();
40         }
41 
drawGMStream::GMSrc42         Status draw(SkCanvas* canvas) override {
43             this->init();
44             canvas->clear(0xffffffff);
45             canvas->concat(gm->getInitialTransform());
46             gm->draw(canvas);
47             return Status::OK;
48         }
49     };
50 
nextGMStream51     std::unique_ptr<Src> next() override {
52         if (!registry) {
53             return nullptr;
54         }
55         GMSrc src;
56         src.factory = registry->factory();
57         registry = registry->next();
58         return move_unique(src);
59     }
60 };
61 static Register gm{"gm", "draw GMs linked into this binary", GMStream::Create};
62 
63 struct SKPStream : Stream {
64     std::string dir;
65     std::vector<std::string> skps;
66 
CreateSKPStream67     static std::unique_ptr<Stream> Create(Options options) {
68         SKPStream stream;
69         stream.dir = options("dir", "skps");
70         SkOSFile::Iter it{stream.dir.c_str(), ".skp"};
71         for (SkString path; it.next(&path); ) {
72             stream.skps.push_back(path.c_str());
73         }
74         return move_unique(stream);
75     }
76 
77     struct SKPSrc : Src {
78         std::string dir, path;
79         sk_sp<SkPicture> pic;
80 
initSKPStream::SKPSrc81         void init() {
82             if (pic) { return; }
83             auto skp = SkData::MakeFromFileName((dir+"/"+path).c_str());
84             pic = SkPicture::MakeFromData(skp.get());
85         }
86 
nameSKPStream::SKPSrc87         std::string name() override {
88             return path;
89         }
90 
sizeSKPStream::SKPSrc91         SkISize size() override {
92             this->init();
93             return pic->cullRect().roundOut().size();
94         }
95 
drawSKPStream::SKPSrc96         Status draw(SkCanvas* canvas) override {
97             this->init();
98             canvas->clear(0xffffffff);
99             pic->playback(canvas);
100             return Status::OK;
101         }
102     };
103 
nextSKPStream104     std::unique_ptr<Src> next() override {
105         if (skps.empty()) {
106             return nullptr;
107         }
108         SKPSrc src;
109         src.dir  = dir;
110         src.path = skps.back();
111         skps.pop_back();
112         return move_unique(src);
113     }
114 };
115 static Register skp{"skp", "draw SKPs from dir=skps", SKPStream::Create};
116