Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include "PropertyValuesHolder.h" |
| 18 | |
| 19 | #include "utils/VectorDrawableUtils.h" |
| 20 | |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | using namespace VectorDrawable; |
| 27 | |
| 28 | float PropertyValuesHolder::getValueFromData(float fraction) { |
| 29 | if (mDataSource.size() == 0) { |
| 30 | LOG_ALWAYS_FATAL("No data source is defined"); |
| 31 | return 0; |
| 32 | } |
| 33 | if (fraction <= 0.0f) { |
| 34 | return mDataSource.front(); |
| 35 | } |
| 36 | if (fraction >= 1.0f) { |
| 37 | return mDataSource.back(); |
| 38 | } |
| 39 | |
| 40 | fraction *= mDataSource.size() - 1; |
| 41 | int lowIndex = floor(fraction); |
| 42 | fraction -= lowIndex; |
| 43 | |
| 44 | float value = mDataSource[lowIndex] * (1.0f - fraction) |
| 45 | + mDataSource[lowIndex + 1] * fraction; |
| 46 | return value; |
| 47 | } |
| 48 | |
| 49 | void GroupPropertyValuesHolder::setFraction(float fraction) { |
| 50 | float animatedValue; |
| 51 | if (mDataSource.size() > 0) { |
| 52 | animatedValue = getValueFromData(fraction); |
| 53 | } else { |
| 54 | animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction; |
| 55 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 56 | mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | inline U8CPU lerp(U8CPU fromValue, U8CPU toValue, float fraction) { |
| 60 | return (U8CPU) (fromValue * (1 - fraction) + toValue * fraction); |
| 61 | } |
| 62 | |
| 63 | // TODO: Add a test for this |
| 64 | SkColor FullPathColorPropertyValuesHolder::interpolateColors(SkColor fromColor, SkColor toColor, |
| 65 | float fraction) { |
| 66 | U8CPU alpha = lerp(SkColorGetA(fromColor), SkColorGetA(toColor), fraction); |
| 67 | U8CPU red = lerp(SkColorGetR(fromColor), SkColorGetR(toColor), fraction); |
| 68 | U8CPU green = lerp(SkColorGetG(fromColor), SkColorGetG(toColor), fraction); |
| 69 | U8CPU blue = lerp(SkColorGetB(fromColor), SkColorGetB(toColor), fraction); |
| 70 | return SkColorSetARGB(alpha, red, green, blue); |
| 71 | } |
| 72 | |
| 73 | void FullPathColorPropertyValuesHolder::setFraction(float fraction) { |
| 74 | SkColor animatedValue = interpolateColors(mStartValue, mEndValue, fraction); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 75 | mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void FullPathPropertyValuesHolder::setFraction(float fraction) { |
| 79 | float animatedValue; |
| 80 | if (mDataSource.size() > 0) { |
| 81 | animatedValue = getValueFromData(fraction); |
| 82 | } else { |
| 83 | animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction; |
| 84 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 85 | mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void PathDataPropertyValuesHolder::setFraction(float fraction) { |
| 89 | VectorDrawableUtils::interpolatePaths(&mPathData, mStartValue, mEndValue, fraction); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 90 | mPath->mutateProperties()->setData(mPathData); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void RootAlphaPropertyValuesHolder::setFraction(float fraction) { |
| 94 | float animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 95 | mTree->mutateProperties()->setRootAlpha(animatedValue); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namepace uirenderer |
| 99 | } // namespace android |