blob: 8c5e909999f4d2aba279622dcb6256c993fc7932 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright 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 CODEC2_BUFFER_H_
18
19#define CODEC2_BUFFER_H_
20
21#include <C2Buffer.h>
Wonsik Kima79c5522022-01-18 16:29:24 -080022#include <C2Config.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080023
Pawin Vongmasa36653902018-11-15 00:10:25 -080024#include <binder/IMemory.h>
25#include <media/hardware/VideoAPI.h>
Wonsik Kimc48ddcf2019-02-11 16:16:57 -080026#include <media/stagefright/foundation/ABuffer.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080027#include <media/MediaCodecBuffer.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080028
29namespace android {
30
Wonsik Kim41d83432020-04-27 16:40:49 -070031namespace hardware {
32class HidlMemory;
33namespace cas {
34namespace native {
35namespace V1_0 {
36struct SharedBuffer;
37} // namespace V1_0
38} // namespace native
39} // namespace cas
40namespace drm {
41namespace V1_0 {
42struct SharedBuffer;
43} // namespace V1_0
44} // namespace drm
45} // namespace hardware
46
Pawin Vongmasa36653902018-11-15 00:10:25 -080047class Codec2Buffer : public MediaCodecBuffer {
48public:
49 using MediaCodecBuffer::MediaCodecBuffer;
50 ~Codec2Buffer() override = default;
51
Wonsik Kimc48ddcf2019-02-11 16:16:57 -080052 sp<ABuffer> getImageData() const { return mImageData; }
53
Wonsik Kimf9b32122020-04-02 11:30:17 -070054 virtual void clearC2BufferRefs() {}
55
Pawin Vongmasa36653902018-11-15 00:10:25 -080056protected:
57 /**
58 * canCopy() implementation for linear buffers.
59 */
60 bool canCopyLinear(const std::shared_ptr<C2Buffer> &buffer) const;
61
62 /**
63 * copy() implementation for linear buffers.
64 */
65 bool copyLinear(const std::shared_ptr<C2Buffer> &buffer);
66
67 /**
68 * sets MediaImage data for flexible graphic buffers
69 */
70 void setImageData(const sp<ABuffer> &imageData);
Wonsik Kimc48ddcf2019-02-11 16:16:57 -080071
72 sp<ABuffer> mImageData;
Pawin Vongmasa36653902018-11-15 00:10:25 -080073};
74
75/**
76 * MediaCodecBuffer implementation on top of local linear buffer. This cannot
77 * cross process boundary so asC2Buffer() returns only nullptr.
78 */
79class LocalLinearBuffer : public Codec2Buffer {
80public:
81 using Codec2Buffer::Codec2Buffer;
82
83 std::shared_ptr<C2Buffer> asC2Buffer() override { return nullptr; }
84 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
85 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
86};
87
88/**
89 * MediaCodecBuffer implementation to be used only as a dummy wrapper around a
90 * C2Buffer object.
91 */
92class DummyContainerBuffer : public Codec2Buffer {
93public:
94 DummyContainerBuffer(
95 const sp<AMessage> &format,
96 const std::shared_ptr<C2Buffer> &buffer = nullptr);
97
98 std::shared_ptr<C2Buffer> asC2Buffer() override;
Wonsik Kimf9b32122020-04-02 11:30:17 -070099 void clearC2BufferRefs() override;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800100 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
101 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
102
103private:
104 std::shared_ptr<C2Buffer> mBufferRef;
105};
106
107/**
108 * MediaCodecBuffer implementation wraps around C2LinearBlock.
109 */
110class LinearBlockBuffer : public Codec2Buffer {
111public:
112 /**
113 * Allocate a new LinearBufferBlock wrapping around C2LinearBlock object.
114 *
115 * \param format mandatory buffer format for MediaCodecBuffer
116 * \param block C2LinearBlock object to wrap around.
117 * \return LinearBlockBuffer object with writable mapping.
118 * nullptr if unsuccessful.
119 */
120 static sp<LinearBlockBuffer> Allocate(
121 const sp<AMessage> &format, const std::shared_ptr<C2LinearBlock> &block);
122
123 virtual ~LinearBlockBuffer() = default;
124
125 std::shared_ptr<C2Buffer> asC2Buffer() override;
126 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
127 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
128
129private:
130 LinearBlockBuffer(
131 const sp<AMessage> &format,
132 C2WriteView &&writeView,
133 const std::shared_ptr<C2LinearBlock> &block);
134 LinearBlockBuffer() = delete;
135
136 C2WriteView mWriteView;
137 std::shared_ptr<C2LinearBlock> mBlock;
138};
139
140/**
141 * MediaCodecBuffer implementation wraps around C2ConstLinearBlock.
142 */
143class ConstLinearBlockBuffer : public Codec2Buffer {
144public:
145 /**
146 * Allocate a new ConstLinearBlockBuffer wrapping around C2Buffer object.
147 *
148 * \param format mandatory buffer format for MediaCodecBuffer
149 * \param buffer linear C2Buffer object to wrap around.
150 * \return ConstLinearBlockBuffer object with readable mapping.
151 * nullptr if unsuccessful.
152 */
153 static sp<ConstLinearBlockBuffer> Allocate(
154 const sp<AMessage> &format, const std::shared_ptr<C2Buffer> &buffer);
155
156 virtual ~ConstLinearBlockBuffer() = default;
157
158 std::shared_ptr<C2Buffer> asC2Buffer() override;
Wonsik Kimf9b32122020-04-02 11:30:17 -0700159 void clearC2BufferRefs() override;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800160
161private:
162 ConstLinearBlockBuffer(
163 const sp<AMessage> &format,
164 C2ReadView &&readView,
165 const std::shared_ptr<C2Buffer> &buffer);
166 ConstLinearBlockBuffer() = delete;
167
168 C2ReadView mReadView;
169 std::shared_ptr<C2Buffer> mBufferRef;
170};
171
172/**
173 * MediaCodecBuffer implementation wraps around C2GraphicBlock.
174 *
175 * This object exposes the underlying bits via accessor APIs and "image-data"
176 * metadata, created automatically at allocation time.
177 */
178class GraphicBlockBuffer : public Codec2Buffer {
179public:
180 /**
181 * Allocate a new GraphicBlockBuffer wrapping around C2GraphicBlock object.
182 * If |block| is not in good color formats, it allocates YV12 local buffer
183 * and copies the content over at asC2Buffer().
184 *
185 * \param format mandatory buffer format for MediaCodecBuffer
186 * \param block C2GraphicBlock object to wrap around.
187 * \param alloc a function to allocate backing ABuffer if needed.
188 * \return GraphicBlockBuffer object with writable mapping.
189 * nullptr if unsuccessful.
190 */
191 static sp<GraphicBlockBuffer> Allocate(
192 const sp<AMessage> &format,
193 const std::shared_ptr<C2GraphicBlock> &block,
194 std::function<sp<ABuffer>(size_t)> alloc);
195
Pawin Vongmasa36653902018-11-15 00:10:25 -0800196 virtual ~GraphicBlockBuffer() = default;
197
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700198 std::shared_ptr<C2Buffer> asC2Buffer() override;
199
Pawin Vongmasa36653902018-11-15 00:10:25 -0800200private:
201 GraphicBlockBuffer(
202 const sp<AMessage> &format,
203 const sp<ABuffer> &buffer,
204 C2GraphicView &&view,
205 const std::shared_ptr<C2GraphicBlock> &block,
206 const sp<ABuffer> &imageData,
207 bool wrapped);
208 GraphicBlockBuffer() = delete;
209
210 inline MediaImage2 *imageData() { return (MediaImage2 *)mImageData->data(); }
211
212 C2GraphicView mView;
213 std::shared_ptr<C2GraphicBlock> mBlock;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800214 const bool mWrapped;
215};
216
217/**
218 * MediaCodecBuffer implementation wraps around VideoNativeMetadata.
219 */
220class GraphicMetadataBuffer : public Codec2Buffer {
221public:
222 /**
223 * Construct a new GraphicMetadataBuffer with local linear buffer for
224 * VideoNativeMetadata.
225 *
226 * \param format mandatory buffer format for MediaCodecBuffer
227 */
228 GraphicMetadataBuffer(
229 const sp<AMessage> &format, const std::shared_ptr<C2Allocator> &alloc);
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700230 virtual ~GraphicMetadataBuffer() = default;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800231
232 std::shared_ptr<C2Buffer> asC2Buffer() override;
233
Pawin Vongmasa36653902018-11-15 00:10:25 -0800234private:
235 GraphicMetadataBuffer() = delete;
236
237 std::shared_ptr<C2Allocator> mAlloc;
238};
239
240/**
241 * MediaCodecBuffer implementation wraps around graphic C2Buffer object.
242 *
243 * This object exposes the underlying bits via accessor APIs and "image-data"
244 * metadata, created automatically at allocation time.
245 */
246class ConstGraphicBlockBuffer : public Codec2Buffer {
247public:
248 /**
249 * Allocate a new ConstGraphicBlockBuffer wrapping around C2Buffer object.
250 * If |buffer| is not in good color formats, it allocates YV12 local buffer
251 * and copies the content of |buffer| over to expose.
252 *
253 * \param format mandatory buffer format for MediaCodecBuffer
254 * \param buffer graphic C2Buffer object to wrap around.
255 * \param alloc a function to allocate backing ABuffer if needed.
256 * \return ConstGraphicBlockBuffer object with readable mapping.
257 * nullptr if unsuccessful.
258 */
259 static sp<ConstGraphicBlockBuffer> Allocate(
260 const sp<AMessage> &format,
261 const std::shared_ptr<C2Buffer> &buffer,
262 std::function<sp<ABuffer>(size_t)> alloc);
263
264 /**
265 * Allocate a new ConstGraphicBlockBuffer which allocates YV12 local buffer
266 * and copies the content of |buffer| over to expose.
267 *
268 * \param format mandatory buffer format for MediaCodecBuffer
269 * \param alloc a function to allocate backing ABuffer if needed.
270 * \return ConstGraphicBlockBuffer object with no wrapping buffer.
271 */
272 static sp<ConstGraphicBlockBuffer> AllocateEmpty(
273 const sp<AMessage> &format,
274 std::function<sp<ABuffer>(size_t)> alloc);
275
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700276 virtual ~ConstGraphicBlockBuffer() = default;
277
Pawin Vongmasa36653902018-11-15 00:10:25 -0800278 std::shared_ptr<C2Buffer> asC2Buffer() override;
Wonsik Kimf9b32122020-04-02 11:30:17 -0700279 void clearC2BufferRefs() override;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800280 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
281 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
282
Pawin Vongmasa36653902018-11-15 00:10:25 -0800283private:
284 ConstGraphicBlockBuffer(
285 const sp<AMessage> &format,
286 const sp<ABuffer> &aBuffer,
287 std::unique_ptr<const C2GraphicView> &&view,
288 const std::shared_ptr<C2Buffer> &buffer,
289 const sp<ABuffer> &imageData,
290 bool wrapped);
291 ConstGraphicBlockBuffer() = delete;
292
293 sp<ABuffer> mImageData;
294 std::unique_ptr<const C2GraphicView> mView;
295 std::shared_ptr<C2Buffer> mBufferRef;
296 const bool mWrapped;
297};
298
299/**
300 * MediaCodecBuffer implementation wraps around C2LinearBlock for component
301 * and IMemory for client. Underlying C2LinearBlock won't be mapped for secure
302 * usecases..
303 */
304class EncryptedLinearBlockBuffer : public Codec2Buffer {
305public:
306 /**
307 * Construct a new EncryptedLinearBufferBlock wrapping around C2LinearBlock
308 * object and writable IMemory region.
309 *
310 * \param format mandatory buffer format for MediaCodecBuffer
311 * \param block C2LinearBlock object to wrap around.
312 * \param memory IMemory object to store encrypted content.
313 * \param heapSeqNum Heap sequence number from ICrypto; -1 if N/A
314 */
315 EncryptedLinearBlockBuffer(
316 const sp<AMessage> &format,
317 const std::shared_ptr<C2LinearBlock> &block,
318 const sp<IMemory> &memory,
319 int32_t heapSeqNum = -1);
320 EncryptedLinearBlockBuffer() = delete;
321
322 virtual ~EncryptedLinearBlockBuffer() = default;
323
324 std::shared_ptr<C2Buffer> asC2Buffer() override;
325
326 /**
327 * Fill the source buffer structure with appropriate value based on
328 * internal IMemory object.
329 *
330 * \param source source buffer structure to fill.
331 */
Robert Shih895fba92019-07-16 16:29:44 -0700332 void fillSourceBuffer(
333 hardware::drm::V1_0::SharedBuffer *source);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800334 void fillSourceBuffer(
335 hardware::cas::native::V1_0::SharedBuffer *source);
336
337 /**
338 * Copy the content of |decrypted| into C2LinearBlock inside. This shall
339 * only be called in non-secure usecases.
340 *
341 * \param decrypted decrypted content to copy from.
342 * \param length length of the content
343 * \return true if successful
344 * false otherwise.
345 */
346 bool copyDecryptedContent(const sp<IMemory> &decrypted, size_t length);
347
348 /**
349 * Copy the content of internal IMemory object into C2LinearBlock inside.
350 * This shall only be called in non-secure usecases.
351 *
352 * \param length length of the content
353 * \return true if successful
354 * false otherwise.
355 */
356 bool copyDecryptedContentFromMemory(size_t length);
357
358 /**
359 * Return native handle of secure buffer understood by ICrypto.
360 *
361 * \return secure buffer handle
362 */
363 native_handle_t *handle() const;
364
Arun Johnson564f3a92024-02-01 19:09:45 +0000365 class MappedBlock {
366 public:
367 explicit MappedBlock(const std::shared_ptr<C2LinearBlock> &block);
368 virtual ~MappedBlock();
369 bool copyDecryptedContent(const sp<IMemory> &decrypted, size_t length);
370 private:
371 C2WriteView mView;
372 };
373
374 void getMappedBlock(std::unique_ptr<MappedBlock> * const mappedBlock) const;
375
Pawin Vongmasa36653902018-11-15 00:10:25 -0800376private:
377
378 std::shared_ptr<C2LinearBlock> mBlock;
379 sp<IMemory> mMemory;
380 sp<hardware::HidlMemory> mHidlMemory;
381 int32_t mHeapSeqNum;
382};
383
Wonsik Kima79c5522022-01-18 16:29:24 -0800384/**
385 * Get HDR metadata from Gralloc4 handle.
386 *
387 * \param[in] handle handle of the allocation
388 * \param[out] staticInfo HDR static info to be filled. Ignored if null;
389 * if |handle| is invalid or does not contain the metadata,
390 * the shared_ptr is reset.
391 * \param[out] dynamicInfo HDR dynamic info to be filled. Ignored if null;
392 * if |handle| is invalid or does not contain the metadata,
393 * the shared_ptr is reset.
394 * \return C2_OK if successful
395 */
396c2_status_t GetHdrMetadataFromGralloc4Handle(
397 const C2Handle *const handle,
398 std::shared_ptr<C2StreamHdrStaticMetadataInfo::input> *staticInfo,
399 std::shared_ptr<C2StreamHdrDynamicMetadataInfo::input> *dynamicInfo);
400
401/**
Hongguangfc1478a2022-07-20 22:56:06 -0700402 * Set metadata to Gralloc4 handle.
Wonsik Kima79c5522022-01-18 16:29:24 -0800403 *
Hongguangfc1478a2022-07-20 22:56:06 -0700404 * \param[in] dataSpace Dataspace to set.
Wonsik Kima79c5522022-01-18 16:29:24 -0800405 * \param[in] staticInfo HDR static info to set. Ignored if null or invalid.
406 * \param[in] dynamicInfo HDR dynamic info to set. Ignored if null or invalid.
407 * \param[out] handle handle of the allocation.
408 * \return C2_OK if successful
409 */
Hongguangfc1478a2022-07-20 22:56:06 -0700410c2_status_t SetMetadataToGralloc4Handle(
411 const android_dataspace_t dataSpace,
Wonsik Kima79c5522022-01-18 16:29:24 -0800412 const std::shared_ptr<const C2StreamHdrStaticMetadataInfo::output> &staticInfo,
413 const std::shared_ptr<const C2StreamHdrDynamicMetadataInfo::output> &dynamicInfo,
414 const C2Handle *const handle);
415
Pawin Vongmasa36653902018-11-15 00:10:25 -0800416} // namespace android
417
418#endif // CODEC2_BUFFER_H_