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) {
27221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (!mVisibleElementMap.size()) {
2821fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
2921fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
30221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
31221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (index >= mVisibleElementMap.size()) {
3221fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
3321fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
34221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
35221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return mElements[mVisibleElementMap[index]];
36221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
37221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
38221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samsconst char * Element::getSubElementName(uint32_t index) {
39221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (!mVisibleElementMap.size()) {
4021fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
4121fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
42221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
43221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (index >= mVisibleElementMap.size()) {
4421fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Illegal sub-element index");
4521fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
46221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
47ab71636b50db242cead6a3787620d6340156684cTim Murray    return mElementNames[mVisibleElementMap[index]].c_str();
48221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
49221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
50221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samssize_t Element::getSubElementArraySize(uint32_t index) {
51221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (!mVisibleElementMap.size()) {
5221fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
5321fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return 0;
54221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
55221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (index >= mVisibleElementMap.size()) {
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) {
63221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (mVisibleElementMap.size()) {
6421fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        mRS->throwError(RS_ERROR_INVALID_PARAMETER, "Element contains no sub-elements");
6521fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return 0;
66221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
67221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (index >= mVisibleElementMap.size()) {
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) { \
76729b6fe58aff47c4f666b22bbb7a6d6114ddefa9Tim Murray    if (rs->mElements.N == NULL) {                                  \
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) { \
103eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    if (rs->mElements.N == NULL) {                                  \
104eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray        rs->mElements.N = createPixel(rs, RS_TYPE_##T, RS_KIND_##K);    \
105eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    }                                                                   \
106eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    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) { \
118eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    if (rs->mElements.N##_2 == NULL) {                                  \
119eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray        rs->mElements.N##_2 = createVector(rs, RS_TYPE_##T, 2);         \
120eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    }                                                                   \
121eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    return rs->mElements.N##_2;                                         \
122eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray}                                                                       \
12389daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::N##_3(android::RSC::sp<RS> rs) { \
124eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    if (rs->mElements.N##_3 == NULL) {                                  \
125eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray        rs->mElements.N##_3 = createVector(rs, RS_TYPE_##T, 3);         \
126eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    }                                                                   \
127eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    return rs->mElements.N##_3;                                         \
128221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams} \
12989daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::N##_4(android::RSC::sp<RS> rs) { \
130eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    if (rs->mElements.N##_4 == NULL) {                                  \
131eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray        rs->mElements.N##_4 = createVector(rs, RS_TYPE_##T, 4);         \
132eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    }                                                                   \
133eb4426dfb63983559cf903b2ea984569e990c4fdTim Murray    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() {
148221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (!mElements.size()) {
149221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return;
150221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
151221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mVisibleElementMap.clear();
152221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
153221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    int noPaddingFieldCount = 0;
154221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    size_t fieldCount = mElementNames.size();
155221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Find out how many elements are not padding
156221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    for (size_t ct = 0; ct < fieldCount; ct ++) {
157ab71636b50db242cead6a3787620d6340156684cTim Murray        if (mElementNames[ct].c_str()[0] != '#') {
158221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            noPaddingFieldCount ++;
159221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        }
160221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
161221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
162221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Make a map that points us at non-padding elements
163221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    for (size_t ct = 0; ct < fieldCount; ct ++) {
164ab71636b50db242cead6a3787620d6340156684cTim Murray        if (mElementNames[ct].c_str()[0] != '#') {
16589daad6bae798779e57f252e9da4fe4e62337124Tim Murray            mVisibleElementMap.push_back((uint32_t)ct);
166221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        }
167221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
168221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
169221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
17089daad6bae798779e57f252e9da4fe4e62337124Tim MurrayElement::Element(void *id, android::RSC::sp<RS> rs,
17189daad6bae798779e57f252e9da4fe4e62337124Tim Murray                 std::vector<android::RSC::sp<Element> > &elements,
172ab71636b50db242cead6a3787620d6340156684cTim Murray                 std::vector<std::string> &elementNames,
17389daad6bae798779e57f252e9da4fe4e62337124Tim Murray                 std::vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
174221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mSizeBytes = 0;
175221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mVectorSize = 1;
176221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mElements = elements;
177221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mArraySizes = arraySizes;
178221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mElementNames = elementNames;
179221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
180221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mType = RS_TYPE_NONE;
181221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mKind = RS_KIND_USER;
182221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
183221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    for (size_t ct = 0; ct < mElements.size(); ct++ ) {
18489daad6bae798779e57f252e9da4fe4e62337124Tim Murray        mOffsetInBytes.push_back(mSizeBytes);
185221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        mSizeBytes += mElements[ct]->mSizeBytes * mArraySizes[ct];
186221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
187221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    updateVisibleSubElements();
188221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
189221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
190221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
191221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samsstatic uint32_t GetSizeInBytesForType(RsDataType dt) {
192221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    switch(dt) {
193221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_NONE:
194221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 0;
195221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SIGNED_8:
196221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_8:
197221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_BOOLEAN:
198221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 1;
199221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
200221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_FLOAT_16:
201221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SIGNED_16:
202221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_16:
203221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_5_6_5:
204221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_5_5_5_1:
205221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_4_4_4_4:
206221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 2;
207221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
208221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_FLOAT_32:
209221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SIGNED_32:
210221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_32:
211221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 4;
212221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
213221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_FLOAT_64:
214221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SIGNED_64:
215221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_UNSIGNED_64:
216221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 8;
217221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
218221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_MATRIX_4X4:
219221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 16 * 4;
220221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_MATRIX_3X3:
221221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 9 * 4;
222221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_MATRIX_2X2:
223221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 4 * 4;
224221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
225221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_TYPE:
226221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_ALLOCATION:
227221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SAMPLER:
228221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_SCRIPT:
229221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_MESH:
230221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_PROGRAM_FRAGMENT:
231221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_PROGRAM_VERTEX:
232221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_PROGRAM_RASTER:
233221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_TYPE_PROGRAM_STORE:
234221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return 4;
235221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
236221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    default:
237221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
238221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
239221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
240221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    ALOGE("Missing type %i", dt);
241221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return 0;
242221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
243221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
24489daad6bae798779e57f252e9da4fe4e62337124Tim MurrayElement::Element(void *id, android::RSC::sp<RS> rs,
245221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams                 RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
246221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    BaseObj(id, rs)
247221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams{
248221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    uint32_t tsize = GetSizeInBytesForType(dt);
249221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if ((dt != RS_TYPE_UNSIGNED_5_6_5) &&
250221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        (dt != RS_TYPE_UNSIGNED_4_4_4_4) &&
251221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        (dt != RS_TYPE_UNSIGNED_5_5_5_1)) {
252221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        if (size == 3) {
253221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            mSizeBytes = tsize * 4;
254221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        } else {
255221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            mSizeBytes = tsize * size;
256221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        }
257221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    } else {
258221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        mSizeBytes = tsize;
259221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
260221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mType = dt;
261221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mKind = dk;
262221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mNormalized = norm;
263221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mVectorSize = size;
264221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
265221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
266221a4b17cda03916a0599628fcbb5f48605a0e5aJason SamsElement::~Element() {
267221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
268221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
269221a4b17cda03916a0599628fcbb5f48605a0e5aJason Samsvoid Element::updateFromNative() {
270221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    BaseObj::updateFromNative();
271221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    updateVisibleSubElements();
272221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
273221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
27489daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::createUser(android::RSC::sp<RS> rs, RsDataType dt) {
275a423096c0d49e5cfe13a400b4323a76f89c6885cTim Murray    void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
276221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return new Element(id, rs, dt, RS_KIND_USER, false, 1);
277221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
278221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
27989daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::createVector(android::RSC::sp<RS> rs, RsDataType dt, uint32_t size) {
280221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (size < 2 || size > 4) {
28121fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Vector size out of range 2-4.");
28221fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
283221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
284a423096c0d49e5cfe13a400b4323a76f89c6885cTim Murray    void *id = RS::dispatch->ElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
285221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return new Element(id, rs, dt, RS_KIND_USER, false, size);
286221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
287221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
28889daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::createPixel(android::RSC::sp<RS> rs, RsDataType dt, RsDataKind dk) {
289221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (!(dk == RS_KIND_PIXEL_L ||
290221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dk == RS_KIND_PIXEL_A ||
291221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dk == RS_KIND_PIXEL_LA ||
292221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dk == RS_KIND_PIXEL_RGB ||
293221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dk == RS_KIND_PIXEL_RGBA ||
294221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dk == RS_KIND_PIXEL_DEPTH)) {
29521fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataKind");
29621fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
297221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
298221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (!(dt == RS_TYPE_UNSIGNED_8 ||
299221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dt == RS_TYPE_UNSIGNED_16 ||
300221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dt == RS_TYPE_UNSIGNED_5_6_5 ||
301221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
302221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams          dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
30321fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Unsupported DataType");
30421fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
305221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
306221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
30721fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
30821fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
309221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
310221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
31121fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
31221fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
313221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
314221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
31521fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
31621fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
317221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
318221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
31921fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        rs->throwError(RS_ERROR_INVALID_PARAMETER, "Bad kind and type combo");
32021fa7a0a23eddab88ff261017f6d7a2548b4d89aTim Murray        return NULL;
321221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
322221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
323221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    int size = 1;
324221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    switch (dk) {
325221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_KIND_PIXEL_LA:
326221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size = 2;
327221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
328221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_KIND_PIXEL_RGB:
329221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size = 3;
330221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
331221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_KIND_PIXEL_RGBA:
332221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size = 4;
333221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
334221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    case RS_KIND_PIXEL_DEPTH:
335221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size = 2;
336221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
337221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    default:
338221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        break;
339221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
340221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
341a423096c0d49e5cfe13a400b4323a76f89c6885cTim Murray    void * id = RS::dispatch->ElementCreate(rs->getContext(), dt, dk, true, size);
342221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return new Element(id, rs, dt, dk, true, size);
343221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
344221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
34510913a5c37ba119bef335320d3e8be25212c05adTim Murraybool Element::isCompatible(android::RSC::sp<const Element>e) const {
346221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Try strict BaseObj equality to start with.
34769cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams    if (this == e.get()) {
348221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        return true;
349221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
350221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
351221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Ignore mKind because it is allowed to be different (user vs. pixel).
352221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // We also ignore mNormalized because it can be different. The mType
353221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // field must be non-null since we require name equivalence for
354221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // user-created Elements.
355221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    return ((mSizeBytes == e->mSizeBytes) &&
35669cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams            (mType != RS_TYPE_NONE) &&
357221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            (mType == e->mType) &&
358221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            (mVectorSize == e->mVectorSize));
359221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
360221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
36189daad6bae798779e57f252e9da4fe4e62337124Tim MurrayElement::Builder::Builder(android::RSC::sp<RS> rs) {
3627758a5b899bc8d230aff12e15c3283ea8c248609Tim Murray    mRS = rs.get();
363221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    mSkipPadding = false;
364221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
365221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
366ab71636b50db242cead6a3787620d6340156684cTim Murrayvoid Element::Builder::add(android::RSC::sp</*const*/ Element>e, std::string &name, uint32_t arraySize) {
367221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    // Skip padding fields after a vector 3 type.
368221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (mSkipPadding) {
369221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        const char *s1 = "#padding_";
370ab71636b50db242cead6a3787620d6340156684cTim Murray        const char *s2 = name.c_str();
371221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        size_t len = strlen(s1);
372221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        if (strlen(s2) >= len) {
373221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            if (!memcmp(s1, s2, len)) {
374221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams                mSkipPadding = false;
375221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams                return;
376221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams            }
377221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        }
378221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
379221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
380221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    if (e->mVectorSize == 3) {
381221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        mSkipPadding = true;
382221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    } else {
383221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        mSkipPadding = false;
384221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
385221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
38689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    mElements.push_back(e);
38789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    mElementNames.push_back(name);
38889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    mArraySizes.push_back(arraySize);
389221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
390221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
39189daad6bae798779e57f252e9da4fe4e62337124Tim Murrayandroid::RSC::sp<const Element> Element::Builder::create() {
392221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    size_t fieldCount = mElements.size();
393221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
39469cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams    const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *));
395221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
396221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
397221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    for (size_t ct = 0; ct < fieldCount; ct++) {
398ab71636b50db242cead6a3787620d6340156684cTim Murray        nameArray[ct] = mElementNames[ct].c_str();
39969cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams        elementArray[ct] = mElements[ct].get();
400221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams        sizeArray[ct] = mElementNames[ct].length();
401221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    }
402221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
403a423096c0d49e5cfe13a400b4323a76f89c6885cTim Murray    void *id = RS::dispatch->ElementCreate2(mRS->getContext(),
40469cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams                                (RsElement *)elementArray, fieldCount,
405221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams                                nameArray, fieldCount * sizeof(size_t),  sizeArray,
40689daad6bae798779e57f252e9da4fe4e62337124Tim Murray                                (const uint32_t *)&mArraySizes[0], fieldCount);
407221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
408221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
409221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    free(nameArray);
410221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams    free(sizeArray);
41169cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams    free(elementArray);
41269cccdf0659a193d6a75420ec745421fb5c436e6Jason Sams    return new Element(id, mRS, mElements, mElementNames, mArraySizes);
413221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams}
414221a4b17cda03916a0599628fcbb5f48605a0e5aJason Sams
415