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