blob: a529b5b40fb06d0363df259221728568ef4b8832 [file] [log] [blame]
chudy@google.com902ebe52012-06-29 14:21:22 +00001/*
2 * Copyright 2012 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#include "SkObjectParser.h"
humper@google.comf515ffc2013-01-07 15:48:19 +00009#include "SkData.h"
10#include "SkFontDescriptor.h"
robertphillips80af6452015-08-24 08:27:38 -070011#include "SkImage.h"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkPath.h"
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000013#include "SkRRect.h"
14#include "SkShader.h"
15#include "SkStream.h"
16#include "SkStringUtils.h"
17#include "SkTypeface.h"
bungeman@google.com428fc4a2013-03-07 20:30:32 +000018#include "SkUtils.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050019#include "SkClipOpPriv.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000020
21/* TODO(chudy): Replace all std::strings with char */
22
chudy@google.com97cee972012-08-07 20:41:37 +000023SkString* SkObjectParser::BitmapToString(const SkBitmap& bitmap) {
robertphillips@google.com195952f2012-10-23 12:13:35 +000024 SkString* mBitmap = new SkString("SkBitmap: ");
25 mBitmap->append("W: ");
26 mBitmap->appendS32(bitmap.width());
27 mBitmap->append(" H: ");
28 mBitmap->appendS32(bitmap.height());
halcanary9d524f22016-03-29 09:03:52 -070029
Brian Salomone41e1762018-01-25 14:07:47 -050030 const char* ctString = "<unknown>";
31 switch (bitmap.colorType()) {
32 case kUnknown_SkColorType: ctString = "None"; break;
33 case kAlpha_8_SkColorType: ctString = "A8"; break;
34 case kRGB_565_SkColorType: ctString = "565"; break;
35 case kARGB_4444_SkColorType: ctString = "4444"; break;
36 case kRGBA_8888_SkColorType: ctString = "RGBA"; break;
37 case kRGB_888x_SkColorType: ctString = "RGB"; break;
38 case kBGRA_8888_SkColorType: ctString = "BGRA"; break;
39 case kRGBA_1010102_SkColorType: ctString = "1010102"; break;
40 case kRGB_101010x_SkColorType: ctString = "101010x"; break;
41 case kGray_8_SkColorType: ctString = "G8"; break;
42 case kRGBA_F16_SkColorType: ctString = "RGBAf16"; break;
43 }
reedc77392e2014-06-02 13:07:26 -070044 mBitmap->append(" ColorType: ");
Brian Salomone41e1762018-01-25 14:07:47 -050045 mBitmap->append(ctString);
robertphillips@google.com195952f2012-10-23 12:13:35 +000046
47 if (bitmap.isOpaque()) {
48 mBitmap->append(" opaque");
49 } else {
50 mBitmap->append(" not-opaque");
51 }
52
53 if (bitmap.isImmutable()) {
54 mBitmap->append(" immutable");
55 } else {
56 mBitmap->append(" not-immutable");
57 }
58
59 if (bitmap.isVolatile()) {
60 mBitmap->append(" volatile");
61 } else {
62 mBitmap->append(" not-volatile");
63 }
64
65 mBitmap->append(" genID: ");
66 mBitmap->appendS32(bitmap.getGenerationID());
67
chudy@google.com902ebe52012-06-29 14:21:22 +000068 return mBitmap;
69}
70
robertphillips80af6452015-08-24 08:27:38 -070071SkString* SkObjectParser::ImageToString(const SkImage* image) {
72 SkString* str = new SkString("SkImage: ");
73 if (!image) {
74 return str;
75 }
76
77 str->append("W: ");
78 str->appendS32(image->width());
79 str->append(" H: ");
80 str->appendS32(image->height());
81
82 if (image->isOpaque()) {
83 str->append(" opaque");
84 } else {
85 str->append(" not-opaque");
86 }
87
88 str->append(" uniqueID: ");
89 str->appendS32(image->uniqueID());
90
91 return str;
92}
93
chudy@google.com97cee972012-08-07 20:41:37 +000094SkString* SkObjectParser::BoolToString(bool doAA) {
95 SkString* mBool = new SkString("Bool doAA: ");
chudy@google.com902ebe52012-06-29 14:21:22 +000096 if (doAA) {
chudy@google.com97cee972012-08-07 20:41:37 +000097 mBool->append("True");
chudy@google.com902ebe52012-06-29 14:21:22 +000098 } else {
chudy@google.com97cee972012-08-07 20:41:37 +000099 mBool->append("False");
chudy@google.com902ebe52012-06-29 14:21:22 +0000100 }
chudy@google.com97cee972012-08-07 20:41:37 +0000101 return mBool;
chudy@google.com902ebe52012-06-29 14:21:22 +0000102}
103
chudy@google.com97cee972012-08-07 20:41:37 +0000104SkString* SkObjectParser::CustomTextToString(const char* text) {
105 SkString* mText = new SkString(text);
106 return mText;
chudy@google.com902ebe52012-06-29 14:21:22 +0000107}
108
chudy@google.com97cee972012-08-07 20:41:37 +0000109SkString* SkObjectParser::IntToString(int x, const char* text) {
110 SkString* mInt = new SkString(text);
111 mInt->append(" ");
112 mInt->appendScalar(SkIntToScalar(x));
113 return mInt;
chudy@google.com902ebe52012-06-29 14:21:22 +0000114}
115
chudy@google.com97cee972012-08-07 20:41:37 +0000116SkString* SkObjectParser::IRectToString(const SkIRect& rect) {
117 SkString* mRect = new SkString("SkIRect: ");
118 mRect->append("L: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +0000119 mRect->appendS32(rect.left());
chudy@google.com97cee972012-08-07 20:41:37 +0000120 mRect->append(", T: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +0000121 mRect->appendS32(rect.top());
chudy@google.com97cee972012-08-07 20:41:37 +0000122 mRect->append(", R: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +0000123 mRect->appendS32(rect.right());
chudy@google.com97cee972012-08-07 20:41:37 +0000124 mRect->append(", B: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +0000125 mRect->appendS32(rect.bottom());
chudy@google.com902ebe52012-06-29 14:21:22 +0000126 return mRect;
127}
128
chudy@google.com97cee972012-08-07 20:41:37 +0000129SkString* SkObjectParser::MatrixToString(const SkMatrix& matrix) {
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000130 SkString* str = new SkString("SkMatrix: ");
131 matrix.toString(str);
132 return str;
chudy@google.com97cee972012-08-07 20:41:37 +0000133}
chudy@google.com902ebe52012-06-29 14:21:22 +0000134
robertphillips@google.com735edb02013-01-10 18:54:11 +0000135SkString* SkObjectParser::PaintToString(const SkPaint& paint) {
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000136 SkString* str = new SkString;
137 paint.toString(str);
138 return str;
chudy@google.com97cee972012-08-07 20:41:37 +0000139}
140
141SkString* SkObjectParser::PathToString(const SkPath& path) {
robertphillipsfe8d5f62015-10-29 09:45:27 -0700142 SkString* mPath = new SkString;
halcanary9d524f22016-03-29 09:03:52 -0700143
robertphillipsfe8d5f62015-10-29 09:45:27 -0700144 mPath->appendf("Path (%d) (", path.getGenerationID());
robertphillips@google.com87201762012-10-18 13:30:18 +0000145
robertphillips@google.com51185fe2012-12-05 19:34:33 +0000146 static const char* gFillStrings[] = {
147 "Winding", "EvenOdd", "InverseWinding", "InverseEvenOdd"
148 };
149
150 mPath->append(gFillStrings[path.getFillType()]);
151 mPath->append(", ");
152
robertphillips@google.com87201762012-10-18 13:30:18 +0000153 static const char* gConvexityStrings[] = {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000154 "Unknown", "Convex", "Concave"
robertphillips@google.com87201762012-10-18 13:30:18 +0000155 };
156 SkASSERT(SkPath::kConcave_Convexity == 2);
157
158 mPath->append(gConvexityStrings[path.getConvexity()]);
159 mPath->append(", ");
160
halcanary96fcdcc2015-08-27 07:41:13 -0700161 if (path.isRect(nullptr)) {
robertphillips@google.com195952f2012-10-23 12:13:35 +0000162 mPath->append("isRect, ");
163 } else {
164 mPath->append("isNotRect, ");
165 }
166
robertphillips3d2e6542015-11-24 06:34:50 -0800167 if (path.isOval(nullptr)) {
168 mPath->append("isOval, ");
169 } else {
170 mPath->append("isNotOval, ");
171 }
172
173 SkRRect rrect;
174 if (path.isRRect(&rrect)) {
175 mPath->append("isRRect, ");
176 } else {
177 mPath->append("isNotRRect, ");
178 }
179
robertphillips@google.com87201762012-10-18 13:30:18 +0000180 mPath->appendS32(path.countVerbs());
181 mPath->append("V, ");
182 mPath->appendS32(path.countPoints());
183 mPath->append("P): ");
184
185 static const char* gVerbStrings[] = {
commit-bot@chromium.org4a3ca942013-06-04 21:51:06 +0000186 "Move", "Line", "Quad", "Conic", "Cubic", "Close", "Done"
robertphillips@google.com87201762012-10-18 13:30:18 +0000187 };
commit-bot@chromium.org4a3ca942013-06-04 21:51:06 +0000188 static const int gPtsPerVerb[] = { 1, 1, 2, 2, 3, 0, 0 };
189 static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 1, 0, 0 };
190 SkASSERT(SkPath::kDone_Verb == 6);
robertphillips@google.com87201762012-10-18 13:30:18 +0000191
192 SkPath::Iter iter(const_cast<SkPath&>(path), false);
193 SkPath::Verb verb;
194 SkPoint points[4];
195
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000196 for(verb = iter.next(points, false);
197 verb != SkPath::kDone_Verb;
robertphillips@google.com87201762012-10-18 13:30:18 +0000198 verb = iter.next(points, false)) {
199
200 mPath->append(gVerbStrings[verb]);
201 mPath->append(" ");
202
203 for (int i = 0; i < gPtsPerVerb[verb]; ++i) {
204 mPath->append("(");
205 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fX);
206 mPath->append(", ");
207 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fY);
commit-bot@chromium.org4a3ca942013-06-04 21:51:06 +0000208 mPath->append(")");
robertphillips@google.com87201762012-10-18 13:30:18 +0000209 }
commit-bot@chromium.org4a3ca942013-06-04 21:51:06 +0000210
211 if (SkPath::kConic_Verb == verb) {
212 mPath->append("(");
213 mPath->appendScalar(iter.conicWeight());
214 mPath->append(")");
215 }
216
217 mPath->append(" ");
chudy@google.com97cee972012-08-07 20:41:37 +0000218 }
robertphillips@google.com87201762012-10-18 13:30:18 +0000219
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000220 SkString* boundStr = SkObjectParser::RectToString(path.getBounds(), " Bound: ");
221
bsalomon49f085d2014-09-05 13:34:00 -0700222 if (boundStr) {
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000223 mPath->append(*boundStr);
halcanary385fe4d2015-08-26 13:07:48 -0700224 delete boundStr;
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000225 }
226
chudy@google.com97cee972012-08-07 20:41:37 +0000227 return mPath;
228}
229
230SkString* SkObjectParser::PointsToString(const SkPoint pts[], size_t count) {
231 SkString* mPoints = new SkString("SkPoints pts[]: ");
232 for (unsigned int i = 0; i < count; i++) {
233 mPoints->append("(");
234 mPoints->appendScalar(pts[i].fX);
235 mPoints->append(",");
236 mPoints->appendScalar(pts[i].fY);
237 mPoints->append(")");
238 }
239 return mPoints;
240}
241
242SkString* SkObjectParser::PointModeToString(SkCanvas::PointMode mode) {
243 SkString* mMode = new SkString("SkCanvas::PointMode: ");
244 if (mode == SkCanvas::kPoints_PointMode) {
245 mMode->append("kPoints_PointMode");
246 } else if (mode == SkCanvas::kLines_PointMode) {
247 mMode->append("kLines_Mode");
248 } else if (mode == SkCanvas::kPolygon_PointMode) {
249 mMode->append("kPolygon_PointMode");
250 }
251 return mMode;
252}
253
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000254SkString* SkObjectParser::RectToString(const SkRect& rect, const char* title) {
255
256 SkString* mRect = new SkString;
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000257
halcanary96fcdcc2015-08-27 07:41:13 -0700258 if (nullptr == title) {
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000259 mRect->append("SkRect: ");
260 } else {
261 mRect->append(title);
262 }
chudy@google.com97cee972012-08-07 20:41:37 +0000263 mRect->append("(");
264 mRect->appendScalar(rect.left());
265 mRect->append(", ");
266 mRect->appendScalar(rect.top());
267 mRect->append(", ");
268 mRect->appendScalar(rect.right());
269 mRect->append(", ");
270 mRect->appendScalar(rect.bottom());
271 mRect->append(")");
272 return mRect;
273}
274
robertphillips@google.com67baba42013-01-02 20:20:31 +0000275SkString* SkObjectParser::RRectToString(const SkRRect& rrect, const char* title) {
276
277 SkString* mRRect = new SkString;
278
halcanary96fcdcc2015-08-27 07:41:13 -0700279 if (nullptr == title) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000280 mRRect->append("SkRRect (");
281 if (rrect.isEmpty()) {
282 mRRect->append("empty");
283 } else if (rrect.isRect()) {
284 mRRect->append("rect");
285 } else if (rrect.isOval()) {
286 mRRect->append("oval");
287 } else if (rrect.isSimple()) {
288 mRRect->append("simple");
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +0000289 } else if (rrect.isNinePatch()) {
290 mRRect->append("nine-patch");
robertphillips@google.com67baba42013-01-02 20:20:31 +0000291 } else {
292 SkASSERT(rrect.isComplex());
293 mRRect->append("complex");
294 }
295 mRRect->append("): ");
296 } else {
297 mRRect->append(title);
298 }
299 mRRect->append("(");
300 mRRect->appendScalar(rrect.rect().left());
301 mRRect->append(", ");
302 mRRect->appendScalar(rrect.rect().top());
303 mRRect->append(", ");
304 mRRect->appendScalar(rrect.rect().right());
305 mRRect->append(", ");
306 mRRect->appendScalar(rrect.rect().bottom());
307 mRRect->append(") radii: (");
308 for (int i = 0; i < 4; ++i) {
309 const SkVector& radii = rrect.radii((SkRRect::Corner) i);
310 mRRect->appendScalar(radii.fX);
311 mRRect->append(", ");
312 mRRect->appendScalar(radii.fY);
313 if (i < 3) {
314 mRRect->append(", ");
315 }
316 }
317 mRRect->append(")");
318 return mRRect;
319}
320
Mike Reedc1f77742016-12-09 09:00:50 -0500321SkString* SkObjectParser::ClipOpToString(SkClipOp op) {
chudy@google.com97cee972012-08-07 20:41:37 +0000322 SkString* mOp = new SkString("SkRegion::Op: ");
Mike Reedc1f77742016-12-09 09:00:50 -0500323 if (op == kDifference_SkClipOp) {
chudy@google.com97cee972012-08-07 20:41:37 +0000324 mOp->append("kDifference_Op");
Mike Reedc1f77742016-12-09 09:00:50 -0500325 } else if (op == kIntersect_SkClipOp) {
chudy@google.com97cee972012-08-07 20:41:37 +0000326 mOp->append("kIntersect_Op");
Mike Reedc1f77742016-12-09 09:00:50 -0500327 } else if (op == kUnion_SkClipOp) {
chudy@google.com97cee972012-08-07 20:41:37 +0000328 mOp->append("kUnion_Op");
Mike Reedc1f77742016-12-09 09:00:50 -0500329 } else if (op == kXOR_SkClipOp) {
chudy@google.com97cee972012-08-07 20:41:37 +0000330 mOp->append("kXOR_Op");
Mike Reedc1f77742016-12-09 09:00:50 -0500331 } else if (op == kReverseDifference_SkClipOp) {
chudy@google.com97cee972012-08-07 20:41:37 +0000332 mOp->append("kReverseDifference_Op");
Mike Reedc1f77742016-12-09 09:00:50 -0500333 } else if (op == kReplace_SkClipOp) {
chudy@google.com97cee972012-08-07 20:41:37 +0000334 mOp->append("kReplace_Op");
335 } else {
336 mOp->append("Unknown Type");
337 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000338 return mOp;
339}
340
chudy@google.com97cee972012-08-07 20:41:37 +0000341SkString* SkObjectParser::RegionToString(const SkRegion& region) {
342 SkString* mRegion = new SkString("SkRegion: Data unavailable.");
343 return mRegion;
chudy@google.com902ebe52012-06-29 14:21:22 +0000344}
345
reed4960eee2015-12-18 07:09:18 -0800346SkString* SkObjectParser::SaveLayerFlagsToString(SkCanvas::SaveLayerFlags saveLayerFlags) {
chudy@google.com97cee972012-08-07 20:41:37 +0000347 SkString* mFlags = new SkString("SkCanvas::SaveFlags: ");
reed4960eee2015-12-18 07:09:18 -0800348 if (saveLayerFlags & SkCanvas::kPreserveLCDText_SaveLayerFlag) {
349 mFlags->append("kPreserveLCDText_SaveLayerFlag ");
chudy@google.com902ebe52012-06-29 14:21:22 +0000350 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000351 return mFlags;
352}
353
chudy@google.com97cee972012-08-07 20:41:37 +0000354SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) {
355 SkString* mScalar = new SkString(text);
356 mScalar->append(" ");
357 mScalar->appendScalar(x);
chudy@google.com902ebe52012-06-29 14:21:22 +0000358 return mScalar;
359}
360
bungeman@google.com428fc4a2013-03-07 20:30:32 +0000361SkString* SkObjectParser::TextToString(const void* text, size_t byteLength,
362 SkPaint::TextEncoding encoding) {
363
364 SkString* decodedText = new SkString();
365 switch (encoding) {
366 case SkPaint::kUTF8_TextEncoding: {
367 decodedText->append("UTF-8: ");
368 decodedText->append((const char*)text, byteLength);
369 break;
370 }
371 case SkPaint::kUTF16_TextEncoding: {
372 decodedText->append("UTF-16: ");
skia.committer@gmail.comf61ebc02013-11-22 07:02:24 +0000373 size_t sizeNeeded = SkUTF16_ToUTF8((uint16_t*)text,
374 SkToS32(byteLength / 2),
halcanary96fcdcc2015-08-27 07:41:13 -0700375 nullptr);
scroggo@google.comb9050d72013-08-26 21:20:04 +0000376 SkAutoSTMalloc<0x100, char> utf8(sizeNeeded);
robertphillips@google.coma4662862013-11-21 14:24:16 +0000377 SkUTF16_ToUTF8((uint16_t*)text, SkToS32(byteLength / 2), utf8);
bungeman@google.com428fc4a2013-03-07 20:30:32 +0000378 decodedText->append(utf8, sizeNeeded);
bungeman@google.com428fc4a2013-03-07 20:30:32 +0000379 break;
380 }
381 case SkPaint::kUTF32_TextEncoding: {
382 decodedText->append("UTF-32: ");
383 const SkUnichar* begin = (const SkUnichar*)text;
384 const SkUnichar* end = (const SkUnichar*)((const char*)text + byteLength);
385 for (const SkUnichar* unichar = begin; unichar < end; ++unichar) {
386 decodedText->appendUnichar(*unichar);
387 }
388 break;
389 }
390 case SkPaint::kGlyphID_TextEncoding: {
391 decodedText->append("GlyphID: ");
392 const uint16_t* begin = (const uint16_t*)text;
393 const uint16_t* end = (const uint16_t*)((const char*)text + byteLength);
394 for (const uint16_t* glyph = begin; glyph < end; ++glyph) {
395 decodedText->append("0x");
396 decodedText->appendHex(*glyph);
397 decodedText->append(" ");
398 }
399 break;
400 }
401 default:
402 decodedText->append("Unknown text encoding.");
403 break;
404 }
405
406 return decodedText;
chudy@google.com902ebe52012-06-29 14:21:22 +0000407}
Stan Ilievac42aeb2017-01-12 16:20:50 -0500408
409SkString* SkObjectParser::LatticeToString(const SkCanvas::Lattice& lattice) {
410 SkString* mLattice = new SkString;
411 mLattice->append("Lattice: ");
412 mLattice->append("(X: ");
413 mLattice->appendS32(lattice.fXCount);
414 mLattice->append(", Y:");
415 mLattice->appendS32(lattice.fYCount);
416 mLattice->append(", Bounds:");
417 if (nullptr != lattice.fBounds) {
418 mLattice->append(*IRectToString(*lattice.fBounds));
419 } else {
420 mLattice->append("null");
421 }
422 mLattice->append(")");
423 return mLattice;
424}