gfx/skia/trunk/src/animator/SkOperandIterpolator.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/animator/SkOperandIterpolator.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2006 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#include "SkOperandInterpolator.h"
    1.14 +#include "SkScript.h"
    1.15 +
    1.16 +SkOperandInterpolator::SkOperandInterpolator() {
    1.17 +    INHERITED::reset(0, 0);
    1.18 +    fType = SkType_Unknown;
    1.19 +}
    1.20 +
    1.21 +SkOperandInterpolator::SkOperandInterpolator(int elemCount, int frameCount,
    1.22 +                                             SkDisplayTypes type)
    1.23 +{
    1.24 +    this->reset(elemCount, frameCount, type);
    1.25 +}
    1.26 +
    1.27 +void SkOperandInterpolator::reset(int elemCount, int frameCount, SkDisplayTypes type)
    1.28 +{
    1.29 +//  SkASSERT(type == SkType_String || type == SkType_Float || type == SkType_Int ||
    1.30 +//      type == SkType_Displayable || type == SkType_Drawable);
    1.31 +    INHERITED::reset(elemCount, frameCount);
    1.32 +    fType = type;
    1.33 +    fStorage = sk_malloc_throw((sizeof(SkOperand) * elemCount + sizeof(SkTimeCode)) * frameCount);
    1.34 +    fTimes = (SkTimeCode*) fStorage;
    1.35 +    fValues = (SkOperand*) ((char*) fStorage + sizeof(SkTimeCode) * frameCount);
    1.36 +#ifdef SK_DEBUG
    1.37 +    fTimesArray = (SkTimeCode(*)[10]) fTimes;
    1.38 +    fValuesArray = (SkOperand(*)[10]) fValues;
    1.39 +#endif
    1.40 +}
    1.41 +
    1.42 +bool SkOperandInterpolator::setKeyFrame(int index, SkMSec time, const SkOperand values[], SkScalar blend)
    1.43 +{
    1.44 +    SkASSERT(values != NULL);
    1.45 +    blend = SkScalarPin(blend, 0, SK_Scalar1);
    1.46 +
    1.47 +    bool success = ~index == SkTSearch<SkMSec>(&fTimes->fTime, index, time, sizeof(SkTimeCode));
    1.48 +    SkASSERT(success);
    1.49 +    if (success) {
    1.50 +        SkTimeCode* timeCode = &fTimes[index];
    1.51 +        timeCode->fTime = time;
    1.52 +        timeCode->fBlend[0] = SK_Scalar1 - blend;
    1.53 +        timeCode->fBlend[1] = 0;
    1.54 +        timeCode->fBlend[2] = 0;
    1.55 +        timeCode->fBlend[3] = SK_Scalar1 - blend;
    1.56 +        SkOperand* dst = &fValues[fElemCount * index];
    1.57 +        memcpy(dst, values, fElemCount * sizeof(SkOperand));
    1.58 +    }
    1.59 +    return success;
    1.60 +}
    1.61 +
    1.62 +SkInterpolatorBase::Result SkOperandInterpolator::timeToValues(SkMSec time, SkOperand values[]) const
    1.63 +{
    1.64 +    SkScalar T;
    1.65 +    int index;
    1.66 +    SkBool exact;
    1.67 +    Result result = timeToT(time, &T, &index, &exact);
    1.68 +    if (values)
    1.69 +    {
    1.70 +        const SkOperand* nextSrc = &fValues[index * fElemCount];
    1.71 +
    1.72 +        if (exact)
    1.73 +            memcpy(values, nextSrc, fElemCount * sizeof(SkScalar));
    1.74 +        else
    1.75 +        {
    1.76 +            SkASSERT(index > 0);
    1.77 +
    1.78 +            const SkOperand* prevSrc = nextSrc - fElemCount;
    1.79 +
    1.80 +            if (fType == SkType_Float || fType == SkType_3D_Point) {
    1.81 +                for (int i = fElemCount - 1; i >= 0; --i)
    1.82 +                    values[i].fScalar = SkScalarInterp(prevSrc[i].fScalar, nextSrc[i].fScalar, T);
    1.83 +            } else if (fType == SkType_Int || fType == SkType_MSec) {
    1.84 +                for (int i = fElemCount - 1; i >= 0; --i) {
    1.85 +                    int32_t a = prevSrc[i].fS32;
    1.86 +                    int32_t b = nextSrc[i].fS32;
    1.87 +                    values[i].fS32 = a + SkScalarRoundToInt((b - a) * T);
    1.88 +                }
    1.89 +            } else
    1.90 +                memcpy(values, prevSrc, sizeof(SkOperand) * fElemCount);
    1.91 +        }
    1.92 +    }
    1.93 +    return result;
    1.94 +}
    1.95 +
    1.96 +///////////////////////////////////////////////////////////////////////////////////////
    1.97 +///////////////////////////////////////////////////////////////////////////////////////
    1.98 +
    1.99 +#ifdef SK_DEBUG
   1.100 +
   1.101 +#ifdef SK_SUPPORT_UNITTEST
   1.102 +    static SkOperand* iset(SkOperand array[3], int a, int b, int c)
   1.103 +    {
   1.104 +        array[0].fScalar = SkIntToScalar(a);
   1.105 +        array[1].fScalar = SkIntToScalar(b);
   1.106 +        array[2].fScalar = SkIntToScalar(c);
   1.107 +        return array;
   1.108 +    }
   1.109 +#endif
   1.110 +
   1.111 +void SkOperandInterpolator::UnitTest()
   1.112 +{
   1.113 +#ifdef SK_SUPPORT_UNITTEST
   1.114 +    SkOperandInterpolator   inter(3, 2, SkType_Float);
   1.115 +    SkOperand       v1[3], v2[3], v[3], vv[3];
   1.116 +    Result          result;
   1.117 +
   1.118 +    inter.setKeyFrame(0, 100, iset(v1, 10, 20, 30), 0);
   1.119 +    inter.setKeyFrame(1, 200, iset(v2, 110, 220, 330));
   1.120 +
   1.121 +    result = inter.timeToValues(0, v);
   1.122 +    SkASSERT(result == kFreezeStart_Result);
   1.123 +    SkASSERT(memcmp(v, v1, sizeof(v)) == 0);
   1.124 +
   1.125 +    result = inter.timeToValues(99, v);
   1.126 +    SkASSERT(result == kFreezeStart_Result);
   1.127 +    SkASSERT(memcmp(v, v1, sizeof(v)) == 0);
   1.128 +
   1.129 +    result = inter.timeToValues(100, v);
   1.130 +    SkASSERT(result == kNormal_Result);
   1.131 +    SkASSERT(memcmp(v, v1, sizeof(v)) == 0);
   1.132 +
   1.133 +    result = inter.timeToValues(200, v);
   1.134 +    SkASSERT(result == kNormal_Result);
   1.135 +    SkASSERT(memcmp(v, v2, sizeof(v)) == 0);
   1.136 +
   1.137 +    result = inter.timeToValues(201, v);
   1.138 +    SkASSERT(result == kFreezeEnd_Result);
   1.139 +    SkASSERT(memcmp(v, v2, sizeof(v)) == 0);
   1.140 +
   1.141 +    result = inter.timeToValues(150, v);
   1.142 +    SkASSERT(result == kNormal_Result);
   1.143 +    SkASSERT(memcmp(v, iset(vv, 60, 120, 180), sizeof(v)) == 0);
   1.144 +
   1.145 +    result = inter.timeToValues(125, v);
   1.146 +    SkASSERT(result == kNormal_Result);
   1.147 +    result = inter.timeToValues(175, v);
   1.148 +    SkASSERT(result == kNormal_Result);
   1.149 +#endif
   1.150 +}
   1.151 +
   1.152 +#endif

mercurial