Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2014 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 #ifndef ANIMATOR_H
     17 #define ANIMATOR_H
     18 
     19 #include <cutils/compiler.h>
     20 #include <utils/RefBase.h>
     21 #include <utils/StrongPointer.h>
     22 
     23 #include "CanvasProperty.h"
     24 #include "Interpolator.h"
     25 #include "TreeInfo.h"
     26 #include "utils/Macros.h"
     27 
     28 namespace android {
     29 namespace uirenderer {
     30 
     31 class AnimationContext;
     32 class BaseRenderNodeAnimator;
     33 class RenderNode;
     34 class RenderProperties;
     35 
     36 class AnimationListener : public VirtualLightRefBase {
     37 public:
     38     ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0;
     39 protected:
     40     ANDROID_API virtual ~AnimationListener() {}
     41 };
     42 
     43 class BaseRenderNodeAnimator : public VirtualLightRefBase {
     44     PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
     45 public:
     46     ANDROID_API void setStartValue(float value);
     47     ANDROID_API void setInterpolator(Interpolator* interpolator);
     48     ANDROID_API void setDuration(nsecs_t durationInMs);
     49     ANDROID_API nsecs_t duration() { return mDuration; }
     50     ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
     51     ANDROID_API nsecs_t startDelay() { return mStartDelay; }
     52     ANDROID_API void setListener(AnimationListener* listener) {
     53         mListener = listener;
     54     }
     55     AnimationListener* listener() { return mListener.get(); }
     56     ANDROID_API void setAllowRunningAsync(bool mayRunAsync) {
     57         mMayRunAsync = mayRunAsync;
     58     }
     59     bool mayRunAsync() { return mMayRunAsync; }
     60     ANDROID_API void start() { mStagingPlayState = RUNNING; onStagingPlayStateChanged(); }
     61     ANDROID_API void end() { mStagingPlayState = FINISHED; onStagingPlayStateChanged(); }
     62 
     63     void attach(RenderNode* target);
     64     virtual void onAttached() {}
     65     void detach() { mTarget = 0; }
     66     void pushStaging(AnimationContext& context);
     67     bool animate(AnimationContext& context);
     68 
     69     bool isRunning() { return mPlayState == RUNNING; }
     70     bool isFinished() { return mPlayState == FINISHED; }
     71     float finalValue() { return mFinalValue; }
     72 
     73     ANDROID_API virtual uint32_t dirtyMask() = 0;
     74 
     75     void forceEndNow(AnimationContext& context);
     76 
     77 protected:
     78     BaseRenderNodeAnimator(float finalValue);
     79     virtual ~BaseRenderNodeAnimator();
     80 
     81     virtual float getValue(RenderNode* target) const = 0;
     82     virtual void setValue(RenderNode* target, float value) = 0;
     83     RenderNode* target() { return mTarget; }
     84 
     85     void callOnFinishedListener(AnimationContext& context);
     86 
     87     virtual void onStagingPlayStateChanged() {}
     88 
     89     enum PlayState {
     90         NOT_STARTED,
     91         RUNNING,
     92         FINISHED,
     93     };
     94 
     95     RenderNode* mTarget;
     96 
     97     float mFinalValue;
     98     float mDeltaValue;
     99     float mFromValue;
    100 
    101     Interpolator* mInterpolator;
    102     PlayState mStagingPlayState;
    103     PlayState mPlayState;
    104     bool mHasStartValue;
    105     nsecs_t mStartTime;
    106     nsecs_t mDuration;
    107     nsecs_t mStartDelay;
    108     bool mMayRunAsync;
    109 
    110     sp<AnimationListener> mListener;
    111 
    112 private:
    113     inline void checkMutable();
    114     virtual void transitionToRunning(AnimationContext& context);
    115     void doSetStartValue(float value);
    116 };
    117 
    118 class RenderPropertyAnimator : public BaseRenderNodeAnimator {
    119 public:
    120     enum RenderProperty {
    121         TRANSLATION_X = 0,
    122         TRANSLATION_Y,
    123         TRANSLATION_Z,
    124         SCALE_X,
    125         SCALE_Y,
    126         ROTATION,
    127         ROTATION_X,
    128         ROTATION_Y,
    129         X,
    130         Y,
    131         Z,
    132         ALPHA,
    133     };
    134 
    135     ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
    136 
    137     ANDROID_API virtual uint32_t dirtyMask();
    138 
    139 protected:
    140     virtual float getValue(RenderNode* target) const;
    141     virtual void setValue(RenderNode* target, float value);
    142     virtual void onAttached();
    143     virtual void onStagingPlayStateChanged();
    144 
    145 private:
    146     typedef bool (RenderProperties::*SetFloatProperty)(float value);
    147     typedef float (RenderProperties::*GetFloatProperty)() const;
    148 
    149     struct PropertyAccessors;
    150     const PropertyAccessors* mPropertyAccess;
    151 
    152     static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
    153 };
    154 
    155 class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
    156 public:
    157     ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
    158             float finalValue);
    159 
    160     ANDROID_API virtual uint32_t dirtyMask();
    161 
    162 protected:
    163     virtual float getValue(RenderNode* target) const;
    164     virtual void setValue(RenderNode* target, float value);
    165 private:
    166     sp<CanvasPropertyPrimitive> mProperty;
    167 };
    168 
    169 class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
    170 public:
    171     enum PaintField {
    172         STROKE_WIDTH = 0,
    173         ALPHA,
    174     };
    175 
    176     ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
    177             PaintField field, float finalValue);
    178 
    179     ANDROID_API virtual uint32_t dirtyMask();
    180 
    181 protected:
    182     virtual float getValue(RenderNode* target) const;
    183     virtual void setValue(RenderNode* target, float value);
    184 private:
    185     sp<CanvasPropertyPaint> mProperty;
    186     PaintField mField;
    187 };
    188 
    189 class RevealAnimator : public BaseRenderNodeAnimator {
    190 public:
    191     ANDROID_API RevealAnimator(int centerX, int centerY,
    192             float startValue, float finalValue);
    193 
    194     ANDROID_API virtual uint32_t dirtyMask();
    195 
    196 protected:
    197     virtual float getValue(RenderNode* target) const;
    198     virtual void setValue(RenderNode* target, float value);
    199 
    200 private:
    201     int mCenterX, mCenterY;
    202 };
    203 
    204 } /* namespace uirenderer */
    205 } /* namespace android */
    206 
    207 #endif /* ANIMATOR_H */
    208