1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Resource wrapper for AAssetManager.
22 *//*--------------------------------------------------------------------*/
23
24 #include "tcuAndroidAssets.hpp"
25
26 namespace tcu
27 {
28 namespace Android
29 {
30
AssetArchive(AAssetManager * assetMgr)31 AssetArchive::AssetArchive(AAssetManager *assetMgr) : m_assetMgr(assetMgr)
32 {
33 }
34
~AssetArchive(void)35 AssetArchive::~AssetArchive(void)
36 {
37 }
38
getResource(const char * name) const39 Resource *AssetArchive::getResource(const char *name) const
40 {
41 return new AssetResource(m_assetMgr, name);
42 }
43
AssetResource(AAssetManager * assetMgr,const char * name)44 AssetResource::AssetResource(AAssetManager *assetMgr, const char *name) : Resource(name), m_asset(DE_NULL)
45 {
46 m_asset = AAssetManager_open(assetMgr, name, AASSET_MODE_RANDOM);
47
48 if (!m_asset)
49 throw ResourceError("Failed to open asset resource", name, __FILE__, __LINE__);
50 }
51
~AssetResource(void)52 AssetResource::~AssetResource(void)
53 {
54 AAsset_close(m_asset);
55 }
56
read(uint8_t * dst,int numBytes)57 void AssetResource::read(uint8_t *dst, int numBytes)
58 {
59 TCU_CHECK(AAsset_read(m_asset, dst, numBytes) == numBytes);
60 }
61
getPosition(void) const62 int AssetResource::getPosition(void) const
63 {
64 return (int)AAsset_getLength(m_asset) - (int)AAsset_getRemainingLength(m_asset);
65 }
66
setPosition(int position)67 void AssetResource::setPosition(int position)
68 {
69 TCU_CHECK(AAsset_seek(m_asset, position, SEEK_SET) == position);
70 }
71
isFinished(void) const72 bool AssetResource::isFinished(void) const
73 {
74 return AAsset_getRemainingLength(m_asset) <= 0;
75 }
76
getSize(void) const77 int AssetResource::getSize(void) const
78 {
79 return (int)AAsset_getLength(m_asset);
80 }
81
82 } // namespace Android
83 } // namespace tcu
84