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 | |
| 17 | #ifndef UTILS_DEFINED |
| 18 | #define UTILS_DEFINED |
| 19 | |
| 20 | #include "SkStream.h" |
| 21 | |
| 22 | #include "android_util_Binder.h" |
| 23 | |
| 24 | #include <jni.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 25 | #include <androidfw/Asset.h> |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 29 | class AssetStreamAdaptor : public SkStreamRewindable { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 30 | public: |
Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 31 | // Enum passed to constructor. If set to kYes_OwnAsset, |
| 32 | // the passed in Asset will be deleted upon destruction. |
| 33 | enum OwnAsset { |
| 34 | kYes_OwnAsset, |
| 35 | kNo_OwnAsset, |
| 36 | }; |
| 37 | |
| 38 | // Enum passed to constructor. If set to kYes_HasMemoryBase, |
| 39 | // getMemoryBase will return the Asset's buffer. |
| 40 | enum HasMemoryBase { |
| 41 | kYes_HasMemoryBase, |
| 42 | kNo_HasMemoryBase, |
| 43 | }; |
| 44 | |
| 45 | AssetStreamAdaptor(Asset*, OwnAsset, HasMemoryBase); |
| 46 | ~AssetStreamAdaptor(); |
| 47 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 48 | virtual bool rewind(); |
| 49 | virtual size_t read(void* buffer, size_t size); |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 50 | virtual bool hasLength() const { return true; } |
| 51 | virtual size_t getLength() const; |
| 52 | virtual bool isAtEnd() const; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 53 | |
Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 54 | virtual const void* getMemoryBase() { return fMemoryBase; } |
| 55 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 56 | virtual SkStreamRewindable* duplicate() const; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 57 | private: |
Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 58 | Asset* fAsset; |
| 59 | const void* const fMemoryBase; |
| 60 | const OwnAsset fOwnAsset; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 61 | }; |
| 62 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 63 | /** |
| 64 | * Make a deep copy of the asset, and return it as a stream, or NULL if there |
| 65 | * was an error. |
| 66 | * FIXME: If we could "ref/reopen" the asset, we may not need to copy it here. |
| 67 | */ |
| 68 | |
| 69 | SkMemoryStream* CopyAssetToStream(Asset*); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 70 | |
| 71 | /** Restore the file descriptor's offset in our destructor |
| 72 | */ |
| 73 | class AutoFDSeek { |
| 74 | public: |
| 75 | AutoFDSeek(int fd) : fFD(fd) { |
| 76 | fCurr = ::lseek(fd, 0, SEEK_CUR); |
| 77 | } |
| 78 | ~AutoFDSeek() { |
| 79 | if (fCurr >= 0) { |
| 80 | ::lseek(fFD, fCurr, SEEK_SET); |
| 81 | } |
| 82 | } |
| 83 | private: |
| 84 | int fFD; |
Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 85 | off64_t fCurr; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | jobject nullObjectReturn(const char msg[]); |
| 89 | |
| 90 | }; // namespace android |
| 91 | |
| 92 | #endif |