• 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 
47   bool operator!=(const StretchEffect& other) const { return !(*this == other); }
48 
isZeroStretchEffect49   static bool isZero(float value) {
50     constexpr float NON_ZERO_EPSILON = 0.001f;
51     return fabsf(value) <= NON_ZERO_EPSILON;
52   }
53 
isNoOpStretchEffect54   bool isNoOp() const { return isZero(vectorX) && isZero(vectorY); }
55 
hasEffectStretchEffect56   bool hasEffect() const { return !isNoOp(); }
57 
sanitizeStretchEffect58   void sanitize() {
59     // If the area is empty, or the max amount is zero, then reset back to defaults
60     if (width == 0.f || height == 0.f || isZero(maxAmountX) ||
61         isZero(maxAmountY)) {
62       *this = StretchEffect{};
63     }
64   }
65 
getStretchWidthMultiplierStretchEffect66   float getStretchWidthMultiplier() const {
67       return CONTENT_DISTANCE_STRETCHED / (1.f + abs(vectorX));
68   }
69 
getStretchHeightMultiplierStretchEffect70   float getStretchHeightMultiplier() const {
71       return CONTENT_DISTANCE_STRETCHED / (1.f + abs(vectorY));
72   }
73 };
74 
75 static_assert(std::is_trivially_copyable<StretchEffect>::value,
76               "StretchEffect must be trivially copyable to be flattenable");
77 
78 } // namespace android