1 /*
2  * Copyright (C) 2016 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 
17 #include "rsContext.h"
18 #include "rsFileA3D.h"
19 
20 using android::Asset;
21 using android::renderscript::Context;
22 using android::renderscript::FileA3D;
23 using android::renderscript::ObjectBase;
24 using android::renderscript::rsuCopyString;
25 
rsaFileA3DGetEntryByIndex(RsContext con,uint32_t index,RsFile file)26 RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file) {
27     FileA3D *fa3d = static_cast<FileA3D *>(file);
28     if (!fa3d) {
29         ALOGE("Can't load entry. No valid file");
30         return nullptr;
31     }
32 
33     ObjectBase *obj = fa3d->initializeFromEntry(index);
34     //ALOGV("Returning object with name %s", obj->getName());
35 
36     return obj;
37 }
38 
39 
rsaFileA3DGetNumIndexEntries(RsContext con,int32_t * numEntries,RsFile file)40 void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file) {
41     FileA3D *fa3d = static_cast<FileA3D *>(file);
42 
43     if (fa3d) {
44         *numEntries = fa3d->getNumIndexEntries();
45     } else {
46         *numEntries = 0;
47     }
48 }
49 
rsaFileA3DGetIndexEntries(RsContext con,RsFileIndexEntry * fileEntries,uint32_t numEntries,RsFile file)50 void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file) {
51     FileA3D *fa3d = static_cast<FileA3D *>(file);
52 
53     if (!fa3d) {
54         ALOGE("Can't load index entries. No valid file");
55         return;
56     }
57 
58     uint32_t numFileEntries = fa3d->getNumIndexEntries();
59     if (numFileEntries != numEntries || numEntries == 0 || fileEntries == nullptr) {
60         ALOGE("Can't load index entries. Invalid number requested");
61         return;
62     }
63 
64     for (uint32_t i = 0; i < numFileEntries; i ++) {
65         const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i);
66         fileEntries[i].classID = entry->getType();
67         fileEntries[i].objectName = rsuCopyString(entry->getObjectName());
68     }
69 }
70 
rsaFileA3DCreateFromMemory(RsContext con,const void * data,uint32_t len)71 RsFile rsaFileA3DCreateFromMemory(RsContext con, const void *data, uint32_t len) {
72     if (data == nullptr) {
73         ALOGE("File load failed. Asset stream is nullptr");
74         return nullptr;
75     }
76 
77     Context *rsc = static_cast<Context *>(con);
78     FileA3D *fa3d = new FileA3D(rsc);
79     fa3d->incUserRef();
80 
81     fa3d->load(data, len);
82     return fa3d;
83 }
84 
rsaFileA3DCreateFromAsset(RsContext con,void * _asset)85 RsFile rsaFileA3DCreateFromAsset(RsContext con, void *_asset) {
86     ALOGE("Calling deprecated %s API", __FUNCTION__);
87     return nullptr;
88 }
89 
rsaFileA3DCreateFromFile(RsContext con,const char * path)90 RsFile rsaFileA3DCreateFromFile(RsContext con, const char *path) {
91     if (path == nullptr) {
92         ALOGE("File load failed. Path is nullptr");
93         return nullptr;
94     }
95 
96     Context *rsc = static_cast<Context *>(con);
97     FileA3D *fa3d = nullptr;
98 
99     FILE *f = fopen(path, "rbe");
100     if (f) {
101         fa3d = new FileA3D(rsc);
102         fa3d->incUserRef();
103         fa3d->load(f);
104         fclose(f);
105     } else {
106         ALOGE("Could not open file %s", path);
107     }
108 
109     return fa3d;
110 }
111