• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #pragma once
18 
19 #include <utils/Flattenable.h>
20 #include "FloatRect.h"
21 
22 #include <math.h>
23 #include <type_traits>
24 
25 namespace android {
26 
27 struct StretchEffect : public LightFlattenablePod<StretchEffect> {
28   constexpr static const float CONTENT_DISTANCE_STRETCHED = 1.f;
29 
30   float width = 0;
31   float height = 0;
32   float vectorX = 0;
33   float vectorY = 0;
34   float maxAmountX = 0;
35   float maxAmountY = 0;
36   FloatRect mappedChildBounds = {0, 0, 0, 0};
37 
38   bool operator==(const StretchEffect& other) const {
39     return width == other.width && height == other.height &&
40         vectorX == other.vectorX &&
41         vectorY == other.vectorY &&
42         maxAmountX == other.maxAmountX &&
43         maxAmountY == other.maxAmountY &&
44         mappedChildBounds == other.mappedChildBounds;
45   }
46 
isZeroStretchEffect47   static bool isZero(float value) {
48     constexpr float NON_ZERO_EPSILON = 0.001f;
49     return fabsf(value) <= NON_ZERO_EPSILON;
50   }
51 
isNoOpStretchEffect52   bool isNoOp() const { return isZero(vectorX) && isZero(vectorY); }
53 
hasEffectStretchEffect54   bool hasEffect() const { return !isNoOp(); }
55 
sanitizeStretchEffect56   void sanitize() {
57     // If the area is empty, or the max amount is zero, then reset back to defaults
58     if (width == 0.f || height == 0.f || isZero(maxAmountX) ||
59         isZero(maxAmountY)) {
60       *this = StretchEffect{};
61     }
62   }
63 
getStretchWidthMultiplierStretchEffect64   float getStretchWidthMultiplier() const {
65       return CONTENT_DISTANCE_STRETCHED / (1.f + abs(vectorX));
66   }
67 
getStretchHeightMultiplierStretchEffect68   float getStretchHeightMultiplier() const {
69       return CONTENT_DISTANCE_STRETCHED / (1.f + abs(vectorY));
70   }
71 };
72 
73 static_assert(std::is_trivially_copyable<StretchEffect>::value,
74               "StretchEffect must be trivially copyable to be flattenable");
75 
76 } // namespace android