2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HWUI_UV_MAPPER_H
18 #define ANDROID_HWUI_UV_MAPPER_H
23 namespace uirenderer {
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.
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.
36 UvMapper(): mIdentity(true), mMinU(0.0f), mMaxU(1.0f), mMinV(0.0f), mMaxV(1.0f) {
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.
43 UvMapper(float minU, float maxU, float minV, float maxV):
44 mMinU(minU), mMaxU(maxU), mMinV(minV), mMaxV(maxV) {
49 * Returns true if calling the map*() methods has no effect (that is,
50 * texture coordinates remain in the 0..1 range.)
52 bool isIdentity() const {
57 * Changes the U and V mapping ranges.
58 * The parameter minU must be < maxU and minV must be < maxV.
60 void setMapping(float minU, float maxU, float minV, float maxV) {
69 * Maps a single value in the U range.
71 void mapU(float& u) const {
72 if (!mIdentity) u = lerp(mMinU, mMaxU, u);
76 * Maps a single value in the V range.
78 void mapV(float& v) const {
79 if (!mIdentity) v = lerp(mMinV, mMaxV, v);
83 * Maps the specified rectangle in place. This method assumes:
89 void map(Rect& texCoords) const {
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);
99 * Maps the specified UV coordinates in place.
101 void map(float& u1, float& v1, float& u2, float& v2) const {
103 u1 = lerp(mMinU, mMaxU, u1);
104 u2 = lerp(mMinU, mMaxU, u2);
105 v1 = lerp(mMinV, mMaxV, v1);
106 v2 = lerp(mMinV, mMaxV, v2);
111 ALOGD("mapper[minU=%.2f maxU=%.2f minV=%.2f maxV=%.2f]", mMinU, mMaxU, mMinV, mMaxV);
115 static float lerp(float start, float stop, float amount) {
116 return start + (stop - start) * amount;
119 void checkIdentity() {
120 mIdentity = mMinU == 0.0f && mMaxU == 1.0f && mMinV == 0.0f && mMaxV == 1.0f;
130 }; // namespace uirenderer
131 }; // namespace android
133 #endif // ANDROID_HWUI_UV_MAPPER_H