blob: 9f5a2145792c748dc25da3e1e7421164119f838b [file] [log] [blame]
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001/*
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"
Leon Scroggins III34497892015-01-20 15:52:43 -050018#include "SkData.h"
Kevin Lubickb63b4ca2023-01-25 15:18:23 +000019#include "SkRefCnt.h"
20#include "SkStream.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080021
Sally Qic5a4f232021-07-13 22:05:31 +000022#include <inttypes.h>
Derek Sollenbergereec1b862019-10-24 09:44:55 -040023#include <log/log.h>
24
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080025using namespace android;
26
Ben Wagnerc2d39572015-01-12 17:06:21 -050027AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050028 : fAsset(asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050029{
30}
31
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080032bool AssetStreamAdaptor::rewind() {
Kenny Rootddb76c42010-11-24 12:56:06 -080033 off64_t pos = fAsset->seek(0, SEEK_SET);
34 if (pos == (off64_t)-1) {
Sally Qi7e3f93b2021-07-15 00:00:54 +000035 ALOGD("----- fAsset->seek(rewind) failed\n");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080036 return false;
37 }
38 return true;
39}
40
Leon Scroggins IIIca320212013-08-20 17:59:39 -040041size_t AssetStreamAdaptor::getLength() const {
42 return fAsset->getLength();
43}
44
45bool AssetStreamAdaptor::isAtEnd() const {
46 return fAsset->getRemainingLength() == 0;
47}
48
Mike Reedbe896ed2017-09-19 17:01:30 -040049SkStreamRewindable* AssetStreamAdaptor::onDuplicate() const {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040050 // Cannot create a duplicate, since each AssetStreamAdaptor
51 // would be modifying the Asset.
52 //return new AssetStreamAdaptor(fAsset);
53 return NULL;
54}
55
Leon Scroggins III0492eef2018-05-07 10:59:31 -040056bool AssetStreamAdaptor::hasPosition() const {
57 return fAsset->seek(0, SEEK_CUR) != -1;
58}
59
60size_t AssetStreamAdaptor::getPosition() const {
61 const off64_t offset = fAsset->seek(0, SEEK_CUR);
62 if (offset == -1) {
Sally Qi7e3f93b2021-07-15 00:00:54 +000063 ALOGD("---- fAsset->seek(0, SEEK_CUR) failed\n");
Leon Scroggins III0492eef2018-05-07 10:59:31 -040064 return 0;
65 }
66
67 return offset;
68}
69
70bool AssetStreamAdaptor::seek(size_t position) {
71 if (fAsset->seek(position, SEEK_SET) == -1) {
Sally Qi7e3f93b2021-07-15 00:00:54 +000072 ALOGD("---- fAsset->seek(0, SEEK_SET) failed\n");
Leon Scroggins III0492eef2018-05-07 10:59:31 -040073 return false;
74 }
75
76 return true;
77}
78
79bool AssetStreamAdaptor::move(long offset) {
80 if (fAsset->seek(offset, SEEK_CUR) == -1) {
Sally Qi7e3f93b2021-07-15 00:00:54 +000081 ALOGD("---- fAsset->seek(%li, SEEK_CUR) failed\n", offset);
Leon Scroggins III0492eef2018-05-07 10:59:31 -040082 return false;
83 }
84
85 return true;
86}
87
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080088size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
89 ssize_t amount;
90
91 if (NULL == buffer) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040092 if (0 == size) {
93 return 0;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080094 }
95 // asset->seek returns new total offset
96 // we want to return amount that was skipped
97
Kenny Rootddb76c42010-11-24 12:56:06 -080098 off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080099 if (-1 == oldOffset) {
Sally Qi7e3f93b2021-07-15 00:00:54 +0000100 ALOGD("---- fAsset->seek(oldOffset) failed\n");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800101 return 0;
102 }
Kenny Rootddb76c42010-11-24 12:56:06 -0800103 off64_t newOffset = fAsset->seek(size, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800104 if (-1 == newOffset) {
Sally Qi7e3f93b2021-07-15 00:00:54 +0000105 ALOGD("---- fAsset->seek(%zu) failed\n", size);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800106 return 0;
107 }
108 amount = newOffset - oldOffset;
109 } else {
110 amount = fAsset->read(buffer, size);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800111 }
112
113 if (amount < 0) {
114 amount = 0;
115 }
116 return amount;
117}
118
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400119sk_sp<SkData> android::CopyAssetToData(Asset* asset) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400120 if (NULL == asset) {
121 return NULL;
122 }
123
Leon Scroggins III34497892015-01-20 15:52:43 -0500124 const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
125 if ((off64_t)-1 == seekReturnVal) {
Sally Qi7e3f93b2021-07-15 00:00:54 +0000126 ALOGD("---- copyAsset: asset rewind failed\n");
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400127 return NULL;
128 }
129
Leon Scroggins III34497892015-01-20 15:52:43 -0500130 const off64_t size = asset->getLength();
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400131 if (size <= 0) {
Sally Qi7e3f93b2021-07-15 00:00:54 +0000132 ALOGD("---- copyAsset: asset->getLength() returned %" PRId64 "\n", size);
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400133 return NULL;
134 }
135
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400136 sk_sp<SkData> data(SkData::MakeUninitialized(size));
Leon Scroggins III34497892015-01-20 15:52:43 -0500137 const off64_t len = asset->read(data->writable_data(), size);
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400138 if (len != size) {
Sally Qi7e3f93b2021-07-15 00:00:54 +0000139 ALOGD("---- copyAsset: asset->read(%" PRId64 ") returned %" PRId64 "\n", size, len);
Leon Scroggins III34497892015-01-20 15:52:43 -0500140 return NULL;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400141 }
Leon Scroggins III34497892015-01-20 15:52:43 -0500142
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400143 return data;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400144}
145
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800146jobject android::nullObjectReturn(const char msg[]) {
147 if (msg) {
Sally Qi7e3f93b2021-07-15 00:00:54 +0000148 ALOGD("--- %s\n", msg);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800149 }
150 return NULL;
151}
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100152
153bool android::isSeekable(int descriptor) {
154 return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
155}
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500156
157JNIEnv* android::get_env_or_die(JavaVM* jvm) {
158 JNIEnv* env;
159 if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
160 LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", jvm);
161 }
162 return env;
163}
Leon Scroggins IIIf97b29d22020-04-06 12:01:01 -0400164
165JNIEnv* android::requireEnv(JavaVM* jvm) {
166 JNIEnv* env;
167 if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
168 if (jvm->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) {
169 LOG_ALWAYS_FATAL("Failed to AttachCurrentThread!");
170 }
171 }
172 return env;
173}