OLD | NEW |
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 "SkJSONRenderer.h" | 8 #include "SkJSONRenderer.h" |
9 | 9 |
10 #include "SkBlurMaskFilter.h" | 10 #include "SkBlurMaskFilter.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 return size; | 152 return size; |
153 } | 153 } |
154 | 154 |
155 static SkFlattenable* load_flattenable(Json::Value jsonFlattenable) { | 155 static SkFlattenable* load_flattenable(Json::Value jsonFlattenable) { |
156 if (!jsonFlattenable.isMember(SKJSONCANVAS_ATTRIBUTE_NAME)) { | 156 if (!jsonFlattenable.isMember(SKJSONCANVAS_ATTRIBUTE_NAME)) { |
157 return nullptr; | 157 return nullptr; |
158 } | 158 } |
159 const char* name = jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_NAME].asCString(); | 159 const char* name = jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_NAME].asCString(); |
160 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(name); | 160 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(name); |
161 if (factory == nullptr) { | 161 if (factory == nullptr) { |
| 162 SkDebugf("no factory for loading '%s'\n", name); |
162 return nullptr; | 163 return nullptr; |
163 } | 164 } |
164 void* data; | 165 void* data; |
165 int size = decode_data(jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_BYTES], &data)
; | 166 int size = decode_data(jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_BYTES], &data)
; |
166 SkValidatingReadBuffer buffer(data, size); | 167 SkValidatingReadBuffer buffer(data, size); |
167 SkFlattenable* result = factory(buffer); | 168 SkFlattenable* result = factory(buffer); |
168 free(data); | 169 free(data); |
169 if (!buffer.isValid()) { | 170 if (!buffer.isValid()) { |
| 171 SkDebugf("invalid buffer loading flattenable\n"); |
170 return nullptr; | 172 return nullptr; |
171 } | 173 } |
172 return result; | 174 return result; |
173 } | 175 } |
174 | 176 |
| 177 static SkColorType colortype_from_name(const char* name) { |
| 178 if (!strcmp(name, SKJSONCANVAS_COLORTYPE_ARGB4444)) { |
| 179 return kARGB_4444_SkColorType; |
| 180 } |
| 181 else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_RGBA8888)) { |
| 182 return kRGBA_8888_SkColorType; |
| 183 } |
| 184 else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_BGRA8888)) { |
| 185 return kBGRA_8888_SkColorType; |
| 186 } |
| 187 else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_565)) { |
| 188 return kRGB_565_SkColorType; |
| 189 } |
| 190 else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_GRAY8)) { |
| 191 return kGray_8_SkColorType; |
| 192 } |
| 193 else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_INDEX8)) { |
| 194 return kIndex_8_SkColorType; |
| 195 } |
| 196 else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_ALPHA8)) { |
| 197 return kAlpha_8_SkColorType; |
| 198 } |
| 199 SkASSERT(false); |
| 200 return kN32_SkColorType; |
| 201 } |
| 202 |
| 203 static SkBitmap* convert_colortype(SkBitmap* bitmap, SkColorType colorType) { |
| 204 if (bitmap->colorType() == colorType ) { |
| 205 return bitmap; |
| 206 } |
| 207 SkBitmap* dst = new SkBitmap(); |
| 208 if (bitmap->copyTo(dst, colorType)) { |
| 209 delete bitmap; |
| 210 return dst; |
| 211 } |
| 212 SkASSERT(false); |
| 213 delete dst; |
| 214 return bitmap; |
| 215 } |
| 216 |
175 // caller is responsible for freeing return value | 217 // caller is responsible for freeing return value |
176 static SkBitmap* load_bitmap(Json::Value jsonBitmap) { | 218 static SkBitmap* load_bitmap(const Json::Value& jsonBitmap) { |
177 if (!jsonBitmap.isMember(SKJSONCANVAS_ATTRIBUTE_BYTES)) { | 219 if (!jsonBitmap.isMember(SKJSONCANVAS_ATTRIBUTE_BYTES)) { |
| 220 SkDebugf("invalid bitmap\n"); |
178 return nullptr; | 221 return nullptr; |
179 } | 222 } |
180 void* data; | 223 void* data; |
181 int size = decode_data(jsonBitmap[SKJSONCANVAS_ATTRIBUTE_BYTES], &data); | 224 int size = decode_data(jsonBitmap[SKJSONCANVAS_ATTRIBUTE_BYTES], &data); |
182 SkMemoryStream stream(data, size); | 225 SkMemoryStream stream(data, size); |
183 SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); | 226 SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); |
184 SkBitmap* bitmap = new SkBitmap(); | 227 SkBitmap* bitmap = new SkBitmap(); |
185 SkImageDecoder::Result result = decoder->decode(&stream, bitmap, | 228 SkImageDecoder::Result result = decoder->decode(&stream, bitmap, |
186 SkImageDecoder::kDecodePixel
s_Mode); | 229 SkImageDecoder::kDecodePixel
s_Mode); |
187 free(decoder); | 230 free(decoder); |
188 if (result != SkImageDecoder::kFailure) { | 231 if (result != SkImageDecoder::kFailure) { |
189 free(data); | 232 free(data); |
| 233 if (jsonBitmap.isMember(SKJSONCANVAS_ATTRIBUTE_COLOR)) { |
| 234 const char* ctName = jsonBitmap[SKJSONCANVAS_ATTRIBUTE_COLOR].asCStr
ing(); |
| 235 SkColorType ct = colortype_from_name(ctName); |
| 236 if (ct != kIndex_8_SkColorType) { |
| 237 bitmap = convert_colortype(bitmap, ct); |
| 238 } |
| 239 } |
190 return bitmap; | 240 return bitmap; |
191 } | 241 } |
192 SkDebugf("image decode failed"); | 242 SkDebugf("image decode failed\n"); |
193 free(data); | 243 free(data); |
194 return nullptr; | 244 return nullptr; |
195 } | 245 } |
196 | 246 |
197 static SkImage* load_image(Json::Value jsonImage) { | 247 static SkImage* load_image(const Json::Value& jsonImage) { |
198 SkBitmap* bitmap = load_bitmap(jsonImage); | 248 SkBitmap* bitmap = load_bitmap(jsonImage); |
199 if (bitmap == nullptr) { | 249 if (bitmap == nullptr) { |
200 return nullptr; | 250 return nullptr; |
201 } | 251 } |
202 SkImage* result = SkImage::NewFromBitmap(*bitmap); | 252 SkImage* result = SkImage::NewFromBitmap(*bitmap); |
203 free(bitmap); | 253 delete bitmap; |
204 return result; | 254 return result; |
205 } | 255 } |
206 | 256 |
207 static void apply_paint_shader(Json::Value& jsonPaint, SkPaint* target) { | 257 static void apply_paint_shader(Json::Value& jsonPaint, SkPaint* target) { |
208 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_SHADER)) { | 258 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_SHADER)) { |
209 Json::Value jsonShader = jsonPaint[SKJSONCANVAS_ATTRIBUTE_SHADER]; | 259 Json::Value jsonShader = jsonPaint[SKJSONCANVAS_ATTRIBUTE_SHADER]; |
210 SkShader* shader = (SkShader*) load_flattenable(jsonShader); | 260 SkShader* shader = (SkShader*) load_flattenable(jsonShader); |
211 if (shader != nullptr) { | 261 if (shader != nullptr) { |
212 target->setShader(shader); | 262 target->setShader(shader); |
213 shader->unref(); | 263 shader->unref(); |
(...skipping 27 matching lines...) Expand all Loading... |
241 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_XFERMODE)) { | 291 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_XFERMODE)) { |
242 Json::Value jsonXfermode = jsonPaint[SKJSONCANVAS_ATTRIBUTE_XFERMODE]; | 292 Json::Value jsonXfermode = jsonPaint[SKJSONCANVAS_ATTRIBUTE_XFERMODE]; |
243 SkXfermode* xfermode = (SkXfermode*) load_flattenable(jsonXfermode); | 293 SkXfermode* xfermode = (SkXfermode*) load_flattenable(jsonXfermode); |
244 if (xfermode != nullptr) { | 294 if (xfermode != nullptr) { |
245 target->setXfermode(xfermode); | 295 target->setXfermode(xfermode); |
246 xfermode->unref(); | 296 xfermode->unref(); |
247 } | 297 } |
248 } | 298 } |
249 } | 299 } |
250 | 300 |
| 301 static void apply_paint_imagefilter(Json::Value& jsonPaint, SkPaint* target) { |
| 302 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_IMAGEFILTER)) { |
| 303 Json::Value jsonImageFilter = jsonPaint[SKJSONCANVAS_ATTRIBUTE_IMAGEFILT
ER]; |
| 304 SkImageFilter* imageFilter = (SkImageFilter*) load_flattenable(jsonImage
Filter); |
| 305 if (imageFilter != nullptr) { |
| 306 target->setImageFilter(imageFilter); |
| 307 imageFilter->unref(); |
| 308 } |
| 309 } |
| 310 } |
| 311 |
251 static void apply_paint_style(Json::Value& jsonPaint, SkPaint* target) { | 312 static void apply_paint_style(Json::Value& jsonPaint, SkPaint* target) { |
252 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STYLE)) { | 313 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STYLE)) { |
253 const char* style = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); | 314 const char* style = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); |
254 if (!strcmp(style, SKJSONCANVAS_STYLE_FILL)) { | 315 if (!strcmp(style, SKJSONCANVAS_STYLE_FILL)) { |
255 target->setStyle(SkPaint::kFill_Style); | 316 target->setStyle(SkPaint::kFill_Style); |
256 } | 317 } |
257 else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKE)) { | 318 else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKE)) { |
258 target->setStyle(SkPaint::kStroke_Style); | 319 target->setStyle(SkPaint::kStroke_Style); |
259 } | 320 } |
260 else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKEANDFILL)) { | 321 else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKEANDFILL)) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 } | 454 } |
394 } | 455 } |
395 | 456 |
396 void Renderer::getPaint(Json::Value& command, SkPaint* result) { | 457 void Renderer::getPaint(Json::Value& command, SkPaint* result) { |
397 Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT]; | 458 Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT]; |
398 apply_paint_color(jsonPaint, result); | 459 apply_paint_color(jsonPaint, result); |
399 apply_paint_shader(jsonPaint, result); | 460 apply_paint_shader(jsonPaint, result); |
400 apply_paint_patheffect(jsonPaint, result); | 461 apply_paint_patheffect(jsonPaint, result); |
401 apply_paint_maskfilter(jsonPaint, result); | 462 apply_paint_maskfilter(jsonPaint, result); |
402 apply_paint_xfermode(jsonPaint, result); | 463 apply_paint_xfermode(jsonPaint, result); |
| 464 apply_paint_imagefilter(jsonPaint, result); |
403 apply_paint_style(jsonPaint, result); | 465 apply_paint_style(jsonPaint, result); |
404 apply_paint_strokewidth(jsonPaint, result); | 466 apply_paint_strokewidth(jsonPaint, result); |
405 apply_paint_strokemiter(jsonPaint, result); | 467 apply_paint_strokemiter(jsonPaint, result); |
406 apply_paint_cap(jsonPaint, result); | 468 apply_paint_cap(jsonPaint, result); |
407 apply_paint_antialias(jsonPaint, result); | 469 apply_paint_antialias(jsonPaint, result); |
408 apply_paint_blur(jsonPaint, result); | 470 apply_paint_blur(jsonPaint, result); |
409 apply_paint_dashing(jsonPaint, result); | 471 apply_paint_dashing(jsonPaint, result); |
410 apply_paint_textalign(jsonPaint, result); | 472 apply_paint_textalign(jsonPaint, result); |
411 apply_paint_textsize(jsonPaint, result); | 473 apply_paint_textsize(jsonPaint, result); |
412 apply_paint_textscalex(jsonPaint, result); | 474 apply_paint_textscalex(jsonPaint, result); |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 renderer.processCommand(commands[i], target); | 842 renderer.processCommand(commands[i], target); |
781 } | 843 } |
782 } | 844 } |
783 else { | 845 else { |
784 SkDebugf(json); | 846 SkDebugf(json); |
785 SkFAIL("json parse failure"); | 847 SkFAIL("json parse failure"); |
786 } | 848 } |
787 } | 849 } |
788 | 850 |
789 } // namespace | 851 } // namespace |
OLD | NEW |