1 /*
2 * Copyright (C) 2010 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 "Utils.h"
18 #include "SkUtils.h"
19 #include "SkData.h"
20
21 #include <log/log.h>
22
23 using namespace android;
24
AssetStreamAdaptor(Asset * asset)25 AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
26 : fAsset(asset)
27 {
28 }
29
rewind()30 bool AssetStreamAdaptor::rewind() {
31 off64_t pos = fAsset->seek(0, SEEK_SET);
32 if (pos == (off64_t)-1) {
33 SkDebugf("----- fAsset->seek(rewind) failed\n");
34 return false;
35 }
36 return true;
37 }
38
getLength() const39 size_t AssetStreamAdaptor::getLength() const {
40 return fAsset->getLength();
41 }
42
isAtEnd() const43 bool AssetStreamAdaptor::isAtEnd() const {
44 return fAsset->getRemainingLength() == 0;
45 }
46
onDuplicate() const47 SkStreamRewindable* AssetStreamAdaptor::onDuplicate() const {
48 // Cannot create a duplicate, since each AssetStreamAdaptor
49 // would be modifying the Asset.
50 //return new AssetStreamAdaptor(fAsset);
51 return NULL;
52 }
53
hasPosition() const54 bool AssetStreamAdaptor::hasPosition() const {
55 return fAsset->seek(0, SEEK_CUR) != -1;
56 }
57
getPosition() const58 size_t AssetStreamAdaptor::getPosition() const {
59 const off64_t offset = fAsset->seek(0, SEEK_CUR);
60 if (offset == -1) {
61 SkDebugf("---- fAsset->seek(0, SEEK_CUR) failed\n");
62 return 0;
63 }
64
65 return offset;
66 }
67
seek(size_t position)68 bool AssetStreamAdaptor::seek(size_t position) {
69 if (fAsset->seek(position, SEEK_SET) == -1) {
70 SkDebugf("---- fAsset->seek(0, SEEK_SET) failed\n");
71 return false;
72 }
73
74 return true;
75 }
76
move(long offset)77 bool AssetStreamAdaptor::move(long offset) {
78 if (fAsset->seek(offset, SEEK_CUR) == -1) {
79 SkDebugf("---- fAsset->seek(%i, SEEK_CUR) failed\n", offset);
80 return false;
81 }
82
83 return true;
84 }
85
read(void * buffer,size_t size)86 size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
87 ssize_t amount;
88
89 if (NULL == buffer) {
90 if (0 == size) {
91 return 0;
92 }
93 // asset->seek returns new total offset
94 // we want to return amount that was skipped
95
96 off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
97 if (-1 == oldOffset) {
98 SkDebugf("---- fAsset->seek(oldOffset) failed\n");
99 return 0;
100 }
101 off64_t newOffset = fAsset->seek(size, SEEK_CUR);
102 if (-1 == newOffset) {
103 SkDebugf("---- fAsset->seek(%d) failed\n", size);
104 return 0;
105 }
106 amount = newOffset - oldOffset;
107 } else {
108 amount = fAsset->read(buffer, size);
109 }
110
111 if (amount < 0) {
112 amount = 0;
113 }
114 return amount;
115 }
116
CopyAssetToData(Asset * asset)117 sk_sp<SkData> android::CopyAssetToData(Asset* asset) {
118 if (NULL == asset) {
119 return NULL;
120 }
121
122 const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
123 if ((off64_t)-1 == seekReturnVal) {
124 SkDebugf("---- copyAsset: asset rewind failed\n");
125 return NULL;
126 }
127
128 const off64_t size = asset->getLength();
129 if (size <= 0) {
130 SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
131 return NULL;
132 }
133
134 sk_sp<SkData> data(SkData::MakeUninitialized(size));
135 const off64_t len = asset->read(data->writable_data(), size);
136 if (len != size) {
137 SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
138 return NULL;
139 }
140
141 return data;
142 }
143
nullObjectReturn(const char msg[])144 jobject android::nullObjectReturn(const char msg[]) {
145 if (msg) {
146 SkDebugf("--- %s\n", msg);
147 }
148 return NULL;
149 }
150
isSeekable(int descriptor)151 bool android::isSeekable(int descriptor) {
152 return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
153 }
154
get_env_or_die(JavaVM * jvm)155 JNIEnv* android::get_env_or_die(JavaVM* jvm) {
156 JNIEnv* env;
157 if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
158 LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", jvm);
159 }
160 return env;
161 }
162
requireEnv(JavaVM * jvm)163 JNIEnv* android::requireEnv(JavaVM* jvm) {
164 JNIEnv* env;
165 if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
166 if (jvm->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) {
167 LOG_ALWAYS_FATAL("Failed to AttachCurrentThread!");
168 }
169 }
170 return env;
171 }
172