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