1 /*
2  * Copyright (C) 2013 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_UV_MAPPER_H
18 #define ANDROID_HWUI_UV_MAPPER_H
19 
20 #include "Rect.h"
21 
22 namespace android {
23 namespace uirenderer {
24 
25 /**
26  * This class can be used to map UV coordinates from the [0..1]
27  * range to other arbitrary ranges. All the methods below assume
28  * that the input values lie in the [0..1] range already.
29  */
30 class UvMapper {
31 public:
32     /**
33      * Using this constructor is equivalent to not using any mapping at all.
34      * UV coordinates in the [0..1] range remain in the [0..1] range.
35      */
UvMapper()36     UvMapper() : mIdentity(true), mMinU(0.0f), mMaxU(1.0f), mMinV(0.0f), mMaxV(1.0f) {}
37 
38     /**
39      * Creates a new mapper with the specified ranges for U and V coordinates.
40      * The parameter minU must be < maxU and minV must be < maxV.
41      */
UvMapper(float minU,float maxU,float minV,float maxV)42     UvMapper(float minU, float maxU, float minV, float maxV)
43             : mMinU(minU), mMaxU(maxU), mMinV(minV), mMaxV(maxV) {
44         checkIdentity();
45     }
46 
47     /**
48      * Returns true if calling the map*() methods has no effect (that is,
49      * texture coordinates remain in the 0..1 range.)
50      */
isIdentity()51     bool isIdentity() const { return mIdentity; }
52 
53     /**
54      * Changes the U and V mapping ranges.
55      * The parameter minU must be < maxU and minV must be < maxV.
56      */
setMapping(float minU,float maxU,float minV,float maxV)57     void setMapping(float minU, float maxU, float minV, float maxV) {
58         mMinU = minU;
59         mMaxU = maxU;
60         mMinV = minV;
61         mMaxV = maxV;
62         checkIdentity();
63     }
64 
65     /**
66      * Maps a single value in the U range.
67      */
mapU(float & u)68     void mapU(float& u) const {
69         if (!mIdentity) u = lerp(mMinU, mMaxU, u);
70     }
71 
72     /**
73      * Maps a single value in the V range.
74      */
mapV(float & v)75     void mapV(float& v) const {
76         if (!mIdentity) v = lerp(mMinV, mMaxV, v);
77     }
78 
79     /**
80      * Maps the specified rectangle in place. This method assumes:
81      * - left = min. U
82      * - top = min. V
83      * - right = max. U
84      * - bottom = max. V
85      */
map(Rect & texCoords)86     void map(Rect& texCoords) const {
87         if (!mIdentity) {
88             texCoords.left = lerp(mMinU, mMaxU, texCoords.left);
89             texCoords.right = lerp(mMinU, mMaxU, texCoords.right);
90             texCoords.top = lerp(mMinV, mMaxV, texCoords.top);
91             texCoords.bottom = lerp(mMinV, mMaxV, texCoords.bottom);
92         }
93     }
94 
95     /**
96      * Maps the specified UV coordinates in place.
97      */
map(float & u1,float & v1,float & u2,float & v2)98     void map(float& u1, float& v1, float& u2, float& v2) const {
99         if (!mIdentity) {
100             u1 = lerp(mMinU, mMaxU, u1);
101             u2 = lerp(mMinU, mMaxU, u2);
102             v1 = lerp(mMinV, mMaxV, v1);
103             v2 = lerp(mMinV, mMaxV, v2);
104         }
105     }
106 
dump()107     void dump() const {
108         ALOGD("mapper[minU=%.2f maxU=%.2f minV=%.2f maxV=%.2f]", mMinU, mMaxU, mMinV, mMaxV);
109     }
110 
111 private:
lerp(float start,float stop,float amount)112     static float lerp(float start, float stop, float amount) {
113         return start + (stop - start) * amount;
114     }
115 
checkIdentity()116     void checkIdentity() {
117         mIdentity = mMinU == 0.0f && mMaxU == 1.0f && mMinV == 0.0f && mMaxV == 1.0f;
118     }
119 
120     bool mIdentity;
121     float mMinU;
122     float mMaxU;
123     float mMinV;
124     float mMaxV;
125 };
126 
127 }  // namespace uirenderer
128 }  // namespace android
129 
130 #endif  // ANDROID_HWUI_UV_MAPPER_H
131