| 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" |
| 11 #include "SkDashPathEffect.h" | 11 #include "SkDashPathEffect.h" |
| 12 #include "SkJSONCanvas.h" | 12 #include "SkJSONCanvas.h" |
| 13 #include "SkJSONCPP.h" | 13 #include "SkJSONCPP.h" |
| 14 #include "SkPath.h" | 14 #include "SkPath.h" |
| 15 #include "SkValidatingReadBuffer.h" |
| 15 | 16 |
| 16 namespace SkJSONRenderer { | 17 namespace SkJSONRenderer { |
| 17 | 18 |
| 18 class Renderer { | 19 class Renderer { |
| 19 public: | 20 public: |
| 20 void getPaint(Json::Value& command, SkPaint* paint); | 21 void getPaint(Json::Value& command, SkPaint* paint); |
| 21 | 22 |
| 22 void getRect(Json::Value& command, const char* name, SkRect* rect); | 23 void getRect(Json::Value& command, const char* name, SkRect* rect); |
| 23 | 24 |
| 24 void getRRect(Json::Value& command, const char* name, SkRRect* rrect); | 25 void getRRect(Json::Value& command, const char* name, SkRRect* rrect); |
| 25 | 26 |
| 26 void getPath(Json::Value& command, SkPath* path); | 27 void getPath(Json::Value& command, SkPath* path); |
| 27 | 28 |
| 28 SkRegion::Op getRegionOp(Json::Value& command); | 29 SkRegion::Op getRegionOp(Json::Value& command); |
| 29 | 30 |
| 30 void processCommand(Json::Value& command, SkCanvas* target); | 31 void processCommand(Json::Value& command, SkCanvas* target); |
| 31 | 32 |
| 32 void processMatrix(Json::Value& command, SkCanvas* target); | 33 void processMatrix(Json::Value& command, SkCanvas* target); |
| 33 | 34 |
| 34 void processSave(Json::Value& command, SkCanvas* target); | 35 void processSave(Json::Value& command, SkCanvas* target); |
| 35 | 36 |
| 36 void processRestore(Json::Value& command, SkCanvas* target); | 37 void processRestore(Json::Value& command, SkCanvas* target); |
| 37 | 38 |
| 39 void processSaveLayer(Json::Value& command, SkCanvas* target); |
| 40 |
| 38 void processPaint(Json::Value& command, SkCanvas* target); | 41 void processPaint(Json::Value& command, SkCanvas* target); |
| 39 | 42 |
| 40 void processRect(Json::Value& command, SkCanvas* target); | 43 void processRect(Json::Value& command, SkCanvas* target); |
| 41 | 44 |
| 42 void processRRect(Json::Value& command, SkCanvas* target); | 45 void processRRect(Json::Value& command, SkCanvas* target); |
| 43 | 46 |
| 44 void processOval(Json::Value& command, SkCanvas* target); | 47 void processOval(Json::Value& command, SkCanvas* target); |
| 45 | 48 |
| 46 void processPath(Json::Value& command, SkCanvas* target); | 49 void processPath(Json::Value& command, SkCanvas* target); |
| 47 | 50 |
| 48 void processText(Json::Value& command, SkCanvas* target); | 51 void processText(Json::Value& command, SkCanvas* target); |
| 49 | 52 |
| 50 void processPosText(Json::Value& command, SkCanvas* target); | 53 void processPosText(Json::Value& command, SkCanvas* target); |
| 51 | 54 |
| 52 void processPoints(Json::Value& command, SkCanvas* target); | 55 void processPoints(Json::Value& command, SkCanvas* target); |
| 53 | 56 |
| 57 void processImage(Json::Value& command, SkCanvas* target); |
| 58 |
| 59 void processImageRect(Json::Value& command, SkCanvas* target); |
| 60 |
| 61 void processBitmap(Json::Value& command, SkCanvas* target); |
| 62 |
| 63 void processBitmapRect(Json::Value& command, SkCanvas* target); |
| 64 |
| 54 void processClipRect(Json::Value& command, SkCanvas* target); | 65 void processClipRect(Json::Value& command, SkCanvas* target); |
| 55 | 66 |
| 56 void processClipRRect(Json::Value& command, SkCanvas* target); | 67 void processClipRRect(Json::Value& command, SkCanvas* target); |
| 57 | 68 |
| 58 void processClipPath(Json::Value& command, SkCanvas* target); | 69 void processClipPath(Json::Value& command, SkCanvas* target); |
| 59 }; | 70 }; |
| 60 | 71 |
| 61 void Renderer::processCommand(Json::Value& command, SkCanvas* target) { | 72 void Renderer::processCommand(Json::Value& command, SkCanvas* target) { |
| 62 const char* name = command[SKJSONCANVAS_COMMAND].asCString(); | 73 const char* name = command[SKJSONCANVAS_COMMAND].asCString(); |
| 63 // TODO speed this up with a hash | 74 // TODO speed this up with a hash |
| 64 if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) { | 75 if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) { |
| 65 this->processMatrix(command, target); | 76 this->processMatrix(command, target); |
| 66 } | 77 } |
| 67 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) { | 78 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) { |
| 68 this->processSave(command, target); | 79 this->processSave(command, target); |
| 69 } | 80 } |
| 70 else if (!strcmp(name, SKJSONCANVAS_COMMAND_RESTORE)) { | 81 else if (!strcmp(name, SKJSONCANVAS_COMMAND_RESTORE)) { |
| 71 this->processRestore(command, target); | 82 this->processRestore(command, target); |
| 72 } | 83 } |
| 84 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVELAYER)) { |
| 85 this->processSaveLayer(command, target); |
| 86 } |
| 73 else if (!strcmp(name, SKJSONCANVAS_COMMAND_PAINT)) { | 87 else if (!strcmp(name, SKJSONCANVAS_COMMAND_PAINT)) { |
| 74 this->processPaint(command, target); | 88 this->processPaint(command, target); |
| 75 } | 89 } |
| 76 else if (!strcmp(name, SKJSONCANVAS_COMMAND_RECT)) { | 90 else if (!strcmp(name, SKJSONCANVAS_COMMAND_RECT)) { |
| 77 this->processRect(command, target); | 91 this->processRect(command, target); |
| 78 } | 92 } |
| 79 else if (!strcmp(name, SKJSONCANVAS_COMMAND_RRECT)) { | 93 else if (!strcmp(name, SKJSONCANVAS_COMMAND_RRECT)) { |
| 80 this->processRRect(command, target); | 94 this->processRRect(command, target); |
| 81 } | 95 } |
| 82 else if (!strcmp(name, SKJSONCANVAS_COMMAND_OVAL)) { | 96 else if (!strcmp(name, SKJSONCANVAS_COMMAND_OVAL)) { |
| 83 this->processOval(command, target); | 97 this->processOval(command, target); |
| 84 } | 98 } |
| 85 else if (!strcmp(name, SKJSONCANVAS_COMMAND_PATH)) { | 99 else if (!strcmp(name, SKJSONCANVAS_COMMAND_PATH)) { |
| 86 this->processPath(command, target); | 100 this->processPath(command, target); |
| 87 } | 101 } |
| 88 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) { | 102 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) { |
| 89 this->processText(command, target); | 103 this->processText(command, target); |
| 90 } | 104 } |
| 91 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) { | 105 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) { |
| 92 this->processPosText(command, target); | 106 this->processPosText(command, target); |
| 93 } | 107 } |
| 94 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { | 108 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { |
| 95 this->processPoints(command, target); | 109 this->processPoints(command, target); |
| 96 } | 110 } |
| 111 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGE)) { |
| 112 this->processImage(command, target); |
| 113 } |
| 114 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGERECT)) { |
| 115 this->processImageRect(command, target); |
| 116 } |
| 117 else if (!strcmp(name, SKJSONCANVAS_COMMAND_BITMAP)) { |
| 118 this->processBitmap(command, target); |
| 119 } |
| 120 else if (!strcmp(name, SKJSONCANVAS_COMMAND_BITMAPRECT)) { |
| 121 this->processBitmapRect(command, target); |
| 122 } |
| 97 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRECT)) { | 123 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRECT)) { |
| 98 this->processClipRect(command, target); | 124 this->processClipRect(command, target); |
| 99 } | 125 } |
| 100 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRRECT)) { | 126 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRRECT)) { |
| 101 this->processClipRRect(command, target); | 127 this->processClipRRect(command, target); |
| 102 } | 128 } |
| 103 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPPATH)) { | 129 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPPATH)) { |
| 104 this->processClipPath(command, target); | 130 this->processClipPath(command, target); |
| 105 } | 131 } |
| 106 else { | 132 else { |
| 107 SkDebugf("unsupported JSON command: %s\n", name); | 133 SkDebugf("unsupported JSON command: %s\n", name); |
| 108 } | 134 } |
| 109 } | 135 } |
| 110 | 136 |
| 111 void Renderer::getPaint(Json::Value& command, SkPaint* result) { | 137 static void apply_paint_color(Json::Value& jsonPaint, SkPaint* target) { |
| 112 Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT]; | |
| 113 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_COLOR)) { | 138 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_COLOR)) { |
| 114 Json::Value color = jsonPaint[SKJSONCANVAS_ATTRIBUTE_COLOR]; | 139 Json::Value color = jsonPaint[SKJSONCANVAS_ATTRIBUTE_COLOR]; |
| 115 result->setColor(SkColorSetARGB(color[0].asInt(), color[1].asInt(), colo
r[2].asInt(), | 140 target->setColor(SkColorSetARGB(color[0].asInt(), color[1].asInt(), colo
r[2].asInt(), |
| 116 color[3].asInt())); | 141 color[3].asInt())); |
| 117 } | 142 } |
| 143 } |
| 144 |
| 145 // note that the caller is responsible for freeing the pointer |
| 146 static Json::ArrayIndex decode_data(Json::Value bytes, void** target) { |
| 147 Json::ArrayIndex size = bytes.size(); |
| 148 *target = sk_malloc_throw(size); |
| 149 for (Json::ArrayIndex i = 0; i < size; i++) { |
| 150 ((uint8_t*) *target)[i] = bytes[i].asInt(); |
| 151 } |
| 152 return size; |
| 153 } |
| 154 |
| 155 static SkFlattenable* load_flattenable(Json::Value jsonFlattenable) { |
| 156 if (!jsonFlattenable.isMember(SKJSONCANVAS_ATTRIBUTE_NAME)) { |
| 157 return nullptr; |
| 158 } |
| 159 const char* name = jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_NAME].asCString(); |
| 160 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(name); |
| 161 if (factory == nullptr) { |
| 162 return nullptr; |
| 163 } |
| 164 void* data; |
| 165 int size = decode_data(jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_BYTES], &data)
; |
| 166 SkValidatingReadBuffer buffer(data, size); |
| 167 SkFlattenable* result = factory(buffer); |
| 168 free(data); |
| 169 if (!buffer.isValid()) { |
| 170 return nullptr; |
| 171 } |
| 172 return result; |
| 173 } |
| 174 |
| 175 // caller is responsible for freeing return value |
| 176 static SkBitmap* load_bitmap(Json::Value jsonBitmap) { |
| 177 if (!jsonBitmap.isMember(SKJSONCANVAS_ATTRIBUTE_BYTES)) { |
| 178 return nullptr; |
| 179 } |
| 180 void* data; |
| 181 int size = decode_data(jsonBitmap[SKJSONCANVAS_ATTRIBUTE_BYTES], &data); |
| 182 SkMemoryStream stream(data, size); |
| 183 SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); |
| 184 SkBitmap* bitmap = new SkBitmap(); |
| 185 SkImageDecoder::Result result = decoder->decode(&stream, bitmap, |
| 186 SkImageDecoder::kDecodePixel
s_Mode); |
| 187 free(decoder); |
| 188 if (result != SkImageDecoder::kFailure) { |
| 189 free(data); |
| 190 return bitmap; |
| 191 } |
| 192 SkDebugf("image decode failed"); |
| 193 free(data); |
| 194 return nullptr; |
| 195 } |
| 196 |
| 197 static SkImage* load_image(Json::Value jsonImage) { |
| 198 SkBitmap* bitmap = load_bitmap(jsonImage); |
| 199 if (bitmap == nullptr) { |
| 200 return nullptr; |
| 201 } |
| 202 SkImage* result = SkImage::NewFromBitmap(*bitmap); |
| 203 free(bitmap); |
| 204 return result; |
| 205 } |
| 206 |
| 207 static void apply_paint_shader(Json::Value& jsonPaint, SkPaint* target) { |
| 208 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_SHADER)) { |
| 209 Json::Value jsonShader = jsonPaint[SKJSONCANVAS_ATTRIBUTE_SHADER]; |
| 210 SkShader* shader = (SkShader*) load_flattenable(jsonShader); |
| 211 if (shader != nullptr) { |
| 212 target->setShader(shader); |
| 213 shader->unref(); |
| 214 } |
| 215 } |
| 216 } |
| 217 |
| 218 static void apply_paint_patheffect(Json::Value& jsonPaint, SkPaint* target) { |
| 219 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_PATHEFFECT)) { |
| 220 Json::Value jsonPathEffect = jsonPaint[SKJSONCANVAS_ATTRIBUTE_PATHEFFECT
]; |
| 221 SkPathEffect* pathEffect = (SkPathEffect*) load_flattenable(jsonPathEffe
ct); |
| 222 if (pathEffect != nullptr) { |
| 223 target->setPathEffect(pathEffect); |
| 224 pathEffect->unref(); |
| 225 } |
| 226 } |
| 227 } |
| 228 |
| 229 static void apply_paint_maskfilter(Json::Value& jsonPaint, SkPaint* target) { |
| 230 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_MASKFILTER)) { |
| 231 Json::Value jsonMaskFilter = jsonPaint[SKJSONCANVAS_ATTRIBUTE_MASKFILTER
]; |
| 232 SkMaskFilter* maskFilter = (SkMaskFilter*) load_flattenable(jsonMaskFilt
er); |
| 233 if (maskFilter != nullptr) { |
| 234 target->setMaskFilter(maskFilter); |
| 235 maskFilter->unref(); |
| 236 } |
| 237 } |
| 238 } |
| 239 |
| 240 static void apply_paint_xfermode(Json::Value& jsonPaint, SkPaint* target) { |
| 241 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_XFERMODE)) { |
| 242 Json::Value jsonXfermode = jsonPaint[SKJSONCANVAS_ATTRIBUTE_XFERMODE]; |
| 243 SkXfermode* xfermode = (SkXfermode*) load_flattenable(jsonXfermode); |
| 244 if (xfermode != nullptr) { |
| 245 target->setXfermode(xfermode); |
| 246 xfermode->unref(); |
| 247 } |
| 248 } |
| 249 } |
| 250 |
| 251 static void apply_paint_style(Json::Value& jsonPaint, SkPaint* target) { |
| 118 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STYLE)) { | 252 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STYLE)) { |
| 119 const char* style = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); | 253 const char* style = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); |
| 120 if (!strcmp(style, SKJSONCANVAS_STYLE_FILL)) { | 254 if (!strcmp(style, SKJSONCANVAS_STYLE_FILL)) { |
| 121 result->setStyle(SkPaint::kFill_Style); | 255 target->setStyle(SkPaint::kFill_Style); |
| 122 } | 256 } |
| 123 else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKE)) { | 257 else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKE)) { |
| 124 result->setStyle(SkPaint::kStroke_Style); | 258 target->setStyle(SkPaint::kStroke_Style); |
| 125 } | 259 } |
| 126 else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKEANDFILL)) { | 260 else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKEANDFILL)) { |
| 127 result->setStyle(SkPaint::kStrokeAndFill_Style); | 261 target->setStyle(SkPaint::kStrokeAndFill_Style); |
| 128 } | 262 } |
| 129 } | 263 } |
| 264 } |
| 265 |
| 266 static void apply_paint_strokewidth(Json::Value& jsonPaint, SkPaint* target) { |
| 130 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH)) { | 267 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH)) { |
| 131 float strokeWidth = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH].asFloa
t(); | 268 float strokeWidth = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH].asFloa
t(); |
| 132 result->setStrokeWidth(strokeWidth); | 269 target->setStrokeWidth(strokeWidth); |
| 270 } |
| 271 } |
| 272 |
| 273 static void apply_paint_strokemiter(Json::Value& jsonPaint, SkPaint* target) { |
| 274 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STROKEMITER)) { |
| 275 float strokeMiter = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STROKEMITER].asFloa
t(); |
| 276 target->setStrokeMiter(strokeMiter); |
| 277 } |
| 278 } |
| 279 |
| 280 static void apply_paint_cap(Json::Value& jsonPaint, SkPaint* target) { |
| 281 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_CAP)) { |
| 282 const char* cap = jsonPaint[SKJSONCANVAS_ATTRIBUTE_CAP].asCString(); |
| 283 if (!strcmp(cap, SKJSONCANVAS_CAP_BUTT)) { |
| 284 target->setStrokeCap(SkPaint::kButt_Cap); |
| 285 } |
| 286 else if (!strcmp(cap, SKJSONCANVAS_CAP_ROUND)) { |
| 287 target->setStrokeCap(SkPaint::kRound_Cap); |
| 288 } |
| 289 else if (!strcmp(cap, SKJSONCANVAS_CAP_SQUARE)) { |
| 290 target->setStrokeCap(SkPaint::kSquare_Cap); |
| 291 } |
| 133 } | 292 } |
| 293 } |
| 294 |
| 295 static void apply_paint_antialias(Json::Value& jsonPaint, SkPaint* target) { |
| 134 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_ANTIALIAS)) { | 296 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_ANTIALIAS)) { |
| 135 result->setAntiAlias(jsonPaint[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool(
)); | 297 target->setAntiAlias(jsonPaint[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool(
)); |
| 136 } | 298 } |
| 299 } |
| 300 |
| 301 static void apply_paint_blur(Json::Value& jsonPaint, SkPaint* target) { |
| 137 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_BLUR)) { | 302 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_BLUR)) { |
| 138 Json::Value blur = jsonPaint[SKJSONCANVAS_ATTRIBUTE_BLUR]; | 303 Json::Value blur = jsonPaint[SKJSONCANVAS_ATTRIBUTE_BLUR]; |
| 139 SkScalar sigma = blur[SKJSONCANVAS_ATTRIBUTE_SIGMA].asFloat(); | 304 SkScalar sigma = blur[SKJSONCANVAS_ATTRIBUTE_SIGMA].asFloat(); |
| 140 SkBlurStyle style; | 305 SkBlurStyle style; |
| 141 const char* jsonStyle = blur[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); | 306 const char* jsonStyle = blur[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); |
| 142 if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_NORMAL)) { | 307 if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_NORMAL)) { |
| 143 style = SkBlurStyle::kNormal_SkBlurStyle; | 308 style = SkBlurStyle::kNormal_SkBlurStyle; |
| 144 } | 309 } |
| 145 else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_SOLID)) { | 310 else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_SOLID)) { |
| 146 style = SkBlurStyle::kSolid_SkBlurStyle; | 311 style = SkBlurStyle::kSolid_SkBlurStyle; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 160 if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_LOW)) { | 325 if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_LOW)) { |
| 161 flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; | 326 flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; |
| 162 } | 327 } |
| 163 else if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_HIGH)) { | 328 else if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_HIGH)) { |
| 164 flags = SkBlurMaskFilter::BlurFlags::kHighQuality_BlurFlag; | 329 flags = SkBlurMaskFilter::BlurFlags::kHighQuality_BlurFlag; |
| 165 } | 330 } |
| 166 else { | 331 else { |
| 167 SkASSERT(false); | 332 SkASSERT(false); |
| 168 flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; | 333 flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; |
| 169 } | 334 } |
| 170 result->setMaskFilter(SkBlurMaskFilter::Create(style, sigma, flags)); | 335 target->setMaskFilter(SkBlurMaskFilter::Create(style, sigma, flags)); |
| 171 } | 336 } |
| 337 } |
| 338 |
| 339 static void apply_paint_dashing(Json::Value& jsonPaint, SkPaint* target) { |
| 172 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_DASHING)) { | 340 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_DASHING)) { |
| 173 Json::Value dash = jsonPaint[SKJSONCANVAS_ATTRIBUTE_DASHING]; | 341 Json::Value dash = jsonPaint[SKJSONCANVAS_ATTRIBUTE_DASHING]; |
| 174 Json::Value jsonIntervals = dash[SKJSONCANVAS_ATTRIBUTE_INTERVALS]; | 342 Json::Value jsonIntervals = dash[SKJSONCANVAS_ATTRIBUTE_INTERVALS]; |
| 175 Json::ArrayIndex count = jsonIntervals.size(); | 343 Json::ArrayIndex count = jsonIntervals.size(); |
| 176 SkScalar* intervals = (SkScalar*) sk_malloc_throw(count * sizeof(SkScala
r)); | 344 SkScalar* intervals = (SkScalar*) sk_malloc_throw(count * sizeof(SkScala
r)); |
| 177 for (Json::ArrayIndex i = 0; i < count; i++) { | 345 for (Json::ArrayIndex i = 0; i < count; i++) { |
| 178 intervals[i] = jsonIntervals[i].asFloat(); | 346 intervals[i] = jsonIntervals[i].asFloat(); |
| 179 } | 347 } |
| 180 SkScalar phase = dash[SKJSONCANVAS_ATTRIBUTE_PHASE].asFloat(); | 348 SkScalar phase = dash[SKJSONCANVAS_ATTRIBUTE_PHASE].asFloat(); |
| 181 result->setPathEffect(SkDashPathEffect::Create(intervals, count, phase))
; | 349 target->setPathEffect(SkDashPathEffect::Create(intervals, count, phase))
; |
| 182 free(intervals); | 350 free(intervals); |
| 183 } | 351 } |
| 352 } |
| 353 |
| 354 static void apply_paint_textalign(Json::Value& jsonPaint, SkPaint* target) { |
| 184 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTALIGN)) { | 355 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTALIGN)) { |
| 185 SkPaint::Align textAlign; | 356 SkPaint::Align textAlign; |
| 186 const char* jsonAlign = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN].asCS
tring(); | 357 const char* jsonAlign = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN].asCS
tring(); |
| 187 if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_LEFT)) { | 358 if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_LEFT)) { |
| 188 textAlign = SkPaint::kLeft_Align; | 359 textAlign = SkPaint::kLeft_Align; |
| 189 } | 360 } |
| 190 else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_CENTER)) { | 361 else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_CENTER)) { |
| 191 textAlign = SkPaint::kCenter_Align; | 362 textAlign = SkPaint::kCenter_Align; |
| 192 } | 363 } |
| 193 else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_RIGHT)) { | 364 else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_RIGHT)) { |
| 194 textAlign = SkPaint::kRight_Align; | 365 textAlign = SkPaint::kRight_Align; |
| 195 } | 366 } |
| 196 else { | 367 else { |
| 197 SkASSERT(false); | 368 SkASSERT(false); |
| 198 textAlign = SkPaint::kLeft_Align; | 369 textAlign = SkPaint::kLeft_Align; |
| 199 } | 370 } |
| 200 result->setTextAlign(textAlign); | 371 target->setTextAlign(textAlign); |
| 201 } | 372 } |
| 373 } |
| 374 |
| 375 static void apply_paint_textsize(Json::Value& jsonPaint, SkPaint* target) { |
| 202 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSIZE)) { | 376 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSIZE)) { |
| 203 float textSize = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSIZE].asFloat(); | 377 float textSize = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSIZE].asFloat(); |
| 204 result->setTextSize(textSize); | 378 target->setTextSize(textSize); |
| 205 } | 379 } |
| 380 } |
| 381 |
| 382 static void apply_paint_textscalex(Json::Value& jsonPaint, SkPaint* target) { |
| 206 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX)) { | 383 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX)) { |
| 207 float textScaleX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat(
); | 384 float textScaleX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat(
); |
| 208 result->setTextScaleX(textScaleX); | 385 target->setTextScaleX(textScaleX); |
| 209 } | 386 } |
| 387 } |
| 388 |
| 389 static void apply_paint_textskewx(Json::Value& jsonPaint, SkPaint* target) { |
| 210 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) { | 390 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) { |
| 211 float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat(); | 391 float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat(); |
| 212 result->setTextSkewX(textSkewX); | 392 target->setTextSkewX(textSkewX); |
| 213 } | 393 } |
| 214 } | 394 } |
| 215 | 395 |
| 396 void Renderer::getPaint(Json::Value& command, SkPaint* result) { |
| 397 Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT]; |
| 398 apply_paint_color(jsonPaint, result); |
| 399 apply_paint_shader(jsonPaint, result); |
| 400 apply_paint_patheffect(jsonPaint, result); |
| 401 apply_paint_maskfilter(jsonPaint, result); |
| 402 apply_paint_xfermode(jsonPaint, result); |
| 403 apply_paint_style(jsonPaint, result); |
| 404 apply_paint_strokewidth(jsonPaint, result); |
| 405 apply_paint_strokemiter(jsonPaint, result); |
| 406 apply_paint_cap(jsonPaint, result); |
| 407 apply_paint_antialias(jsonPaint, result); |
| 408 apply_paint_blur(jsonPaint, result); |
| 409 apply_paint_dashing(jsonPaint, result); |
| 410 apply_paint_textalign(jsonPaint, result); |
| 411 apply_paint_textsize(jsonPaint, result); |
| 412 apply_paint_textscalex(jsonPaint, result); |
| 413 apply_paint_textskewx(jsonPaint, result); |
| 414 } |
| 415 |
| 216 void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) { | 416 void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) { |
| 217 Json::Value rect = command[name]; | 417 Json::Value rect = command[name]; |
| 218 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3]
.asFloat()); | 418 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3]
.asFloat()); |
| 219 } | 419 } |
| 220 | 420 |
| 221 void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result)
{ | 421 void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result)
{ |
| 222 Json::Value rrect = command[name]; | 422 Json::Value rrect = command[name]; |
| 223 SkVector radii[4] = { | 423 SkVector radii[4] = { |
| 224 { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, | 424 { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, |
| 225 { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, | 425 { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 } | 523 } |
| 324 | 524 |
| 325 void Renderer::processSave(Json::Value& command, SkCanvas* target) { | 525 void Renderer::processSave(Json::Value& command, SkCanvas* target) { |
| 326 target->save(); | 526 target->save(); |
| 327 } | 527 } |
| 328 | 528 |
| 329 void Renderer::processRestore(Json::Value& command, SkCanvas* target) { | 529 void Renderer::processRestore(Json::Value& command, SkCanvas* target) { |
| 330 target->restore(); | 530 target->restore(); |
| 331 } | 531 } |
| 332 | 532 |
| 533 void Renderer::processSaveLayer(Json::Value& command, SkCanvas* target) { |
| 534 SkCanvas::SaveLayerRec rec; |
| 535 SkRect bounds; |
| 536 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BOUNDS)) { |
| 537 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_BOUNDS, &bounds); |
| 538 rec.fBounds = &bounds; |
| 539 } |
| 540 SkPaint paint; |
| 541 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 542 this->getPaint(command, &paint); |
| 543 rec.fPaint = &paint; |
| 544 } |
| 545 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BACKDROP)) { |
| 546 rec.fBackdrop = (SkImageFilter*) load_flattenable(command[SKJSONCANVAS_A
TTRIBUTE_BACKDROP]); |
| 547 } |
| 548 target->saveLayer(rec); |
| 549 if (rec.fBackdrop != nullptr) { |
| 550 rec.fBackdrop->unref(); |
| 551 } |
| 552 } |
| 553 |
| 333 void Renderer::processPaint(Json::Value& command, SkCanvas* target) { | 554 void Renderer::processPaint(Json::Value& command, SkCanvas* target) { |
| 334 SkPaint paint; | 555 SkPaint paint; |
| 335 this->getPaint(command, &paint); | 556 this->getPaint(command, &paint); |
| 336 target->drawPaint(paint); | 557 target->drawPaint(paint); |
| 337 } | 558 } |
| 338 | 559 |
| 339 void Renderer::processRect(Json::Value& command, SkCanvas* target) { | 560 void Renderer::processRect(Json::Value& command, SkCanvas* target) { |
| 340 SkRect rect; | 561 SkRect rect; |
| 341 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); | 562 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); |
| 342 SkPaint paint; | 563 SkPaint paint; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); | 654 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
| 434 } | 655 } |
| 435 | 656 |
| 436 void Renderer::processClipPath(Json::Value& command, SkCanvas* target) { | 657 void Renderer::processClipPath(Json::Value& command, SkCanvas* target) { |
| 437 SkPath path; | 658 SkPath path; |
| 438 this->getPath(command, &path); | 659 this->getPath(command, &path); |
| 439 target->clipPath(path, this->getRegionOp(command), | 660 target->clipPath(path, this->getRegionOp(command), |
| 440 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); | 661 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
| 441 } | 662 } |
| 442 | 663 |
| 664 void Renderer::processImage(Json::Value& command, SkCanvas* target) { |
| 665 SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_IMAGE]); |
| 666 if (image == nullptr) { |
| 667 return; |
| 668 } |
| 669 Json::Value point = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
| 670 SkPaint* paintPtr; |
| 671 SkPaint paint; |
| 672 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 673 this->getPaint(command, &paint); |
| 674 paintPtr = &paint; |
| 675 } |
| 676 else { |
| 677 paintPtr = nullptr; |
| 678 } |
| 679 target->drawImage(image, point[0].asFloat(), point[1].asFloat(), paintPtr); |
| 680 image->unref(); |
| 681 } |
| 682 |
| 683 void Renderer::processImageRect(Json::Value& command, SkCanvas* target) { |
| 684 SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_IMAGE]); |
| 685 if (image == nullptr) { |
| 686 return; |
| 687 } |
| 688 SkRect dst; |
| 689 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_DST, &dst); |
| 690 SkPaint* paintPtr; |
| 691 SkPaint paint; |
| 692 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 693 this->getPaint(command, &paint); |
| 694 paintPtr = &paint; |
| 695 } |
| 696 else { |
| 697 paintPtr = nullptr; |
| 698 } |
| 699 SkCanvas::SrcRectConstraint constraint; |
| 700 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_STRICT) && |
| 701 command[SKJSONCANVAS_ATTRIBUTE_STRICT].asBool()) { |
| 702 constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 703 } |
| 704 else { |
| 705 constraint = SkCanvas::kFast_SrcRectConstraint; |
| 706 } |
| 707 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) { |
| 708 SkRect src; |
| 709 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_SRC, &src); |
| 710 target->drawImageRect(image, src, dst, paintPtr, constraint); |
| 711 } |
| 712 else { |
| 713 target->drawImageRect(image, dst, paintPtr, constraint); |
| 714 } |
| 715 image->unref(); |
| 716 } |
| 717 |
| 718 void Renderer::processBitmap(Json::Value& command, SkCanvas* target) { |
| 719 SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_BITMAP]); |
| 720 if (image == nullptr) { |
| 721 return; |
| 722 } |
| 723 Json::Value point = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
| 724 SkPaint* paintPtr; |
| 725 SkPaint paint; |
| 726 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 727 this->getPaint(command, &paint); |
| 728 paintPtr = &paint; |
| 729 } |
| 730 else { |
| 731 paintPtr = nullptr; |
| 732 } |
| 733 target->drawImage(image, point[0].asFloat(), point[1].asFloat(), paintPtr); |
| 734 image->unref(); |
| 735 } |
| 736 |
| 737 void Renderer::processBitmapRect(Json::Value& command, SkCanvas* target) { |
| 738 SkBitmap* bitmap = load_bitmap(command[SKJSONCANVAS_ATTRIBUTE_BITMAP]); |
| 739 if (bitmap == nullptr) { |
| 740 return; |
| 741 } |
| 742 SkRect dst; |
| 743 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_DST, &dst); |
| 744 SkPaint* paintPtr; |
| 745 SkPaint paint; |
| 746 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 747 this->getPaint(command, &paint); |
| 748 paintPtr = &paint; |
| 749 } |
| 750 else { |
| 751 paintPtr = nullptr; |
| 752 } |
| 753 SkCanvas::SrcRectConstraint constraint; |
| 754 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_STRICT) && |
| 755 command[SKJSONCANVAS_ATTRIBUTE_STRICT].asBool()) { |
| 756 constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 757 } |
| 758 else { |
| 759 constraint = SkCanvas::kFast_SrcRectConstraint; |
| 760 } |
| 761 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) { |
| 762 SkRect src; |
| 763 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_SRC, &src); |
| 764 target->drawBitmapRect(*bitmap, src, dst, paintPtr, constraint); |
| 765 } |
| 766 else { |
| 767 target->drawBitmapRect(*bitmap, dst, paintPtr, constraint); |
| 768 } |
| 769 free(bitmap); |
| 770 } |
| 771 |
| 443 void render(const char* json, SkCanvas* target) { | 772 void render(const char* json, SkCanvas* target) { |
| 444 Renderer renderer; | 773 Renderer renderer; |
| 445 Json::Reader reader; | 774 Json::Reader reader; |
| 446 Json::Value root; | 775 Json::Value root; |
| 447 if (reader.parse(std::string(json), root)) { | 776 if (reader.parse(std::string(json), root)) { |
| 448 SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1); | 777 SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1); |
| 449 Json::Value commands = root[SKJSONCANVAS_COMMANDS]; | 778 Json::Value commands = root[SKJSONCANVAS_COMMANDS]; |
| 450 for (Json::ArrayIndex i = 0; i < commands.size(); i++) { | 779 for (Json::ArrayIndex i = 0; i < commands.size(); i++) { |
| 451 renderer.processCommand(commands[i], target); | 780 renderer.processCommand(commands[i], target); |
| 452 } | 781 } |
| 453 } | 782 } |
| 454 else { | 783 else { |
| 455 SkDebugf(json); | 784 SkDebugf(json); |
| 456 SkFAIL("json parse failure"); | 785 SkFAIL("json parse failure"); |
| 457 } | 786 } |
| 458 } | 787 } |
| 459 | 788 |
| 460 } // namespace | 789 } // namespace |
| OLD | NEW |