blob: 69a1b165af9acc4d0843f0cf914514c816328be3 [file] [log] [blame]
Andreas Huber9266f992016-08-25 11:21:21 -07001/*
2 * Copyright (C) 2016 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_OS_HW_BLOB_H
18#define ANDROID_OS_HW_BLOB_H
19
20#include <android-base/macros.h>
21#include <jni.h>
22#include <hidl/HidlSupport.h>
Yifan Hongddf31f62016-11-15 16:02:40 -080023#include <hwbinder/Parcel.h>
Andreas Huber9266f992016-08-25 11:21:21 -070024#include <utils/RefBase.h>
25#include <utils/Vector.h>
26
27namespace android {
28
29struct JHwBlob : public RefBase {
Nirav Atre9850dd92018-07-24 17:03:44 -070030 enum class BlobType {
31 GENERIC,
32 NATIVE_HANDLE,
33 };
34
Andreas Huber9266f992016-08-25 11:21:21 -070035 static void InitClass(JNIEnv *env);
36
37 static sp<JHwBlob> SetNativeContext(
38 JNIEnv *env, jobject thiz, const sp<JHwBlob> &context);
39
40 static sp<JHwBlob> GetNativeContext(JNIEnv *env, jobject thiz);
41
42 static jobject NewObject(JNIEnv *env, const void *ptr, size_t handle);
43 static jobject NewObject(JNIEnv *env, size_t size);
44
45 JHwBlob(JNIEnv *env, jobject thiz, size_t size);
46
47 void setTo(const void *ptr, size_t handle);
48
49 status_t getHandle(size_t *handle) const;
50
51 status_t read(size_t offset, void *data, size_t size) const;
52 status_t write(size_t offset, const void *data, size_t size);
53
54 status_t getString(
55 size_t offset, const android::hardware::hidl_string **s) const;
56
57 const void *data() const;
Andreas Huber0eb37e02017-10-31 11:51:50 -070058 void *data();
59
Andreas Huber9266f992016-08-25 11:21:21 -070060 size_t size() const;
61
Nirav Atre9850dd92018-07-24 17:03:44 -070062 void specializeBlobTo(BlobType type);
63 BlobType type() const;
64
Andreas Huber9266f992016-08-25 11:21:21 -070065 status_t putBlob(size_t offset, const sp<JHwBlob> &blob);
66
67 status_t writeToParcel(hardware::Parcel *parcel) const;
68
69 status_t writeEmbeddedToParcel(
70 hardware::Parcel *parcel,
71 size_t parentHandle,
72 size_t parentOffset) const;
73
74protected:
75 virtual ~JHwBlob();
76
77private:
78 struct BlobInfo {
79 size_t mOffset;
80 sp<JHwBlob> mBlob;
81 };
82
Andreas Huber9266f992016-08-25 11:21:21 -070083 void *mBuffer;
84 size_t mSize;
Nirav Atre9850dd92018-07-24 17:03:44 -070085 BlobType mType;
Andreas Huber9266f992016-08-25 11:21:21 -070086 bool mOwnsBuffer;
87
88 size_t mHandle;
89
90 Vector<BlobInfo> mSubBlobs;
91
Nirav Atre9850dd92018-07-24 17:03:44 -070092 status_t writeSubBlobsToParcel(hardware::Parcel *parcel, size_t parentHandle) const;
93
Andreas Huber9266f992016-08-25 11:21:21 -070094 DISALLOW_COPY_AND_ASSIGN(JHwBlob);
95};
96
97int register_android_os_HwBlob(JNIEnv *env);
98
99} // namespace android
100
101#endif // ANDROID_OS_HW_BLOB_H
102