1/*
2 * Copyright 2010 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 GrRect_DEFINED
9#define GrRect_DEFINED
10
11#include "SkTypes.h"
12#include "SkRect.h"
13
14struct GrIRect16 {
15    int16_t fLeft, fTop, fRight, fBottom;
16
17    static GrIRect16 SK_WARN_UNUSED_RESULT MakeEmpty() {
18        GrIRect16 r;
19        r.setEmpty();
20        return r;
21    }
22
23    static GrIRect16 SK_WARN_UNUSED_RESULT MakeWH(int16_t w, int16_t h) {
24        GrIRect16 r;
25        r.set(0, 0, w, h);
26        return r;
27    }
28
29    static GrIRect16 SK_WARN_UNUSED_RESULT MakeXYWH(int16_t x, int16_t y, int16_t w, int16_t h) {
30        GrIRect16 r;
31        r.set(x, y, x + w, y + h);
32        return r;
33    }
34
35    int width() const { return fRight - fLeft; }
36    int height() const { return fBottom - fTop; }
37    int area() const { return this->width() * this->height(); }
38    bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
39
40    void setEmpty() { memset(this, 0, sizeof(*this)); }
41
42    void set(int16_t left, int16_t top, int16_t right, int16_t bottom) {
43        fLeft = left;
44        fTop = top;
45        fRight = right;
46        fBottom = bottom;
47    }
48
49    void set(const SkIRect& r) {
50        fLeft   = SkToS16(r.fLeft);
51        fTop    = SkToS16(r.fTop);
52        fRight  = SkToS16(r.fRight);
53        fBottom = SkToS16(r.fBottom);
54    }
55};
56
57/** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be
58    infinitely small but not "inverted". */
59static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) {
60    // See skbug.com/6607 about the isFinite() checks.
61    SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
62    SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
63    return a.fRight > b.fLeft && a.fBottom > b.fTop && b.fRight > a.fLeft && b.fBottom > a.fTop;
64}
65
66/** Returns true if the rectangles overlap or share an edge or corner. It assumed that rects can be
67    infinitely small but not "inverted". */
68static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) {
69    // See skbug.com/6607 about the isFinite() checks.
70    SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
71    SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
72    return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop;
73}
74#endif
75