Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 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" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 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 | |
| 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 Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 27 | void init() { |
| 28 | if (gm) { return; } |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 29 | gm.reset(factory(nullptr)); |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | std::string name() override { |
| 33 | this->init(); |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 34 | return gm->getName(); |
| 35 | } |
| 36 | |
| 37 | SkISize size() override { |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 38 | this->init(); |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 39 | return gm->getISize(); |
| 40 | } |
| 41 | |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 42 | Status draw(SkCanvas* canvas) override { |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 43 | this->init(); |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 44 | canvas->clear(0xffffffff); |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 45 | gm->draw(canvas); |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 46 | return Status::OK; |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 47 | } |
| 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 Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 60 | static Register gm{"gm", "draw GMs linked into this binary", GMStream::Create}; |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 61 | |
| 62 | struct 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 Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 80 | void init() { |
| 81 | if (pic) { return; } |
| 82 | auto skp = SkData::MakeFromFileName((dir+"/"+path).c_str()); |
| 83 | pic = SkPicture::MakeFromData(skp.get()); |
| 84 | } |
| 85 | |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 86 | std::string name() override { |
| 87 | return path; |
| 88 | } |
| 89 | |
| 90 | SkISize size() override { |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 91 | this->init(); |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 92 | return pic->cullRect().roundOut().size(); |
| 93 | } |
| 94 | |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 95 | Status draw(SkCanvas* canvas) override { |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 96 | this->init(); |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 97 | canvas->clear(0xffffffff); |
| 98 | pic->playback(canvas); |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 99 | return Status::OK; |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 100 | } |
| 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 Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 114 | static Register skp{"skp", "draw SKPs from dir=skps", SKPStream::Create}; |