1/*
2 * Copyright (C) 2011 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
17package com.android.scenegraph;
18
19import java.lang.Math;
20import java.util.ArrayList;
21
22import android.renderscript.*;
23import android.renderscript.Matrix4f;
24import android.util.Log;
25
26/**
27 * @hide
28 */
29public abstract class Transform extends SceneGraphBase {
30    Transform mParent;
31    ArrayList<Transform> mChildren;
32
33    ScriptField_SgTransform mField;
34    ScriptField_SgTransform.Item mTransformData;
35
36    public Transform() {
37        mChildren = new ArrayList<Transform>();
38        mParent = null;
39    }
40
41    public void appendChild(Transform t) {
42        mChildren.add(t);
43        t.mParent = this;
44        updateRSChildData(true);
45    }
46
47    abstract void initLocalData();
48
49    void updateRSChildData(boolean copyData) {
50        if (mField == null) {
51            return;
52        }
53        RenderScriptGL rs = SceneManager.getRS();
54        if (mChildren.size() != 0) {
55            Allocation childRSData = Allocation.createSized(rs, Element.ALLOCATION(rs),
56                                                            mChildren.size());
57            mTransformData.children = childRSData;
58
59            Allocation[] childrenAllocs = new Allocation[mChildren.size()];
60            for (int i = 0; i < mChildren.size(); i ++) {
61                Transform child = mChildren.get(i);
62                childrenAllocs[i] = child.getRSData().getAllocation();
63            }
64            childRSData.copyFrom(childrenAllocs);
65        }
66        if (copyData) {
67            mField.set(mTransformData, 0, true);
68        }
69    }
70
71    ScriptField_SgTransform getRSData() {
72        if (mField != null) {
73            return mField;
74        }
75
76        RenderScriptGL rs = SceneManager.getRS();
77        if (rs == null) {
78            return null;
79        }
80        mField = new ScriptField_SgTransform(rs, 1);
81
82        mTransformData = new ScriptField_SgTransform.Item();
83        mTransformData.name = getNameAlloc(rs);
84        mTransformData.isDirty = 1;
85        mTransformData.timestamp = 1;
86
87        initLocalData();
88        updateRSChildData(false);
89
90        mField.set(mTransformData, 0, true);
91        return mField;
92    }
93}
94
95
96
97
98
99