blob: 6cdf44d85a5a7d8c518513f12e3b9868572b62aa [file] [log] [blame]
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001/*
2 * Copyright (C) 2006 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
Andreas Gampeed6b9df2014-11-20 22:02:20 -080017#ifndef _ANDROID_GRAPHICS_UTILS_H_
18#define _ANDROID_GRAPHICS_UTILS_H_
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080019
20#include "SkStream.h"
21
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080022#include <jni.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080023#include <androidfw/Asset.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080024
25namespace android {
26
Leon Scroggins IIIca320212013-08-20 17:59:39 -040027class AssetStreamAdaptor : public SkStreamRewindable {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080028public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070029 explicit AssetStreamAdaptor(Asset*);
Leon Scroggins III5e49b492013-12-03 16:26:51 -050030
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080031 virtual bool rewind();
32 virtual size_t read(void* buffer, size_t size);
Leon Scroggins IIIca320212013-08-20 17:59:39 -040033 virtual bool hasLength() const { return true; }
34 virtual size_t getLength() const;
Leon Scroggins III0492eef2018-05-07 10:59:31 -040035 virtual bool hasPosition() const;
36 virtual size_t getPosition() const;
37 virtual bool seek(size_t position);
38 virtual bool move(long offset);
Leon Scroggins IIIca320212013-08-20 17:59:39 -040039 virtual bool isAtEnd() const;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080040
Mike Reedbe896ed2017-09-19 17:01:30 -040041protected:
42 SkStreamRewindable* onDuplicate() const override;
43
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080044private:
Ben Wagnerc2d39572015-01-12 17:06:21 -050045 Asset* fAsset;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080046};
47
Leon Scroggins IIIca320212013-08-20 17:59:39 -040048/**
Leon Scroggins III23ac0362020-05-04 15:38:58 -040049 * Make a deep copy of the asset, and return it as an SkData, or NULL if there
Leon Scroggins IIIca320212013-08-20 17:59:39 -040050 * was an error.
Leon Scroggins IIIca320212013-08-20 17:59:39 -040051 */
52
Leon Scroggins III23ac0362020-05-04 15:38:58 -040053sk_sp<SkData> CopyAssetToData(Asset*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080054
55/** Restore the file descriptor's offset in our destructor
56 */
57class AutoFDSeek {
58public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070059 explicit AutoFDSeek(int fd) : fFD(fd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080060 fCurr = ::lseek(fd, 0, SEEK_CUR);
61 }
62 ~AutoFDSeek() {
63 if (fCurr >= 0) {
64 ::lseek(fFD, fCurr, SEEK_SET);
65 }
66 }
67private:
68 int fFD;
Kenny Rootddb76c42010-11-24 12:56:06 -080069 off64_t fCurr;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080070};
71
72jobject nullObjectReturn(const char msg[]);
73
Yujie Qinc1d7b7f2016-02-29 14:00:29 +010074/** Check if the file descriptor is seekable.
75 */
76bool isSeekable(int descriptor);
77
Leon Scroggins III127d31a2018-01-19 12:29:47 -050078JNIEnv* get_env_or_die(JavaVM* jvm);
79
Leon Scroggins IIIf97b29d22020-04-06 12:01:01 -040080/**
81 * Helper method for accessing the JNI interface pointer.
82 *
83 * Image decoding (which this supports) is started on a thread that is already
84 * attached to the Java VM. But an AnimatedImageDrawable continues decoding on
85 * the AnimatedImageThread, which is not attached. This will attach if
86 * necessary.
87 */
88JNIEnv* requireEnv(JavaVM* jvm);
89
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080090}; // namespace android
91
Andreas Gampeed6b9df2014-11-20 22:02:20 -080092#endif // _ANDROID_GRAPHICS_UTILS_H_