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" |
| 18 | #include "SkUtils.h" |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 19 | #include "SkData.h" |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 20 | |
Derek Sollenberger | eec1b86 | 2019-10-24 09:44:55 -0400 | [diff] [blame] | 21 | #include <log/log.h> |
| 22 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 23 | using namespace android; |
| 24 | |
Ben Wagner | c2d3957 | 2015-01-12 17:06:21 -0500 | [diff] [blame] | 25 | AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset) |
Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 26 | : fAsset(asset) |
Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 27 | { |
| 28 | } |
| 29 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 30 | bool AssetStreamAdaptor::rewind() { |
Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 31 | off64_t pos = fAsset->seek(0, SEEK_SET); |
| 32 | if (pos == (off64_t)-1) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 33 | SkDebugf("----- fAsset->seek(rewind) failed\n"); |
| 34 | return false; |
| 35 | } |
| 36 | return true; |
| 37 | } |
| 38 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 39 | size_t AssetStreamAdaptor::getLength() const { |
| 40 | return fAsset->getLength(); |
| 41 | } |
| 42 | |
| 43 | bool AssetStreamAdaptor::isAtEnd() const { |
| 44 | return fAsset->getRemainingLength() == 0; |
| 45 | } |
| 46 | |
Mike Reed | be896ed | 2017-09-19 17:01:30 -0400 | [diff] [blame] | 47 | SkStreamRewindable* AssetStreamAdaptor::onDuplicate() const { |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 48 | // Cannot create a duplicate, since each AssetStreamAdaptor |
| 49 | // would be modifying the Asset. |
| 50 | //return new AssetStreamAdaptor(fAsset); |
| 51 | return NULL; |
| 52 | } |
| 53 | |
Leon Scroggins III | 0492eef | 2018-05-07 10:59:31 -0400 | [diff] [blame] | 54 | bool AssetStreamAdaptor::hasPosition() const { |
| 55 | return fAsset->seek(0, SEEK_CUR) != -1; |
| 56 | } |
| 57 | |
| 58 | 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 | |
| 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 | |
| 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 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 86 | size_t AssetStreamAdaptor::read(void* buffer, size_t size) { |
| 87 | ssize_t amount; |
| 88 | |
| 89 | if (NULL == buffer) { |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 90 | if (0 == size) { |
| 91 | return 0; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 92 | } |
| 93 | // asset->seek returns new total offset |
| 94 | // we want to return amount that was skipped |
| 95 | |
Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 96 | off64_t oldOffset = fAsset->seek(0, SEEK_CUR); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 97 | if (-1 == oldOffset) { |
| 98 | SkDebugf("---- fAsset->seek(oldOffset) failed\n"); |
| 99 | return 0; |
| 100 | } |
Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 101 | off64_t newOffset = fAsset->seek(size, SEEK_CUR); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 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); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | if (amount < 0) { |
| 112 | amount = 0; |
| 113 | } |
| 114 | return amount; |
| 115 | } |
| 116 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 117 | SkMemoryStream* android::CopyAssetToStream(Asset* asset) { |
| 118 | if (NULL == asset) { |
| 119 | return NULL; |
| 120 | } |
| 121 | |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 122 | const off64_t seekReturnVal = asset->seek(0, SEEK_SET); |
| 123 | if ((off64_t)-1 == seekReturnVal) { |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 124 | SkDebugf("---- copyAsset: asset rewind failed\n"); |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 128 | const off64_t size = asset->getLength(); |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 129 | if (size <= 0) { |
| 130 | SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
Ben Wagner | d8b5c31 | 2016-08-03 15:55:25 -0400 | [diff] [blame] | 134 | sk_sp<SkData> data(SkData::MakeUninitialized(size)); |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 135 | const off64_t len = asset->read(data->writable_data(), size); |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 136 | if (len != size) { |
| 137 | SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len); |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 138 | return NULL; |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 139 | } |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 140 | |
Ben Wagner | d8b5c31 | 2016-08-03 15:55:25 -0400 | [diff] [blame] | 141 | return new SkMemoryStream(std::move(data)); |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 144 | jobject android::nullObjectReturn(const char msg[]) { |
| 145 | if (msg) { |
| 146 | SkDebugf("--- %s\n", msg); |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
Yujie Qin | c1d7b7f | 2016-02-29 14:00:29 +0100 | [diff] [blame] | 150 | |
| 151 | bool android::isSeekable(int descriptor) { |
| 152 | return ::lseek64(descriptor, 0, SEEK_CUR) != -1; |
| 153 | } |
Leon Scroggins III | 127d31a | 2018-01-19 12:29:47 -0500 | [diff] [blame] | 154 | |
| 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 | } |
Leon Scroggins III | f97b29d2 | 2020-04-06 12:01:01 -0400 | [diff] [blame^] | 162 | |
| 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 | } |