Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ANDROID_HWUI_RECT_H
     18 #define ANDROID_HWUI_RECT_H
     19 
     20 #include <cmath>
     21 
     22 #include <utils/Log.h>
     23 
     24 namespace android {
     25 namespace uirenderer {
     26 
     27 ///////////////////////////////////////////////////////////////////////////////
     28 // Structs
     29 ///////////////////////////////////////////////////////////////////////////////
     30 
     31 class Rect {
     32     static inline float min(float a, float b) { return (a<b) ? a : b; }
     33     static inline float max(float a, float b) { return (a>b) ? a : b; }
     34     Rect intersectWith(float l, float t, float r, float b) const {
     35         Rect tmp;
     36         tmp.left    = max(left, l);
     37         tmp.top     = max(top, t);
     38         tmp.right   = min(right, r);
     39         tmp.bottom  = min(bottom, b);
     40         return tmp;
     41     }
     42 
     43 public:
     44     float left;
     45     float top;
     46     float right;
     47     float bottom;
     48 
     49     // Used by Region
     50     typedef float value_type;
     51 
     52     // we don't provide copy-ctor and operator= on purpose
     53     // because we want the compiler generated versions
     54 
     55     inline Rect():
     56             left(0),
     57             top(0),
     58             right(0),
     59             bottom(0) {
     60     }
     61 
     62     inline Rect(float left, float top, float right, float bottom):
     63             left(left),
     64             top(top),
     65             right(right),
     66             bottom(bottom) {
     67     }
     68 
     69     inline Rect(float width, float height):
     70             left(0.0f),
     71             top(0.0f),
     72             right(width),
     73             bottom(height) {
     74     }
     75 
     76     friend int operator==(const Rect& a, const Rect& b) {
     77         return !memcmp(&a, &b, sizeof(a));
     78     }
     79 
     80     friend int operator!=(const Rect& a, const Rect& b) {
     81         return memcmp(&a, &b, sizeof(a));
     82     }
     83 
     84     inline void clear() {
     85         left = top = right = bottom = 0.0f;
     86     }
     87 
     88     inline bool isEmpty() const {
     89         // this is written in such way this it'll handle NANs to return
     90         // true (empty)
     91         return !((left < right) && (top < bottom));
     92     }
     93 
     94     inline void setEmpty() {
     95         left = top = right = bottom = 0.0f;
     96     }
     97 
     98     inline void set(float left, float top, float right, float bottom) {
     99         this->left = left;
    100         this->right = right;
    101         this->top = top;
    102         this->bottom = bottom;
    103     }
    104 
    105     inline void set(const Rect& r) {
    106         set(r.left, r.top, r.right, r.bottom);
    107     }
    108 
    109     inline float getWidth() const {
    110         return right - left;
    111     }
    112 
    113     inline float getHeight() const {
    114         return bottom - top;
    115     }
    116 
    117     bool intersects(float l, float t, float r, float b) const {
    118         return !intersectWith(l,t,r,b).isEmpty();
    119     }
    120 
    121     bool intersects(const Rect& r) const {
    122         return intersects(r.left, r.top, r.right, r.bottom);
    123     }
    124 
    125     bool intersect(float l, float t, float r, float b) {
    126         Rect tmp(intersectWith(l,t,r,b));
    127         if (!tmp.isEmpty()) {
    128             set(tmp);
    129             return true;
    130         }
    131         return false;
    132     }
    133 
    134     bool intersect(const Rect& r) {
    135         return intersect(r.left, r.top, r.right, r.bottom);
    136     }
    137 
    138     bool unionWith(const Rect& r) {
    139         if (r.left < r.right && r.top < r.bottom) {
    140             if (left < right && top < bottom) {
    141                 if (left > r.left) left = r.left;
    142                 if (top > r.top) top = r.top;
    143                 if (right < r.right) right = r.right;
    144                 if (bottom < r.bottom) bottom = r.bottom;
    145                 return true;
    146             } else {
    147                 left = r.left;
    148                 top = r.top;
    149                 right = r.right;
    150                 bottom = r.bottom;
    151                 return true;
    152             }
    153         }
    154         return false;
    155     }
    156 
    157     void translate(float dx, float dy) {
    158         left += dx;
    159         right += dx;
    160         top += dy;
    161         bottom += dy;
    162     }
    163 
    164     void snapToPixelBoundaries() {
    165         left = floorf(left + 0.5f);
    166         top = floorf(top + 0.5f);
    167         right = floorf(right + 0.5f);
    168         bottom = floorf(bottom + 0.5f);
    169     }
    170 
    171     void dump() const {
    172         LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
    173     }
    174 
    175 }; // class Rect
    176 
    177 }; // namespace uirenderer
    178 }; // namespace android
    179 
    180 #endif // ANDROID_HWUI_RECT_H
    181