blob: 1259bb5a4f65cf5c89b47aaf6f9d3b6f90ebcd4d [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"
18#include "SkUtils.h"
Leon Scroggins III34497892015-01-20 15:52:43 -050019#include "SkData.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080020
Sally Qic5a4f232021-07-13 22:05:31 +000021#include <inttypes.h>
Derek Sollenbergereec1b862019-10-24 09:44:55 -040022#include <log/log.h>
23
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080024using namespace android;
25
Ben Wagnerc2d39572015-01-12 17:06:21 -050026AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050027 : fAsset(asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050028{
29}
30
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080031bool AssetStreamAdaptor::rewind() {
Kenny Rootddb76c42010-11-24 12:56:06 -080032 off64_t pos = fAsset->seek(0, SEEK_SET);
33 if (pos == (off64_t)-1) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080034 SkDebugf("----- fAsset->seek(rewind) failed\n");
35 return false;
36 }
37 return true;
38}
39
Leon Scroggins IIIca320212013-08-20 17:59:39 -040040size_t AssetStreamAdaptor::getLength() const {
41 return fAsset->getLength();
42}
43
44bool AssetStreamAdaptor::isAtEnd() const {
45 return fAsset->getRemainingLength() == 0;
46}
47
Mike Reedbe896ed2017-09-19 17:01:30 -040048SkStreamRewindable* AssetStreamAdaptor::onDuplicate() const {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040049 // Cannot create a duplicate, since each AssetStreamAdaptor
50 // would be modifying the Asset.
51 //return new AssetStreamAdaptor(fAsset);
52 return NULL;
53}
54
Leon Scroggins III0492eef2018-05-07 10:59:31 -040055bool AssetStreamAdaptor::hasPosition() const {
56 return fAsset->seek(0, SEEK_CUR) != -1;
57}
58
59size_t AssetStreamAdaptor::getPosition() const {
60 const off64_t offset = fAsset->seek(0, SEEK_CUR);
61 if (offset == -1) {
62 SkDebugf("---- fAsset->seek(0, SEEK_CUR) failed\n");
63 return 0;
64 }
65
66 return offset;
67}
68
69bool AssetStreamAdaptor::seek(size_t position) {
70 if (fAsset->seek(position, SEEK_SET) == -1) {
71 SkDebugf("---- fAsset->seek(0, SEEK_SET) failed\n");
72 return false;
73 }
74
75 return true;
76}
77
78bool AssetStreamAdaptor::move(long offset) {
79 if (fAsset->seek(offset, SEEK_CUR) == -1) {
Sally Qic5a4f232021-07-13 22:05:31 +000080 SkDebugf("---- fAsset->seek(%li, SEEK_CUR) failed\n", offset);
Leon Scroggins III0492eef2018-05-07 10:59:31 -040081 return false;
82 }
83
84 return true;
85}
86
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080087size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
88 ssize_t amount;
89
90 if (NULL == buffer) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040091 if (0 == size) {
92 return 0;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080093 }
94 // asset->seek returns new total offset
95 // we want to return amount that was skipped
96
Kenny Rootddb76c42010-11-24 12:56:06 -080097 off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080098 if (-1 == oldOffset) {
99 SkDebugf("---- fAsset->seek(oldOffset) failed\n");
100 return 0;
101 }
Kenny Rootddb76c42010-11-24 12:56:06 -0800102 off64_t newOffset = fAsset->seek(size, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800103 if (-1 == newOffset) {
Sally Qic5a4f232021-07-13 22:05:31 +0000104 SkDebugf("---- fAsset->seek(%zu) failed\n", size);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800105 return 0;
106 }
107 amount = newOffset - oldOffset;
108 } else {
109 amount = fAsset->read(buffer, size);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800110 }
111
112 if (amount < 0) {
113 amount = 0;
114 }
115 return amount;
116}
117
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400118sk_sp<SkData> android::CopyAssetToData(Asset* asset) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400119 if (NULL == asset) {
120 return NULL;
121 }
122
Leon Scroggins III34497892015-01-20 15:52:43 -0500123 const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
124 if ((off64_t)-1 == seekReturnVal) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400125 SkDebugf("---- copyAsset: asset rewind failed\n");
126 return NULL;
127 }
128
Leon Scroggins III34497892015-01-20 15:52:43 -0500129 const off64_t size = asset->getLength();
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400130 if (size <= 0) {
Sally Qic5a4f232021-07-13 22:05:31 +0000131 SkDebugf("---- copyAsset: asset->getLength() returned %" PRId64 "\n", size);
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400132 return NULL;
133 }
134
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400135 sk_sp<SkData> data(SkData::MakeUninitialized(size));
Leon Scroggins III34497892015-01-20 15:52:43 -0500136 const off64_t len = asset->read(data->writable_data(), size);
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400137 if (len != size) {
Sally Qic5a4f232021-07-13 22:05:31 +0000138 SkDebugf("---- copyAsset: asset->read(%" PRId64 ") returned %" PRId64 "\n", size, len);
Leon Scroggins III34497892015-01-20 15:52:43 -0500139 return NULL;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400140 }
Leon Scroggins III34497892015-01-20 15:52:43 -0500141
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400142 return data;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400143}
144
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800145jobject android::nullObjectReturn(const char msg[]) {
146 if (msg) {
147 SkDebugf("--- %s\n", msg);
148 }
149 return NULL;
150}
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100151
152bool android::isSeekable(int descriptor) {
153 return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
154}
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500155
156JNIEnv* android::get_env_or_die(JavaVM* jvm) {
157 JNIEnv* env;
158 if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
159 LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", jvm);
160 }
161 return env;
162}
Leon Scroggins IIIf97b29d22020-04-06 12:01:01 -0400163
164JNIEnv* android::requireEnv(JavaVM* jvm) {
165 JNIEnv* env;
166 if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
167 if (jvm->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) {
168 LOG_ALWAYS_FATAL("Failed to AttachCurrentThread!");
169 }
170 }
171 return env;
172}