1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/utils/debugger/SkObjectParser.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,372 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2012 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 +#include "SkObjectParser.h" 1.13 +#include "SkData.h" 1.14 +#include "SkFontDescriptor.h" 1.15 +#include "SkRRect.h" 1.16 +#include "SkShader.h" 1.17 +#include "SkStream.h" 1.18 +#include "SkStringUtils.h" 1.19 +#include "SkTypeface.h" 1.20 +#include "SkUtils.h" 1.21 + 1.22 +/* TODO(chudy): Replace all std::strings with char */ 1.23 + 1.24 +SkString* SkObjectParser::BitmapToString(const SkBitmap& bitmap) { 1.25 + SkString* mBitmap = new SkString("SkBitmap: "); 1.26 + mBitmap->append("W: "); 1.27 + mBitmap->appendS32(bitmap.width()); 1.28 + mBitmap->append(" H: "); 1.29 + mBitmap->appendS32(bitmap.height()); 1.30 + 1.31 + const char* gConfigStrings[] = { 1.32 + "None", "A8", "Index8", "RGB565", "ARGB4444", "ARGB8888" 1.33 + }; 1.34 + SkASSERT(SkBitmap::kConfigCount == SK_ARRAY_COUNT(gConfigStrings)); 1.35 + 1.36 + mBitmap->append(" Config: "); 1.37 + mBitmap->append(gConfigStrings[bitmap.config()]); 1.38 + 1.39 + if (bitmap.isOpaque()) { 1.40 + mBitmap->append(" opaque"); 1.41 + } else { 1.42 + mBitmap->append(" not-opaque"); 1.43 + } 1.44 + 1.45 + if (bitmap.isImmutable()) { 1.46 + mBitmap->append(" immutable"); 1.47 + } else { 1.48 + mBitmap->append(" not-immutable"); 1.49 + } 1.50 + 1.51 + if (bitmap.isVolatile()) { 1.52 + mBitmap->append(" volatile"); 1.53 + } else { 1.54 + mBitmap->append(" not-volatile"); 1.55 + } 1.56 + 1.57 + mBitmap->append(" genID: "); 1.58 + mBitmap->appendS32(bitmap.getGenerationID()); 1.59 + 1.60 + return mBitmap; 1.61 +} 1.62 + 1.63 +SkString* SkObjectParser::BoolToString(bool doAA) { 1.64 + SkString* mBool = new SkString("Bool doAA: "); 1.65 + if (doAA) { 1.66 + mBool->append("True"); 1.67 + } else { 1.68 + mBool->append("False"); 1.69 + } 1.70 + return mBool; 1.71 +} 1.72 + 1.73 +SkString* SkObjectParser::CustomTextToString(const char* text) { 1.74 + SkString* mText = new SkString(text); 1.75 + return mText; 1.76 +} 1.77 + 1.78 +SkString* SkObjectParser::IntToString(int x, const char* text) { 1.79 + SkString* mInt = new SkString(text); 1.80 + mInt->append(" "); 1.81 + mInt->appendScalar(SkIntToScalar(x)); 1.82 + return mInt; 1.83 +} 1.84 + 1.85 +SkString* SkObjectParser::IRectToString(const SkIRect& rect) { 1.86 + SkString* mRect = new SkString("SkIRect: "); 1.87 + mRect->append("L: "); 1.88 + mRect->appendS32(rect.left()); 1.89 + mRect->append(", T: "); 1.90 + mRect->appendS32(rect.top()); 1.91 + mRect->append(", R: "); 1.92 + mRect->appendS32(rect.right()); 1.93 + mRect->append(", B: "); 1.94 + mRect->appendS32(rect.bottom()); 1.95 + return mRect; 1.96 +} 1.97 + 1.98 +SkString* SkObjectParser::MatrixToString(const SkMatrix& matrix) { 1.99 + SkString* str = new SkString("SkMatrix: "); 1.100 +#ifndef SK_IGNORE_TO_STRING 1.101 + matrix.toString(str); 1.102 +#endif 1.103 + return str; 1.104 +} 1.105 + 1.106 +SkString* SkObjectParser::PaintToString(const SkPaint& paint) { 1.107 + SkString* str = new SkString; 1.108 +#ifndef SK_IGNORE_TO_STRING 1.109 + paint.toString(str); 1.110 +#endif 1.111 + return str; 1.112 +} 1.113 + 1.114 +SkString* SkObjectParser::PathToString(const SkPath& path) { 1.115 + SkString* mPath = new SkString("Path ("); 1.116 + 1.117 + static const char* gFillStrings[] = { 1.118 + "Winding", "EvenOdd", "InverseWinding", "InverseEvenOdd" 1.119 + }; 1.120 + 1.121 + mPath->append(gFillStrings[path.getFillType()]); 1.122 + mPath->append(", "); 1.123 + 1.124 + static const char* gConvexityStrings[] = { 1.125 + "Unknown", "Convex", "Concave" 1.126 + }; 1.127 + SkASSERT(SkPath::kConcave_Convexity == 2); 1.128 + 1.129 + mPath->append(gConvexityStrings[path.getConvexity()]); 1.130 + mPath->append(", "); 1.131 + 1.132 + if (path.isRect(NULL)) { 1.133 + mPath->append("isRect, "); 1.134 + } else { 1.135 + mPath->append("isNotRect, "); 1.136 + } 1.137 + 1.138 + mPath->appendS32(path.countVerbs()); 1.139 + mPath->append("V, "); 1.140 + mPath->appendS32(path.countPoints()); 1.141 + mPath->append("P): "); 1.142 + 1.143 + static const char* gVerbStrings[] = { 1.144 + "Move", "Line", "Quad", "Conic", "Cubic", "Close", "Done" 1.145 + }; 1.146 + static const int gPtsPerVerb[] = { 1, 1, 2, 2, 3, 0, 0 }; 1.147 + static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 1, 0, 0 }; 1.148 + SkASSERT(SkPath::kDone_Verb == 6); 1.149 + 1.150 + SkPath::Iter iter(const_cast<SkPath&>(path), false); 1.151 + SkPath::Verb verb; 1.152 + SkPoint points[4]; 1.153 + 1.154 + for(verb = iter.next(points, false); 1.155 + verb != SkPath::kDone_Verb; 1.156 + verb = iter.next(points, false)) { 1.157 + 1.158 + mPath->append(gVerbStrings[verb]); 1.159 + mPath->append(" "); 1.160 + 1.161 + for (int i = 0; i < gPtsPerVerb[verb]; ++i) { 1.162 + mPath->append("("); 1.163 + mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fX); 1.164 + mPath->append(", "); 1.165 + mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fY); 1.166 + mPath->append(")"); 1.167 + } 1.168 + 1.169 + if (SkPath::kConic_Verb == verb) { 1.170 + mPath->append("("); 1.171 + mPath->appendScalar(iter.conicWeight()); 1.172 + mPath->append(")"); 1.173 + } 1.174 + 1.175 + mPath->append(" "); 1.176 + } 1.177 + 1.178 + SkString* boundStr = SkObjectParser::RectToString(path.getBounds(), " Bound: "); 1.179 + 1.180 + if (NULL != boundStr) { 1.181 + mPath->append(*boundStr); 1.182 + SkDELETE(boundStr); 1.183 + } 1.184 + 1.185 + return mPath; 1.186 +} 1.187 + 1.188 +SkString* SkObjectParser::PointsToString(const SkPoint pts[], size_t count) { 1.189 + SkString* mPoints = new SkString("SkPoints pts[]: "); 1.190 + for (unsigned int i = 0; i < count; i++) { 1.191 + mPoints->append("("); 1.192 + mPoints->appendScalar(pts[i].fX); 1.193 + mPoints->append(","); 1.194 + mPoints->appendScalar(pts[i].fY); 1.195 + mPoints->append(")"); 1.196 + } 1.197 + return mPoints; 1.198 +} 1.199 + 1.200 +SkString* SkObjectParser::PointModeToString(SkCanvas::PointMode mode) { 1.201 + SkString* mMode = new SkString("SkCanvas::PointMode: "); 1.202 + if (mode == SkCanvas::kPoints_PointMode) { 1.203 + mMode->append("kPoints_PointMode"); 1.204 + } else if (mode == SkCanvas::kLines_PointMode) { 1.205 + mMode->append("kLines_Mode"); 1.206 + } else if (mode == SkCanvas::kPolygon_PointMode) { 1.207 + mMode->append("kPolygon_PointMode"); 1.208 + } 1.209 + return mMode; 1.210 +} 1.211 + 1.212 +SkString* SkObjectParser::RectToString(const SkRect& rect, const char* title) { 1.213 + 1.214 + SkString* mRect = new SkString; 1.215 + 1.216 + if (NULL == title) { 1.217 + mRect->append("SkRect: "); 1.218 + } else { 1.219 + mRect->append(title); 1.220 + } 1.221 + mRect->append("("); 1.222 + mRect->appendScalar(rect.left()); 1.223 + mRect->append(", "); 1.224 + mRect->appendScalar(rect.top()); 1.225 + mRect->append(", "); 1.226 + mRect->appendScalar(rect.right()); 1.227 + mRect->append(", "); 1.228 + mRect->appendScalar(rect.bottom()); 1.229 + mRect->append(")"); 1.230 + return mRect; 1.231 +} 1.232 + 1.233 +SkString* SkObjectParser::RRectToString(const SkRRect& rrect, const char* title) { 1.234 + 1.235 + SkString* mRRect = new SkString; 1.236 + 1.237 + if (NULL == title) { 1.238 + mRRect->append("SkRRect ("); 1.239 + if (rrect.isEmpty()) { 1.240 + mRRect->append("empty"); 1.241 + } else if (rrect.isRect()) { 1.242 + mRRect->append("rect"); 1.243 + } else if (rrect.isOval()) { 1.244 + mRRect->append("oval"); 1.245 + } else if (rrect.isSimple()) { 1.246 + mRRect->append("simple"); 1.247 + } else { 1.248 + SkASSERT(rrect.isComplex()); 1.249 + mRRect->append("complex"); 1.250 + } 1.251 + mRRect->append("): "); 1.252 + } else { 1.253 + mRRect->append(title); 1.254 + } 1.255 + mRRect->append("("); 1.256 + mRRect->appendScalar(rrect.rect().left()); 1.257 + mRRect->append(", "); 1.258 + mRRect->appendScalar(rrect.rect().top()); 1.259 + mRRect->append(", "); 1.260 + mRRect->appendScalar(rrect.rect().right()); 1.261 + mRRect->append(", "); 1.262 + mRRect->appendScalar(rrect.rect().bottom()); 1.263 + mRRect->append(") radii: ("); 1.264 + for (int i = 0; i < 4; ++i) { 1.265 + const SkVector& radii = rrect.radii((SkRRect::Corner) i); 1.266 + mRRect->appendScalar(radii.fX); 1.267 + mRRect->append(", "); 1.268 + mRRect->appendScalar(radii.fY); 1.269 + if (i < 3) { 1.270 + mRRect->append(", "); 1.271 + } 1.272 + } 1.273 + mRRect->append(")"); 1.274 + return mRRect; 1.275 +} 1.276 + 1.277 +SkString* SkObjectParser::RegionOpToString(SkRegion::Op op) { 1.278 + SkString* mOp = new SkString("SkRegion::Op: "); 1.279 + if (op == SkRegion::kDifference_Op) { 1.280 + mOp->append("kDifference_Op"); 1.281 + } else if (op == SkRegion::kIntersect_Op) { 1.282 + mOp->append("kIntersect_Op"); 1.283 + } else if (op == SkRegion::kUnion_Op) { 1.284 + mOp->append("kUnion_Op"); 1.285 + } else if (op == SkRegion::kXOR_Op) { 1.286 + mOp->append("kXOR_Op"); 1.287 + } else if (op == SkRegion::kReverseDifference_Op) { 1.288 + mOp->append("kReverseDifference_Op"); 1.289 + } else if (op == SkRegion::kReplace_Op) { 1.290 + mOp->append("kReplace_Op"); 1.291 + } else { 1.292 + mOp->append("Unknown Type"); 1.293 + } 1.294 + return mOp; 1.295 +} 1.296 + 1.297 +SkString* SkObjectParser::RegionToString(const SkRegion& region) { 1.298 + SkString* mRegion = new SkString("SkRegion: Data unavailable."); 1.299 + return mRegion; 1.300 +} 1.301 + 1.302 +SkString* SkObjectParser::SaveFlagsToString(SkCanvas::SaveFlags flags) { 1.303 + SkString* mFlags = new SkString("SkCanvas::SaveFlags: "); 1.304 + if (flags & SkCanvas::kMatrix_SaveFlag) { 1.305 + mFlags->append("kMatrix_SaveFlag "); 1.306 + } 1.307 + if (flags & SkCanvas::kClip_SaveFlag) { 1.308 + mFlags->append("kClip_SaveFlag "); 1.309 + } 1.310 + if (flags & SkCanvas::kHasAlphaLayer_SaveFlag) { 1.311 + mFlags->append("kHasAlphaLayer_SaveFlag "); 1.312 + } 1.313 + if (flags & SkCanvas::kFullColorLayer_SaveFlag) { 1.314 + mFlags->append("kFullColorLayer_SaveFlag "); 1.315 + } 1.316 + if (flags & SkCanvas::kClipToLayer_SaveFlag) { 1.317 + mFlags->append("kClipToLayer_SaveFlag "); 1.318 + } 1.319 + return mFlags; 1.320 +} 1.321 + 1.322 +SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) { 1.323 + SkString* mScalar = new SkString(text); 1.324 + mScalar->append(" "); 1.325 + mScalar->appendScalar(x); 1.326 + return mScalar; 1.327 +} 1.328 + 1.329 +SkString* SkObjectParser::TextToString(const void* text, size_t byteLength, 1.330 + SkPaint::TextEncoding encoding) { 1.331 + 1.332 + SkString* decodedText = new SkString(); 1.333 + switch (encoding) { 1.334 + case SkPaint::kUTF8_TextEncoding: { 1.335 + decodedText->append("UTF-8: "); 1.336 + decodedText->append((const char*)text, byteLength); 1.337 + break; 1.338 + } 1.339 + case SkPaint::kUTF16_TextEncoding: { 1.340 + decodedText->append("UTF-16: "); 1.341 + size_t sizeNeeded = SkUTF16_ToUTF8((uint16_t*)text, 1.342 + SkToS32(byteLength / 2), 1.343 + NULL); 1.344 + SkAutoSTMalloc<0x100, char> utf8(sizeNeeded); 1.345 + SkUTF16_ToUTF8((uint16_t*)text, SkToS32(byteLength / 2), utf8); 1.346 + decodedText->append(utf8, sizeNeeded); 1.347 + break; 1.348 + } 1.349 + case SkPaint::kUTF32_TextEncoding: { 1.350 + decodedText->append("UTF-32: "); 1.351 + const SkUnichar* begin = (const SkUnichar*)text; 1.352 + const SkUnichar* end = (const SkUnichar*)((const char*)text + byteLength); 1.353 + for (const SkUnichar* unichar = begin; unichar < end; ++unichar) { 1.354 + decodedText->appendUnichar(*unichar); 1.355 + } 1.356 + break; 1.357 + } 1.358 + case SkPaint::kGlyphID_TextEncoding: { 1.359 + decodedText->append("GlyphID: "); 1.360 + const uint16_t* begin = (const uint16_t*)text; 1.361 + const uint16_t* end = (const uint16_t*)((const char*)text + byteLength); 1.362 + for (const uint16_t* glyph = begin; glyph < end; ++glyph) { 1.363 + decodedText->append("0x"); 1.364 + decodedText->appendHex(*glyph); 1.365 + decodedText->append(" "); 1.366 + } 1.367 + break; 1.368 + } 1.369 + default: 1.370 + decodedText->append("Unknown text encoding."); 1.371 + break; 1.372 + } 1.373 + 1.374 + return decodedText; 1.375 +}