SkRectPriv.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2018 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. #ifndef SkRectPriv_DEFINED
  8. #define SkRectPriv_DEFINED
  9. #include "include/core/SkRect.h"
  10. #include "src/core/SkMathPriv.h"
  11. class SkRectPriv {
  12. public:
  13. // Returns an irect that is very large, and can be safely round-trip with SkRect and still
  14. // be considered non-empty (i.e. width/height > 0) even if we round-out the SkRect.
  15. static SkIRect MakeILarge() {
  16. // SK_MaxS32 >> 1 seemed better, but it did not survive round-trip with SkRect and rounding.
  17. // Also, 1 << 29 can be perfectly represented in float, while SK_MaxS32 >> 1 cannot.
  18. const int32_t large = 1 << 29;
  19. return { -large, -large, large, large };
  20. }
  21. static SkIRect MakeILargestInverted() {
  22. return { SK_MaxS32, SK_MaxS32, SK_MinS32, SK_MinS32 };
  23. }
  24. static SkRect MakeLargeS32() {
  25. SkRect r;
  26. r.set(MakeILarge());
  27. return r;
  28. }
  29. static SkRect MakeLargest() {
  30. return { SK_ScalarMin, SK_ScalarMin, SK_ScalarMax, SK_ScalarMax };
  31. }
  32. static constexpr SkRect MakeLargestInverted() {
  33. return { SK_ScalarMax, SK_ScalarMax, SK_ScalarMin, SK_ScalarMin };
  34. }
  35. static void GrowToInclude(SkRect* r, const SkPoint& pt) {
  36. r->fLeft = SkMinScalar(pt.fX, r->fLeft);
  37. r->fRight = SkMaxScalar(pt.fX, r->fRight);
  38. r->fTop = SkMinScalar(pt.fY, r->fTop);
  39. r->fBottom = SkMaxScalar(pt.fY, r->fBottom);
  40. }
  41. // Conservative check if r can be expressed in fixed-point.
  42. // Will return false for very large values that might have fit
  43. static bool FitsInFixed(const SkRect& r) {
  44. return SkFitsInFixed(r.fLeft) && SkFitsInFixed(r.fTop) &&
  45. SkFitsInFixed(r.fRight) && SkFitsInFixed(r.fBottom);
  46. }
  47. static bool Is16Bit(const SkIRect& r) {
  48. return SkTFitsIn<int16_t>(r.fLeft) && SkTFitsIn<int16_t>(r.fTop) &&
  49. SkTFitsIn<int16_t>(r.fRight) && SkTFitsIn<int16_t>(r.fBottom);
  50. }
  51. };
  52. #endif