SkRRect.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  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. #include "include/core/SkMatrix.h"
  8. #include "include/private/SkMalloc.h"
  9. #include "src/core/SkBuffer.h"
  10. #include "src/core/SkRRectPriv.h"
  11. #include "src/core/SkScaleToSides.h"
  12. #include <cmath>
  13. #include <utility>
  14. ///////////////////////////////////////////////////////////////////////////////
  15. void SkRRect::setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) {
  16. if (!this->initializeRect(rect)) {
  17. return;
  18. }
  19. if (!SkScalarsAreFinite(xRad, yRad)) {
  20. xRad = yRad = 0; // devolve into a simple rect
  21. }
  22. if (fRect.width() < xRad+xRad || fRect.height() < yRad+yRad) {
  23. // At most one of these two divides will be by zero, and neither numerator is zero.
  24. SkScalar scale = SkMinScalar(sk_ieee_float_divide(fRect. width(), xRad + xRad),
  25. sk_ieee_float_divide(fRect.height(), yRad + yRad));
  26. SkASSERT(scale < SK_Scalar1);
  27. xRad *= scale;
  28. yRad *= scale;
  29. }
  30. if (xRad <= 0 || yRad <= 0) {
  31. // all corners are square in this case
  32. this->setRect(rect);
  33. return;
  34. }
  35. for (int i = 0; i < 4; ++i) {
  36. fRadii[i].set(xRad, yRad);
  37. }
  38. fType = kSimple_Type;
  39. if (xRad >= SkScalarHalf(fRect.width()) && yRad >= SkScalarHalf(fRect.height())) {
  40. fType = kOval_Type;
  41. // TODO: assert that all the x&y radii are already W/2 & H/2
  42. }
  43. SkASSERT(this->isValid());
  44. }
  45. void SkRRect::setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad,
  46. SkScalar rightRad, SkScalar bottomRad) {
  47. if (!this->initializeRect(rect)) {
  48. return;
  49. }
  50. const SkScalar array[4] = { leftRad, topRad, rightRad, bottomRad };
  51. if (!SkScalarsAreFinite(array, 4)) {
  52. this->setRect(rect); // devolve into a simple rect
  53. return;
  54. }
  55. leftRad = SkMaxScalar(leftRad, 0);
  56. topRad = SkMaxScalar(topRad, 0);
  57. rightRad = SkMaxScalar(rightRad, 0);
  58. bottomRad = SkMaxScalar(bottomRad, 0);
  59. SkScalar scale = SK_Scalar1;
  60. if (leftRad + rightRad > fRect.width()) {
  61. scale = fRect.width() / (leftRad + rightRad);
  62. }
  63. if (topRad + bottomRad > fRect.height()) {
  64. scale = SkMinScalar(scale, fRect.height() / (topRad + bottomRad));
  65. }
  66. if (scale < SK_Scalar1) {
  67. leftRad *= scale;
  68. topRad *= scale;
  69. rightRad *= scale;
  70. bottomRad *= scale;
  71. }
  72. if (leftRad == rightRad && topRad == bottomRad) {
  73. if (leftRad >= SkScalarHalf(fRect.width()) && topRad >= SkScalarHalf(fRect.height())) {
  74. fType = kOval_Type;
  75. } else if (0 == leftRad || 0 == topRad) {
  76. // If the left and (by equality check above) right radii are zero then it is a rect.
  77. // Same goes for top/bottom.
  78. fType = kRect_Type;
  79. leftRad = 0;
  80. topRad = 0;
  81. rightRad = 0;
  82. bottomRad = 0;
  83. } else {
  84. fType = kSimple_Type;
  85. }
  86. } else {
  87. fType = kNinePatch_Type;
  88. }
  89. fRadii[kUpperLeft_Corner].set(leftRad, topRad);
  90. fRadii[kUpperRight_Corner].set(rightRad, topRad);
  91. fRadii[kLowerRight_Corner].set(rightRad, bottomRad);
  92. fRadii[kLowerLeft_Corner].set(leftRad, bottomRad);
  93. SkASSERT(this->isValid());
  94. }
  95. // These parameters intentionally double. Apropos crbug.com/463920, if one of the
  96. // radii is huge while the other is small, single precision math can completely
  97. // miss the fact that a scale is required.
  98. static double compute_min_scale(double rad1, double rad2, double limit, double curMin) {
  99. if ((rad1 + rad2) > limit) {
  100. return SkTMin(curMin, limit / (rad1 + rad2));
  101. }
  102. return curMin;
  103. }
  104. static bool clamp_to_zero(SkVector radii[4]) {
  105. bool allCornersSquare = true;
  106. // Clamp negative radii to zero
  107. for (int i = 0; i < 4; ++i) {
  108. if (radii[i].fX <= 0 || radii[i].fY <= 0) {
  109. // In this case we are being a little fast & loose. Since one of
  110. // the radii is 0 the corner is square. However, the other radii
  111. // could still be non-zero and play in the global scale factor
  112. // computation.
  113. radii[i].fX = 0;
  114. radii[i].fY = 0;
  115. } else {
  116. allCornersSquare = false;
  117. }
  118. }
  119. return allCornersSquare;
  120. }
  121. void SkRRect::setRectRadii(const SkRect& rect, const SkVector radii[4]) {
  122. if (!this->initializeRect(rect)) {
  123. return;
  124. }
  125. if (!SkScalarsAreFinite(&radii[0].fX, 8)) {
  126. this->setRect(rect); // devolve into a simple rect
  127. return;
  128. }
  129. memcpy(fRadii, radii, sizeof(fRadii));
  130. if (clamp_to_zero(fRadii)) {
  131. this->setRect(rect);
  132. return;
  133. }
  134. this->scaleRadii(rect);
  135. }
  136. bool SkRRect::initializeRect(const SkRect& rect) {
  137. // Check this before sorting because sorting can hide nans.
  138. if (!rect.isFinite()) {
  139. *this = SkRRect();
  140. return false;
  141. }
  142. fRect = rect.makeSorted();
  143. if (fRect.isEmpty()) {
  144. memset(fRadii, 0, sizeof(fRadii));
  145. fType = kEmpty_Type;
  146. return false;
  147. }
  148. return true;
  149. }
  150. // If we can't distinguish one of the radii relative to the other, force it to zero so it
  151. // doesn't confuse us later. See crbug.com/850350
  152. //
  153. static void flush_to_zero(SkScalar& a, SkScalar& b) {
  154. SkASSERT(a >= 0);
  155. SkASSERT(b >= 0);
  156. if (a + b == a) {
  157. b = 0;
  158. } else if (a + b == b) {
  159. a = 0;
  160. }
  161. }
  162. void SkRRect::scaleRadii(const SkRect& rect) {
  163. // Proportionally scale down all radii to fit. Find the minimum ratio
  164. // of a side and the radii on that side (for all four sides) and use
  165. // that to scale down _all_ the radii. This algorithm is from the
  166. // W3 spec (http://www.w3.org/TR/css3-background/) section 5.5 - Overlapping
  167. // Curves:
  168. // "Let f = min(Li/Si), where i is one of { top, right, bottom, left },
  169. // Si is the sum of the two corresponding radii of the corners on side i,
  170. // and Ltop = Lbottom = the width of the box,
  171. // and Lleft = Lright = the height of the box.
  172. // If f < 1, then all corner radii are reduced by multiplying them by f."
  173. double scale = 1.0;
  174. // The sides of the rectangle may be larger than a float.
  175. double width = (double)fRect.fRight - (double)fRect.fLeft;
  176. double height = (double)fRect.fBottom - (double)fRect.fTop;
  177. scale = compute_min_scale(fRadii[0].fX, fRadii[1].fX, width, scale);
  178. scale = compute_min_scale(fRadii[1].fY, fRadii[2].fY, height, scale);
  179. scale = compute_min_scale(fRadii[2].fX, fRadii[3].fX, width, scale);
  180. scale = compute_min_scale(fRadii[3].fY, fRadii[0].fY, height, scale);
  181. flush_to_zero(fRadii[0].fX, fRadii[1].fX);
  182. flush_to_zero(fRadii[1].fY, fRadii[2].fY);
  183. flush_to_zero(fRadii[2].fX, fRadii[3].fX);
  184. flush_to_zero(fRadii[3].fY, fRadii[0].fY);
  185. if (scale < 1.0) {
  186. SkScaleToSides::AdjustRadii(width, scale, &fRadii[0].fX, &fRadii[1].fX);
  187. SkScaleToSides::AdjustRadii(height, scale, &fRadii[1].fY, &fRadii[2].fY);
  188. SkScaleToSides::AdjustRadii(width, scale, &fRadii[2].fX, &fRadii[3].fX);
  189. SkScaleToSides::AdjustRadii(height, scale, &fRadii[3].fY, &fRadii[0].fY);
  190. }
  191. // adjust radii may set x or y to zero; set companion to zero as well
  192. if (clamp_to_zero(fRadii)) {
  193. this->setRect(rect);
  194. return;
  195. }
  196. // At this point we're either oval, simple, or complex (not empty or rect).
  197. this->computeType();
  198. SkASSERT(this->isValid());
  199. }
  200. // This method determines if a point known to be inside the RRect's bounds is
  201. // inside all the corners.
  202. bool SkRRect::checkCornerContainment(SkScalar x, SkScalar y) const {
  203. SkPoint canonicalPt; // (x,y) translated to one of the quadrants
  204. int index;
  205. if (kOval_Type == this->type()) {
  206. canonicalPt.set(x - fRect.centerX(), y - fRect.centerY());
  207. index = kUpperLeft_Corner; // any corner will do in this case
  208. } else {
  209. if (x < fRect.fLeft + fRadii[kUpperLeft_Corner].fX &&
  210. y < fRect.fTop + fRadii[kUpperLeft_Corner].fY) {
  211. // UL corner
  212. index = kUpperLeft_Corner;
  213. canonicalPt.set(x - (fRect.fLeft + fRadii[kUpperLeft_Corner].fX),
  214. y - (fRect.fTop + fRadii[kUpperLeft_Corner].fY));
  215. SkASSERT(canonicalPt.fX < 0 && canonicalPt.fY < 0);
  216. } else if (x < fRect.fLeft + fRadii[kLowerLeft_Corner].fX &&
  217. y > fRect.fBottom - fRadii[kLowerLeft_Corner].fY) {
  218. // LL corner
  219. index = kLowerLeft_Corner;
  220. canonicalPt.set(x - (fRect.fLeft + fRadii[kLowerLeft_Corner].fX),
  221. y - (fRect.fBottom - fRadii[kLowerLeft_Corner].fY));
  222. SkASSERT(canonicalPt.fX < 0 && canonicalPt.fY > 0);
  223. } else if (x > fRect.fRight - fRadii[kUpperRight_Corner].fX &&
  224. y < fRect.fTop + fRadii[kUpperRight_Corner].fY) {
  225. // UR corner
  226. index = kUpperRight_Corner;
  227. canonicalPt.set(x - (fRect.fRight - fRadii[kUpperRight_Corner].fX),
  228. y - (fRect.fTop + fRadii[kUpperRight_Corner].fY));
  229. SkASSERT(canonicalPt.fX > 0 && canonicalPt.fY < 0);
  230. } else if (x > fRect.fRight - fRadii[kLowerRight_Corner].fX &&
  231. y > fRect.fBottom - fRadii[kLowerRight_Corner].fY) {
  232. // LR corner
  233. index = kLowerRight_Corner;
  234. canonicalPt.set(x - (fRect.fRight - fRadii[kLowerRight_Corner].fX),
  235. y - (fRect.fBottom - fRadii[kLowerRight_Corner].fY));
  236. SkASSERT(canonicalPt.fX > 0 && canonicalPt.fY > 0);
  237. } else {
  238. // not in any of the corners
  239. return true;
  240. }
  241. }
  242. // A point is in an ellipse (in standard position) if:
  243. // x^2 y^2
  244. // ----- + ----- <= 1
  245. // a^2 b^2
  246. // or :
  247. // b^2*x^2 + a^2*y^2 <= (ab)^2
  248. SkScalar dist = SkScalarSquare(canonicalPt.fX) * SkScalarSquare(fRadii[index].fY) +
  249. SkScalarSquare(canonicalPt.fY) * SkScalarSquare(fRadii[index].fX);
  250. return dist <= SkScalarSquare(fRadii[index].fX * fRadii[index].fY);
  251. }
  252. bool SkRRectPriv::AllCornersCircular(const SkRRect& rr, SkScalar tolerance) {
  253. return SkScalarNearlyEqual(rr.fRadii[0].fX, rr.fRadii[0].fY, tolerance) &&
  254. SkScalarNearlyEqual(rr.fRadii[1].fX, rr.fRadii[1].fY, tolerance) &&
  255. SkScalarNearlyEqual(rr.fRadii[2].fX, rr.fRadii[2].fY, tolerance) &&
  256. SkScalarNearlyEqual(rr.fRadii[3].fX, rr.fRadii[3].fY, tolerance);
  257. }
  258. bool SkRRect::contains(const SkRect& rect) const {
  259. if (!this->getBounds().contains(rect)) {
  260. // If 'rect' isn't contained by the RR's bounds then the
  261. // RR definitely doesn't contain it
  262. return false;
  263. }
  264. if (this->isRect()) {
  265. // the prior test was sufficient
  266. return true;
  267. }
  268. // At this point we know all four corners of 'rect' are inside the
  269. // bounds of of this RR. Check to make sure all the corners are inside
  270. // all the curves
  271. return this->checkCornerContainment(rect.fLeft, rect.fTop) &&
  272. this->checkCornerContainment(rect.fRight, rect.fTop) &&
  273. this->checkCornerContainment(rect.fRight, rect.fBottom) &&
  274. this->checkCornerContainment(rect.fLeft, rect.fBottom);
  275. }
  276. static bool radii_are_nine_patch(const SkVector radii[4]) {
  277. return radii[SkRRect::kUpperLeft_Corner].fX == radii[SkRRect::kLowerLeft_Corner].fX &&
  278. radii[SkRRect::kUpperLeft_Corner].fY == radii[SkRRect::kUpperRight_Corner].fY &&
  279. radii[SkRRect::kUpperRight_Corner].fX == radii[SkRRect::kLowerRight_Corner].fX &&
  280. radii[SkRRect::kLowerLeft_Corner].fY == radii[SkRRect::kLowerRight_Corner].fY;
  281. }
  282. // There is a simplified version of this method in setRectXY
  283. void SkRRect::computeType() {
  284. if (fRect.isEmpty()) {
  285. SkASSERT(fRect.isSorted());
  286. for (size_t i = 0; i < SK_ARRAY_COUNT(fRadii); ++i) {
  287. SkASSERT((fRadii[i] == SkVector{0, 0}));
  288. }
  289. fType = kEmpty_Type;
  290. SkASSERT(this->isValid());
  291. return;
  292. }
  293. bool allRadiiEqual = true; // are all x radii equal and all y radii?
  294. bool allCornersSquare = 0 == fRadii[0].fX || 0 == fRadii[0].fY;
  295. for (int i = 1; i < 4; ++i) {
  296. if (0 != fRadii[i].fX && 0 != fRadii[i].fY) {
  297. // if either radius is zero the corner is square so both have to
  298. // be non-zero to have a rounded corner
  299. allCornersSquare = false;
  300. }
  301. if (fRadii[i].fX != fRadii[i-1].fX || fRadii[i].fY != fRadii[i-1].fY) {
  302. allRadiiEqual = false;
  303. }
  304. }
  305. if (allCornersSquare) {
  306. fType = kRect_Type;
  307. SkASSERT(this->isValid());
  308. return;
  309. }
  310. if (allRadiiEqual) {
  311. if (fRadii[0].fX >= SkScalarHalf(fRect.width()) &&
  312. fRadii[0].fY >= SkScalarHalf(fRect.height())) {
  313. fType = kOval_Type;
  314. } else {
  315. fType = kSimple_Type;
  316. }
  317. SkASSERT(this->isValid());
  318. return;
  319. }
  320. if (radii_are_nine_patch(fRadii)) {
  321. fType = kNinePatch_Type;
  322. } else {
  323. fType = kComplex_Type;
  324. }
  325. SkASSERT(this->isValid());
  326. }
  327. bool SkRRect::transform(const SkMatrix& matrix, SkRRect* dst) const {
  328. if (nullptr == dst) {
  329. return false;
  330. }
  331. // Assert that the caller is not trying to do this in place, which
  332. // would violate const-ness. Do not return false though, so that
  333. // if they know what they're doing and want to violate it they can.
  334. SkASSERT(dst != this);
  335. if (matrix.isIdentity()) {
  336. *dst = *this;
  337. return true;
  338. }
  339. if (!matrix.preservesAxisAlignment()) {
  340. return false;
  341. }
  342. SkRect newRect;
  343. if (!matrix.mapRect(&newRect, fRect)) {
  344. return false;
  345. }
  346. // The matrix may have scaled us to zero (or due to float madness, we now have collapsed
  347. // some dimension of the rect, so we need to check for that. Note that matrix must be
  348. // scale and translate and mapRect() produces a sorted rect. So an empty rect indicates
  349. // loss of precision.
  350. if (!newRect.isFinite() || newRect.isEmpty()) {
  351. return false;
  352. }
  353. // At this point, this is guaranteed to succeed, so we can modify dst.
  354. dst->fRect = newRect;
  355. // Since the only transforms that were allowed are axis aligned, the type
  356. // remains unchanged.
  357. dst->fType = fType;
  358. if (kRect_Type == fType) {
  359. SkASSERT(dst->isValid());
  360. return true;
  361. }
  362. if (kOval_Type == fType) {
  363. for (int i = 0; i < 4; ++i) {
  364. dst->fRadii[i].fX = SkScalarHalf(newRect.width());
  365. dst->fRadii[i].fY = SkScalarHalf(newRect.height());
  366. }
  367. SkASSERT(dst->isValid());
  368. return true;
  369. }
  370. // Now scale each corner
  371. SkScalar xScale = matrix.getScaleX();
  372. SkScalar yScale = matrix.getScaleY();
  373. // There is a rotation of 90 (Clockwise 90) or 270 (Counter clockwise 90).
  374. // 180 degrees rotations are simply flipX with a flipY and would come under
  375. // a scale transform.
  376. if (!matrix.isScaleTranslate()) {
  377. const bool isClockwise = matrix.getSkewX() < 0;
  378. // The matrix location for scale changes if there is a rotation.
  379. xScale = matrix.getSkewY() * (isClockwise ? 1 : -1);
  380. yScale = matrix.getSkewX() * (isClockwise ? -1 : 1);
  381. const int dir = isClockwise ? 3 : 1;
  382. for (int i = 0; i < 4; ++i) {
  383. const int src = (i + dir) >= 4 ? (i + dir) % 4 : (i + dir);
  384. // Swap X and Y axis for the radii.
  385. dst->fRadii[i].fX = fRadii[src].fY;
  386. dst->fRadii[i].fY = fRadii[src].fX;
  387. }
  388. } else {
  389. for (int i = 0; i < 4; ++i) {
  390. dst->fRadii[i].fX = fRadii[i].fX;
  391. dst->fRadii[i].fY = fRadii[i].fY;
  392. }
  393. }
  394. const bool flipX = xScale < 0;
  395. if (flipX) {
  396. xScale = -xScale;
  397. }
  398. const bool flipY = yScale < 0;
  399. if (flipY) {
  400. yScale = -yScale;
  401. }
  402. // Scale the radii without respecting the flip.
  403. for (int i = 0; i < 4; ++i) {
  404. dst->fRadii[i].fX *= xScale;
  405. dst->fRadii[i].fY *= yScale;
  406. }
  407. // Now swap as necessary.
  408. using std::swap;
  409. if (flipX) {
  410. if (flipY) {
  411. // Swap with opposite corners
  412. swap(dst->fRadii[kUpperLeft_Corner], dst->fRadii[kLowerRight_Corner]);
  413. swap(dst->fRadii[kUpperRight_Corner], dst->fRadii[kLowerLeft_Corner]);
  414. } else {
  415. // Only swap in x
  416. swap(dst->fRadii[kUpperRight_Corner], dst->fRadii[kUpperLeft_Corner]);
  417. swap(dst->fRadii[kLowerRight_Corner], dst->fRadii[kLowerLeft_Corner]);
  418. }
  419. } else if (flipY) {
  420. // Only swap in y
  421. swap(dst->fRadii[kUpperLeft_Corner], dst->fRadii[kLowerLeft_Corner]);
  422. swap(dst->fRadii[kUpperRight_Corner], dst->fRadii[kLowerRight_Corner]);
  423. }
  424. if (!AreRectAndRadiiValid(dst->fRect, dst->fRadii)) {
  425. return false;
  426. }
  427. dst->scaleRadii(dst->fRect);
  428. dst->isValid();
  429. return true;
  430. }
  431. ///////////////////////////////////////////////////////////////////////////////
  432. void SkRRect::inset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
  433. SkRect r = fRect.makeInset(dx, dy);
  434. bool degenerate = false;
  435. if (r.fRight <= r.fLeft) {
  436. degenerate = true;
  437. r.fLeft = r.fRight = SkScalarAve(r.fLeft, r.fRight);
  438. }
  439. if (r.fBottom <= r.fTop) {
  440. degenerate = true;
  441. r.fTop = r.fBottom = SkScalarAve(r.fTop, r.fBottom);
  442. }
  443. if (degenerate) {
  444. dst->fRect = r;
  445. memset(dst->fRadii, 0, sizeof(dst->fRadii));
  446. dst->fType = kEmpty_Type;
  447. return;
  448. }
  449. if (!r.isFinite()) {
  450. *dst = SkRRect();
  451. return;
  452. }
  453. SkVector radii[4];
  454. memcpy(radii, fRadii, sizeof(radii));
  455. for (int i = 0; i < 4; ++i) {
  456. if (radii[i].fX) {
  457. radii[i].fX -= dx;
  458. }
  459. if (radii[i].fY) {
  460. radii[i].fY -= dy;
  461. }
  462. }
  463. dst->setRectRadii(r, radii);
  464. }
  465. ///////////////////////////////////////////////////////////////////////////////
  466. size_t SkRRect::writeToMemory(void* buffer) const {
  467. // Serialize only the rect and corners, but not the derived type tag.
  468. memcpy(buffer, this, kSizeInMemory);
  469. return kSizeInMemory;
  470. }
  471. void SkRRectPriv::WriteToBuffer(const SkRRect& rr, SkWBuffer* buffer) {
  472. // Serialize only the rect and corners, but not the derived type tag.
  473. buffer->write(&rr, SkRRect::kSizeInMemory);
  474. }
  475. size_t SkRRect::readFromMemory(const void* buffer, size_t length) {
  476. if (length < kSizeInMemory) {
  477. return 0;
  478. }
  479. SkRRect raw;
  480. memcpy(&raw, buffer, kSizeInMemory);
  481. this->setRectRadii(raw.fRect, raw.fRadii);
  482. return kSizeInMemory;
  483. }
  484. bool SkRRectPriv::ReadFromBuffer(SkRBuffer* buffer, SkRRect* rr) {
  485. if (buffer->available() < SkRRect::kSizeInMemory) {
  486. return false;
  487. }
  488. SkRRect storage;
  489. return buffer->read(&storage, SkRRect::kSizeInMemory) &&
  490. (rr->readFromMemory(&storage, SkRRect::kSizeInMemory) == SkRRect::kSizeInMemory);
  491. }
  492. #include "include/core/SkString.h"
  493. #include "src/core/SkStringUtils.h"
  494. void SkRRect::dump(bool asHex) const {
  495. SkScalarAsStringType asType = asHex ? kHex_SkScalarAsStringType : kDec_SkScalarAsStringType;
  496. fRect.dump(asHex);
  497. SkString line("const SkPoint corners[] = {\n");
  498. for (int i = 0; i < 4; ++i) {
  499. SkString strX, strY;
  500. SkAppendScalar(&strX, fRadii[i].x(), asType);
  501. SkAppendScalar(&strY, fRadii[i].y(), asType);
  502. line.appendf(" { %s, %s },", strX.c_str(), strY.c_str());
  503. if (asHex) {
  504. line.appendf(" /* %f %f */", fRadii[i].x(), fRadii[i].y());
  505. }
  506. line.append("\n");
  507. }
  508. line.append("};");
  509. SkDebugf("%s\n", line.c_str());
  510. }
  511. ///////////////////////////////////////////////////////////////////////////////
  512. /**
  513. * We need all combinations of predicates to be true to have a "safe" radius value.
  514. */
  515. static bool are_radius_check_predicates_valid(SkScalar rad, SkScalar min, SkScalar max) {
  516. return (min <= max) && (rad <= max - min) && (min + rad <= max) && (max - rad >= min) &&
  517. rad >= 0;
  518. }
  519. bool SkRRect::isValid() const {
  520. if (!AreRectAndRadiiValid(fRect, fRadii)) {
  521. return false;
  522. }
  523. bool allRadiiZero = (0 == fRadii[0].fX && 0 == fRadii[0].fY);
  524. bool allCornersSquare = (0 == fRadii[0].fX || 0 == fRadii[0].fY);
  525. bool allRadiiSame = true;
  526. for (int i = 1; i < 4; ++i) {
  527. if (0 != fRadii[i].fX || 0 != fRadii[i].fY) {
  528. allRadiiZero = false;
  529. }
  530. if (fRadii[i].fX != fRadii[i-1].fX || fRadii[i].fY != fRadii[i-1].fY) {
  531. allRadiiSame = false;
  532. }
  533. if (0 != fRadii[i].fX && 0 != fRadii[i].fY) {
  534. allCornersSquare = false;
  535. }
  536. }
  537. bool patchesOfNine = radii_are_nine_patch(fRadii);
  538. if (fType < 0 || fType > kLastType) {
  539. return false;
  540. }
  541. switch (fType) {
  542. case kEmpty_Type:
  543. if (!fRect.isEmpty() || !allRadiiZero || !allRadiiSame || !allCornersSquare) {
  544. return false;
  545. }
  546. break;
  547. case kRect_Type:
  548. if (fRect.isEmpty() || !allRadiiZero || !allRadiiSame || !allCornersSquare) {
  549. return false;
  550. }
  551. break;
  552. case kOval_Type:
  553. if (fRect.isEmpty() || allRadiiZero || !allRadiiSame || allCornersSquare) {
  554. return false;
  555. }
  556. for (int i = 0; i < 4; ++i) {
  557. if (!SkScalarNearlyEqual(fRadii[i].fX, SkScalarHalf(fRect.width())) ||
  558. !SkScalarNearlyEqual(fRadii[i].fY, SkScalarHalf(fRect.height()))) {
  559. return false;
  560. }
  561. }
  562. break;
  563. case kSimple_Type:
  564. if (fRect.isEmpty() || allRadiiZero || !allRadiiSame || allCornersSquare) {
  565. return false;
  566. }
  567. break;
  568. case kNinePatch_Type:
  569. if (fRect.isEmpty() || allRadiiZero || allRadiiSame || allCornersSquare ||
  570. !patchesOfNine) {
  571. return false;
  572. }
  573. break;
  574. case kComplex_Type:
  575. if (fRect.isEmpty() || allRadiiZero || allRadiiSame || allCornersSquare ||
  576. patchesOfNine) {
  577. return false;
  578. }
  579. break;
  580. }
  581. return true;
  582. }
  583. bool SkRRect::AreRectAndRadiiValid(const SkRect& rect, const SkVector radii[4]) {
  584. if (!rect.isFinite() || !rect.isSorted()) {
  585. return false;
  586. }
  587. for (int i = 0; i < 4; ++i) {
  588. if (!are_radius_check_predicates_valid(radii[i].fX, rect.fLeft, rect.fRight) ||
  589. !are_radius_check_predicates_valid(radii[i].fY, rect.fTop, rect.fBottom)) {
  590. return false;
  591. }
  592. }
  593. return true;
  594. }
  595. ///////////////////////////////////////////////////////////////////////////////