rsAnimation.cpp revision afb743aca56c18beb7ab924e75cb6e070ef3e55a
1c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams/* 2c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * Copyright (C) 2009 The Android Open Source Project 3c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * 4c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * Licensed under the Apache License, Version 2.0 (the "License"); 5c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * you may not use this file except in compliance with the License. 6c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * You may obtain a copy of the License at 7c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * 8c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * http://www.apache.org/licenses/LICENSE-2.0 9c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * 10c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * Unless required by applicable law or agreed to in writing, software 11c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * distributed under the License is distributed on an "AS IS" BASIS, 12c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * See the License for the specific language governing permissions and 14c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams * limitations under the License. 15c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams */ 16c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 17fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk#ifndef ANDROID_RS_BUILD_FOR_HOST 18c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams#include "rsContext.h" 19fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk#else 20fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk#include "rsContextHostStub.h" 21fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk#endif //ANDROID_RS_BUILD_FOR_HOST 22fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk 23c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams#include "rsAnimation.h" 24c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 25c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 26c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Samsusing namespace android; 27c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Samsusing namespace android::renderscript; 28c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 29afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid Animation::serialize(OStream *stream) const { 30fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk} 31fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk 32afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukAnimation *Animation::createFromStream(Context *rsc, IStream *stream) { 33fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk return NULL; 34fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk} 35fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk 36c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams/* 37c1ed589021e280cda59a0521cb96b3e9eb629e1bJason SamsAnimation::Animation(Context *rsc) : ObjectBase(rsc) 38c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams{ 39c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mAllocFile = __FILE__; 40c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mAllocLine = __LINE__; 41c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 42c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mValuesInput = NULL; 43c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mValuesOutput = NULL; 44c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mValueCount = 0; 45c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mInterpolation = RS_ANIMATION_INTERPOLATION_STEP; 46c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mEdgePre = RS_ANIMATION_EDGE_UNDEFINED; 47c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mEdgePost = RS_ANIMATION_EDGE_UNDEFINED; 48c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mInputMin = 0; 49c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams mInputMax = 0; 50c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams} 51c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 52c1ed589021e280cda59a0521cb96b3e9eb629e1bJason SamsAnimation * Animation::create(Context *rsc, 53c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams const float *inValues, const float *outValues, 54c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams uint32_t valueCount, RsAnimationInterpolation interp, 55c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams RsAnimationEdge pre, RsAnimationEdge post) 56c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams{ 57c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams if (valueCount < 2) { 58c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams rsc->setError(RS_ERROR_BAD_VALUE, "Animations require more than 2 values."); 59c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams return NULL; 60c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 61c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams Animation *a = new Animation(rsc); 62c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams if (!a) { 63c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams rsc->setError(RS_ERROR_OUT_OF_MEMORY); 64c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams return NULL; 65c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 66c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 67c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams float *vin = (float *)malloc(valueCount * sizeof(float)); 68c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams float *vout = (float *)malloc(valueCount * sizeof(float)); 69c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mValuesInput = vin; 70c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mValuesOutput = vout; 71c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams if (a->mValuesInput == NULL || a->mValuesOutput == NULL) { 72c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams delete a; 73c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams rsc->setError(RS_ERROR_OUT_OF_MEMORY); 74c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams return NULL; 75c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 76c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 77c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mEdgePre = pre; 78c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mEdgePost = post; 79c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mInterpolation = interp; 80c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mValueCount = valueCount; 81c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 82c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams memcpy(vin, inValues, valueCount * sizeof(float)); 83c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams memcpy(vout, outValues, valueCount * sizeof(float)); 84c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mInputMin = inValues[0]; 85c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mInputMax = inValues[0]; 86c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 87c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams bool needSort = false; 88c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams for (uint32_t ct=1; ct < valueCount; ct++) { 89c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams if (a->mInputMin > vin[ct]) { 90c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams needSort = true; 91c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mInputMin = vin[ct]; 92c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 93c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams if (a->mInputMax < vin[ct]) { 94c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->mInputMax = vin[ct]; 95c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } else { 96c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams needSort = true; 97c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 98c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 99c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 100c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams while (1) { 101c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams bool changed = false; 102c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams for (uint32_t ct=1; ct < valueCount; ct++) { 103c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams if (vin[ct-1] > vin[ct]) { 104c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams float t = vin[ct-1]; 105c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams vin[ct-1] = vin[ct]; 106c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams vin[ct] = t; 107c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams t = vout[ct-1]; 108c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams vout[ct-1] = vout[ct]; 109c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams vout[ct] = t; 110c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams changed = true; 111c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 112c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 113c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams if (!changed) break; 114c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 115c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 116c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams return a; 117c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams} 118c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams*/ 119c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 120c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 121c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams///////////////////////////////////////// 122c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams// 123c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 124c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Samsnamespace android { 125c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Samsnamespace renderscript { 126c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 127c1ed589021e280cda59a0521cb96b3e9eb629e1bJason SamsRsAnimation rsi_AnimationCreate(Context *rsc, 128c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams const float *inValues, 129c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams const float *outValues, 130c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams uint32_t valueCount, 131c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams RsAnimationInterpolation interp, 132c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams RsAnimationEdge pre, 133afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk RsAnimationEdge post) { 134c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams //LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize); 135c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams Animation *a = NULL;//Animation::create(rsc, inValues, outValues, valueCount, interp, pre, post); 136c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams if (a != NULL) { 137c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams a->incUserRef(); 138c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams } 139c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams return (RsAnimation)a; 140c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams} 141c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 142c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 143c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams} 144c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams} 145c1ed589021e280cda59a0521cb96b3e9eb629e1bJason Sams 146