Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: src/core/SkValue.cpp

Issue 1605093003: SkValue: improve SkFromValue<T> implementation (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: hal Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkValue.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include <unordered_map> 8 #include <unordered_map>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 SkData* SkValue::bytes() const { 128 SkData* SkValue::bytes() const {
129 SkASSERT(this->isData()); 129 SkASSERT(this->isData());
130 return fBytes; 130 return fBytes;
131 } 131 }
132 132
133 void SkValue::set(SkValue::Key k, SkValue v) { 133 void SkValue::set(SkValue::Key k, SkValue v) {
134 SkASSERT(this->isObject()); 134 SkASSERT(this->isObject());
135 fObject->set(k, std::move(v)); 135 fObject->set(k, std::move(v));
136 } 136 }
137 137
138 const SkValue* SkValue::get(Key k) const {
139 SkASSERT(this->isObject());
140 return fObject->get(k);
141 }
142
138 void SkValue::foreach(std::function<void(Key, const SkValue&)> fn) const { 143 void SkValue::foreach(std::function<void(Key, const SkValue&)> fn) const {
139 SkASSERT(this->isObject()); 144 SkASSERT(this->isObject());
140 fObject->foreach(fn); 145 fObject->foreach(fn);
141 } 146 }
142 147
143 size_t SkValue::length() const { 148 size_t SkValue::length() const {
144 SkASSERT(Array == fType); 149 SkASSERT(Array == fType);
145 return fArray->length(); 150 return fArray->length();
146 } 151 }
147 152
(...skipping 27 matching lines...) Expand all
175 SkASSERT(0 == (reinterpret_cast<uintptr_t>(data->bytes()) & (sizeof(T)-1))); 180 SkASSERT(0 == (reinterpret_cast<uintptr_t>(data->bytes()) & (sizeof(T)-1)));
176 return val; 181 return val;
177 } 182 }
178 183
179 SkValue SkValue::FromU16s(SkData* d) { return FromTs<uint16_t>(U16s, d); } 184 SkValue SkValue::FromU16s(SkData* d) { return FromTs<uint16_t>(U16s, d); }
180 SkValue SkValue::FromU32s(SkData* d) { return FromTs<uint32_t>(U32s, d); } 185 SkValue SkValue::FromU32s(SkData* d) { return FromTs<uint32_t>(U32s, d); }
181 SkValue SkValue::FromF32s(SkData* d) { return FromTs< float>(F32s, d); } 186 SkValue SkValue::FromF32s(SkData* d) { return FromTs< float>(F32s, d); }
182 187
183 //////////////////////////////////////////////////////////////////////////////// 188 ////////////////////////////////////////////////////////////////////////////////
184 189
190 #define REQUIRE(cond) do { if (!(cond)) { SkASSERT(false); return false; } } whi le (false)
191
185 template<> SkValue SkToValue<SkMatrix>(const SkMatrix& mat) { 192 template<> SkValue SkToValue<SkMatrix>(const SkMatrix& mat) {
186 auto val = SkValue::Object(SkValue::Matrix); 193 auto val = SkValue::Object(SkValue::Matrix);
187 for (int i = 0; i < 9; ++i) { 194 for (int i = 0; i < 9; ++i) {
188 if (mat[i] != SkMatrix::I()[i]) { 195 if (mat[i] != SkMatrix::I()[i]) {
189 val.set(i, SkValue::FromF32(mat[i])); 196 val.set(i, SkValue::FromF32(mat[i]));
190 } 197 }
191 } 198 }
192 return val; 199 return val;
193 } 200 }
194 201
202 template<> bool SkFromValue<float>(const SkValue& val, float* f) {
203 REQUIRE(val.type() == SkValue::F32);
204 *f = val.f32();
205 return true;
206 }
207
195 template<> bool SkFromValue<SkMatrix>(const SkValue& val, SkMatrix* m){ 208 template<> bool SkFromValue<SkMatrix>(const SkValue& val, SkMatrix* m){
196 SkASSERT(val.type() == SkValue::Matrix); 209 REQUIRE(val.type() == SkValue::Matrix);
197 if (val.type() != SkValue::Matrix) { 210
198 return false; 211 *m = SkMatrix::I();
212 for (int i = 0; i < 9; i++) {
213 if (auto v = val.get(i)) {
214 REQUIRE(SkFromValue(*v, &(*m)[i]));
215 }
199 } 216 }
200 *m = SkMatrix::I(); 217 return true;
201 bool good = true;
202 auto fn = [&](SkValue::Key key, const SkValue& v) {
203 if (key < 9) {
204 if (v.type() != SkValue::F32) {
205 SkASSERT(false);
206 good = false;
207 } else {
208 (*m)[key] = v.f32();
209 }
210 } else {
211 SkASSERT(false);
212 good = false;
213 }
214 };
215 val.foreach(fn);
216 return good;
217 } 218 }
219
220 #undef REQUIRE
OLDNEW
« no previous file with comments | « src/core/SkValue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698