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 
26 /**
27  * Unwrap the native handle from a Codec2 handle allocated by C2AllocatorGralloc.
28  *
29  * @param handle a handle allocated by C2AllocatorGralloc. This includes handles returned for a
30  * graphic block allocation handle returned.
31  *
32  * @return a new NON-OWNING native handle that must be deleted using native_handle_delete.
33  */
34 native_handle_t *UnwrapNativeCodec2GrallocHandle(const C2Handle *const handle);
35 
36 /**
37  * Wrap the gralloc handle and metadata into Codec2 handle recognized by
38  * C2AllocatorGralloc.
39  *
40  * @return a new NON-OWNING C2Handle that must be closed and deleted using native_handle_close and
41  * native_handle_delete.
42  */
43 C2Handle *WrapNativeCodec2GrallocHandle(
44         const native_handle_t *const handle,
45         uint32_t width, uint32_t height, uint32_t format, uint64_t usage, uint32_t stride,
46         uint32_t generation = 0, uint64_t igbp_id = 0, uint32_t igbp_slot = 0);
47 
48 /**
49  * When the gralloc handle is migrated to another bufferqueue, update
50  * bufferqueue information.
51  *
52  * @return {@code true} when native_handle is a wrapped codec2 handle.
53  */
54 bool MigrateNativeCodec2GrallocHandle(
55         native_handle_t *handle,
56         uint32_t generation, uint64_t igbp_id, uint32_t igbp_slot);
57 
58 /**
59  * \todo Get this from the buffer
60  */
61 void _UnwrapNativeCodec2GrallocMetadata(
62         const C2Handle *const handle,
63         uint32_t *width, uint32_t *height, uint32_t *format, uint64_t *usage, uint32_t *stride,
64         uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot);
65 
66 class C2AllocatorGralloc : public C2Allocator {
67 public:
68     virtual id_t getId() const override;
69 
70     virtual C2String getName() const override;
71 
72     virtual std::shared_ptr<const Traits> getTraits() const override;
73 
74     virtual c2_status_t newGraphicAllocation(
75             uint32_t width, uint32_t height, uint32_t format, C2MemoryUsage usage,
76             std::shared_ptr<C2GraphicAllocation> *allocation) override;
77 
78     virtual c2_status_t priorGraphicAllocation(
79             const C2Handle *handle,
80             std::shared_ptr<C2GraphicAllocation> *allocation) override;
81 
82     C2AllocatorGralloc(id_t id, bool bufferQueue = false);
83 
84     c2_status_t status() const;
85 
86     virtual ~C2AllocatorGralloc() override;
87 
checkHandle(const C2Handle * const o)88     virtual bool checkHandle(const C2Handle* const o) const override { return CheckHandle(o); }
89 
90     static bool CheckHandle(const C2Handle* const o);
91 
92     // deprecated
isValid(const C2Handle * const o)93     static bool isValid(const C2Handle* const o) { return CheckHandle(o); }
94 
95 private:
96     class Impl;
97     Impl *mImpl;
98 };
99 
100 } // namespace android
101 
102 #endif // STAGEFRIGHT_CODEC2_ALLOCATOR_GRALLOC_H_
103