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 | #ifndef ok_DEFINED |
| 9 | #define ok_DEFINED |
| 10 | |
| 11 | #include "SkCanvas.h" |
| 12 | #include <functional> |
Mike Klein | 154e6da | 2017-07-26 15:13:47 -0400 | [diff] [blame] | 13 | #include <future> |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | |
| 18 | // Not really ok-specific, but just kind of generally handy. |
| 19 | template <typename T> |
| 20 | static std::unique_ptr<T> move_unique(T& v) { |
| 21 | return std::unique_ptr<T>{new T{std::move(v)}}; |
| 22 | } |
| 23 | |
Mike Klein | 200f6da | 2017-03-28 09:30:11 -0400 | [diff] [blame] | 24 | void ok_log(const char*); |
| 25 | |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 26 | enum class Status { OK, Failed, Crashed, Skipped, None }; |
| 27 | |
Mike Klein | 154e6da | 2017-07-26 15:13:47 -0400 | [diff] [blame] | 28 | struct Engine { |
| 29 | virtual ~Engine() {} |
| 30 | virtual bool crashproof() = 0; |
| 31 | virtual std::future<Status> spawn(std::function<Status(void)>) = 0; |
| 32 | }; |
| 33 | |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 34 | struct Src { |
| 35 | virtual ~Src() {} |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 36 | virtual std::string name() = 0; |
| 37 | virtual SkISize size() = 0; |
| 38 | virtual Status draw(SkCanvas*) = 0; |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct Stream { |
| 42 | virtual ~Stream() {} |
| 43 | virtual std::unique_ptr<Src> next() = 0; |
| 44 | }; |
| 45 | |
| 46 | struct Dst { |
| 47 | virtual ~Dst() {} |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 48 | virtual Status draw(Src*) = 0; |
Mike Klein | 0222e70 | 2017-03-25 15:53:14 -0400 | [diff] [blame] | 49 | virtual sk_sp<SkImage> image() = 0; |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | class Options { |
| 53 | std::map<std::string, std::string> kv; |
| 54 | public: |
Mike Klein | 88f9c1e | 2017-03-27 12:43:44 -0400 | [diff] [blame] | 55 | explicit Options(std::string = ""); |
| 56 | std::string& operator[](std::string k); |
| 57 | std::string operator()(std::string k, std::string fallback = "") const; |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // Create globals to register your new type of Stream or Dst. |
| 61 | struct Register { |
Mike Klein | 154e6da | 2017-07-26 15:13:47 -0400 | [diff] [blame] | 62 | Register(const char* name, const char* help, std::unique_ptr<Engine> (*factory)(Options)); |
Mike Klein | e15a7b5 | 2017-03-29 12:41:13 -0400 | [diff] [blame] | 63 | Register(const char* name, const char* help, std::unique_ptr<Stream> (*factory)(Options)); |
| 64 | Register(const char* name, const char* help, std::unique_ptr<Dst> (*factory)(Options)); |
| 65 | Register(const char* name, const char* help, |
| 66 | std::unique_ptr<Dst>(*factory)(Options, std::unique_ptr<Dst>)); |
Mike Klein | 7ac0483 | 2017-03-25 11:29:41 -0400 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | #endif//ok_DEFINED |