blob: 89255177ba2ef8e124246ed99d9dd147edc9cce5 [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/**
49 * Make a deep copy of the asset, and return it as a stream, or NULL if there
50 * was an error.
51 * FIXME: If we could "ref/reopen" the asset, we may not need to copy it here.
52 */
53
54SkMemoryStream* CopyAssetToStream(Asset*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080055
56/** Restore the file descriptor's offset in our destructor
57 */
58class AutoFDSeek {
59public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070060 explicit AutoFDSeek(int fd) : fFD(fd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080061 fCurr = ::lseek(fd, 0, SEEK_CUR);
62 }
63 ~AutoFDSeek() {
64 if (fCurr >= 0) {
65 ::lseek(fFD, fCurr, SEEK_SET);
66 }
67 }
68private:
69 int fFD;
Kenny Rootddb76c42010-11-24 12:56:06 -080070 off64_t fCurr;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080071};
72
73jobject nullObjectReturn(const char msg[]);
74
Yujie Qinc1d7b7f2016-02-29 14:00:29 +010075/** Check if the file descriptor is seekable.
76 */
77bool isSeekable(int descriptor);
78
Leon Scroggins III127d31a2018-01-19 12:29:47 -050079JNIEnv* get_env_or_die(JavaVM* jvm);
80
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080081}; // namespace android
82
Andreas Gampeed6b9df2014-11-20 22:02:20 -080083#endif // _ANDROID_GRAPHICS_UTILS_H_