blob: f6e3a0eeaa0e3b26d7b81a53546343af29e8b218 [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
Kevin Lubick1175dc02022-02-28 12:41:27 -050020#include "SkRefCnt.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080021#include "SkStream.h"
22
Kevin Lubick1175dc02022-02-28 12:41:27 -050023class SkData;
24
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080025#include <jni.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080026#include <androidfw/Asset.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080027
28namespace android {
29
Leon Scroggins IIIca320212013-08-20 17:59:39 -040030class AssetStreamAdaptor : public SkStreamRewindable {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080031public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070032 explicit AssetStreamAdaptor(Asset*);
Leon Scroggins III5e49b492013-12-03 16:26:51 -050033
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080034 virtual bool rewind();
35 virtual size_t read(void* buffer, size_t size);
Leon Scroggins IIIca320212013-08-20 17:59:39 -040036 virtual bool hasLength() const { return true; }
37 virtual size_t getLength() const;
Leon Scroggins III0492eef2018-05-07 10:59:31 -040038 virtual bool hasPosition() const;
39 virtual size_t getPosition() const;
40 virtual bool seek(size_t position);
41 virtual bool move(long offset);
Leon Scroggins IIIca320212013-08-20 17:59:39 -040042 virtual bool isAtEnd() const;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080043
Mike Reedbe896ed2017-09-19 17:01:30 -040044protected:
45 SkStreamRewindable* onDuplicate() const override;
46
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080047private:
Ben Wagnerc2d39572015-01-12 17:06:21 -050048 Asset* fAsset;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080049};
50
Leon Scroggins IIIca320212013-08-20 17:59:39 -040051/**
Leon Scroggins III23ac0362020-05-04 15:38:58 -040052 * 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 -040053 * was an error.
Leon Scroggins IIIca320212013-08-20 17:59:39 -040054 */
55
Leon Scroggins III23ac0362020-05-04 15:38:58 -040056sk_sp<SkData> CopyAssetToData(Asset*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080057
58/** Restore the file descriptor's offset in our destructor
59 */
60class AutoFDSeek {
61public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070062 explicit AutoFDSeek(int fd) : fFD(fd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080063 fCurr = ::lseek(fd, 0, SEEK_CUR);
64 }
65 ~AutoFDSeek() {
66 if (fCurr >= 0) {
67 ::lseek(fFD, fCurr, SEEK_SET);
68 }
69 }
70private:
71 int fFD;
Kenny Rootddb76c42010-11-24 12:56:06 -080072 off64_t fCurr;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080073};
74
75jobject nullObjectReturn(const char msg[]);
76
Yujie Qinc1d7b7f2016-02-29 14:00:29 +010077/** Check if the file descriptor is seekable.
78 */
79bool isSeekable(int descriptor);
80
Leon Scroggins III127d31a2018-01-19 12:29:47 -050081JNIEnv* get_env_or_die(JavaVM* jvm);
82
Leon Scroggins IIIf97b29d22020-04-06 12:01:01 -040083/**
84 * Helper method for accessing the JNI interface pointer.
85 *
86 * Image decoding (which this supports) is started on a thread that is already
87 * attached to the Java VM. But an AnimatedImageDrawable continues decoding on
88 * the AnimatedImageThread, which is not attached. This will attach if
89 * necessary.
90 */
91JNIEnv* requireEnv(JavaVM* jvm);
92
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080093}; // namespace android
94
Andreas Gampeed6b9df2014-11-20 22:02:20 -080095#endif // _ANDROID_GRAPHICS_UTILS_H_