| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 1 | /* | 
|  | 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 Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 17 | #ifndef _ANDROID_GRAPHICS_UTILS_H_ | 
|  | 18 | #define _ANDROID_GRAPHICS_UTILS_H_ | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 19 |  | 
| Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 20 | #include "SkRefCnt.h" | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 21 | #include "SkStream.h" | 
|  | 22 |  | 
| Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 23 | class SkData; | 
|  | 24 |  | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 25 | #include <jni.h> | 
| Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 26 | #include <androidfw/Asset.h> | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 27 |  | 
|  | 28 | namespace android { | 
|  | 29 |  | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 30 | class AssetStreamAdaptor : public SkStreamRewindable { | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 31 | public: | 
| Chih-Hung Hsieh | a654328 | 2016-08-29 14:46:35 -0700 | [diff] [blame] | 32 | explicit AssetStreamAdaptor(Asset*); | 
| Leon Scroggins III | 5e49b49 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 33 |  | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 34 | virtual bool rewind(); | 
|  | 35 | virtual size_t read(void* buffer, size_t size); | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 36 | virtual bool hasLength() const { return true; } | 
|  | 37 | virtual size_t getLength() const; | 
| Leon Scroggins III | 0492eef | 2018-05-07 10:59:31 -0400 | [diff] [blame] | 38 | 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 III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 42 | virtual bool isAtEnd() const; | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 43 |  | 
| Mike Reed | be896ed | 2017-09-19 17:01:30 -0400 | [diff] [blame] | 44 | protected: | 
|  | 45 | SkStreamRewindable* onDuplicate() const override; | 
|  | 46 |  | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 47 | private: | 
| Ben Wagner | c2d3957 | 2015-01-12 17:06:21 -0500 | [diff] [blame] | 48 | Asset* fAsset; | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 49 | }; | 
|  | 50 |  | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 51 | /** | 
| Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 52 | *  Make a deep copy of the asset, and return it as an SkData, or NULL if there | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 53 | *  was an error. | 
| Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 54 | */ | 
|  | 55 |  | 
| Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 56 | sk_sp<SkData> CopyAssetToData(Asset*); | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 57 |  | 
|  | 58 | /** Restore the file descriptor's offset in our destructor | 
|  | 59 | */ | 
|  | 60 | class AutoFDSeek { | 
|  | 61 | public: | 
| Chih-Hung Hsieh | a654328 | 2016-08-29 14:46:35 -0700 | [diff] [blame] | 62 | explicit AutoFDSeek(int fd) : fFD(fd) { | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 63 | fCurr = ::lseek(fd, 0, SEEK_CUR); | 
|  | 64 | } | 
|  | 65 | ~AutoFDSeek() { | 
|  | 66 | if (fCurr >= 0) { | 
|  | 67 | ::lseek(fFD, fCurr, SEEK_SET); | 
|  | 68 | } | 
|  | 69 | } | 
|  | 70 | private: | 
|  | 71 | int     fFD; | 
| Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 72 | off64_t   fCurr; | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 73 | }; | 
|  | 74 |  | 
|  | 75 | jobject nullObjectReturn(const char msg[]); | 
|  | 76 |  | 
| Yujie Qin | c1d7b7f | 2016-02-29 14:00:29 +0100 | [diff] [blame] | 77 | /** Check if the file descriptor is seekable. | 
|  | 78 | */ | 
|  | 79 | bool isSeekable(int descriptor); | 
|  | 80 |  | 
| Leon Scroggins III | 127d31a | 2018-01-19 12:29:47 -0500 | [diff] [blame] | 81 | JNIEnv* get_env_or_die(JavaVM* jvm); | 
|  | 82 |  | 
| Leon Scroggins III | f97b29d2 | 2020-04-06 12:01:01 -0400 | [diff] [blame] | 83 | /** | 
|  | 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 | */ | 
|  | 91 | JNIEnv* requireEnv(JavaVM* jvm); | 
|  | 92 |  | 
| Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 93 | }; // namespace android | 
|  | 94 |  | 
| Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 95 | #endif  // _ANDROID_GRAPHICS_UTILS_H_ |