blob: 34fd6687d52ca48b93a303a7e1ab21611545b95c [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
Derek Sollenbergereec1b862019-10-24 09:44:55 -040021#include <log/log.h>
22
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080023using namespace android;
24
Ben Wagnerc2d39572015-01-12 17:06:21 -050025AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050026 : fAsset(asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050027{
28}
29
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080030bool AssetStreamAdaptor::rewind() {
Kenny Rootddb76c42010-11-24 12:56:06 -080031 off64_t pos = fAsset->seek(0, SEEK_SET);
32 if (pos == (off64_t)-1) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080033 SkDebugf("----- fAsset->seek(rewind) failed\n");
34 return false;
35 }
36 return true;
37}
38
Leon Scroggins IIIca320212013-08-20 17:59:39 -040039size_t AssetStreamAdaptor::getLength() const {
40 return fAsset->getLength();
41}
42
43bool AssetStreamAdaptor::isAtEnd() const {
44 return fAsset->getRemainingLength() == 0;
45}
46
Mike Reedbe896ed2017-09-19 17:01:30 -040047SkStreamRewindable* AssetStreamAdaptor::onDuplicate() const {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040048 // 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 III0492eef2018-05-07 10:59:31 -040054bool AssetStreamAdaptor::hasPosition() const {
55 return fAsset->seek(0, SEEK_CUR) != -1;
56}
57
58size_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
68bool 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
77bool 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 Chen6b849e22010-09-07 17:32:18 +080086size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
87 ssize_t amount;
88
89 if (NULL == buffer) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040090 if (0 == size) {
91 return 0;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080092 }
93 // asset->seek returns new total offset
94 // we want to return amount that was skipped
95
Kenny Rootddb76c42010-11-24 12:56:06 -080096 off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080097 if (-1 == oldOffset) {
98 SkDebugf("---- fAsset->seek(oldOffset) failed\n");
99 return 0;
100 }
Kenny Rootddb76c42010-11-24 12:56:06 -0800101 off64_t newOffset = fAsset->seek(size, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800102 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 Chen6b849e22010-09-07 17:32:18 +0800109 }
110
111 if (amount < 0) {
112 amount = 0;
113 }
114 return amount;
115}
116
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400117SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
118 if (NULL == asset) {
119 return NULL;
120 }
121
Leon Scroggins III34497892015-01-20 15:52:43 -0500122 const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
123 if ((off64_t)-1 == seekReturnVal) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400124 SkDebugf("---- copyAsset: asset rewind failed\n");
125 return NULL;
126 }
127
Leon Scroggins III34497892015-01-20 15:52:43 -0500128 const off64_t size = asset->getLength();
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400129 if (size <= 0) {
130 SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
131 return NULL;
132 }
133
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400134 sk_sp<SkData> data(SkData::MakeUninitialized(size));
Leon Scroggins III34497892015-01-20 15:52:43 -0500135 const off64_t len = asset->read(data->writable_data(), size);
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400136 if (len != size) {
137 SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
Leon Scroggins III34497892015-01-20 15:52:43 -0500138 return NULL;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400139 }
Leon Scroggins III34497892015-01-20 15:52:43 -0500140
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400141 return new SkMemoryStream(std::move(data));
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400142}
143
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800144jobject android::nullObjectReturn(const char msg[]) {
145 if (msg) {
146 SkDebugf("--- %s\n", msg);
147 }
148 return NULL;
149}
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100150
151bool android::isSeekable(int descriptor) {
152 return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
153}
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500154
155JNIEnv* 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 IIIf97b29d22020-04-06 12:01:01 -0400162
163JNIEnv* 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}