blob: d1a74a0adf6cf65002901cdae0f472de8bce4471 [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
22#include "android_util_Binder.h"
23
24#include <jni.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080025#include <androidfw/Asset.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080026
27namespace android {
28
Leon Scroggins IIIca320212013-08-20 17:59:39 -040029class AssetStreamAdaptor : public SkStreamRewindable {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080030public:
Ben Wagnerc2d39572015-01-12 17:06:21 -050031 AssetStreamAdaptor(Asset*);
Leon Scroggins III5e49b492013-12-03 16:26:51 -050032
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080033 virtual bool rewind();
34 virtual size_t read(void* buffer, size_t size);
Leon Scroggins IIIca320212013-08-20 17:59:39 -040035 virtual bool hasLength() const { return true; }
36 virtual size_t getLength() const;
37 virtual bool isAtEnd() const;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080038
Leon Scroggins IIIca320212013-08-20 17:59:39 -040039 virtual SkStreamRewindable* duplicate() const;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080040private:
Ben Wagnerc2d39572015-01-12 17:06:21 -050041 Asset* fAsset;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080042};
43
Leon Scroggins IIIca320212013-08-20 17:59:39 -040044/**
45 * Make a deep copy of the asset, and return it as a stream, or NULL if there
46 * was an error.
47 * FIXME: If we could "ref/reopen" the asset, we may not need to copy it here.
48 */
49
50SkMemoryStream* CopyAssetToStream(Asset*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080051
52/** Restore the file descriptor's offset in our destructor
53 */
54class AutoFDSeek {
55public:
56 AutoFDSeek(int fd) : fFD(fd) {
57 fCurr = ::lseek(fd, 0, SEEK_CUR);
58 }
59 ~AutoFDSeek() {
60 if (fCurr >= 0) {
61 ::lseek(fFD, fCurr, SEEK_SET);
62 }
63 }
64private:
65 int fFD;
Kenny Rootddb76c42010-11-24 12:56:06 -080066 off64_t fCurr;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080067};
68
69jobject nullObjectReturn(const char msg[]);
70
Yujie Qinc1d7b7f2016-02-29 14:00:29 +010071/** Check if the file descriptor is seekable.
72 */
73bool isSeekable(int descriptor);
74
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080075}; // namespace android
76
Andreas Gampeed6b9df2014-11-20 22:02:20 -080077#endif // _ANDROID_GRAPHICS_UTILS_H_