1/*
2 * Copyright (C) 2017 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_PLATFORM_SUPPORT_H_
18#define STAGEFRIGHT_CODEC2_PLATFORM_SUPPORT_H_
19
20#include <C2Component.h>
21#include <C2ComponentFactory.h>
22
23#include <memory>
24
25namespace android {
26
27/**
28 * Returns the platform allocator store.
29 * \retval nullptr if the platform allocator store could not be obtained
30 */
31std::shared_ptr<C2AllocatorStore> GetCodec2PlatformAllocatorStore();
32
33/**
34 * Platform allocator store IDs
35 */
36class C2PlatformAllocatorStore : public C2AllocatorStore {
37public:
38    enum : id_t {
39        /**
40         * ID of the ion backed platform allocator.
41         *
42         * C2Handle consists of:
43         *   fd  shared ion buffer handle
44         *   int size (lo 32 bits)
45         *   int size (hi 32 bits)
46         *   int magic '\xc2io\x00'
47         */
48        ION = PLATFORM_START,
49
50        /**
51         * ID of the gralloc backed platform allocator.
52         *
53         * C2Handle layout is not public. Use C2AllocatorGralloc::UnwrapNativeCodec2GrallocHandle
54         * to get the underlying gralloc handle from a C2Handle, and WrapNativeCodec2GrallocHandle
55         * to create a C2Handle from a gralloc handle - for C2Allocator::priorAllocation.
56         */
57        GRALLOC,
58
59        /**
60         * ID of the bufferqueue backed platform allocator.
61         *
62         * C2Handle layout is not public. Use C2AllocatorGralloc::UnwrapNativeCodec2GrallocHandle
63         * to get the underlying handle from a C2Handle, and WrapNativeCodec2GrallocHandle
64         * to create a C2Handle from a handle - for C2Allocator::priorAllocation.
65         */
66        BUFFERQUEUE,
67
68        /**
69         * ID of indicating the end of platform allocator definition.
70         *
71         * \note always put this macro in the last place.
72         *
73         * Extended platform store plugin should use this macro as the start ID of its own allocator
74         * types.
75         */
76        PLATFORM_END,
77    };
78};
79
80/**
81 * Retrieves a block pool for a component.
82 *
83 * \param id        the local ID of the block pool
84 * \param component the component using the block pool (must be non-null)
85 * \param pool      pointer to where the obtained block pool shall be stored on success. nullptr
86 *                  will be stored here on failure
87 *
88 * \retval C2_OK        the operation was successful
89 * \retval C2_BAD_VALUE the component is null
90 * \retval C2_NOT_FOUND if the block pool does not exist
91 * \retval C2_NO_MEMORY not enough memory to fetch the block pool (this return value is only
92 *                      possible for basic pools)
93 * \retval C2_TIMED_OUT the operation timed out (this return value is only possible for basic pools)
94 * \retval C2_REFUSED   no permission to complete any required allocation (this return value is only
95 *                      possible for basic pools)
96 * \retval C2_CORRUPTED some unknown, unrecoverable error occured during operation (unexpected,
97 *                      this return value is only possible for basic pools)
98 */
99c2_status_t GetCodec2BlockPool(
100        C2BlockPool::local_id_t id, std::shared_ptr<const C2Component> component,
101        std::shared_ptr<C2BlockPool> *pool);
102
103/**
104 * Creates a block pool.
105 * \param allocatorId  the allocator ID which is used to allocate blocks
106 * \param component     the component using the block pool (must be non-null)
107 * \param pool          pointer to where the created block pool shall be store on success.
108 *                      nullptr will be stored here on failure
109 *
110 * \retval C2_OK        the operation was successful
111 * \retval C2_BAD_VALUE the component is null
112 * \retval C2_NOT_FOUND if the allocator does not exist
113 * \retval C2_NO_MEMORY not enough memory to create a block pool
114 */
115c2_status_t CreateCodec2BlockPool(
116        C2PlatformAllocatorStore::id_t allocatorId,
117        std::shared_ptr<const C2Component> component,
118        std::shared_ptr<C2BlockPool> *pool);
119
120/**
121 * Returns the platform component store.
122 * \retval nullptr if the platform component store could not be obtained
123 */
124std::shared_ptr<C2ComponentStore> GetCodec2PlatformComponentStore();
125
126/**
127 * Sets the preferred component store in this process for the sole purpose of accessing its
128 * interface. If this is not called, the default IComponentStore HAL (if exists) is the preferred
129 * store for this purpose. If the default IComponentStore HAL is not present, the platform
130 * component store is used.
131 */
132void SetPreferredCodec2ComponentStore(std::shared_ptr<C2ComponentStore> store);
133
134} // namespace android
135
136#endif // STAGEFRIGHT_CODEC2_PLATFORM_SUPPORT_H_
137