• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 STAGEFRIGHT_CODEC2_ALLOCATOR_GRALLOC_H_
18 #define STAGEFRIGHT_CODEC2_ALLOCATOR_GRALLOC_H_
19 
20 #include <functional>
21 
22 #include <C2Buffer.h>
23 
24 namespace android {
25 // VNDK
26 /**
27  * Extract pixel format from the extra data of gralloc handle.
28  *
29  * @return 0 when no valid pixel format exists.
30  */
31 uint32_t ExtractFormatFromCodec2GrallocHandle(const C2Handle *const handle);
32 
33 /**
34  * Extract metadata from the extra data of gralloc handle.
35  *
36  * @return {@code false} if extraction was failed, {@code true} otherwise.
37  */
38 bool ExtractMetadataFromCodec2GrallocHandle(
39     const C2Handle *const handle,
40     uint32_t *width, uint32_t *height, uint32_t *format, uint64_t *usage, uint32_t  *stride);
41 
42 // Not for VNDK (system partition and inside vndk only)
43 /**
44  * Unwrap the native handle from a Codec2 handle allocated by C2AllocatorGralloc.
45  *
46  * @param handle a handle allocated by C2AllocatorGralloc. This includes handles returned for a
47  * graphic block allocation handle returned.
48  *
49  * @return a new NON-OWNING native handle that must be deleted using native_handle_delete.
50  */
51 native_handle_t *UnwrapNativeCodec2GrallocHandle(const C2Handle *const handle);
52 
53 /**
54  * Wrap the gralloc handle and metadata into Codec2 handle recognized by
55  * C2AllocatorGralloc.
56  *
57  * @return a new NON-OWNING C2Handle that must be closed and deleted using native_handle_close and
58  * native_handle_delete.
59  */
60 C2Handle *WrapNativeCodec2GrallocHandle(
61         const native_handle_t *const handle,
62         uint32_t width, uint32_t height, uint32_t format, uint64_t usage, uint32_t stride,
63         uint32_t generation = 0, uint64_t igbp_id = 0, uint32_t igbp_slot = 0);
64 
65 /**
66  * When the gralloc handle is migrated to another bufferqueue, update
67  * bufferqueue information.
68  *
69  * @return {@code true} when native_handle is a wrapped codec2 handle.
70  */
71 bool MigrateNativeCodec2GrallocHandle(
72         native_handle_t *handle,
73         uint32_t generation, uint64_t igbp_id, uint32_t igbp_slot);
74 
75 /**
76  * \todo Get this from the buffer
77  */
78 void _UnwrapNativeCodec2GrallocMetadata(
79         const C2Handle *const handle,
80         uint32_t *width, uint32_t *height, uint32_t *format, uint64_t *usage, uint32_t *stride,
81         uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot);
82 
83 /**
84  * Wrap the gralloc handle and metadata based on AHardwareBuffer into Codec2 handle
85  * recognized by C2AllocatorAhwb.
86  *
87  * @return a new NON-OWNING C2Handle that must be closed and deleted using native_handle_close and
88  * native_handle_delete.
89  */
90 C2Handle *WrapNativeCodec2AhwbHandle(
91         const native_handle_t *const handle,
92         uint32_t width, uint32_t height, uint32_t format, uint64_t usage, uint32_t stride,
93         uint64_t origId);
94 
95 class C2AllocatorGralloc : public C2Allocator {
96 public:
97     virtual id_t getId() const override;
98 
99     virtual C2String getName() const override;
100 
101     virtual std::shared_ptr<const Traits> getTraits() const override;
102 
103     virtual c2_status_t newGraphicAllocation(
104             uint32_t width, uint32_t height, uint32_t format, C2MemoryUsage usage,
105             std::shared_ptr<C2GraphicAllocation> *allocation) override;
106 
107     virtual c2_status_t priorGraphicAllocation(
108             const C2Handle *handle,
109             std::shared_ptr<C2GraphicAllocation> *allocation) override;
110 
111     C2AllocatorGralloc(id_t id, bool bufferQueue = false);
112 
113     c2_status_t status() const;
114 
115     virtual ~C2AllocatorGralloc() override;
116 
checkHandle(const C2Handle * const o)117     virtual bool checkHandle(const C2Handle* const o) const override { return CheckHandle(o); }
118 
119     static bool CheckHandle(const C2Handle* const o);
120 
121     // deprecated
isValid(const C2Handle * const o)122     static bool isValid(const C2Handle* const o) { return CheckHandle(o); }
123 
124 private:
125     class Impl;
126     Impl *mImpl;
127 };
128 
129 /**
130  * C2Allocator for AHardwareBuffer based allocation.
131  *
132  * C2Allocator interface is based on C2Handle, which is actually wrapped
133  * native_handle_t. This is based on extracted handle from AHardwareBuffer.
134  * Trying to recover an AHardwareBuffer from C2GraphicAllocation created by the
135  * allocator will creates a new AHardwareBuffer with a different unique Id, but
136  * it is identical and will use same memory by the handle.
137  *
138  * C2GraphicAllocation does not have the original AHardwareBuffer. But
139  * C2GraphicBlock and C2ConstGraphicBlock has the original AHardwareBuffer,
140  * which can be sent to the other processes.
141  *
142  * TODO: Bundle AHardwareBuffer for C2GraphicAllocation.
143  * TODO: Add support for C2AllocatorBlob.
144  */
145 class C2AllocatorAhwb : public C2Allocator {
146 public:
147     virtual id_t getId() const override;
148 
149     virtual C2String getName() const override;
150 
151     virtual std::shared_ptr<const Traits> getTraits() const override;
152 
153     virtual c2_status_t newGraphicAllocation(
154             uint32_t width, uint32_t height, uint32_t format, C2MemoryUsage usage,
155             std::shared_ptr<C2GraphicAllocation> *allocation) override;
156 
157     virtual c2_status_t priorGraphicAllocation(
158             const C2Handle *handle,
159             std::shared_ptr<C2GraphicAllocation> *allocation) override;
160 
161     C2AllocatorAhwb(id_t id);
162 
163     c2_status_t status() const;
164 
165     virtual ~C2AllocatorAhwb() override;
166 
checkHandle(const C2Handle * const o)167     virtual bool checkHandle(const C2Handle* const o) const override { return CheckHandle(o); }
168 
169     static bool CheckHandle(const C2Handle* const o);
170 
171 private:
172     class Impl;
173     Impl *mImpl;
174 };
175 
176 } // namespace android
177 
178 #endif // STAGEFRIGHT_CODEC2_ALLOCATOR_GRALLOC_H_
179