1221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams/*
269cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams * Copyright (C) 2012 The Android Open Source Project
3221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams *
4221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams * you may not use this file except in compliance with the License.
6221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams * You may obtain a copy of the License at
7221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams *
8221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams *
10221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams * Unless required by applicable law or agreed to in writing, software
11221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams * See the License for the specific language governing permissions and
14221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams * limitations under the License.
15221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams */
16221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
17221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams#include <malloc.h>
18221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams#include <string.h>
19221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
20221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams#include "RenderScript.h"
21eeaf7142d7e06efb3e0ddc7ef542884ab1d527dcTim Murray#include "rsCppInternal.h"
22221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
2369cccdf0659a193d6a75420ec745421fb5c436e6Jason Samsusing namespace android;
249eb7f4b90120ebe4be74343856e86b46495f72dfTim Murrayusing namespace RSC;
25221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
2689daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::getSubElement(uint32_t index) {
27bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (!mVisibleElementMapSize) {
2821fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
2944bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
30221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
31bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (index >= mVisibleElementMapSize) {
3221fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
3344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
34221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
35221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return mElements[mVisibleElementMap[index]];
36221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
37221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
38221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samsconst char * Element::getSubElementName(uint32_t index) {
39bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (!mVisibleElementMapSize) {
4021fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
4144bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
42221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
43bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (index >= mVisibleElementMapSize) {
4421fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
4544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
46221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
47bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    return mElementNames[mVisibleElementMap[index]];
48221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
49221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
50221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samssize_t Element::getSubElementArraySize(uint32_t index) {
51bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (!mVisibleElementMapSize) {
5221fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
5321fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return 0;
54221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
55bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (index >= mVisibleElementMapSize) {
5621fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
5721fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return 0;
58221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
59221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return mArraySizes[mVisibleElementMap[index]];
60221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
61221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
62221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samsuint32_t Element::getSubElementOffsetBytes(uint32_t index) {
63bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (!mVisibleElementMapSize) {
6421fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
6521fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return 0;
66221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
67bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (index >= mVisibleElementMapSize) {
6821fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
6921fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return 0;
70221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
71221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return mOffsetInBytes[mVisibleElementMap[index]];
72221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
73221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
74221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
7589daad6bae798779e57f252e9da4fe4e62337124Tim Murray#define CREATE_USER(N, T) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \
7644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (rs->mElements.N == nullptr) {                               \
77eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray        rs->mElements.N = (createUser(rs, RS_TYPE_##T));            \
78729b6fe58aff47c4f666b22bbb7a6d6114ddefa9Tim Murray    }                                                               \
79729b6fe58aff47c4f666b22bbb7a6d6114ddefa9Tim Murray    return rs->mElements.N;                                         \
80729b6fe58aff47c4f666b22bbb7a6d6114ddefa9Tim Murray    }
81729b6fe58aff47c4f666b22bbb7a6d6114ddefa9Tim Murray
82221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(BOOLEAN, BOOLEAN);
83221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(U8, UNSIGNED_8);
84221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(I8, SIGNED_8);
85221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(U16, UNSIGNED_16);
86221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(I16, SIGNED_16);
87221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(U32, UNSIGNED_32);
88221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(I32, SIGNED_32);
89221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(U64, UNSIGNED_64);
90221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(I64, SIGNED_64);
91221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(F32, FLOAT_32);
92221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(F64, FLOAT_64);
93221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(ELEMENT, ELEMENT);
94221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(TYPE, TYPE);
95221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(ALLOCATION, ALLOCATION);
96221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(SAMPLER, SAMPLER);
97221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(SCRIPT, SCRIPT);
98221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(MATRIX_4X4, MATRIX_4X4);
99221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(MATRIX_3X3, MATRIX_3X3);
100221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_USER(MATRIX_2X2, MATRIX_2X2);
101221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
10289daad6bae798779e57f252e9da4fe4e62337124Tim Murray#define CREATE_PIXEL(N, T, K) android::RSC::sp<const Element> Element::N(android::RSC::sp<RS> rs) { \
10344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (rs->mElements.N == nullptr) {                                \
10444bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        rs->mElements.N = createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
10544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    }                                                                \
10644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    return rs->mElements.N;                                          \
107221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
108eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray
109221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
110221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_PIXEL(RGB_565, UNSIGNED_5_6_5, PIXEL_RGB);
111221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_PIXEL(RGB_888, UNSIGNED_8, PIXEL_RGB);
112221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
113221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
114eb4426dfb63983559cf903b2ea984569e990c4fdTim MurrayCREATE_PIXEL(YUV, UNSIGNED_8, PIXEL_YUV);
115071aee6b0cffde1ce3ece5e32e34361db2105547Tim MurrayCREATE_PIXEL(RGBA_5551, UNSIGNED_5_5_5_1, PIXEL_RGBA);
116221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
11789daad6bae798779e57f252e9da4fe4e62337124Tim Murray#define CREATE_VECTOR(N, T) android::RSC::sp<const Element> Element::N##_2(android::RSC::sp<RS> rs) { \
11844bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (rs->mElements.N##_2 == nullptr) {                                 \
11944bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        rs->mElements.N##_2 = createVector(rs, RS_TYPE_##T, 2);           \
12044bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    }                                                                     \
12144bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    return rs->mElements.N##_2;                                           \
12244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes}                                                                         \
12389daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::N##_3(android::RSC::sp<RS> rs) { \
12444bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (rs->mElements.N##_3 == nullptr) {                                 \
12544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        rs->mElements.N##_3 = createVector(rs, RS_TYPE_##T, 3);           \
12644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    }                                                                     \
12744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    return rs->mElements.N##_3;                                           \
128221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams} \
12989daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::N##_4(android::RSC::sp<RS> rs) { \
13044bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (rs->mElements.N##_4 == nullptr) {                                 \
13144bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        rs->mElements.N##_4 = createVector(rs, RS_TYPE_##T, 4);           \
13244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    }                                                                     \
13344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    return rs->mElements.N##_4;                                           \
134221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
135221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(U8, UNSIGNED_8);
136221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(I8, SIGNED_8);
137221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(U16, UNSIGNED_16);
138221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(I16, SIGNED_16);
139221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(U32, UNSIGNED_32);
140221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(I32, SIGNED_32);
141221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(U64, UNSIGNED_64);
142221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(I64, SIGNED_64);
143221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(F32, FLOAT_32);
144221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsCREATE_VECTOR(F64, FLOAT_64);
145221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
146221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
147221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samsvoid Element::updateVisibleSubElements() {
148bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (!mElementsCount) {
149221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return;
150221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
151bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (mVisibleElementMapSize) {
152bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mVisibleElementMap);
153bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        mVisibleElementMapSize = 0;
154bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    }
155bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mVisibleElementMap = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t));
156221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
157221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    int noPaddingFieldCount = 0;
158bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    size_t fieldCount = mElementsCount;
159221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Find out how many elements are not padding
160221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    for (size_t ct = 0; ct < fieldCount; ct ++) {
161bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        if (mElementNames[ct][0] != '#') {
162221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            noPaddingFieldCount ++;
163221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        }
164221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
165221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
166221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Make a map that points us at non-padding elements
167bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    size_t i = 0;
168221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    for (size_t ct = 0; ct < fieldCount; ct ++) {
169bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        if (mElementNames[ct][0] != '#') {
170bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang            mVisibleElementMap[i++] = (uint32_t)ct;
171221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        }
172221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
173bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mVisibleElementMapSize = i;
174221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
175221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
17689daad6bae798779e57f252e9da4fe4e62337124Tim MurrayElement::Element(void *id, android::RSC::sp<RS> rs,
177bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang                 android::RSC::sp<const Element> * elements,
178bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang                 size_t elementCount,
179bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang                 const char ** elementNames,
180bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang                 size_t * elementNameLengths,
181bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang                 uint32_t * arraySizes) : BaseObj(id, rs) {
182221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mSizeBytes = 0;
183221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mVectorSize = 1;
184bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementsCount = elementCount;
185bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mVisibleElementMap = nullptr;
186bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mVisibleElementMapSize = 0;
187bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
188bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElements = (android::RSC::sp<const Element> *)calloc(mElementsCount, sizeof(android::RSC::sp<Element>));
189bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementNames = (char **)calloc(mElementsCount, sizeof(char *));
190bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementNameLengths = (size_t*)calloc(mElementsCount, sizeof(size_t));
191bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mArraySizes = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t));
192bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mOffsetInBytes = (uint32_t*)calloc(mElementsCount, sizeof(uint32_t));
193bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
194bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    memcpy(mElements, elements, mElementsCount * sizeof(android::RSC::sp<Element>));
195bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    memcpy(mArraySizes, arraySizes, mElementsCount * sizeof(uint32_t));
196bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
197bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    //copy strings (char array)
198bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    memcpy(mElementNameLengths, elementNameLengths, mElementsCount * sizeof(size_t));
199bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    for (size_t ct = 0; ct < mElementsCount; ct++ ) {
200bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        size_t elemNameLen = mElementNameLengths[ct];
201bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        mElementNames[ct] = (char *)calloc(elemNameLen, sizeof(char));
202bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        memcpy(mElementNames[ct], elementNames[ct], elemNameLen);
203bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    }
204221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
205221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mType = RS_TYPE_NONE;
206221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mKind = RS_KIND_USER;
207221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
208bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    for (size_t ct = 0; ct < mElementsCount; ct++ ) {
209bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        mOffsetInBytes[ct] = mSizeBytes;
210221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
211221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
212221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    updateVisibleSubElements();
213221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
214221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
215221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
216221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samsstatic uint32_t GetSizeInBytesForType(RsDataType dt) {
217221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    switch(dt) {
218221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_NONE:
219221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 0;
220221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SIGNED_8:
221221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_8:
222221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_BOOLEAN:
223221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 1;
224221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
225221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_FLOAT_16:
226221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SIGNED_16:
227221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_16:
228221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_5_6_5:
229221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_5_5_5_1:
230221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_4_4_4_4:
231221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 2;
232221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
233221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_FLOAT_32:
234221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SIGNED_32:
235221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_32:
236221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 4;
237221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
238221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_FLOAT_64:
239221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SIGNED_64:
240221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_64:
241221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 8;
242221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
243221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_MATRIX_4X4:
244221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 16 * 4;
245221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_MATRIX_3X3:
246221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 9 * 4;
247221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_MATRIX_2X2:
248221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 4 * 4;
249221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
250221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_TYPE:
251221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_ALLOCATION:
252221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SAMPLER:
253221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SCRIPT:
254221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_MESH:
255221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_PROGRAM_FRAGMENT:
256221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_PROGRAM_VERTEX:
257221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_PROGRAM_RASTER:
258221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_PROGRAM_STORE:
259221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 4;
260221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
261221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    default:
262221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
263221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
264221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
265221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    ALOGE("Missing type %i", dt);
266221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return 0;
267221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
268221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
26989daad6bae798779e57f252e9da4fe4e62337124Tim MurrayElement::Element(void *id, android::RSC::sp<RS> rs,
270221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams                 RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
271221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    BaseObj(id, rs)
272221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams{
273221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    uint32_t tsize = GetSizeInBytesForType(dt);
274221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if ((dt != RS_TYPE_UNSIGNED_5_6_5) &&
275221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        (dt != RS_TYPE_UNSIGNED_4_4_4_4) &&
276221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        (dt != RS_TYPE_UNSIGNED_5_5_5_1)) {
277221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        if (size == 3) {
278221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            mSizeBytes = tsize * 4;
279221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        } else {
280221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            mSizeBytes = tsize * size;
281221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        }
282221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    } else {
283221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        mSizeBytes = tsize;
284221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
285221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mType = dt;
286221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mKind = dk;
287221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mNormalized = norm;
288221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mVectorSize = size;
289bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementsCount = 0;
290bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mVisibleElementMap = 0;
291221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
292221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
293221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsElement::~Element() {
294bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (mElementsCount) {
295bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mElements);
296bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        for (size_t ct = 0; ct < mElementsCount; ct++ ) {
297bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang            free(mElementNames[ct]);
298bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        }
299bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mElementNames);
300bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mElementNameLengths);
301bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mArraySizes);
302bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mOffsetInBytes);
303bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    }
304bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (mVisibleElementMapSize) {
305bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mVisibleElementMap);
306bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    }
307221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
308221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
309221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samsvoid Element::updateFromNative() {
310221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    BaseObj::updateFromNative();
311221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    updateVisibleSubElements();
312221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
313221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
31489daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::createUser(android::RSC::sp<RS> rs, RsDataType dt) {
315a423096c0d49e5cfe13a400b4323a76f89c6885cTim Murray    void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
316221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return new Element(id, rs, dt, RS_KIND_USER, false, 1);
317221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
318221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
31989daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::createVector(android::RSC::sp<RS> rs, RsDataType dt, uint32_t size) {
320221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (size < 2 || size > 4) {
32121fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Vector size out of range 2-4.");
32244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
323221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
324a423096c0d49e5cfe13a400b4323a76f89c6885cTim Murray    void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
325221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return new Element(id, rs, dt, RS_KIND_USER, false, size);
326221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
327221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
32889daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::createPixel(android::RSC::sp<RS> rs, RsDataType dt, RsDataKind dk) {
329221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (!(dk == RS_KIND_PIXEL_L ||
330221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dk == RS_KIND_PIXEL_A ||
331221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dk == RS_KIND_PIXEL_LA ||
332221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dk == RS_KIND_PIXEL_RGB ||
333221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dk == RS_KIND_PIXEL_RGBA ||
33443a6cba1be3ef984cd619c85373973cbb93edf5eMiao Wang          dk == RS_KIND_PIXEL_DEPTH ||
33543a6cba1be3ef984cd619c85373973cbb93edf5eMiao Wang          dk == RS_KIND_PIXEL_YUV)) {
33621fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataKind");
33744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
338221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
339221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (!(dt == RS_TYPE_UNSIGNED_8 ||
340221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dt == RS_TYPE_UNSIGNED_16 ||
341221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dt == RS_TYPE_UNSIGNED_5_6_5 ||
342221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
343221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
34421fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataType");
34544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
346221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
347221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
34821fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
34944bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
350221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
351221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
35221fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
35344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
354221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
355221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
35621fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
35744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
358221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
359221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
36021fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
36144bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
362221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
363221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
364221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    int size = 1;
365221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    switch (dk) {
366221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_KIND_PIXEL_LA:
367221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size = 2;
368221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
369221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_KIND_PIXEL_RGB:
370221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size = 3;
371221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
372221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_KIND_PIXEL_RGBA:
373221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size = 4;
374221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
375221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_KIND_PIXEL_DEPTH:
376221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size = 2;
377221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
378221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    default:
379221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
380221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
381221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
382a423096c0d49e5cfe13a400b4323a76f89c6885cTim Murray    void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size);
383221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return new Element(id, rs, dt, dk, true, size);
384221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
385221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
38610913a5c37ba119bef335320d3e8be25212c05adTim Murraybool Element::isCompatible(android::RSC::sp<const Element>e) const {
387221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Try strict BaseObj equality to start with.
38869cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams    if (this == e.get()) {
389221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return true;
390221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
391221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
392221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Ignore mKind because it is allowed to be different (user vs. pixel).
393221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // We also ignore mNormalized because it can be different. The mType
394221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // field must be non-null since we require name equivalence for
395221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // user-created Elements.
396221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return ((mSizeBytes == e->mSizeBytes) &&
39769cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams            (mType != RS_TYPE_NONE) &&
398221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            (mType == e->mType) &&
399221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            (mVectorSize == e->mVectorSize));
400221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
401221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
40289daad6bae798779e57f252e9da4fe4e62337124Tim MurrayElement::Builder::Builder(android::RSC::sp<RS> rs) {
4033560907fa9b1624eefbd6db5e9c3ccc25ab23b92Tim Murray    mRS = rs.get();
404221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mSkipPadding = false;
405bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementsVecSize = 8;
406bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementsCount = 0;
407bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    // Initialize space
408bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<Element>));
409bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementNames = (char **)calloc(mElementsVecSize, sizeof(char *));
410bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementNameLengths = (size_t*)calloc(mElementsVecSize, sizeof(size_t));
411bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mArraySizes = (uint32_t*)calloc(mElementsVecSize, sizeof(uint32_t));
412221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
413221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
414bc10dff26207bb8c02051b28326bb134a8f28eb3Miao WangElement::Builder::~Builder() {
415bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    // free allocated space
416bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    free(mElements);
417bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    for (size_t ct = 0; ct < mElementsCount; ct++ ) {
418bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mElementNames[ct]);
419bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    }
420bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    free(mElementNameLengths);
421bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    free(mElementNames);
422bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    free(mArraySizes);
423bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang}
424bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
425bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wangvoid Element::Builder::add(android::RSC::sp<const Element>e, const char * name, uint32_t arraySize) {
426221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Skip padding fields after a vector 3 type.
427221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (mSkipPadding) {
428221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        const char *s1 = "#padding_";
429bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        const char *s2 = name;
430221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size_t len = strlen(s1);
431221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        if (strlen(s2) >= len) {
432221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            if (!memcmp(s1, s2, len)) {
433221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams                mSkipPadding = false;
434221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams                return;
435221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            }
436221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        }
437221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
438221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
439221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (e->mVectorSize == 3) {
440221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        mSkipPadding = true;
441221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    } else {
442221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        mSkipPadding = false;
443221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
444221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
445bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    if (mElementsCount >= mElementsVecSize) {
446bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        //if pre-allocated space is full, allocate a larger one.
447bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        mElementsVecSize += 8;
448bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
449bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        android::RSC::sp<const Element> * newElements = (android::RSC::sp<const Element> *)calloc(mElementsVecSize, sizeof(android::RSC::sp<Element>));
450bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        char ** newElementNames = (char **)calloc(mElementsVecSize, sizeof(char *));
451bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        size_t * newElementNameLengths = (size_t*)calloc(mElementsVecSize, sizeof(size_t));
452bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        uint32_t * newArraySizes = (uint32_t*)calloc(mElementsVecSize, sizeof(uint32_t));
453bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
454bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        memcpy(newElements, mElements, mElementsCount * sizeof(android::RSC::sp<Element>));
455bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        memcpy(newElementNames, mElementNames, mElementsCount * sizeof(char *));
456bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        memcpy(newElementNameLengths, mElementNameLengths, mElementsCount * sizeof(size_t));
457bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        memcpy(newArraySizes, mArraySizes, mElementsCount * sizeof(uint32_t));
458bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
459bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        //free the old arrays
460bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mElements);
461bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mElementNames);
462bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mArraySizes);
463bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        free(mElementNameLengths);
464bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
465bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        mElements = newElements;
466bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        mElementNames = newElementNames;
467bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        mArraySizes = newArraySizes;
468bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        mElementNameLengths = newElementNameLengths;
469bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    }
470bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElements[mElementsCount] = e;
471bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mArraySizes[mElementsCount] = arraySize;
472bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
473bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    size_t nameLen = strlen(name);
474bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementNameLengths[mElementsCount] = nameLen + 1;
475bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementNames[mElementsCount] = (char *)calloc(nameLen + 1, sizeof(char));
476bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    memcpy(mElementNames[mElementsCount], name, nameLen);
477bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementNames[mElementsCount][nameLen] = 0;
478bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang
479bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    mElementsCount++;
480221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
481221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
48289daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::Builder::create() {
483bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    size_t fieldCount = mElementsCount;
484bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    void ** elementArray = (void **)calloc(fieldCount, sizeof(void *));
485221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
486221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    for (size_t ct = 0; ct < fieldCount; ct++) {
487bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang        elementArray[ct] = mElements[ct]->getID();
488221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
489221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
490a423096c0d49e5cfe13a400b4323a76f89c6885cTim Murray    void *id = RS::dispatch->ElementCreate2(mRS->getContext(),
491bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang                                            (RsElement *)elementArray, fieldCount,
492bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang                                            (const char **)mElementNames, fieldCount, mElementNameLengths,
493bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang                                            mArraySizes, fieldCount);
49469cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams    free(elementArray);
495bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang    return new Element(id, mRS, mElements, mElementsCount, (const char **)mElementNames, mElementNameLengths, mArraySizes);
496221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
497