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