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 _ANDROID_GRAPHICS_UTILS_H_ 18 #define _ANDROID_GRAPHICS_UTILS_H_ 19 20 #include "SkStream.h" 21 22 #include <jni.h> 23 #include <androidfw/Asset.h> 24 25 namespace android { 26 27 class AssetStreamAdaptor : public SkStreamRewindable { 28 public: 29 explicit AssetStreamAdaptor(Asset*); 30 31 virtual bool rewind(); 32 virtual size_t read(void* buffer, size_t size); hasLength()33 virtual bool hasLength() const { return true; } 34 virtual size_t getLength() const; 35 virtual bool hasPosition() const; 36 virtual size_t getPosition() const; 37 virtual bool seek(size_t position); 38 virtual bool move(long offset); 39 virtual bool isAtEnd() const; 40 41 protected: 42 SkStreamRewindable* onDuplicate() const override; 43 44 private: 45 Asset* fAsset; 46 }; 47 48 /** 49 * Make a deep copy of the asset, and return it as an SkData, or NULL if there 50 * was an error. 51 */ 52 53 sk_sp<SkData> CopyAssetToData(Asset*); 54 55 /** Restore the file descriptor's offset in our destructor 56 */ 57 class AutoFDSeek { 58 public: AutoFDSeek(int fd)59 explicit AutoFDSeek(int fd) : fFD(fd) { 60 fCurr = ::lseek(fd, 0, SEEK_CUR); 61 } ~AutoFDSeek()62 ~AutoFDSeek() { 63 if (fCurr >= 0) { 64 ::lseek(fFD, fCurr, SEEK_SET); 65 } 66 } 67 private: 68 int fFD; 69 off64_t fCurr; 70 }; 71 72 jobject nullObjectReturn(const char msg[]); 73 74 /** Check if the file descriptor is seekable. 75 */ 76 bool isSeekable(int descriptor); 77 78 JNIEnv* get_env_or_die(JavaVM* jvm); 79 80 /** 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 */ 88 JNIEnv* requireEnv(JavaVM* jvm); 89 90 }; // namespace android 91 92 #endif // _ANDROID_GRAPHICS_UTILS_H_ 93