| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 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" | 
| Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 18 | #include "SkData.h" | 
| Kevin Lubick | b63b4ca | 2023-01-25 15:18:23 +0000 | [diff] [blame] | 19 | #include "SkRefCnt.h" | 
|  | 20 | #include "SkStream.h" | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 21 |  | 
| Sally Qi | c5a4f23 | 2021-07-13 22:05:31 +0000 | [diff] [blame] | 22 | #include <inttypes.h> | 
| Derek Sollenberger | eec1b86 | 2019-10-24 09:44:55 -0400 | [diff] [blame] | 23 | #include <log/log.h> | 
|  | 24 |  | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 25 | using namespace android; | 
|  | 26 |  | 
| Ben Wagner | c2d3957 | 2015-01-12 17:06:21 -0500 | [diff] [blame] | 27 | AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset) | 
| Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 28 | : fAsset(asset) | 
| Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 29 | { | 
|  | 30 | } | 
|  | 31 |  | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 32 | bool AssetStreamAdaptor::rewind() { | 
| Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 33 | off64_t pos = fAsset->seek(0, SEEK_SET); | 
|  | 34 | if (pos == (off64_t)-1) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 35 | ALOGD("----- fAsset->seek(rewind) failed\n"); | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 36 | return false; | 
|  | 37 | } | 
|  | 38 | return true; | 
|  | 39 | } | 
|  | 40 |  | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 41 | size_t AssetStreamAdaptor::getLength() const { | 
|  | 42 | return fAsset->getLength(); | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | bool AssetStreamAdaptor::isAtEnd() const { | 
|  | 46 | return fAsset->getRemainingLength() == 0; | 
|  | 47 | } | 
|  | 48 |  | 
| Mike Reed | be896ed | 2017-09-19 17:01:30 -0400 | [diff] [blame] | 49 | SkStreamRewindable* AssetStreamAdaptor::onDuplicate() const { | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 50 | // 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 III | 0492eef | 2018-05-07 10:59:31 -0400 | [diff] [blame] | 56 | bool AssetStreamAdaptor::hasPosition() const { | 
|  | 57 | return fAsset->seek(0, SEEK_CUR) != -1; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | size_t AssetStreamAdaptor::getPosition() const { | 
|  | 61 | const off64_t offset = fAsset->seek(0, SEEK_CUR); | 
|  | 62 | if (offset == -1) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 63 | ALOGD("---- fAsset->seek(0, SEEK_CUR) failed\n"); | 
| Leon Scroggins III | 0492eef | 2018-05-07 10:59:31 -0400 | [diff] [blame] | 64 | return 0; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | return offset; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | bool AssetStreamAdaptor::seek(size_t position) { | 
|  | 71 | if (fAsset->seek(position, SEEK_SET) == -1) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 72 | ALOGD("---- fAsset->seek(0, SEEK_SET) failed\n"); | 
| Leon Scroggins III | 0492eef | 2018-05-07 10:59:31 -0400 | [diff] [blame] | 73 | return false; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | return true; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | bool AssetStreamAdaptor::move(long offset) { | 
|  | 80 | if (fAsset->seek(offset, SEEK_CUR) == -1) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 81 | ALOGD("---- fAsset->seek(%li, SEEK_CUR) failed\n", offset); | 
| Leon Scroggins III | 0492eef | 2018-05-07 10:59:31 -0400 | [diff] [blame] | 82 | return false; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | return true; | 
|  | 86 | } | 
|  | 87 |  | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 88 | size_t AssetStreamAdaptor::read(void* buffer, size_t size) { | 
|  | 89 | ssize_t amount; | 
|  | 90 |  | 
|  | 91 | if (NULL == buffer) { | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 92 | if (0 == size) { | 
|  | 93 | return 0; | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 94 | } | 
|  | 95 | // asset->seek returns new total offset | 
|  | 96 | // we want to return amount that was skipped | 
|  | 97 |  | 
| Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 98 | off64_t oldOffset = fAsset->seek(0, SEEK_CUR); | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 99 | if (-1 == oldOffset) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 100 | ALOGD("---- fAsset->seek(oldOffset) failed\n"); | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 101 | return 0; | 
|  | 102 | } | 
| Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 103 | off64_t newOffset = fAsset->seek(size, SEEK_CUR); | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 104 | if (-1 == newOffset) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 105 | ALOGD("---- fAsset->seek(%zu) failed\n", size); | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 106 | return 0; | 
|  | 107 | } | 
|  | 108 | amount = newOffset - oldOffset; | 
|  | 109 | } else { | 
|  | 110 | amount = fAsset->read(buffer, size); | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
|  | 113 | if (amount < 0) { | 
|  | 114 | amount = 0; | 
|  | 115 | } | 
|  | 116 | return amount; | 
|  | 117 | } | 
|  | 118 |  | 
| Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 119 | sk_sp<SkData> android::CopyAssetToData(Asset* asset) { | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 120 | if (NULL == asset) { | 
|  | 121 | return NULL; | 
|  | 122 | } | 
|  | 123 |  | 
| Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 124 | const off64_t seekReturnVal = asset->seek(0, SEEK_SET); | 
|  | 125 | if ((off64_t)-1 == seekReturnVal) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 126 | ALOGD("---- copyAsset: asset rewind failed\n"); | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 127 | return NULL; | 
|  | 128 | } | 
|  | 129 |  | 
| Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 130 | const off64_t size = asset->getLength(); | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 131 | if (size <= 0) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 132 | ALOGD("---- copyAsset: asset->getLength() returned %" PRId64 "\n", size); | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 133 | return NULL; | 
|  | 134 | } | 
|  | 135 |  | 
| Ben Wagner | d8b5c31 | 2016-08-03 15:55:25 -0400 | [diff] [blame] | 136 | sk_sp<SkData> data(SkData::MakeUninitialized(size)); | 
| Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 137 | const off64_t len = asset->read(data->writable_data(), size); | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 138 | if (len != size) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 139 | ALOGD("---- copyAsset: asset->read(%" PRId64 ") returned %" PRId64 "\n", size, len); | 
| Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 140 | return NULL; | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 141 | } | 
| Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 142 |  | 
| Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 143 | return data; | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 146 | jobject android::nullObjectReturn(const char msg[]) { | 
|  | 147 | if (msg) { | 
| Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 148 | ALOGD("--- %s\n", msg); | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 149 | } | 
|  | 150 | return NULL; | 
|  | 151 | } | 
| Yujie Qin | c1d7b7f | 2016-02-29 14:00:29 +0100 | [diff] [blame] | 152 |  | 
|  | 153 | bool android::isSeekable(int descriptor) { | 
|  | 154 | return ::lseek64(descriptor, 0, SEEK_CUR) != -1; | 
|  | 155 | } | 
| Leon Scroggins III | 127d31a | 2018-01-19 12:29:47 -0500 | [diff] [blame] | 156 |  | 
|  | 157 | JNIEnv* 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 III | f97b29d2 | 2020-04-06 12:01:01 -0400 | [diff] [blame] | 164 |  | 
|  | 165 | JNIEnv* 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 | } |