Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018, 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "Codec2Buffer" |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_VIDEO |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 20 | #include <utils/Log.h> |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 21 | #include <utils/Trace.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 22 | |
Wonsik Kim | 1c86727 | 2024-09-04 20:47:54 +0000 | [diff] [blame] | 23 | #include <android_media_codec.h> |
| 24 | |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 25 | #include <aidl/android/hardware/graphics/common/Cta861_3.h> |
| 26 | #include <aidl/android/hardware/graphics/common/Smpte2086.h> |
Wonsik Kim | 8981431 | 2023-02-01 10:46:16 -0800 | [diff] [blame] | 27 | #include <android-base/no_destructor.h> |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 28 | #include <android-base/properties.h> |
Wonsik Kim | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 29 | #include <android/hardware/cas/native/1.0/types.h> |
| 30 | #include <android/hardware/drm/1.0/types.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 31 | #include <hidlmemory/FrameworkUtils.h> |
| 32 | #include <media/hardware/HardwareAPI.h> |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 33 | #include <media/stagefright/CodecBase.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 34 | #include <media/stagefright/MediaCodecConstants.h> |
| 35 | #include <media/stagefright/foundation/ABuffer.h> |
| 36 | #include <media/stagefright/foundation/AMessage.h> |
| 37 | #include <media/stagefright/foundation/AUtils.h> |
Wonsik Kim | 1c86727 | 2024-09-04 20:47:54 +0000 | [diff] [blame] | 38 | #include <media/stagefright/foundation/ColorUtils.h> |
Wonsik Kim | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 39 | #include <mediadrm/ICrypto.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 40 | #include <nativebase/nativebase.h> |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 41 | #include <ui/GraphicBufferMapper.h> |
Wonsik Kim | ebe0f9e | 2019-07-03 11:06:51 -0700 | [diff] [blame] | 42 | #include <ui/Fence.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 43 | |
| 44 | #include <C2AllocatorGralloc.h> |
| 45 | #include <C2BlockInternal.h> |
| 46 | #include <C2Debug.h> |
| 47 | |
| 48 | #include "Codec2Buffer.h" |
Wonsik Kim | 3ce32b1 | 2024-08-14 00:17:48 +0000 | [diff] [blame] | 49 | #include "Codec2BufferUtils.h" |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 50 | |
| 51 | namespace android { |
| 52 | |
| 53 | // Codec2Buffer |
| 54 | |
| 55 | bool Codec2Buffer::canCopyLinear(const std::shared_ptr<C2Buffer> &buffer) const { |
| 56 | if (const_cast<Codec2Buffer *>(this)->base() == nullptr) { |
| 57 | return false; |
| 58 | } |
| 59 | if (!buffer) { |
| 60 | // Nothing to copy, so we can copy by doing nothing. |
| 61 | return true; |
| 62 | } |
| 63 | if (buffer->data().type() != C2BufferData::LINEAR) { |
| 64 | return false; |
| 65 | } |
| 66 | if (buffer->data().linearBlocks().size() == 0u) { |
| 67 | // Nothing to copy, so we can copy by doing nothing. |
| 68 | return true; |
| 69 | } else if (buffer->data().linearBlocks().size() > 1u) { |
| 70 | // We don't know how to copy more than one blocks. |
| 71 | return false; |
| 72 | } |
| 73 | if (buffer->data().linearBlocks()[0].size() > capacity()) { |
| 74 | // It won't fit. |
| 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool Codec2Buffer::copyLinear(const std::shared_ptr<C2Buffer> &buffer) { |
| 81 | // We assume that all canCopyLinear() checks passed. |
| 82 | if (!buffer || buffer->data().linearBlocks().size() == 0u |
| 83 | || buffer->data().linearBlocks()[0].size() == 0u) { |
| 84 | setRange(0, 0); |
| 85 | return true; |
| 86 | } |
| 87 | C2ReadView view = buffer->data().linearBlocks()[0].map().get(); |
| 88 | if (view.error() != C2_OK) { |
| 89 | ALOGD("Error while mapping: %d", view.error()); |
| 90 | return false; |
| 91 | } |
| 92 | if (view.capacity() > capacity()) { |
| 93 | ALOGD("C2ConstLinearBlock lied --- it actually doesn't fit: view(%u) > this(%zu)", |
| 94 | view.capacity(), capacity()); |
| 95 | return false; |
| 96 | } |
| 97 | memcpy(base(), view.data(), view.capacity()); |
| 98 | setRange(0, view.capacity()); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | void Codec2Buffer::setImageData(const sp<ABuffer> &imageData) { |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 103 | mImageData = imageData; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // LocalLinearBuffer |
| 107 | |
| 108 | bool LocalLinearBuffer::canCopy(const std::shared_ptr<C2Buffer> &buffer) const { |
| 109 | return canCopyLinear(buffer); |
| 110 | } |
| 111 | |
| 112 | bool LocalLinearBuffer::copy(const std::shared_ptr<C2Buffer> &buffer) { |
| 113 | return copyLinear(buffer); |
| 114 | } |
| 115 | |
| 116 | // DummyContainerBuffer |
| 117 | |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 118 | static uint8_t sDummyByte[1] = { 0 }; |
| 119 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 120 | DummyContainerBuffer::DummyContainerBuffer( |
| 121 | const sp<AMessage> &format, const std::shared_ptr<C2Buffer> &buffer) |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 122 | : Codec2Buffer(format, new ABuffer(sDummyByte, 1)), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 123 | mBufferRef(buffer) { |
| 124 | setRange(0, buffer ? 1 : 0); |
| 125 | } |
| 126 | |
| 127 | std::shared_ptr<C2Buffer> DummyContainerBuffer::asC2Buffer() { |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 128 | return mBufferRef; |
| 129 | } |
| 130 | |
| 131 | void DummyContainerBuffer::clearC2BufferRefs() { |
| 132 | mBufferRef.reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | bool DummyContainerBuffer::canCopy(const std::shared_ptr<C2Buffer> &) const { |
| 136 | return !mBufferRef; |
| 137 | } |
| 138 | |
| 139 | bool DummyContainerBuffer::copy(const std::shared_ptr<C2Buffer> &buffer) { |
| 140 | mBufferRef = buffer; |
| 141 | setRange(0, mBufferRef ? 1 : 0); |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | // LinearBlockBuffer |
| 146 | |
| 147 | // static |
| 148 | sp<LinearBlockBuffer> LinearBlockBuffer::Allocate( |
| 149 | const sp<AMessage> &format, const std::shared_ptr<C2LinearBlock> &block) { |
| 150 | C2WriteView writeView(block->map().get()); |
| 151 | if (writeView.error() != C2_OK) { |
| 152 | return nullptr; |
| 153 | } |
| 154 | return new LinearBlockBuffer(format, std::move(writeView), block); |
| 155 | } |
| 156 | |
| 157 | std::shared_ptr<C2Buffer> LinearBlockBuffer::asC2Buffer() { |
| 158 | return C2Buffer::CreateLinearBuffer(mBlock->share(offset(), size(), C2Fence())); |
| 159 | } |
| 160 | |
| 161 | bool LinearBlockBuffer::canCopy(const std::shared_ptr<C2Buffer> &buffer) const { |
| 162 | return canCopyLinear(buffer); |
| 163 | } |
| 164 | |
| 165 | bool LinearBlockBuffer::copy(const std::shared_ptr<C2Buffer> &buffer) { |
| 166 | return copyLinear(buffer); |
| 167 | } |
| 168 | |
| 169 | LinearBlockBuffer::LinearBlockBuffer( |
| 170 | const sp<AMessage> &format, |
| 171 | C2WriteView&& writeView, |
| 172 | const std::shared_ptr<C2LinearBlock> &block) |
| 173 | : Codec2Buffer(format, new ABuffer(writeView.data(), writeView.size())), |
| 174 | mWriteView(writeView), |
| 175 | mBlock(block) { |
| 176 | } |
| 177 | |
| 178 | // ConstLinearBlockBuffer |
| 179 | |
| 180 | // static |
| 181 | sp<ConstLinearBlockBuffer> ConstLinearBlockBuffer::Allocate( |
| 182 | const sp<AMessage> &format, const std::shared_ptr<C2Buffer> &buffer) { |
| 183 | if (!buffer |
| 184 | || buffer->data().type() != C2BufferData::LINEAR |
| 185 | || buffer->data().linearBlocks().size() != 1u) { |
Wonsik Kim | 68af8e5 | 2024-04-11 17:57:57 +0000 | [diff] [blame] | 186 | if (!buffer) { |
| 187 | ALOGD("ConstLinearBlockBuffer::Allocate: null buffer"); |
| 188 | } else { |
| 189 | ALOGW("ConstLinearBlockBuffer::Allocate: type=%d # linear blocks=%zu", |
| 190 | buffer->data().type(), buffer->data().linearBlocks().size()); |
| 191 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 192 | return nullptr; |
| 193 | } |
| 194 | C2ReadView readView(buffer->data().linearBlocks()[0].map().get()); |
| 195 | if (readView.error() != C2_OK) { |
Wonsik Kim | 68af8e5 | 2024-04-11 17:57:57 +0000 | [diff] [blame] | 196 | ALOGW("ConstLinearBlockBuffer::Allocate: readView.error()=%d", readView.error()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 197 | return nullptr; |
| 198 | } |
| 199 | return new ConstLinearBlockBuffer(format, std::move(readView), buffer); |
| 200 | } |
| 201 | |
| 202 | ConstLinearBlockBuffer::ConstLinearBlockBuffer( |
| 203 | const sp<AMessage> &format, |
| 204 | C2ReadView&& readView, |
| 205 | const std::shared_ptr<C2Buffer> &buffer) |
| 206 | : Codec2Buffer(format, new ABuffer( |
| 207 | // NOTE: ABuffer only takes non-const pointer but this data is |
| 208 | // supposed to be read-only. |
| 209 | const_cast<uint8_t *>(readView.data()), readView.capacity())), |
| 210 | mReadView(readView), |
| 211 | mBufferRef(buffer) { |
| 212 | } |
| 213 | |
| 214 | std::shared_ptr<C2Buffer> ConstLinearBlockBuffer::asC2Buffer() { |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 215 | return mBufferRef; |
| 216 | } |
| 217 | |
| 218 | void ConstLinearBlockBuffer::clearC2BufferRefs() { |
| 219 | mBufferRef.reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 222 | // GraphicBlockBuffer |
| 223 | |
| 224 | // static |
| 225 | sp<GraphicBlockBuffer> GraphicBlockBuffer::Allocate( |
| 226 | const sp<AMessage> &format, |
| 227 | const std::shared_ptr<C2GraphicBlock> &block, |
| 228 | std::function<sp<ABuffer>(size_t)> alloc) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 229 | ATRACE_BEGIN("GraphicBlockBuffer::Allocate block->map()"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 230 | C2GraphicView view(block->map().get()); |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 231 | ATRACE_END(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 232 | if (view.error() != C2_OK) { |
| 233 | ALOGD("C2GraphicBlock::map failed: %d", view.error()); |
| 234 | return nullptr; |
| 235 | } |
| 236 | |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 237 | GraphicView2MediaImageConverter converter(view, format, false /* copy */); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 238 | if (converter.initCheck() != OK) { |
| 239 | ALOGD("Converter init failed: %d", converter.initCheck()); |
| 240 | return nullptr; |
| 241 | } |
| 242 | bool wrapped = true; |
| 243 | sp<ABuffer> buffer = converter.wrap(); |
| 244 | if (buffer == nullptr) { |
| 245 | buffer = alloc(converter.backBufferSize()); |
| 246 | if (!converter.setBackBuffer(buffer)) { |
| 247 | ALOGD("Converter failed to set back buffer"); |
| 248 | return nullptr; |
| 249 | } |
| 250 | wrapped = false; |
| 251 | } |
| 252 | return new GraphicBlockBuffer( |
| 253 | format, |
| 254 | buffer, |
| 255 | std::move(view), |
| 256 | block, |
| 257 | converter.imageData(), |
| 258 | wrapped); |
| 259 | } |
| 260 | |
| 261 | GraphicBlockBuffer::GraphicBlockBuffer( |
| 262 | const sp<AMessage> &format, |
| 263 | const sp<ABuffer> &buffer, |
| 264 | C2GraphicView &&view, |
| 265 | const std::shared_ptr<C2GraphicBlock> &block, |
| 266 | const sp<ABuffer> &imageData, |
| 267 | bool wrapped) |
| 268 | : Codec2Buffer(format, buffer), |
| 269 | mView(view), |
| 270 | mBlock(block), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 271 | mWrapped(wrapped) { |
| 272 | setImageData(imageData); |
| 273 | } |
| 274 | |
| 275 | std::shared_ptr<C2Buffer> GraphicBlockBuffer::asC2Buffer() { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 276 | ATRACE_CALL(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 277 | uint32_t width = mView.width(); |
| 278 | uint32_t height = mView.height(); |
| 279 | if (!mWrapped) { |
| 280 | (void)ImageCopy(mView, base(), imageData()); |
| 281 | } |
| 282 | return C2Buffer::CreateGraphicBuffer( |
| 283 | mBlock->share(C2Rect(width, height), C2Fence())); |
| 284 | } |
| 285 | |
| 286 | // GraphicMetadataBuffer |
| 287 | GraphicMetadataBuffer::GraphicMetadataBuffer( |
| 288 | const sp<AMessage> &format, |
| 289 | const std::shared_ptr<C2Allocator> &alloc) |
| 290 | : Codec2Buffer(format, new ABuffer(sizeof(VideoNativeMetadata))), |
| 291 | mAlloc(alloc) { |
| 292 | ((VideoNativeMetadata *)base())->pBuffer = nullptr; |
| 293 | } |
| 294 | |
| 295 | std::shared_ptr<C2Buffer> GraphicMetadataBuffer::asC2Buffer() { |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 296 | #ifdef __LP64__ |
| 297 | static std::once_flag s_checkOnce; |
Harish Mahendrakar | 731e914 | 2021-04-21 17:20:39 -0700 | [diff] [blame] | 298 | static bool s_is64bitOk {true}; |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 299 | std::call_once(s_checkOnce, [&](){ |
| 300 | const std::string abi32list = |
| 301 | ::android::base::GetProperty("ro.product.cpu.abilist32", ""); |
Harish Mahendrakar | 731e914 | 2021-04-21 17:20:39 -0700 | [diff] [blame] | 302 | if (!abi32list.empty()) { |
| 303 | int32_t inputSurfaceSetting = |
| 304 | ::android::base::GetIntProperty("debug.stagefright.c2inputsurface", int32_t(0)); |
| 305 | s_is64bitOk = inputSurfaceSetting != 0; |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 306 | } |
| 307 | }); |
| 308 | |
Harish Mahendrakar | 731e914 | 2021-04-21 17:20:39 -0700 | [diff] [blame] | 309 | if (!s_is64bitOk) { |
| 310 | ALOGE("GraphicMetadataBuffer does not work in 32+64 system if compiled as 64-bit object"\ |
| 311 | "when debug.stagefright.c2inputsurface is set to 0"); |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 312 | return nullptr; |
| 313 | } |
| 314 | #endif |
| 315 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 316 | VideoNativeMetadata *meta = (VideoNativeMetadata *)base(); |
| 317 | ANativeWindowBuffer *buffer = (ANativeWindowBuffer *)meta->pBuffer; |
| 318 | if (buffer == nullptr) { |
| 319 | ALOGD("VideoNativeMetadata contains null buffer"); |
| 320 | return nullptr; |
| 321 | } |
| 322 | |
| 323 | ALOGV("VideoNativeMetadata: %dx%d", buffer->width, buffer->height); |
| 324 | C2Handle *handle = WrapNativeCodec2GrallocHandle( |
Sungtak Lee | a4d13be | 2019-01-23 15:24:46 -0800 | [diff] [blame] | 325 | buffer->handle, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 326 | buffer->width, |
| 327 | buffer->height, |
| 328 | buffer->format, |
| 329 | buffer->usage, |
| 330 | buffer->stride); |
| 331 | std::shared_ptr<C2GraphicAllocation> alloc; |
| 332 | c2_status_t err = mAlloc->priorGraphicAllocation(handle, &alloc); |
| 333 | if (err != C2_OK) { |
| 334 | ALOGD("Failed to wrap VideoNativeMetadata into C2GraphicAllocation"); |
Chih-Yu Huang | c0ac355 | 2021-03-11 14:37:10 +0900 | [diff] [blame] | 335 | native_handle_close(handle); |
| 336 | native_handle_delete(handle); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 337 | return nullptr; |
| 338 | } |
| 339 | std::shared_ptr<C2GraphicBlock> block = _C2BlockFactory::CreateGraphicBlock(alloc); |
| 340 | |
| 341 | meta->pBuffer = 0; |
Wonsik Kim | ebe0f9e | 2019-07-03 11:06:51 -0700 | [diff] [blame] | 342 | // TODO: wrap this in C2Fence so that the component can wait when it |
| 343 | // actually starts processing. |
| 344 | if (meta->nFenceFd >= 0) { |
| 345 | sp<Fence> fence(new Fence(meta->nFenceFd)); |
| 346 | fence->waitForever(LOG_TAG); |
| 347 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 348 | return C2Buffer::CreateGraphicBuffer( |
| 349 | block->share(C2Rect(buffer->width, buffer->height), C2Fence())); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | // ConstGraphicBlockBuffer |
| 353 | |
| 354 | // static |
| 355 | sp<ConstGraphicBlockBuffer> ConstGraphicBlockBuffer::Allocate( |
| 356 | const sp<AMessage> &format, |
| 357 | const std::shared_ptr<C2Buffer> &buffer, |
| 358 | std::function<sp<ABuffer>(size_t)> alloc) { |
| 359 | if (!buffer |
| 360 | || buffer->data().type() != C2BufferData::GRAPHIC |
| 361 | || buffer->data().graphicBlocks().size() != 1u) { |
| 362 | ALOGD("C2Buffer precond fail"); |
| 363 | return nullptr; |
| 364 | } |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 365 | ATRACE_BEGIN("ConstGraphicBlockBuffer::Allocate block->map()"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 366 | std::unique_ptr<const C2GraphicView> view(std::make_unique<const C2GraphicView>( |
| 367 | buffer->data().graphicBlocks()[0].map().get())); |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 368 | ATRACE_END(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 369 | std::unique_ptr<const C2GraphicView> holder; |
| 370 | |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 371 | GraphicView2MediaImageConverter converter(*view, format, false /* copy */); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 372 | if (converter.initCheck() != OK) { |
| 373 | ALOGD("Converter init failed: %d", converter.initCheck()); |
| 374 | return nullptr; |
| 375 | } |
| 376 | bool wrapped = true; |
| 377 | sp<ABuffer> aBuffer = converter.wrap(); |
| 378 | if (aBuffer == nullptr) { |
| 379 | aBuffer = alloc(converter.backBufferSize()); |
| 380 | if (!converter.setBackBuffer(aBuffer)) { |
| 381 | ALOGD("Converter failed to set back buffer"); |
| 382 | return nullptr; |
| 383 | } |
| 384 | wrapped = false; |
| 385 | converter.copyToMediaImage(); |
| 386 | // We don't need the view. |
| 387 | holder = std::move(view); |
| 388 | } |
| 389 | return new ConstGraphicBlockBuffer( |
| 390 | format, |
| 391 | aBuffer, |
| 392 | std::move(view), |
| 393 | buffer, |
| 394 | converter.imageData(), |
| 395 | wrapped); |
| 396 | } |
| 397 | |
| 398 | // static |
| 399 | sp<ConstGraphicBlockBuffer> ConstGraphicBlockBuffer::AllocateEmpty( |
| 400 | const sp<AMessage> &format, |
| 401 | std::function<sp<ABuffer>(size_t)> alloc) { |
| 402 | int32_t width, height; |
| 403 | if (!format->findInt32("width", &width) |
| 404 | || !format->findInt32("height", &height)) { |
| 405 | ALOGD("format had no width / height"); |
| 406 | return nullptr; |
| 407 | } |
Wonsik Kim | 08a8a2b | 2021-05-10 19:03:47 -0700 | [diff] [blame] | 408 | int32_t colorFormat = COLOR_FormatYUV420Flexible; |
| 409 | int32_t bpp = 12; // 8(Y) + 2(U) + 2(V) |
| 410 | if (format->findInt32(KEY_COLOR_FORMAT, &colorFormat)) { |
| 411 | if (colorFormat == COLOR_FormatYUVP010) { |
| 412 | bpp = 24; // 16(Y) + 4(U) + 4(V) |
| 413 | } |
| 414 | } |
| 415 | sp<ABuffer> aBuffer(alloc(align(width, 16) * align(height, 16) * bpp / 8)); |
Jiajia Cong | 9ed0ab2 | 2020-12-02 12:00:49 +0800 | [diff] [blame] | 416 | if (aBuffer == nullptr) { |
| 417 | ALOGD("%s: failed to allocate buffer", __func__); |
| 418 | return nullptr; |
| 419 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 420 | return new ConstGraphicBlockBuffer( |
| 421 | format, |
| 422 | aBuffer, |
| 423 | nullptr, |
| 424 | nullptr, |
| 425 | nullptr, |
| 426 | false); |
| 427 | } |
| 428 | |
| 429 | ConstGraphicBlockBuffer::ConstGraphicBlockBuffer( |
| 430 | const sp<AMessage> &format, |
| 431 | const sp<ABuffer> &aBuffer, |
| 432 | std::unique_ptr<const C2GraphicView> &&view, |
| 433 | const std::shared_ptr<C2Buffer> &buffer, |
| 434 | const sp<ABuffer> &imageData, |
| 435 | bool wrapped) |
| 436 | : Codec2Buffer(format, aBuffer), |
| 437 | mView(std::move(view)), |
| 438 | mBufferRef(buffer), |
| 439 | mWrapped(wrapped) { |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 440 | setImageData(imageData); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | std::shared_ptr<C2Buffer> ConstGraphicBlockBuffer::asC2Buffer() { |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 444 | return mBufferRef; |
| 445 | } |
| 446 | |
| 447 | void ConstGraphicBlockBuffer::clearC2BufferRefs() { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 448 | mView.reset(); |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 449 | mBufferRef.reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | bool ConstGraphicBlockBuffer::canCopy(const std::shared_ptr<C2Buffer> &buffer) const { |
| 453 | if (mWrapped || mBufferRef) { |
| 454 | ALOGD("ConstGraphicBlockBuffer::canCopy: %swrapped ; buffer ref %s", |
| 455 | mWrapped ? "" : "not ", mBufferRef ? "exists" : "doesn't exist"); |
| 456 | return false; |
| 457 | } |
| 458 | if (!buffer) { |
| 459 | // Nothing to copy, so we can copy by doing nothing. |
| 460 | return true; |
| 461 | } |
| 462 | if (buffer->data().type() != C2BufferData::GRAPHIC) { |
| 463 | ALOGD("ConstGraphicBlockBuffer::canCopy: buffer precondition unsatisfied"); |
| 464 | return false; |
| 465 | } |
| 466 | if (buffer->data().graphicBlocks().size() == 0) { |
| 467 | return true; |
| 468 | } else if (buffer->data().graphicBlocks().size() != 1u) { |
| 469 | ALOGD("ConstGraphicBlockBuffer::canCopy: too many blocks"); |
| 470 | return false; |
| 471 | } |
| 472 | |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 473 | ATRACE_BEGIN("ConstGraphicBlockBuffer::canCopy block->map()"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 474 | GraphicView2MediaImageConverter converter( |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 475 | buffer->data().graphicBlocks()[0].map().get(), |
| 476 | // FIXME: format() is not const, but we cannot change it, so do a const cast here |
| 477 | const_cast<ConstGraphicBlockBuffer *>(this)->format(), |
| 478 | true /* copy */); |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 479 | ATRACE_END(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 480 | if (converter.initCheck() != OK) { |
| 481 | ALOGD("ConstGraphicBlockBuffer::canCopy: converter init failed: %d", converter.initCheck()); |
| 482 | return false; |
| 483 | } |
| 484 | if (converter.backBufferSize() > capacity()) { |
| 485 | ALOGD("ConstGraphicBlockBuffer::canCopy: insufficient capacity: req %u has %zu", |
| 486 | converter.backBufferSize(), capacity()); |
| 487 | return false; |
| 488 | } |
| 489 | return true; |
| 490 | } |
| 491 | |
| 492 | bool ConstGraphicBlockBuffer::copy(const std::shared_ptr<C2Buffer> &buffer) { |
| 493 | if (!buffer || buffer->data().graphicBlocks().size() == 0) { |
| 494 | setRange(0, 0); |
| 495 | return true; |
| 496 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 497 | |
| 498 | GraphicView2MediaImageConverter converter( |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 499 | buffer->data().graphicBlocks()[0].map().get(), format(), true /* copy */); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 500 | if (converter.initCheck() != OK) { |
| 501 | ALOGD("ConstGraphicBlockBuffer::copy: converter init failed: %d", converter.initCheck()); |
| 502 | return false; |
| 503 | } |
| 504 | sp<ABuffer> aBuffer = new ABuffer(base(), capacity()); |
| 505 | if (!converter.setBackBuffer(aBuffer)) { |
| 506 | ALOGD("ConstGraphicBlockBuffer::copy: set back buffer failed"); |
| 507 | return false; |
| 508 | } |
Pin-chih Lin | 1971e2c | 2019-04-15 19:36:26 +0800 | [diff] [blame] | 509 | setRange(0, aBuffer->size()); // align size info |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 510 | converter.copyToMediaImage(); |
| 511 | setImageData(converter.imageData()); |
| 512 | mBufferRef = buffer; |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | // EncryptedLinearBlockBuffer |
| 517 | |
| 518 | EncryptedLinearBlockBuffer::EncryptedLinearBlockBuffer( |
| 519 | const sp<AMessage> &format, |
| 520 | const std::shared_ptr<C2LinearBlock> &block, |
| 521 | const sp<IMemory> &memory, |
| 522 | int32_t heapSeqNum) |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 523 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 524 | // (see declaration for details). |
| 525 | // Either document why it is safe in this case or address the |
| 526 | // issue (e.g. by copying). |
| 527 | : Codec2Buffer(format, new ABuffer(memory->unsecurePointer(), memory->size())), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 528 | mBlock(block), |
| 529 | mMemory(memory), |
| 530 | mHeapSeqNum(heapSeqNum) { |
| 531 | } |
| 532 | |
| 533 | std::shared_ptr<C2Buffer> EncryptedLinearBlockBuffer::asC2Buffer() { |
| 534 | return C2Buffer::CreateLinearBuffer(mBlock->share(offset(), size(), C2Fence())); |
| 535 | } |
| 536 | |
| 537 | void EncryptedLinearBlockBuffer::fillSourceBuffer( |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 538 | hardware::drm::V1_0::SharedBuffer *source) { |
| 539 | BufferChannelBase::IMemoryToSharedBuffer(mMemory, mHeapSeqNum, source); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void EncryptedLinearBlockBuffer::fillSourceBuffer( |
| 543 | hardware::cas::native::V1_0::SharedBuffer *source) { |
| 544 | ssize_t offset; |
| 545 | size_t size; |
| 546 | |
| 547 | mHidlMemory = hardware::fromHeap(mMemory->getMemory(&offset, &size)); |
| 548 | source->heapBase = *mHidlMemory; |
| 549 | source->offset = offset; |
| 550 | source->size = size; |
| 551 | } |
| 552 | |
| 553 | bool EncryptedLinearBlockBuffer::copyDecryptedContent( |
| 554 | const sp<IMemory> &decrypted, size_t length) { |
| 555 | C2WriteView view = mBlock->map().get(); |
| 556 | if (view.error() != C2_OK) { |
| 557 | return false; |
| 558 | } |
| 559 | if (view.size() < length) { |
| 560 | return false; |
| 561 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 562 | memcpy(view.data(), decrypted->unsecurePointer(), length); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 563 | return true; |
| 564 | } |
| 565 | |
| 566 | bool EncryptedLinearBlockBuffer::copyDecryptedContentFromMemory(size_t length) { |
| 567 | return copyDecryptedContent(mMemory, length); |
| 568 | } |
| 569 | |
| 570 | native_handle_t *EncryptedLinearBlockBuffer::handle() const { |
| 571 | return const_cast<native_handle_t *>(mBlock->handle()); |
| 572 | } |
| 573 | |
Arun Johnson | 564f3a9 | 2024-02-01 19:09:45 +0000 | [diff] [blame] | 574 | void EncryptedLinearBlockBuffer::getMappedBlock( |
| 575 | std::unique_ptr<MappedBlock> * const mappedBlock) const { |
| 576 | if (mappedBlock) { |
| 577 | mappedBlock->reset(new EncryptedLinearBlockBuffer::MappedBlock(mBlock)); |
| 578 | } |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | EncryptedLinearBlockBuffer::MappedBlock::MappedBlock( |
| 583 | const std::shared_ptr<C2LinearBlock> &block) : mView(block->map().get()) { |
| 584 | } |
| 585 | |
| 586 | bool EncryptedLinearBlockBuffer::MappedBlock::copyDecryptedContent( |
| 587 | const sp<IMemory> &decrypted, size_t length) { |
| 588 | if (mView.error() != C2_OK) { |
| 589 | return false; |
| 590 | } |
| 591 | if (mView.size() < length) { |
| 592 | ALOGE("View size(%d) less than decrypted length(%zu)", |
| 593 | mView.size(), length); |
| 594 | return false; |
| 595 | } |
| 596 | memcpy(mView.data(), decrypted->unsecurePointer(), length); |
| 597 | mView.setOffset(mView.offset() + length); |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | EncryptedLinearBlockBuffer::MappedBlock::~MappedBlock() { |
| 602 | mView.setOffset(0); |
| 603 | } |
| 604 | |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 605 | using ::aidl::android::hardware::graphics::common::Cta861_3; |
| 606 | using ::aidl::android::hardware::graphics::common::Smpte2086; |
| 607 | |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 608 | namespace { |
| 609 | |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 610 | class GrallocBuffer { |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 611 | public: |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 612 | GrallocBuffer(const C2Handle *const handle) : mBuffer(nullptr) { |
| 613 | GraphicBufferMapper& mapper = GraphicBufferMapper::get(); |
| 614 | |
Wonsik Kim | e2aa240 | 2022-04-08 14:07:14 -0700 | [diff] [blame] | 615 | // Unwrap raw buffer handle from the C2Handle |
| 616 | native_handle_t *nh = UnwrapNativeCodec2GrallocHandle(handle); |
| 617 | if (!nh) { |
Sungtak Lee | 1620472 | 2024-02-07 22:26:19 +0000 | [diff] [blame] | 618 | ALOGE("handle is not compatible to any gralloc C2Handle types"); |
| 619 | return; |
Wonsik Kim | e2aa240 | 2022-04-08 14:07:14 -0700 | [diff] [blame] | 620 | } |
| 621 | // Import the raw handle so IMapper can use the buffer. The imported |
| 622 | // handle must be freed when the client is done with the buffer. |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 623 | status_t status = mapper.importBufferNoValidate( |
| 624 | nh, |
| 625 | &mBuffer); |
| 626 | |
| 627 | if (status != OK) { |
| 628 | ALOGE("Failed to import buffer. Status: %d.", status); |
| 629 | return; |
| 630 | } |
Wonsik Kim | e2aa240 | 2022-04-08 14:07:14 -0700 | [diff] [blame] | 631 | |
| 632 | // TRICKY: UnwrapNativeCodec2GrallocHandle creates a new handle but |
| 633 | // does not clone the fds. Thus we need to delete the handle |
| 634 | // without closing it. |
| 635 | native_handle_delete(nh); |
| 636 | } |
| 637 | |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 638 | ~GrallocBuffer() { |
| 639 | GraphicBufferMapper& mapper = GraphicBufferMapper::get(); |
| 640 | if (mBuffer) { |
Wonsik Kim | e2aa240 | 2022-04-08 14:07:14 -0700 | [diff] [blame] | 641 | // Free the imported buffer handle. This does not release the |
| 642 | // underlying buffer itself. |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 643 | mapper.freeBuffer(mBuffer); |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 644 | } |
| 645 | } |
Wonsik Kim | e2aa240 | 2022-04-08 14:07:14 -0700 | [diff] [blame] | 646 | |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 647 | buffer_handle_t get() const { return mBuffer; } |
Wonsik Kim | e2aa240 | 2022-04-08 14:07:14 -0700 | [diff] [blame] | 648 | operator bool() const { return (mBuffer != nullptr); } |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 649 | private: |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 650 | buffer_handle_t mBuffer; |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 651 | }; |
| 652 | |
| 653 | } // namspace |
| 654 | |
| 655 | c2_status_t GetHdrMetadataFromGralloc4Handle( |
| 656 | const C2Handle *const handle, |
| 657 | std::shared_ptr<C2StreamHdrStaticMetadataInfo::input> *staticInfo, |
| 658 | std::shared_ptr<C2StreamHdrDynamicMetadataInfo::input> *dynamicInfo) { |
| 659 | c2_status_t err = C2_OK; |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 660 | GraphicBufferMapper& mapper = GraphicBufferMapper::get(); |
| 661 | GrallocBuffer buffer(handle); |
| 662 | if (!buffer) { |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 663 | // Gralloc4 not supported; nothing to do |
| 664 | return err; |
| 665 | } |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 666 | if (staticInfo) { |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 667 | ALOGV("Grabbing static HDR info from gralloc metadata"); |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 668 | staticInfo->reset(new C2StreamHdrStaticMetadataInfo::input(0u)); |
| 669 | memset(&(*staticInfo)->mastering, 0, sizeof((*staticInfo)->mastering)); |
| 670 | (*staticInfo)->maxCll = 0; |
| 671 | (*staticInfo)->maxFall = 0; |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 672 | |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 673 | std::optional<Smpte2086> smpte2086; |
| 674 | status_t status = mapper.getSmpte2086(buffer.get(), &smpte2086); |
Yanqiang Fan | 4f4e19b | 2024-03-07 15:18:36 +0800 | [diff] [blame] | 675 | if (status != OK || !smpte2086) { |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 676 | err = C2_CORRUPTED; |
| 677 | } else { |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 678 | if (smpte2086) { |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 679 | (*staticInfo)->mastering.red.x = smpte2086->primaryRed.x; |
| 680 | (*staticInfo)->mastering.red.y = smpte2086->primaryRed.y; |
| 681 | (*staticInfo)->mastering.green.x = smpte2086->primaryGreen.x; |
| 682 | (*staticInfo)->mastering.green.y = smpte2086->primaryGreen.y; |
| 683 | (*staticInfo)->mastering.blue.x = smpte2086->primaryBlue.x; |
| 684 | (*staticInfo)->mastering.blue.y = smpte2086->primaryBlue.y; |
| 685 | (*staticInfo)->mastering.white.x = smpte2086->whitePoint.x; |
| 686 | (*staticInfo)->mastering.white.y = smpte2086->whitePoint.y; |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 687 | |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 688 | (*staticInfo)->mastering.maxLuminance = smpte2086->maxLuminance; |
| 689 | (*staticInfo)->mastering.minLuminance = smpte2086->minLuminance; |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 690 | } |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 691 | } |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 692 | |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 693 | std::optional<Cta861_3> cta861_3; |
| 694 | status = mapper.getCta861_3(buffer.get(), &cta861_3); |
Yanqiang Fan | 4f4e19b | 2024-03-07 15:18:36 +0800 | [diff] [blame] | 695 | if (status != OK || !cta861_3) { |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 696 | err = C2_CORRUPTED; |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 697 | } else { |
| 698 | if (cta861_3) { |
| 699 | (*staticInfo)->maxCll = cta861_3->maxContentLightLevel; |
| 700 | (*staticInfo)->maxFall = cta861_3->maxFrameAverageLightLevel; |
| 701 | } |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 702 | } |
| 703 | } |
Jongchan Choi | bd2ced7 | 2023-06-26 19:36:11 +0000 | [diff] [blame] | 704 | |
| 705 | if (err != C2_OK) { |
| 706 | staticInfo->reset(); |
| 707 | } |
| 708 | |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 709 | if (dynamicInfo) { |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 710 | ALOGV("Grabbing dynamic HDR info from gralloc metadata"); |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 711 | dynamicInfo->reset(); |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 712 | std::optional<std::vector<uint8_t>> vec; |
| 713 | status_t status = mapper.getSmpte2094_40(buffer.get(), &vec); |
Yanqiang Fan | 4f4e19b | 2024-03-07 15:18:36 +0800 | [diff] [blame] | 714 | if (status != OK || !vec) { |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 715 | dynamicInfo->reset(); |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 716 | err = C2_CORRUPTED; |
| 717 | } else { |
| 718 | if (vec) { |
| 719 | *dynamicInfo = C2StreamHdrDynamicMetadataInfo::input::AllocShared( |
| 720 | vec->size(), 0u, C2Config::HDR_DYNAMIC_METADATA_TYPE_SMPTE_2094_40); |
| 721 | memcpy((*dynamicInfo)->m.data, vec->data(), vec->size()); |
| 722 | } |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | return err; |
| 727 | } |
| 728 | |
Hongguang | fc1478a | 2022-07-20 22:56:06 -0700 | [diff] [blame] | 729 | c2_status_t SetMetadataToGralloc4Handle( |
| 730 | android_dataspace_t dataSpace, |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 731 | const std::shared_ptr<const C2StreamHdrStaticMetadataInfo::output> &staticInfo, |
| 732 | const std::shared_ptr<const C2StreamHdrDynamicMetadataInfo::output> &dynamicInfo, |
| 733 | const C2Handle *const handle) { |
| 734 | c2_status_t err = C2_OK; |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 735 | GraphicBufferMapper& mapper = GraphicBufferMapper::get(); |
| 736 | GrallocBuffer buffer(handle); |
| 737 | if (!buffer) { |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 738 | // Gralloc4 not supported; nothing to do |
| 739 | return err; |
| 740 | } |
Wonsik Kim | 1c86727 | 2024-09-04 20:47:54 +0000 | [diff] [blame] | 741 | // Use V0 dataspaces for Gralloc4+ |
| 742 | if (android::media::codec::provider_->dataspace_v0_partial()) { |
| 743 | ColorUtils::convertDataSpaceToV0(dataSpace); |
| 744 | } |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 745 | status_t status = mapper.setDataspace(buffer.get(), static_cast<ui::Dataspace>(dataSpace)); |
| 746 | if (status != OK) { |
| 747 | err = C2_CORRUPTED; |
Hongguang | fc1478a | 2022-07-20 22:56:06 -0700 | [diff] [blame] | 748 | } |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 749 | if (staticInfo && *staticInfo) { |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 750 | ALOGV("Setting static HDR info as gralloc metadata"); |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 751 | std::optional<Smpte2086> smpte2086 = Smpte2086{ |
| 752 | {staticInfo->mastering.red.x, staticInfo->mastering.red.y}, |
| 753 | {staticInfo->mastering.green.x, staticInfo->mastering.green.y}, |
| 754 | {staticInfo->mastering.blue.x, staticInfo->mastering.blue.y}, |
| 755 | {staticInfo->mastering.white.x, staticInfo->mastering.white.y}, |
| 756 | staticInfo->mastering.maxLuminance, |
| 757 | staticInfo->mastering.minLuminance, |
| 758 | }; |
Wonsik Kim | e2aa240 | 2022-04-08 14:07:14 -0700 | [diff] [blame] | 759 | if (0.0 <= smpte2086->primaryRed.x && smpte2086->primaryRed.x <= 1.0 |
| 760 | && 0.0 <= smpte2086->primaryRed.y && smpte2086->primaryRed.y <= 1.0 |
| 761 | && 0.0 <= smpte2086->primaryGreen.x && smpte2086->primaryGreen.x <= 1.0 |
| 762 | && 0.0 <= smpte2086->primaryGreen.y && smpte2086->primaryGreen.y <= 1.0 |
| 763 | && 0.0 <= smpte2086->primaryBlue.x && smpte2086->primaryBlue.x <= 1.0 |
| 764 | && 0.0 <= smpte2086->primaryBlue.y && smpte2086->primaryBlue.y <= 1.0 |
| 765 | && 0.0 <= smpte2086->whitePoint.x && smpte2086->whitePoint.x <= 1.0 |
| 766 | && 0.0 <= smpte2086->whitePoint.y && smpte2086->whitePoint.y <= 1.0 |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 767 | && 0.0 <= smpte2086->maxLuminance && 0.0 <= smpte2086->minLuminance) { |
| 768 | status = mapper.setSmpte2086(buffer.get(), smpte2086); |
| 769 | if (status != OK) { |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 770 | err = C2_CORRUPTED; |
| 771 | } |
| 772 | } |
| 773 | std::optional<Cta861_3> cta861_3 = Cta861_3{ |
| 774 | staticInfo->maxCll, |
| 775 | staticInfo->maxFall, |
| 776 | }; |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 777 | if (0.0 <= cta861_3->maxContentLightLevel && 0.0 <= cta861_3->maxFrameAverageLightLevel) { |
| 778 | status = mapper.setCta861_3(buffer.get(), cta861_3); |
| 779 | if (status != OK) { |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 780 | err = C2_CORRUPTED; |
| 781 | } |
| 782 | } |
| 783 | } |
Wonsik Kim | e2aa240 | 2022-04-08 14:07:14 -0700 | [diff] [blame] | 784 | if (dynamicInfo && *dynamicInfo && dynamicInfo->flexCount() > 0) { |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 785 | ALOGV("Setting dynamic HDR info as gralloc metadata"); |
| 786 | if (dynamicInfo->m.type_ == C2Config::HDR_DYNAMIC_METADATA_TYPE_SMPTE_2094_40) { |
| 787 | std::optional<std::vector<uint8_t>> smpte2094_40 = std::vector<uint8_t>(); |
| 788 | smpte2094_40->resize(dynamicInfo->flexCount()); |
| 789 | memcpy(smpte2094_40->data(), dynamicInfo->m.data, dynamicInfo->flexCount()); |
Taehwan Kim | 3f3d1ac | 2022-05-12 10:40:29 +0900 | [diff] [blame] | 790 | |
Devin Moore | 8e506ac | 2023-08-25 00:13:51 +0000 | [diff] [blame] | 791 | status = mapper.setSmpte2094_40(buffer.get(), smpte2094_40); |
| 792 | if (status != OK) { |
| 793 | err = C2_CORRUPTED; |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 794 | } |
| 795 | } else { |
| 796 | err = C2_BAD_VALUE; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | return err; |
| 801 | } |
| 802 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 803 | } // namespace android |