OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2015 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 SkRemote_DEFINED |
| 9 #define SkRemote_DEFINED |
| 10 |
| 11 #include "SkCanvas.h" |
| 12 #include "SkPaint.h" |
| 13 #include "SkRemote_protocol.h" |
| 14 #include "SkTHash.h" |
| 15 #include "SkTypes.h" |
| 16 |
| 17 // TODO: document |
| 18 |
| 19 namespace SkRemote { |
| 20 // TODO: document |
| 21 struct Misc { |
| 22 SkColor fColor; |
| 23 SkFilterQuality fFilterQuality; |
| 24 bool fAntiAlias, fDither; |
| 25 |
| 26 static Misc CreateFrom(const SkPaint&); |
| 27 void applyTo(SkPaint*) const; |
| 28 }; |
| 29 |
| 30 // TODO: document |
| 31 struct Stroke { |
| 32 SkScalar fWidth, fMiter; |
| 33 SkPaint::Cap fCap; |
| 34 SkPaint::Join fJoin; |
| 35 |
| 36 static Stroke CreateFrom(const SkPaint&); |
| 37 void applyTo(SkPaint*) const; |
| 38 }; |
| 39 |
| 40 // TODO: document |
| 41 struct Encoder { |
| 42 virtual ~Encoder() {} |
| 43 |
| 44 virtual void define(ID, const SkMatrix&) = 0; |
| 45 virtual void define(ID, const Misc&) = 0; |
| 46 virtual void define(ID, const SkPath&) = 0; |
| 47 virtual void define(ID, const Stroke&) = 0; |
| 48 |
| 49 virtual void undefine(ID) = 0; |
| 50 |
| 51 virtual void save() = 0; |
| 52 virtual void restore() = 0; |
| 53 |
| 54 virtual void setMatrix(ID matrix) = 0; |
| 55 |
| 56 virtual void clipPath(ID path, SkRegion::Op, bool aa) = 0; |
| 57 virtual void fillPath(ID path, ID misc) = 0; |
| 58 virtual void strokePath(ID path, ID misc, ID stroke) = 0; |
| 59 }; |
| 60 |
| 61 struct Cache; |
| 62 |
| 63 // TODO: document |
| 64 class LookupScope { |
| 65 public: |
| 66 LookupScope(Cache* cache, Encoder* encoder) : fCache(cache), fEncoder(en
coder) {} |
| 67 ~LookupScope() { for (ID id : fToUndefine) { fEncoder->undefine(id); } } |
| 68 void undefineWhenDone(ID id) { fToUndefine.push_back(id); } |
| 69 |
| 70 template <typename T> |
| 71 ID lookup(const T&); |
| 72 private: |
| 73 Cache* fCache; |
| 74 Encoder* fEncoder; |
| 75 SkSTArray<4, ID> fToUndefine; |
| 76 }; |
| 77 |
| 78 // TODO: document |
| 79 struct Cache { |
| 80 virtual ~Cache() {} |
| 81 |
| 82 static Cache* CreateNeverCache(); |
| 83 static Cache* CreateAlwaysCache(); |
| 84 |
| 85 virtual bool lookup(const SkMatrix&, ID*, LookupScope*) = 0; |
| 86 virtual bool lookup(const Misc&, ID*, LookupScope*) = 0; |
| 87 virtual bool lookup(const SkPath&, ID*, LookupScope*) = 0; |
| 88 virtual bool lookup(const Stroke&, ID*, LookupScope*) = 0; |
| 89 |
| 90 virtual void cleanup(Encoder*) = 0; |
| 91 }; |
| 92 |
| 93 // TODO: document |
| 94 class Client final : public SkCanvas { |
| 95 public: |
| 96 Client(Cache*, Encoder*); |
| 97 ~Client(); |
| 98 |
| 99 private: |
| 100 void willSave() override; |
| 101 void didRestore() override; |
| 102 |
| 103 void didConcat(const SkMatrix&) override; |
| 104 void didSetMatrix(const SkMatrix&) override; |
| 105 |
| 106 void onClipPath (const SkPath&, SkRegion::Op, ClipEdgeStyle) override; |
| 107 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override; |
| 108 void onClipRect (const SkRect&, SkRegion::Op, ClipEdgeStyle) override; |
| 109 |
| 110 void onDrawOval(const SkRect&, const SkPaint&) override; |
| 111 void onDrawPath(const SkPath&, const SkPaint&) override; |
| 112 void onDrawRect(const SkRect&, const SkPaint&) override; |
| 113 |
| 114 Cache* fCache; |
| 115 Encoder* fEncoder; |
| 116 }; |
| 117 |
| 118 // TODO: document |
| 119 class Server final : public Encoder { |
| 120 public: |
| 121 explicit Server(SkCanvas*); |
| 122 |
| 123 private: |
| 124 void define(ID, const SkMatrix&) override; |
| 125 void define(ID, const Misc&) override; |
| 126 void define(ID, const SkPath&) override; |
| 127 void define(ID, const Stroke&) override; |
| 128 |
| 129 void undefine(ID) override; |
| 130 |
| 131 void save() override; |
| 132 void restore() override; |
| 133 |
| 134 void setMatrix(ID matrix) override; |
| 135 |
| 136 void clipPath(ID path, SkRegion::Op, bool aa) override; |
| 137 void fillPath(ID path, ID misc) override; |
| 138 void strokePath(ID path, ID misc, ID stroke) override; |
| 139 |
| 140 template <typename T, Type kType> |
| 141 class IDMap { |
| 142 public: |
| 143 void set(const ID& id, const T& val) { |
| 144 SkASSERT(id.type() == kType); |
| 145 fMap.set(id, val); |
| 146 } |
| 147 |
| 148 void remove(const ID& id) { |
| 149 SkASSERT(id.type() == kType); |
| 150 fMap.remove(id); |
| 151 } |
| 152 |
| 153 const T& find(const ID& id) const { |
| 154 SkASSERT(id.type() == kType); |
| 155 T* val = fMap.find(id); |
| 156 SkASSERT(val != nullptr); |
| 157 return *val; |
| 158 } |
| 159 |
| 160 private: |
| 161 SkTHashMap<ID, T> fMap; |
| 162 }; |
| 163 |
| 164 IDMap<SkMatrix, Type::kMatrix> fMatrix; |
| 165 IDMap<Misc , Type::kMisc > fMisc; |
| 166 IDMap<SkPath , Type::kPath > fPath; |
| 167 IDMap<Stroke , Type::kStroke> fStroke; |
| 168 |
| 169 SkCanvas* fCanvas; |
| 170 }; |
| 171 |
| 172 } // namespace SkRemote |
| 173 |
| 174 #endif//SkRemote_DEFINED |
OLD | NEW |