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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2006 The Android Open Source Project
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10 #include "SkOperandInterpolator.h"
michael@0 11 #include "SkScript.h"
michael@0 12
michael@0 13 SkOperandInterpolator::SkOperandInterpolator() {
michael@0 14 INHERITED::reset(0, 0);
michael@0 15 fType = SkType_Unknown;
michael@0 16 }
michael@0 17
michael@0 18 SkOperandInterpolator::SkOperandInterpolator(int elemCount, int frameCount,
michael@0 19 SkDisplayTypes type)
michael@0 20 {
michael@0 21 this->reset(elemCount, frameCount, type);
michael@0 22 }
michael@0 23
michael@0 24 void SkOperandInterpolator::reset(int elemCount, int frameCount, SkDisplayTypes type)
michael@0 25 {
michael@0 26 // SkASSERT(type == SkType_String || type == SkType_Float || type == SkType_Int ||
michael@0 27 // type == SkType_Displayable || type == SkType_Drawable);
michael@0 28 INHERITED::reset(elemCount, frameCount);
michael@0 29 fType = type;
michael@0 30 fStorage = sk_malloc_throw((sizeof(SkOperand) * elemCount + sizeof(SkTimeCode)) * frameCount);
michael@0 31 fTimes = (SkTimeCode*) fStorage;
michael@0 32 fValues = (SkOperand*) ((char*) fStorage + sizeof(SkTimeCode) * frameCount);
michael@0 33 #ifdef SK_DEBUG
michael@0 34 fTimesArray = (SkTimeCode(*)[10]) fTimes;
michael@0 35 fValuesArray = (SkOperand(*)[10]) fValues;
michael@0 36 #endif
michael@0 37 }
michael@0 38
michael@0 39 bool SkOperandInterpolator::setKeyFrame(int index, SkMSec time, const SkOperand values[], SkScalar blend)
michael@0 40 {
michael@0 41 SkASSERT(values != NULL);
michael@0 42 blend = SkScalarPin(blend, 0, SK_Scalar1);
michael@0 43
michael@0 44 bool success = ~index == SkTSearch<SkMSec>(&fTimes->fTime, index, time, sizeof(SkTimeCode));
michael@0 45 SkASSERT(success);
michael@0 46 if (success) {
michael@0 47 SkTimeCode* timeCode = &fTimes[index];
michael@0 48 timeCode->fTime = time;
michael@0 49 timeCode->fBlend[0] = SK_Scalar1 - blend;
michael@0 50 timeCode->fBlend[1] = 0;
michael@0 51 timeCode->fBlend[2] = 0;
michael@0 52 timeCode->fBlend[3] = SK_Scalar1 - blend;
michael@0 53 SkOperand* dst = &fValues[fElemCount * index];
michael@0 54 memcpy(dst, values, fElemCount * sizeof(SkOperand));
michael@0 55 }
michael@0 56 return success;
michael@0 57 }
michael@0 58
michael@0 59 SkInterpolatorBase::Result SkOperandInterpolator::timeToValues(SkMSec time, SkOperand values[]) const
michael@0 60 {
michael@0 61 SkScalar T;
michael@0 62 int index;
michael@0 63 SkBool exact;
michael@0 64 Result result = timeToT(time, &T, &index, &exact);
michael@0 65 if (values)
michael@0 66 {
michael@0 67 const SkOperand* nextSrc = &fValues[index * fElemCount];
michael@0 68
michael@0 69 if (exact)
michael@0 70 memcpy(values, nextSrc, fElemCount * sizeof(SkScalar));
michael@0 71 else
michael@0 72 {
michael@0 73 SkASSERT(index > 0);
michael@0 74
michael@0 75 const SkOperand* prevSrc = nextSrc - fElemCount;
michael@0 76
michael@0 77 if (fType == SkType_Float || fType == SkType_3D_Point) {
michael@0 78 for (int i = fElemCount - 1; i >= 0; --i)
michael@0 79 values[i].fScalar = SkScalarInterp(prevSrc[i].fScalar, nextSrc[i].fScalar, T);
michael@0 80 } else if (fType == SkType_Int || fType == SkType_MSec) {
michael@0 81 for (int i = fElemCount - 1; i >= 0; --i) {
michael@0 82 int32_t a = prevSrc[i].fS32;
michael@0 83 int32_t b = nextSrc[i].fS32;
michael@0 84 values[i].fS32 = a + SkScalarRoundToInt((b - a) * T);
michael@0 85 }
michael@0 86 } else
michael@0 87 memcpy(values, prevSrc, sizeof(SkOperand) * fElemCount);
michael@0 88 }
michael@0 89 }
michael@0 90 return result;
michael@0 91 }
michael@0 92
michael@0 93 ///////////////////////////////////////////////////////////////////////////////////////
michael@0 94 ///////////////////////////////////////////////////////////////////////////////////////
michael@0 95
michael@0 96 #ifdef SK_DEBUG
michael@0 97
michael@0 98 #ifdef SK_SUPPORT_UNITTEST
michael@0 99 static SkOperand* iset(SkOperand array[3], int a, int b, int c)
michael@0 100 {
michael@0 101 array[0].fScalar = SkIntToScalar(a);
michael@0 102 array[1].fScalar = SkIntToScalar(b);
michael@0 103 array[2].fScalar = SkIntToScalar(c);
michael@0 104 return array;
michael@0 105 }
michael@0 106 #endif
michael@0 107
michael@0 108 void SkOperandInterpolator::UnitTest()
michael@0 109 {
michael@0 110 #ifdef SK_SUPPORT_UNITTEST
michael@0 111 SkOperandInterpolator inter(3, 2, SkType_Float);
michael@0 112 SkOperand v1[3], v2[3], v[3], vv[3];
michael@0 113 Result result;
michael@0 114
michael@0 115 inter.setKeyFrame(0, 100, iset(v1, 10, 20, 30), 0);
michael@0 116 inter.setKeyFrame(1, 200, iset(v2, 110, 220, 330));
michael@0 117
michael@0 118 result = inter.timeToValues(0, v);
michael@0 119 SkASSERT(result == kFreezeStart_Result);
michael@0 120 SkASSERT(memcmp(v, v1, sizeof(v)) == 0);
michael@0 121
michael@0 122 result = inter.timeToValues(99, v);
michael@0 123 SkASSERT(result == kFreezeStart_Result);
michael@0 124 SkASSERT(memcmp(v, v1, sizeof(v)) == 0);
michael@0 125
michael@0 126 result = inter.timeToValues(100, v);
michael@0 127 SkASSERT(result == kNormal_Result);
michael@0 128 SkASSERT(memcmp(v, v1, sizeof(v)) == 0);
michael@0 129
michael@0 130 result = inter.timeToValues(200, v);
michael@0 131 SkASSERT(result == kNormal_Result);
michael@0 132 SkASSERT(memcmp(v, v2, sizeof(v)) == 0);
michael@0 133
michael@0 134 result = inter.timeToValues(201, v);
michael@0 135 SkASSERT(result == kFreezeEnd_Result);
michael@0 136 SkASSERT(memcmp(v, v2, sizeof(v)) == 0);
michael@0 137
michael@0 138 result = inter.timeToValues(150, v);
michael@0 139 SkASSERT(result == kNormal_Result);
michael@0 140 SkASSERT(memcmp(v, iset(vv, 60, 120, 180), sizeof(v)) == 0);
michael@0 141
michael@0 142 result = inter.timeToValues(125, v);
michael@0 143 SkASSERT(result == kNormal_Result);
michael@0 144 result = inter.timeToValues(175, v);
michael@0 145 SkASSERT(result == kNormal_Result);
michael@0 146 #endif
michael@0 147 }
michael@0 148
michael@0 149 #endif

mercurial