michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkOperandInterpolator.h" michael@0: #include "SkScript.h" michael@0: michael@0: SkOperandInterpolator::SkOperandInterpolator() { michael@0: INHERITED::reset(0, 0); michael@0: fType = SkType_Unknown; michael@0: } michael@0: michael@0: SkOperandInterpolator::SkOperandInterpolator(int elemCount, int frameCount, michael@0: SkDisplayTypes type) michael@0: { michael@0: this->reset(elemCount, frameCount, type); michael@0: } michael@0: michael@0: void SkOperandInterpolator::reset(int elemCount, int frameCount, SkDisplayTypes type) michael@0: { michael@0: // SkASSERT(type == SkType_String || type == SkType_Float || type == SkType_Int || michael@0: // type == SkType_Displayable || type == SkType_Drawable); michael@0: INHERITED::reset(elemCount, frameCount); michael@0: fType = type; michael@0: fStorage = sk_malloc_throw((sizeof(SkOperand) * elemCount + sizeof(SkTimeCode)) * frameCount); michael@0: fTimes = (SkTimeCode*) fStorage; michael@0: fValues = (SkOperand*) ((char*) fStorage + sizeof(SkTimeCode) * frameCount); michael@0: #ifdef SK_DEBUG michael@0: fTimesArray = (SkTimeCode(*)[10]) fTimes; michael@0: fValuesArray = (SkOperand(*)[10]) fValues; michael@0: #endif michael@0: } michael@0: michael@0: bool SkOperandInterpolator::setKeyFrame(int index, SkMSec time, const SkOperand values[], SkScalar blend) michael@0: { michael@0: SkASSERT(values != NULL); michael@0: blend = SkScalarPin(blend, 0, SK_Scalar1); michael@0: michael@0: bool success = ~index == SkTSearch(&fTimes->fTime, index, time, sizeof(SkTimeCode)); michael@0: SkASSERT(success); michael@0: if (success) { michael@0: SkTimeCode* timeCode = &fTimes[index]; michael@0: timeCode->fTime = time; michael@0: timeCode->fBlend[0] = SK_Scalar1 - blend; michael@0: timeCode->fBlend[1] = 0; michael@0: timeCode->fBlend[2] = 0; michael@0: timeCode->fBlend[3] = SK_Scalar1 - blend; michael@0: SkOperand* dst = &fValues[fElemCount * index]; michael@0: memcpy(dst, values, fElemCount * sizeof(SkOperand)); michael@0: } michael@0: return success; michael@0: } michael@0: michael@0: SkInterpolatorBase::Result SkOperandInterpolator::timeToValues(SkMSec time, SkOperand values[]) const michael@0: { michael@0: SkScalar T; michael@0: int index; michael@0: SkBool exact; michael@0: Result result = timeToT(time, &T, &index, &exact); michael@0: if (values) michael@0: { michael@0: const SkOperand* nextSrc = &fValues[index * fElemCount]; michael@0: michael@0: if (exact) michael@0: memcpy(values, nextSrc, fElemCount * sizeof(SkScalar)); michael@0: else michael@0: { michael@0: SkASSERT(index > 0); michael@0: michael@0: const SkOperand* prevSrc = nextSrc - fElemCount; michael@0: michael@0: if (fType == SkType_Float || fType == SkType_3D_Point) { michael@0: for (int i = fElemCount - 1; i >= 0; --i) michael@0: values[i].fScalar = SkScalarInterp(prevSrc[i].fScalar, nextSrc[i].fScalar, T); michael@0: } else if (fType == SkType_Int || fType == SkType_MSec) { michael@0: for (int i = fElemCount - 1; i >= 0; --i) { michael@0: int32_t a = prevSrc[i].fS32; michael@0: int32_t b = nextSrc[i].fS32; michael@0: values[i].fS32 = a + SkScalarRoundToInt((b - a) * T); michael@0: } michael@0: } else michael@0: memcpy(values, prevSrc, sizeof(SkOperand) * fElemCount); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG michael@0: michael@0: #ifdef SK_SUPPORT_UNITTEST michael@0: static SkOperand* iset(SkOperand array[3], int a, int b, int c) michael@0: { michael@0: array[0].fScalar = SkIntToScalar(a); michael@0: array[1].fScalar = SkIntToScalar(b); michael@0: array[2].fScalar = SkIntToScalar(c); michael@0: return array; michael@0: } michael@0: #endif michael@0: michael@0: void SkOperandInterpolator::UnitTest() michael@0: { michael@0: #ifdef SK_SUPPORT_UNITTEST michael@0: SkOperandInterpolator inter(3, 2, SkType_Float); michael@0: SkOperand v1[3], v2[3], v[3], vv[3]; michael@0: Result result; michael@0: michael@0: inter.setKeyFrame(0, 100, iset(v1, 10, 20, 30), 0); michael@0: inter.setKeyFrame(1, 200, iset(v2, 110, 220, 330)); michael@0: michael@0: result = inter.timeToValues(0, v); michael@0: SkASSERT(result == kFreezeStart_Result); michael@0: SkASSERT(memcmp(v, v1, sizeof(v)) == 0); michael@0: michael@0: result = inter.timeToValues(99, v); michael@0: SkASSERT(result == kFreezeStart_Result); michael@0: SkASSERT(memcmp(v, v1, sizeof(v)) == 0); michael@0: michael@0: result = inter.timeToValues(100, v); michael@0: SkASSERT(result == kNormal_Result); michael@0: SkASSERT(memcmp(v, v1, sizeof(v)) == 0); michael@0: michael@0: result = inter.timeToValues(200, v); michael@0: SkASSERT(result == kNormal_Result); michael@0: SkASSERT(memcmp(v, v2, sizeof(v)) == 0); michael@0: michael@0: result = inter.timeToValues(201, v); michael@0: SkASSERT(result == kFreezeEnd_Result); michael@0: SkASSERT(memcmp(v, v2, sizeof(v)) == 0); michael@0: michael@0: result = inter.timeToValues(150, v); michael@0: SkASSERT(result == kNormal_Result); michael@0: SkASSERT(memcmp(v, iset(vv, 60, 120, 180), sizeof(v)) == 0); michael@0: michael@0: result = inter.timeToValues(125, v); michael@0: SkASSERT(result == kNormal_Result); michael@0: result = inter.timeToValues(175, v); michael@0: SkASSERT(result == kNormal_Result); michael@0: #endif michael@0: } michael@0: michael@0: #endif