blob: 833ca4a79ce52231ba11992bb85cbecf38f5d52c [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 */
John Reck1bcacfd2017-11-03 10:12:19 -070036 UvMapper() : mIdentity(true), mMinU(0.0f), mMaxU(1.0f), mMinV(0.0f), mMaxV(1.0f) {}
Romain Guy3b748a42013-04-17 18:54:38 -070037
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 */
John Reck1bcacfd2017-11-03 10:12:19 -070042 UvMapper(float minU, float maxU, float minV, float maxV)
43 : mMinU(minU), mMaxU(maxU), mMinV(minV), mMaxV(maxV) {
Romain Guy3b748a42013-04-17 18:54:38 -070044 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 */
John Reck1bcacfd2017-11-03 10:12:19 -070051 bool isIdentity() const { return mIdentity; }
Romain Guy3b748a42013-04-17 18:54:38 -070052
53 /**
54 * Changes the U and V mapping ranges.
55 * The parameter minU must be < maxU and minV must be < maxV.
56 */
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 */
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 */
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 */
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 */
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
107 void dump() const {
108 ALOGD("mapper[minU=%.2f maxU=%.2f minV=%.2f maxV=%.2f]", mMinU, mMaxU, mMinV, mMaxV);
109 }
110
111private:
112 static float lerp(float start, float stop, float amount) {
113 return start + (stop - start) * amount;
114 }
115
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
Chris Blume7b8a8082018-11-30 15:51:58 -0800127} // namespace uirenderer
128} // namespace android
Romain Guy3b748a42013-04-17 18:54:38 -0700129
John Reck1bcacfd2017-11-03 10:12:19 -0700130#endif // ANDROID_HWUI_UV_MAPPER_H