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 | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 23 | #include <aidl/android/hardware/graphics/common/Cta861_3.h> |
| 24 | #include <aidl/android/hardware/graphics/common/Smpte2086.h> |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 25 | #include <android-base/properties.h> |
Wonsik Kim | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 26 | #include <android/hardware/cas/native/1.0/types.h> |
| 27 | #include <android/hardware/drm/1.0/types.h> |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 28 | #include <android/hardware/graphics/common/1.2/types.h> |
| 29 | #include <android/hardware/graphics/mapper/4.0/IMapper.h> |
| 30 | #include <gralloctypes/Gralloc4.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 | 41d8343 | 2020-04-27 16:40:49 -0700 | [diff] [blame] | 38 | #include <mediadrm/ICrypto.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 39 | #include <nativebase/nativebase.h> |
Wonsik Kim | ebe0f9e | 2019-07-03 11:06:51 -0700 | [diff] [blame] | 40 | #include <ui/Fence.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 41 | |
| 42 | #include <C2AllocatorGralloc.h> |
| 43 | #include <C2BlockInternal.h> |
| 44 | #include <C2Debug.h> |
| 45 | |
| 46 | #include "Codec2Buffer.h" |
| 47 | |
| 48 | namespace android { |
| 49 | |
| 50 | // Codec2Buffer |
| 51 | |
| 52 | bool Codec2Buffer::canCopyLinear(const std::shared_ptr<C2Buffer> &buffer) const { |
| 53 | if (const_cast<Codec2Buffer *>(this)->base() == nullptr) { |
| 54 | return false; |
| 55 | } |
| 56 | if (!buffer) { |
| 57 | // Nothing to copy, so we can copy by doing nothing. |
| 58 | return true; |
| 59 | } |
| 60 | if (buffer->data().type() != C2BufferData::LINEAR) { |
| 61 | return false; |
| 62 | } |
| 63 | if (buffer->data().linearBlocks().size() == 0u) { |
| 64 | // Nothing to copy, so we can copy by doing nothing. |
| 65 | return true; |
| 66 | } else if (buffer->data().linearBlocks().size() > 1u) { |
| 67 | // We don't know how to copy more than one blocks. |
| 68 | return false; |
| 69 | } |
| 70 | if (buffer->data().linearBlocks()[0].size() > capacity()) { |
| 71 | // It won't fit. |
| 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool Codec2Buffer::copyLinear(const std::shared_ptr<C2Buffer> &buffer) { |
| 78 | // We assume that all canCopyLinear() checks passed. |
| 79 | if (!buffer || buffer->data().linearBlocks().size() == 0u |
| 80 | || buffer->data().linearBlocks()[0].size() == 0u) { |
| 81 | setRange(0, 0); |
| 82 | return true; |
| 83 | } |
| 84 | C2ReadView view = buffer->data().linearBlocks()[0].map().get(); |
| 85 | if (view.error() != C2_OK) { |
| 86 | ALOGD("Error while mapping: %d", view.error()); |
| 87 | return false; |
| 88 | } |
| 89 | if (view.capacity() > capacity()) { |
| 90 | ALOGD("C2ConstLinearBlock lied --- it actually doesn't fit: view(%u) > this(%zu)", |
| 91 | view.capacity(), capacity()); |
| 92 | return false; |
| 93 | } |
| 94 | memcpy(base(), view.data(), view.capacity()); |
| 95 | setRange(0, view.capacity()); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | void Codec2Buffer::setImageData(const sp<ABuffer> &imageData) { |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 100 | mImageData = imageData; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // LocalLinearBuffer |
| 104 | |
| 105 | bool LocalLinearBuffer::canCopy(const std::shared_ptr<C2Buffer> &buffer) const { |
| 106 | return canCopyLinear(buffer); |
| 107 | } |
| 108 | |
| 109 | bool LocalLinearBuffer::copy(const std::shared_ptr<C2Buffer> &buffer) { |
| 110 | return copyLinear(buffer); |
| 111 | } |
| 112 | |
| 113 | // DummyContainerBuffer |
| 114 | |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 115 | static uint8_t sDummyByte[1] = { 0 }; |
| 116 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 117 | DummyContainerBuffer::DummyContainerBuffer( |
| 118 | const sp<AMessage> &format, const std::shared_ptr<C2Buffer> &buffer) |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 119 | : Codec2Buffer(format, new ABuffer(sDummyByte, 1)), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 120 | mBufferRef(buffer) { |
| 121 | setRange(0, buffer ? 1 : 0); |
| 122 | } |
| 123 | |
| 124 | std::shared_ptr<C2Buffer> DummyContainerBuffer::asC2Buffer() { |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 125 | return mBufferRef; |
| 126 | } |
| 127 | |
| 128 | void DummyContainerBuffer::clearC2BufferRefs() { |
| 129 | mBufferRef.reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | bool DummyContainerBuffer::canCopy(const std::shared_ptr<C2Buffer> &) const { |
| 133 | return !mBufferRef; |
| 134 | } |
| 135 | |
| 136 | bool DummyContainerBuffer::copy(const std::shared_ptr<C2Buffer> &buffer) { |
| 137 | mBufferRef = buffer; |
| 138 | setRange(0, mBufferRef ? 1 : 0); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | // LinearBlockBuffer |
| 143 | |
| 144 | // static |
| 145 | sp<LinearBlockBuffer> LinearBlockBuffer::Allocate( |
| 146 | const sp<AMessage> &format, const std::shared_ptr<C2LinearBlock> &block) { |
| 147 | C2WriteView writeView(block->map().get()); |
| 148 | if (writeView.error() != C2_OK) { |
| 149 | return nullptr; |
| 150 | } |
| 151 | return new LinearBlockBuffer(format, std::move(writeView), block); |
| 152 | } |
| 153 | |
| 154 | std::shared_ptr<C2Buffer> LinearBlockBuffer::asC2Buffer() { |
| 155 | return C2Buffer::CreateLinearBuffer(mBlock->share(offset(), size(), C2Fence())); |
| 156 | } |
| 157 | |
| 158 | bool LinearBlockBuffer::canCopy(const std::shared_ptr<C2Buffer> &buffer) const { |
| 159 | return canCopyLinear(buffer); |
| 160 | } |
| 161 | |
| 162 | bool LinearBlockBuffer::copy(const std::shared_ptr<C2Buffer> &buffer) { |
| 163 | return copyLinear(buffer); |
| 164 | } |
| 165 | |
| 166 | LinearBlockBuffer::LinearBlockBuffer( |
| 167 | const sp<AMessage> &format, |
| 168 | C2WriteView&& writeView, |
| 169 | const std::shared_ptr<C2LinearBlock> &block) |
| 170 | : Codec2Buffer(format, new ABuffer(writeView.data(), writeView.size())), |
| 171 | mWriteView(writeView), |
| 172 | mBlock(block) { |
| 173 | } |
| 174 | |
| 175 | // ConstLinearBlockBuffer |
| 176 | |
| 177 | // static |
| 178 | sp<ConstLinearBlockBuffer> ConstLinearBlockBuffer::Allocate( |
| 179 | const sp<AMessage> &format, const std::shared_ptr<C2Buffer> &buffer) { |
| 180 | if (!buffer |
| 181 | || buffer->data().type() != C2BufferData::LINEAR |
| 182 | || buffer->data().linearBlocks().size() != 1u) { |
| 183 | return nullptr; |
| 184 | } |
| 185 | C2ReadView readView(buffer->data().linearBlocks()[0].map().get()); |
| 186 | if (readView.error() != C2_OK) { |
| 187 | return nullptr; |
| 188 | } |
| 189 | return new ConstLinearBlockBuffer(format, std::move(readView), buffer); |
| 190 | } |
| 191 | |
| 192 | ConstLinearBlockBuffer::ConstLinearBlockBuffer( |
| 193 | const sp<AMessage> &format, |
| 194 | C2ReadView&& readView, |
| 195 | const std::shared_ptr<C2Buffer> &buffer) |
| 196 | : Codec2Buffer(format, new ABuffer( |
| 197 | // NOTE: ABuffer only takes non-const pointer but this data is |
| 198 | // supposed to be read-only. |
| 199 | const_cast<uint8_t *>(readView.data()), readView.capacity())), |
| 200 | mReadView(readView), |
| 201 | mBufferRef(buffer) { |
| 202 | } |
| 203 | |
| 204 | std::shared_ptr<C2Buffer> ConstLinearBlockBuffer::asC2Buffer() { |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 205 | return mBufferRef; |
| 206 | } |
| 207 | |
| 208 | void ConstLinearBlockBuffer::clearC2BufferRefs() { |
| 209 | mBufferRef.reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // GraphicView2MediaImageConverter |
| 213 | |
| 214 | namespace { |
| 215 | |
| 216 | class GraphicView2MediaImageConverter { |
| 217 | public: |
| 218 | /** |
| 219 | * Creates a C2GraphicView <=> MediaImage converter |
| 220 | * |
| 221 | * \param view C2GraphicView object |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 222 | * \param format buffer format |
Wonsik Kim | 7d96631 | 2019-06-04 14:00:49 -0700 | [diff] [blame] | 223 | * \param copy whether the converter is used for copy or not |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 224 | */ |
| 225 | GraphicView2MediaImageConverter( |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 226 | const C2GraphicView &view, const sp<AMessage> &format, bool copy) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 227 | : mInitCheck(NO_INIT), |
| 228 | mView(view), |
| 229 | mWidth(view.width()), |
| 230 | mHeight(view.height()), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 231 | mAllocatedDepth(0), |
| 232 | mBackBufferSize(0), |
| 233 | mMediaImage(new ABuffer(sizeof(MediaImage2))) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 234 | ATRACE_CALL(); |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 235 | if (!format->findInt32(KEY_COLOR_FORMAT, &mClientColorFormat)) { |
| 236 | mClientColorFormat = COLOR_FormatYUV420Flexible; |
| 237 | } |
| 238 | if (!format->findInt32("android._color-format", &mComponentColorFormat)) { |
| 239 | mComponentColorFormat = COLOR_FormatYUV420Flexible; |
| 240 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 241 | if (view.error() != C2_OK) { |
| 242 | ALOGD("Converter: view.error() = %d", view.error()); |
| 243 | mInitCheck = BAD_VALUE; |
| 244 | return; |
| 245 | } |
| 246 | MediaImage2 *mediaImage = (MediaImage2 *)mMediaImage->base(); |
| 247 | const C2PlanarLayout &layout = view.layout(); |
| 248 | if (layout.numPlanes == 0) { |
| 249 | ALOGD("Converter: 0 planes"); |
| 250 | mInitCheck = BAD_VALUE; |
| 251 | return; |
| 252 | } |
Harish Mahendrakar | cac5385 | 2019-02-20 10:59:10 -0800 | [diff] [blame] | 253 | memset(mediaImage, 0, sizeof(*mediaImage)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 254 | mAllocatedDepth = layout.planes[0].allocatedDepth; |
| 255 | uint32_t bitDepth = layout.planes[0].bitDepth; |
| 256 | |
| 257 | // align width and height to support subsampling cleanly |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 258 | uint32_t stride = align(view.crop().width, 2) * divUp(layout.planes[0].allocatedDepth, 8u); |
| 259 | uint32_t vStride = align(view.crop().height, 2); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 260 | |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 261 | bool tryWrapping = !copy; |
| 262 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 263 | switch (layout.type) { |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 264 | case C2PlanarLayout::TYPE_YUV: { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 265 | mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV; |
| 266 | if (layout.numPlanes != 3) { |
| 267 | ALOGD("Converter: %d planes for YUV layout", layout.numPlanes); |
| 268 | mInitCheck = BAD_VALUE; |
| 269 | return; |
| 270 | } |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 271 | C2PlaneInfo yPlane = layout.planes[C2PlanarLayout::PLANE_Y]; |
| 272 | C2PlaneInfo uPlane = layout.planes[C2PlanarLayout::PLANE_U]; |
| 273 | C2PlaneInfo vPlane = layout.planes[C2PlanarLayout::PLANE_V]; |
| 274 | if (yPlane.channel != C2PlaneInfo::CHANNEL_Y |
| 275 | || uPlane.channel != C2PlaneInfo::CHANNEL_CB |
| 276 | || vPlane.channel != C2PlaneInfo::CHANNEL_CR) { |
| 277 | ALOGD("Converter: not YUV layout"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 278 | mInitCheck = BAD_VALUE; |
| 279 | return; |
| 280 | } |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 281 | bool yuv420888 = yPlane.rowSampling == 1 && yPlane.colSampling == 1 |
| 282 | && uPlane.rowSampling == 2 && uPlane.colSampling == 2 |
| 283 | && vPlane.rowSampling == 2 && vPlane.colSampling == 2; |
| 284 | if (yuv420888) { |
| 285 | for (uint32_t i = 0; i < 3; ++i) { |
| 286 | const C2PlaneInfo &plane = layout.planes[i]; |
| 287 | if (plane.allocatedDepth != 8 || plane.bitDepth != 8) { |
| 288 | yuv420888 = false; |
| 289 | break; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 290 | } |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 291 | } |
| 292 | yuv420888 = yuv420888 && yPlane.colInc == 1 && uPlane.rowInc == vPlane.rowInc; |
| 293 | } |
| 294 | int32_t copyFormat = mClientColorFormat; |
| 295 | if (yuv420888 && mClientColorFormat == COLOR_FormatYUV420Flexible) { |
| 296 | if (uPlane.colInc == 2 && vPlane.colInc == 2 |
| 297 | && yPlane.rowInc == uPlane.rowInc) { |
| 298 | copyFormat = COLOR_FormatYUV420PackedSemiPlanar; |
| 299 | } else if (uPlane.colInc == 1 && vPlane.colInc == 1 |
| 300 | && yPlane.rowInc == uPlane.rowInc * 2) { |
| 301 | copyFormat = COLOR_FormatYUV420PackedPlanar; |
| 302 | } |
| 303 | } |
| 304 | ALOGV("client_fmt=0x%x y:{colInc=%d rowInc=%d} u:{colInc=%d rowInc=%d} " |
| 305 | "v:{colInc=%d rowInc=%d}", |
| 306 | mClientColorFormat, |
| 307 | yPlane.colInc, yPlane.rowInc, |
| 308 | uPlane.colInc, uPlane.rowInc, |
| 309 | vPlane.colInc, vPlane.rowInc); |
| 310 | switch (copyFormat) { |
| 311 | case COLOR_FormatYUV420Flexible: |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 312 | case COLOR_FormatYUV420Planar: |
| 313 | case COLOR_FormatYUV420PackedPlanar: |
| 314 | mediaImage->mPlane[mediaImage->Y].mOffset = 0; |
| 315 | mediaImage->mPlane[mediaImage->Y].mColInc = 1; |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 316 | mediaImage->mPlane[mediaImage->Y].mRowInc = stride; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 317 | mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = 1; |
| 318 | mediaImage->mPlane[mediaImage->Y].mVertSubsampling = 1; |
| 319 | |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 320 | mediaImage->mPlane[mediaImage->U].mOffset = stride * vStride; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 321 | mediaImage->mPlane[mediaImage->U].mColInc = 1; |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 322 | mediaImage->mPlane[mediaImage->U].mRowInc = stride / 2; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 323 | mediaImage->mPlane[mediaImage->U].mHorizSubsampling = 2; |
| 324 | mediaImage->mPlane[mediaImage->U].mVertSubsampling = 2; |
| 325 | |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 326 | mediaImage->mPlane[mediaImage->V].mOffset = stride * vStride * 5 / 4; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 327 | mediaImage->mPlane[mediaImage->V].mColInc = 1; |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 328 | mediaImage->mPlane[mediaImage->V].mRowInc = stride / 2; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 329 | mediaImage->mPlane[mediaImage->V].mHorizSubsampling = 2; |
| 330 | mediaImage->mPlane[mediaImage->V].mVertSubsampling = 2; |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 331 | |
| 332 | if (tryWrapping && mClientColorFormat != COLOR_FormatYUV420Flexible) { |
| 333 | tryWrapping = yuv420888 && uPlane.colInc == 1 && vPlane.colInc == 1 |
| 334 | && yPlane.rowInc == uPlane.rowInc * 2 |
| 335 | && view.data()[0] < view.data()[1] |
| 336 | && view.data()[1] < view.data()[2]; |
| 337 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 338 | break; |
| 339 | |
| 340 | case COLOR_FormatYUV420SemiPlanar: |
| 341 | case COLOR_FormatYUV420PackedSemiPlanar: |
| 342 | mediaImage->mPlane[mediaImage->Y].mOffset = 0; |
| 343 | mediaImage->mPlane[mediaImage->Y].mColInc = 1; |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 344 | mediaImage->mPlane[mediaImage->Y].mRowInc = stride; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 345 | mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = 1; |
| 346 | mediaImage->mPlane[mediaImage->Y].mVertSubsampling = 1; |
| 347 | |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 348 | mediaImage->mPlane[mediaImage->U].mOffset = stride * vStride; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 349 | mediaImage->mPlane[mediaImage->U].mColInc = 2; |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 350 | mediaImage->mPlane[mediaImage->U].mRowInc = stride; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 351 | mediaImage->mPlane[mediaImage->U].mHorizSubsampling = 2; |
| 352 | mediaImage->mPlane[mediaImage->U].mVertSubsampling = 2; |
| 353 | |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 354 | mediaImage->mPlane[mediaImage->V].mOffset = stride * vStride + 1; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 355 | mediaImage->mPlane[mediaImage->V].mColInc = 2; |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 356 | mediaImage->mPlane[mediaImage->V].mRowInc = stride; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 357 | mediaImage->mPlane[mediaImage->V].mHorizSubsampling = 2; |
| 358 | mediaImage->mPlane[mediaImage->V].mVertSubsampling = 2; |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 359 | |
| 360 | if (tryWrapping && mClientColorFormat != COLOR_FormatYUV420Flexible) { |
| 361 | tryWrapping = yuv420888 && uPlane.colInc == 2 && vPlane.colInc == 2 |
| 362 | && yPlane.rowInc == uPlane.rowInc |
| 363 | && view.data()[0] < view.data()[1] |
| 364 | && view.data()[1] < view.data()[2]; |
| 365 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 366 | break; |
| 367 | |
Wonsik Kim | 29e3c4d | 2020-09-02 12:19:44 -0700 | [diff] [blame] | 368 | case COLOR_FormatYUVP010: |
Wonsik Kim | 08a8a2b | 2021-05-10 19:03:47 -0700 | [diff] [blame] | 369 | // stride is in bytes |
Wonsik Kim | 29e3c4d | 2020-09-02 12:19:44 -0700 | [diff] [blame] | 370 | mediaImage->mPlane[mediaImage->Y].mOffset = 0; |
| 371 | mediaImage->mPlane[mediaImage->Y].mColInc = 2; |
Wonsik Kim | 08a8a2b | 2021-05-10 19:03:47 -0700 | [diff] [blame] | 372 | mediaImage->mPlane[mediaImage->Y].mRowInc = stride; |
Wonsik Kim | 29e3c4d | 2020-09-02 12:19:44 -0700 | [diff] [blame] | 373 | mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = 1; |
| 374 | mediaImage->mPlane[mediaImage->Y].mVertSubsampling = 1; |
| 375 | |
Wonsik Kim | 08a8a2b | 2021-05-10 19:03:47 -0700 | [diff] [blame] | 376 | mediaImage->mPlane[mediaImage->U].mOffset = stride * vStride; |
Wonsik Kim | 29e3c4d | 2020-09-02 12:19:44 -0700 | [diff] [blame] | 377 | mediaImage->mPlane[mediaImage->U].mColInc = 4; |
Wonsik Kim | 08a8a2b | 2021-05-10 19:03:47 -0700 | [diff] [blame] | 378 | mediaImage->mPlane[mediaImage->U].mRowInc = stride; |
Wonsik Kim | 29e3c4d | 2020-09-02 12:19:44 -0700 | [diff] [blame] | 379 | mediaImage->mPlane[mediaImage->U].mHorizSubsampling = 2; |
| 380 | mediaImage->mPlane[mediaImage->U].mVertSubsampling = 2; |
| 381 | |
Wonsik Kim | 08a8a2b | 2021-05-10 19:03:47 -0700 | [diff] [blame] | 382 | mediaImage->mPlane[mediaImage->V].mOffset = stride * vStride + 2; |
Wonsik Kim | 29e3c4d | 2020-09-02 12:19:44 -0700 | [diff] [blame] | 383 | mediaImage->mPlane[mediaImage->V].mColInc = 4; |
Wonsik Kim | 08a8a2b | 2021-05-10 19:03:47 -0700 | [diff] [blame] | 384 | mediaImage->mPlane[mediaImage->V].mRowInc = stride; |
Wonsik Kim | 29e3c4d | 2020-09-02 12:19:44 -0700 | [diff] [blame] | 385 | mediaImage->mPlane[mediaImage->V].mHorizSubsampling = 2; |
| 386 | mediaImage->mPlane[mediaImage->V].mVertSubsampling = 2; |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 387 | if (tryWrapping) { |
| 388 | tryWrapping = yPlane.allocatedDepth == 16 |
| 389 | && uPlane.allocatedDepth == 16 |
| 390 | && vPlane.allocatedDepth == 16 |
| 391 | && yPlane.bitDepth == 10 |
| 392 | && uPlane.bitDepth == 10 |
| 393 | && vPlane.bitDepth == 10 |
| 394 | && yPlane.rightShift == 6 |
| 395 | && uPlane.rightShift == 6 |
| 396 | && vPlane.rightShift == 6 |
| 397 | && yPlane.rowSampling == 1 && yPlane.colSampling == 1 |
| 398 | && uPlane.rowSampling == 2 && uPlane.colSampling == 2 |
| 399 | && vPlane.rowSampling == 2 && vPlane.colSampling == 2 |
| 400 | && yPlane.colInc == 2 |
| 401 | && uPlane.colInc == 4 |
| 402 | && vPlane.colInc == 4 |
| 403 | && yPlane.rowInc == uPlane.rowInc |
| 404 | && yPlane.rowInc == vPlane.rowInc; |
| 405 | } |
Wonsik Kim | 29e3c4d | 2020-09-02 12:19:44 -0700 | [diff] [blame] | 406 | break; |
| 407 | |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 408 | default: { |
| 409 | // default to fully planar format --- this will be overridden if wrapping |
| 410 | // TODO: keep interleaved format |
| 411 | int32_t colInc = divUp(mAllocatedDepth, 8u); |
| 412 | int32_t rowInc = stride * colInc / yPlane.colSampling; |
| 413 | mediaImage->mPlane[mediaImage->Y].mOffset = 0; |
| 414 | mediaImage->mPlane[mediaImage->Y].mColInc = colInc; |
| 415 | mediaImage->mPlane[mediaImage->Y].mRowInc = rowInc; |
| 416 | mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = yPlane.colSampling; |
| 417 | mediaImage->mPlane[mediaImage->Y].mVertSubsampling = yPlane.rowSampling; |
| 418 | int32_t offset = rowInc * vStride / yPlane.rowSampling; |
| 419 | |
| 420 | rowInc = stride * colInc / uPlane.colSampling; |
| 421 | mediaImage->mPlane[mediaImage->U].mOffset = offset; |
| 422 | mediaImage->mPlane[mediaImage->U].mColInc = colInc; |
| 423 | mediaImage->mPlane[mediaImage->U].mRowInc = rowInc; |
| 424 | mediaImage->mPlane[mediaImage->U].mHorizSubsampling = uPlane.colSampling; |
| 425 | mediaImage->mPlane[mediaImage->U].mVertSubsampling = uPlane.rowSampling; |
| 426 | offset += rowInc * vStride / uPlane.rowSampling; |
| 427 | |
| 428 | rowInc = stride * colInc / vPlane.colSampling; |
| 429 | mediaImage->mPlane[mediaImage->V].mOffset = offset; |
| 430 | mediaImage->mPlane[mediaImage->V].mColInc = colInc; |
| 431 | mediaImage->mPlane[mediaImage->V].mRowInc = rowInc; |
| 432 | mediaImage->mPlane[mediaImage->V].mHorizSubsampling = vPlane.colSampling; |
| 433 | mediaImage->mPlane[mediaImage->V].mVertSubsampling = vPlane.rowSampling; |
| 434 | break; |
| 435 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 436 | } |
| 437 | break; |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 440 | case C2PlanarLayout::TYPE_YUVA: |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 441 | ALOGD("Converter: unrecognized color format " |
| 442 | "(client %d component %d) for YUVA layout", |
| 443 | mClientColorFormat, mComponentColorFormat); |
| 444 | mInitCheck = NO_INIT; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 445 | return; |
| 446 | case C2PlanarLayout::TYPE_RGB: |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 447 | ALOGD("Converter: unrecognized color format " |
| 448 | "(client %d component %d) for RGB layout", |
| 449 | mClientColorFormat, mComponentColorFormat); |
| 450 | mInitCheck = NO_INIT; |
| 451 | // TODO: support MediaImage layout |
| 452 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 453 | case C2PlanarLayout::TYPE_RGBA: |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 454 | ALOGD("Converter: unrecognized color format " |
| 455 | "(client %d component %d) for RGBA layout", |
| 456 | mClientColorFormat, mComponentColorFormat); |
| 457 | mInitCheck = NO_INIT; |
| 458 | // TODO: support MediaImage layout |
| 459 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 460 | default: |
| 461 | mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_UNKNOWN; |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 462 | if (layout.numPlanes == 1) { |
| 463 | const C2PlaneInfo &plane = layout.planes[0]; |
| 464 | if (plane.colInc < 0 || plane.rowInc < 0) { |
| 465 | // Copy-only if we have negative colInc/rowInc |
| 466 | tryWrapping = false; |
| 467 | } |
| 468 | mediaImage->mPlane[0].mOffset = 0; |
| 469 | mediaImage->mPlane[0].mColInc = std::abs(plane.colInc); |
| 470 | mediaImage->mPlane[0].mRowInc = std::abs(plane.rowInc); |
| 471 | mediaImage->mPlane[0].mHorizSubsampling = plane.colSampling; |
| 472 | mediaImage->mPlane[0].mVertSubsampling = plane.rowSampling; |
| 473 | } else { |
| 474 | ALOGD("Converter: unrecognized layout: color format (client %d component %d)", |
| 475 | mClientColorFormat, mComponentColorFormat); |
| 476 | mInitCheck = NO_INIT; |
| 477 | return; |
| 478 | } |
| 479 | break; |
| 480 | } |
| 481 | if (tryWrapping) { |
| 482 | // try to map directly. check if the planes are near one another |
| 483 | const uint8_t *minPtr = mView.data()[0]; |
| 484 | const uint8_t *maxPtr = mView.data()[0]; |
| 485 | int32_t planeSize = 0; |
| 486 | for (uint32_t i = 0; i < layout.numPlanes; ++i) { |
| 487 | const C2PlaneInfo &plane = layout.planes[i]; |
| 488 | int64_t planeStride = std::abs(plane.rowInc / plane.colInc); |
| 489 | ssize_t minOffset = plane.minOffset( |
| 490 | mWidth / plane.colSampling, mHeight / plane.rowSampling); |
| 491 | ssize_t maxOffset = plane.maxOffset( |
| 492 | mWidth / plane.colSampling, mHeight / plane.rowSampling); |
| 493 | if (minPtr > mView.data()[i] + minOffset) { |
| 494 | minPtr = mView.data()[i] + minOffset; |
| 495 | } |
| 496 | if (maxPtr < mView.data()[i] + maxOffset) { |
| 497 | maxPtr = mView.data()[i] + maxOffset; |
| 498 | } |
| 499 | planeSize += planeStride * divUp(mAllocatedDepth, 8u) |
| 500 | * align(mHeight, 64) / plane.rowSampling; |
| 501 | } |
| 502 | |
Wonsik Kim | dc17340 | 2021-07-22 19:38:17 -0700 | [diff] [blame] | 503 | if (minPtr == mView.data()[0] && (maxPtr - minPtr + 1) <= planeSize) { |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 504 | // FIXME: this is risky as reading/writing data out of bound results |
| 505 | // in an undefined behavior, but gralloc does assume a |
| 506 | // contiguous mapping |
| 507 | for (uint32_t i = 0; i < layout.numPlanes; ++i) { |
| 508 | const C2PlaneInfo &plane = layout.planes[i]; |
| 509 | mediaImage->mPlane[i].mOffset = mView.data()[i] - minPtr; |
| 510 | mediaImage->mPlane[i].mColInc = plane.colInc; |
| 511 | mediaImage->mPlane[i].mRowInc = plane.rowInc; |
| 512 | mediaImage->mPlane[i].mHorizSubsampling = plane.colSampling; |
| 513 | mediaImage->mPlane[i].mVertSubsampling = plane.rowSampling; |
| 514 | } |
| 515 | mWrapped = new ABuffer(const_cast<uint8_t *>(minPtr), |
| 516 | maxPtr - minPtr + 1); |
| 517 | ALOGV("Converter: wrapped (capacity=%zu)", mWrapped->capacity()); |
| 518 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 519 | } |
| 520 | mediaImage->mNumPlanes = layout.numPlanes; |
Harish Mahendrakar | f7c49e2 | 2019-05-24 14:19:16 -0700 | [diff] [blame] | 521 | mediaImage->mWidth = view.crop().width; |
| 522 | mediaImage->mHeight = view.crop().height; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 523 | mediaImage->mBitDepth = bitDepth; |
| 524 | mediaImage->mBitDepthAllocated = mAllocatedDepth; |
| 525 | |
| 526 | uint32_t bufferSize = 0; |
| 527 | for (uint32_t i = 0; i < layout.numPlanes; ++i) { |
| 528 | const C2PlaneInfo &plane = layout.planes[i]; |
| 529 | if (plane.allocatedDepth < plane.bitDepth |
| 530 | || plane.rightShift != plane.allocatedDepth - plane.bitDepth) { |
| 531 | ALOGD("rightShift value of %u unsupported", plane.rightShift); |
| 532 | mInitCheck = BAD_VALUE; |
| 533 | return; |
| 534 | } |
| 535 | if (plane.allocatedDepth > 8 && plane.endianness != C2PlaneInfo::NATIVE) { |
| 536 | ALOGD("endianness value of %u unsupported", plane.endianness); |
| 537 | mInitCheck = BAD_VALUE; |
| 538 | return; |
| 539 | } |
| 540 | if (plane.allocatedDepth != mAllocatedDepth || plane.bitDepth != bitDepth) { |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 541 | ALOGD("different allocatedDepth/bitDepth per plane unsupported"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 542 | mInitCheck = BAD_VALUE; |
| 543 | return; |
| 544 | } |
Wonsik Kim | 08a8a2b | 2021-05-10 19:03:47 -0700 | [diff] [blame] | 545 | // stride is in bytes |
| 546 | bufferSize += stride * vStride / plane.rowSampling / plane.colSampling; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | mBackBufferSize = bufferSize; |
| 550 | mInitCheck = OK; |
| 551 | } |
| 552 | |
| 553 | status_t initCheck() const { return mInitCheck; } |
| 554 | |
| 555 | uint32_t backBufferSize() const { return mBackBufferSize; } |
| 556 | |
| 557 | /** |
| 558 | * Wrap C2GraphicView using a MediaImage2. Note that if not wrapped, the content is not mapped |
| 559 | * in this function --- the caller should use CopyGraphicView2MediaImage() function to copy the |
| 560 | * data into a backing buffer explicitly. |
| 561 | * |
| 562 | * \return media buffer. This is null if wrapping failed. |
| 563 | */ |
| 564 | sp<ABuffer> wrap() const { |
| 565 | if (mBackBuffer == nullptr) { |
| 566 | return mWrapped; |
| 567 | } |
| 568 | return nullptr; |
| 569 | } |
| 570 | |
| 571 | bool setBackBuffer(const sp<ABuffer> &backBuffer) { |
Wonsik Kim | 186fdbf | 2019-01-29 13:30:01 -0800 | [diff] [blame] | 572 | if (backBuffer == nullptr) { |
| 573 | return false; |
| 574 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 575 | if (backBuffer->capacity() < mBackBufferSize) { |
| 576 | return false; |
| 577 | } |
| 578 | backBuffer->setRange(0, mBackBufferSize); |
| 579 | mBackBuffer = backBuffer; |
| 580 | return true; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Copy C2GraphicView to MediaImage2. |
| 585 | */ |
| 586 | status_t copyToMediaImage() { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 587 | ATRACE_CALL(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 588 | if (mInitCheck != OK) { |
| 589 | return mInitCheck; |
| 590 | } |
| 591 | return ImageCopy(mBackBuffer->base(), getMediaImage(), mView); |
| 592 | } |
| 593 | |
| 594 | const sp<ABuffer> &imageData() const { return mMediaImage; } |
| 595 | |
| 596 | private: |
| 597 | status_t mInitCheck; |
| 598 | |
| 599 | const C2GraphicView mView; |
| 600 | uint32_t mWidth; |
| 601 | uint32_t mHeight; |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 602 | int32_t mClientColorFormat; ///< SDK color format for MediaImage |
| 603 | int32_t mComponentColorFormat; ///< SDK color format from component |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 604 | sp<ABuffer> mWrapped; ///< wrapped buffer (if we can map C2Buffer to an ABuffer) |
| 605 | uint32_t mAllocatedDepth; |
| 606 | uint32_t mBackBufferSize; |
| 607 | sp<ABuffer> mMediaImage; |
| 608 | std::function<sp<ABuffer>(size_t)> mAlloc; |
| 609 | |
| 610 | sp<ABuffer> mBackBuffer; ///< backing buffer if we have to copy C2Buffer <=> ABuffer |
| 611 | |
| 612 | MediaImage2 *getMediaImage() { |
| 613 | return (MediaImage2 *)mMediaImage->base(); |
| 614 | } |
| 615 | }; |
| 616 | |
| 617 | } // namespace |
| 618 | |
| 619 | // GraphicBlockBuffer |
| 620 | |
| 621 | // static |
| 622 | sp<GraphicBlockBuffer> GraphicBlockBuffer::Allocate( |
| 623 | const sp<AMessage> &format, |
| 624 | const std::shared_ptr<C2GraphicBlock> &block, |
| 625 | std::function<sp<ABuffer>(size_t)> alloc) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 626 | ATRACE_BEGIN("GraphicBlockBuffer::Allocate block->map()"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 627 | C2GraphicView view(block->map().get()); |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 628 | ATRACE_END(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 629 | if (view.error() != C2_OK) { |
| 630 | ALOGD("C2GraphicBlock::map failed: %d", view.error()); |
| 631 | return nullptr; |
| 632 | } |
| 633 | |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 634 | GraphicView2MediaImageConverter converter(view, format, false /* copy */); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 635 | if (converter.initCheck() != OK) { |
| 636 | ALOGD("Converter init failed: %d", converter.initCheck()); |
| 637 | return nullptr; |
| 638 | } |
| 639 | bool wrapped = true; |
| 640 | sp<ABuffer> buffer = converter.wrap(); |
| 641 | if (buffer == nullptr) { |
| 642 | buffer = alloc(converter.backBufferSize()); |
| 643 | if (!converter.setBackBuffer(buffer)) { |
| 644 | ALOGD("Converter failed to set back buffer"); |
| 645 | return nullptr; |
| 646 | } |
| 647 | wrapped = false; |
| 648 | } |
| 649 | return new GraphicBlockBuffer( |
| 650 | format, |
| 651 | buffer, |
| 652 | std::move(view), |
| 653 | block, |
| 654 | converter.imageData(), |
| 655 | wrapped); |
| 656 | } |
| 657 | |
| 658 | GraphicBlockBuffer::GraphicBlockBuffer( |
| 659 | const sp<AMessage> &format, |
| 660 | const sp<ABuffer> &buffer, |
| 661 | C2GraphicView &&view, |
| 662 | const std::shared_ptr<C2GraphicBlock> &block, |
| 663 | const sp<ABuffer> &imageData, |
| 664 | bool wrapped) |
| 665 | : Codec2Buffer(format, buffer), |
| 666 | mView(view), |
| 667 | mBlock(block), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 668 | mWrapped(wrapped) { |
| 669 | setImageData(imageData); |
| 670 | } |
| 671 | |
| 672 | std::shared_ptr<C2Buffer> GraphicBlockBuffer::asC2Buffer() { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 673 | ATRACE_CALL(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 674 | uint32_t width = mView.width(); |
| 675 | uint32_t height = mView.height(); |
| 676 | if (!mWrapped) { |
| 677 | (void)ImageCopy(mView, base(), imageData()); |
| 678 | } |
| 679 | return C2Buffer::CreateGraphicBuffer( |
| 680 | mBlock->share(C2Rect(width, height), C2Fence())); |
| 681 | } |
| 682 | |
| 683 | // GraphicMetadataBuffer |
| 684 | GraphicMetadataBuffer::GraphicMetadataBuffer( |
| 685 | const sp<AMessage> &format, |
| 686 | const std::shared_ptr<C2Allocator> &alloc) |
| 687 | : Codec2Buffer(format, new ABuffer(sizeof(VideoNativeMetadata))), |
| 688 | mAlloc(alloc) { |
| 689 | ((VideoNativeMetadata *)base())->pBuffer = nullptr; |
| 690 | } |
| 691 | |
| 692 | std::shared_ptr<C2Buffer> GraphicMetadataBuffer::asC2Buffer() { |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 693 | #ifdef __LP64__ |
| 694 | static std::once_flag s_checkOnce; |
Harish Mahendrakar | 731e914 | 2021-04-21 17:20:39 -0700 | [diff] [blame] | 695 | static bool s_is64bitOk {true}; |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 696 | std::call_once(s_checkOnce, [&](){ |
| 697 | const std::string abi32list = |
| 698 | ::android::base::GetProperty("ro.product.cpu.abilist32", ""); |
Harish Mahendrakar | 731e914 | 2021-04-21 17:20:39 -0700 | [diff] [blame] | 699 | if (!abi32list.empty()) { |
| 700 | int32_t inputSurfaceSetting = |
| 701 | ::android::base::GetIntProperty("debug.stagefright.c2inputsurface", int32_t(0)); |
| 702 | s_is64bitOk = inputSurfaceSetting != 0; |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 703 | } |
| 704 | }); |
| 705 | |
Harish Mahendrakar | 731e914 | 2021-04-21 17:20:39 -0700 | [diff] [blame] | 706 | if (!s_is64bitOk) { |
| 707 | ALOGE("GraphicMetadataBuffer does not work in 32+64 system if compiled as 64-bit object"\ |
| 708 | "when debug.stagefright.c2inputsurface is set to 0"); |
bohu | a222c0b | 2021-01-12 18:54:53 -0800 | [diff] [blame] | 709 | return nullptr; |
| 710 | } |
| 711 | #endif |
| 712 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 713 | VideoNativeMetadata *meta = (VideoNativeMetadata *)base(); |
| 714 | ANativeWindowBuffer *buffer = (ANativeWindowBuffer *)meta->pBuffer; |
| 715 | if (buffer == nullptr) { |
| 716 | ALOGD("VideoNativeMetadata contains null buffer"); |
| 717 | return nullptr; |
| 718 | } |
| 719 | |
| 720 | ALOGV("VideoNativeMetadata: %dx%d", buffer->width, buffer->height); |
| 721 | C2Handle *handle = WrapNativeCodec2GrallocHandle( |
Sungtak Lee | a4d13be | 2019-01-23 15:24:46 -0800 | [diff] [blame] | 722 | buffer->handle, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 723 | buffer->width, |
| 724 | buffer->height, |
| 725 | buffer->format, |
| 726 | buffer->usage, |
| 727 | buffer->stride); |
| 728 | std::shared_ptr<C2GraphicAllocation> alloc; |
| 729 | c2_status_t err = mAlloc->priorGraphicAllocation(handle, &alloc); |
| 730 | if (err != C2_OK) { |
| 731 | ALOGD("Failed to wrap VideoNativeMetadata into C2GraphicAllocation"); |
Chih-Yu Huang | c0ac355 | 2021-03-11 14:37:10 +0900 | [diff] [blame] | 732 | native_handle_close(handle); |
| 733 | native_handle_delete(handle); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 734 | return nullptr; |
| 735 | } |
| 736 | std::shared_ptr<C2GraphicBlock> block = _C2BlockFactory::CreateGraphicBlock(alloc); |
| 737 | |
| 738 | meta->pBuffer = 0; |
Wonsik Kim | ebe0f9e | 2019-07-03 11:06:51 -0700 | [diff] [blame] | 739 | // TODO: wrap this in C2Fence so that the component can wait when it |
| 740 | // actually starts processing. |
| 741 | if (meta->nFenceFd >= 0) { |
| 742 | sp<Fence> fence(new Fence(meta->nFenceFd)); |
| 743 | fence->waitForever(LOG_TAG); |
| 744 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 745 | return C2Buffer::CreateGraphicBuffer( |
| 746 | block->share(C2Rect(buffer->width, buffer->height), C2Fence())); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | // ConstGraphicBlockBuffer |
| 750 | |
| 751 | // static |
| 752 | sp<ConstGraphicBlockBuffer> ConstGraphicBlockBuffer::Allocate( |
| 753 | const sp<AMessage> &format, |
| 754 | const std::shared_ptr<C2Buffer> &buffer, |
| 755 | std::function<sp<ABuffer>(size_t)> alloc) { |
| 756 | if (!buffer |
| 757 | || buffer->data().type() != C2BufferData::GRAPHIC |
| 758 | || buffer->data().graphicBlocks().size() != 1u) { |
| 759 | ALOGD("C2Buffer precond fail"); |
| 760 | return nullptr; |
| 761 | } |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 762 | ATRACE_BEGIN("ConstGraphicBlockBuffer::Allocate block->map()"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 763 | std::unique_ptr<const C2GraphicView> view(std::make_unique<const C2GraphicView>( |
| 764 | buffer->data().graphicBlocks()[0].map().get())); |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 765 | ATRACE_END(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 766 | std::unique_ptr<const C2GraphicView> holder; |
| 767 | |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 768 | GraphicView2MediaImageConverter converter(*view, format, false /* copy */); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 769 | if (converter.initCheck() != OK) { |
| 770 | ALOGD("Converter init failed: %d", converter.initCheck()); |
| 771 | return nullptr; |
| 772 | } |
| 773 | bool wrapped = true; |
| 774 | sp<ABuffer> aBuffer = converter.wrap(); |
| 775 | if (aBuffer == nullptr) { |
| 776 | aBuffer = alloc(converter.backBufferSize()); |
| 777 | if (!converter.setBackBuffer(aBuffer)) { |
| 778 | ALOGD("Converter failed to set back buffer"); |
| 779 | return nullptr; |
| 780 | } |
| 781 | wrapped = false; |
| 782 | converter.copyToMediaImage(); |
| 783 | // We don't need the view. |
| 784 | holder = std::move(view); |
| 785 | } |
| 786 | return new ConstGraphicBlockBuffer( |
| 787 | format, |
| 788 | aBuffer, |
| 789 | std::move(view), |
| 790 | buffer, |
| 791 | converter.imageData(), |
| 792 | wrapped); |
| 793 | } |
| 794 | |
| 795 | // static |
| 796 | sp<ConstGraphicBlockBuffer> ConstGraphicBlockBuffer::AllocateEmpty( |
| 797 | const sp<AMessage> &format, |
| 798 | std::function<sp<ABuffer>(size_t)> alloc) { |
| 799 | int32_t width, height; |
| 800 | if (!format->findInt32("width", &width) |
| 801 | || !format->findInt32("height", &height)) { |
| 802 | ALOGD("format had no width / height"); |
| 803 | return nullptr; |
| 804 | } |
Wonsik Kim | 08a8a2b | 2021-05-10 19:03:47 -0700 | [diff] [blame] | 805 | int32_t colorFormat = COLOR_FormatYUV420Flexible; |
| 806 | int32_t bpp = 12; // 8(Y) + 2(U) + 2(V) |
| 807 | if (format->findInt32(KEY_COLOR_FORMAT, &colorFormat)) { |
| 808 | if (colorFormat == COLOR_FormatYUVP010) { |
| 809 | bpp = 24; // 16(Y) + 4(U) + 4(V) |
| 810 | } |
| 811 | } |
| 812 | sp<ABuffer> aBuffer(alloc(align(width, 16) * align(height, 16) * bpp / 8)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 813 | return new ConstGraphicBlockBuffer( |
| 814 | format, |
| 815 | aBuffer, |
| 816 | nullptr, |
| 817 | nullptr, |
| 818 | nullptr, |
| 819 | false); |
| 820 | } |
| 821 | |
| 822 | ConstGraphicBlockBuffer::ConstGraphicBlockBuffer( |
| 823 | const sp<AMessage> &format, |
| 824 | const sp<ABuffer> &aBuffer, |
| 825 | std::unique_ptr<const C2GraphicView> &&view, |
| 826 | const std::shared_ptr<C2Buffer> &buffer, |
| 827 | const sp<ABuffer> &imageData, |
| 828 | bool wrapped) |
| 829 | : Codec2Buffer(format, aBuffer), |
| 830 | mView(std::move(view)), |
| 831 | mBufferRef(buffer), |
| 832 | mWrapped(wrapped) { |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 833 | setImageData(imageData); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | std::shared_ptr<C2Buffer> ConstGraphicBlockBuffer::asC2Buffer() { |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 837 | return mBufferRef; |
| 838 | } |
| 839 | |
| 840 | void ConstGraphicBlockBuffer::clearC2BufferRefs() { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 841 | mView.reset(); |
Wonsik Kim | f9b3212 | 2020-04-02 11:30:17 -0700 | [diff] [blame] | 842 | mBufferRef.reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | bool ConstGraphicBlockBuffer::canCopy(const std::shared_ptr<C2Buffer> &buffer) const { |
| 846 | if (mWrapped || mBufferRef) { |
| 847 | ALOGD("ConstGraphicBlockBuffer::canCopy: %swrapped ; buffer ref %s", |
| 848 | mWrapped ? "" : "not ", mBufferRef ? "exists" : "doesn't exist"); |
| 849 | return false; |
| 850 | } |
| 851 | if (!buffer) { |
| 852 | // Nothing to copy, so we can copy by doing nothing. |
| 853 | return true; |
| 854 | } |
| 855 | if (buffer->data().type() != C2BufferData::GRAPHIC) { |
| 856 | ALOGD("ConstGraphicBlockBuffer::canCopy: buffer precondition unsatisfied"); |
| 857 | return false; |
| 858 | } |
| 859 | if (buffer->data().graphicBlocks().size() == 0) { |
| 860 | return true; |
| 861 | } else if (buffer->data().graphicBlocks().size() != 1u) { |
| 862 | ALOGD("ConstGraphicBlockBuffer::canCopy: too many blocks"); |
| 863 | return false; |
| 864 | } |
| 865 | |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 866 | ATRACE_BEGIN("ConstGraphicBlockBuffer::canCopy block->map()"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 867 | GraphicView2MediaImageConverter converter( |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 868 | buffer->data().graphicBlocks()[0].map().get(), |
| 869 | // FIXME: format() is not const, but we cannot change it, so do a const cast here |
| 870 | const_cast<ConstGraphicBlockBuffer *>(this)->format(), |
| 871 | true /* copy */); |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame^] | 872 | ATRACE_END(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 873 | if (converter.initCheck() != OK) { |
| 874 | ALOGD("ConstGraphicBlockBuffer::canCopy: converter init failed: %d", converter.initCheck()); |
| 875 | return false; |
| 876 | } |
| 877 | if (converter.backBufferSize() > capacity()) { |
| 878 | ALOGD("ConstGraphicBlockBuffer::canCopy: insufficient capacity: req %u has %zu", |
| 879 | converter.backBufferSize(), capacity()); |
| 880 | return false; |
| 881 | } |
| 882 | return true; |
| 883 | } |
| 884 | |
| 885 | bool ConstGraphicBlockBuffer::copy(const std::shared_ptr<C2Buffer> &buffer) { |
| 886 | if (!buffer || buffer->data().graphicBlocks().size() == 0) { |
| 887 | setRange(0, 0); |
| 888 | return true; |
| 889 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 890 | |
| 891 | GraphicView2MediaImageConverter converter( |
Wonsik Kim | d79ee1f | 2020-08-27 17:41:56 -0700 | [diff] [blame] | 892 | buffer->data().graphicBlocks()[0].map().get(), format(), true /* copy */); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 893 | if (converter.initCheck() != OK) { |
| 894 | ALOGD("ConstGraphicBlockBuffer::copy: converter init failed: %d", converter.initCheck()); |
| 895 | return false; |
| 896 | } |
| 897 | sp<ABuffer> aBuffer = new ABuffer(base(), capacity()); |
| 898 | if (!converter.setBackBuffer(aBuffer)) { |
| 899 | ALOGD("ConstGraphicBlockBuffer::copy: set back buffer failed"); |
| 900 | return false; |
| 901 | } |
Pin-chih Lin | 1971e2c | 2019-04-15 19:36:26 +0800 | [diff] [blame] | 902 | setRange(0, aBuffer->size()); // align size info |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 903 | converter.copyToMediaImage(); |
| 904 | setImageData(converter.imageData()); |
| 905 | mBufferRef = buffer; |
| 906 | return true; |
| 907 | } |
| 908 | |
| 909 | // EncryptedLinearBlockBuffer |
| 910 | |
| 911 | EncryptedLinearBlockBuffer::EncryptedLinearBlockBuffer( |
| 912 | const sp<AMessage> &format, |
| 913 | const std::shared_ptr<C2LinearBlock> &block, |
| 914 | const sp<IMemory> &memory, |
| 915 | int32_t heapSeqNum) |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 916 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 917 | // (see declaration for details). |
| 918 | // Either document why it is safe in this case or address the |
| 919 | // issue (e.g. by copying). |
| 920 | : Codec2Buffer(format, new ABuffer(memory->unsecurePointer(), memory->size())), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 921 | mBlock(block), |
| 922 | mMemory(memory), |
| 923 | mHeapSeqNum(heapSeqNum) { |
| 924 | } |
| 925 | |
| 926 | std::shared_ptr<C2Buffer> EncryptedLinearBlockBuffer::asC2Buffer() { |
| 927 | return C2Buffer::CreateLinearBuffer(mBlock->share(offset(), size(), C2Fence())); |
| 928 | } |
| 929 | |
| 930 | void EncryptedLinearBlockBuffer::fillSourceBuffer( |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 931 | hardware::drm::V1_0::SharedBuffer *source) { |
| 932 | BufferChannelBase::IMemoryToSharedBuffer(mMemory, mHeapSeqNum, source); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | void EncryptedLinearBlockBuffer::fillSourceBuffer( |
| 936 | hardware::cas::native::V1_0::SharedBuffer *source) { |
| 937 | ssize_t offset; |
| 938 | size_t size; |
| 939 | |
| 940 | mHidlMemory = hardware::fromHeap(mMemory->getMemory(&offset, &size)); |
| 941 | source->heapBase = *mHidlMemory; |
| 942 | source->offset = offset; |
| 943 | source->size = size; |
| 944 | } |
| 945 | |
| 946 | bool EncryptedLinearBlockBuffer::copyDecryptedContent( |
| 947 | const sp<IMemory> &decrypted, size_t length) { |
| 948 | C2WriteView view = mBlock->map().get(); |
| 949 | if (view.error() != C2_OK) { |
| 950 | return false; |
| 951 | } |
| 952 | if (view.size() < length) { |
| 953 | return false; |
| 954 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 955 | memcpy(view.data(), decrypted->unsecurePointer(), length); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 956 | return true; |
| 957 | } |
| 958 | |
| 959 | bool EncryptedLinearBlockBuffer::copyDecryptedContentFromMemory(size_t length) { |
| 960 | return copyDecryptedContent(mMemory, length); |
| 961 | } |
| 962 | |
| 963 | native_handle_t *EncryptedLinearBlockBuffer::handle() const { |
| 964 | return const_cast<native_handle_t *>(mBlock->handle()); |
| 965 | } |
| 966 | |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 967 | using ::aidl::android::hardware::graphics::common::Cta861_3; |
| 968 | using ::aidl::android::hardware::graphics::common::Smpte2086; |
| 969 | |
| 970 | using ::android::gralloc4::MetadataType_Cta861_3; |
| 971 | using ::android::gralloc4::MetadataType_Smpte2086; |
| 972 | using ::android::gralloc4::MetadataType_Smpte2094_40; |
| 973 | |
| 974 | using ::android::hardware::Return; |
| 975 | using ::android::hardware::hidl_vec; |
| 976 | |
| 977 | using Error4 = ::android::hardware::graphics::mapper::V4_0::Error; |
| 978 | using IMapper4 = ::android::hardware::graphics::mapper::V4_0::IMapper; |
| 979 | |
| 980 | namespace { |
| 981 | |
| 982 | sp<IMapper4> GetMapper4() { |
| 983 | static sp<IMapper4> sMapper = IMapper4::getService(); |
| 984 | return sMapper; |
| 985 | } |
| 986 | |
| 987 | class NativeHandleDeleter { |
| 988 | public: |
| 989 | explicit NativeHandleDeleter(native_handle_t *handle) : mHandle(handle) {} |
| 990 | ~NativeHandleDeleter() { |
| 991 | if (mHandle) { |
| 992 | native_handle_delete(mHandle); |
| 993 | } |
| 994 | } |
| 995 | private: |
| 996 | native_handle_t *mHandle; |
| 997 | }; |
| 998 | |
| 999 | } // namspace |
| 1000 | |
| 1001 | c2_status_t GetHdrMetadataFromGralloc4Handle( |
| 1002 | const C2Handle *const handle, |
| 1003 | std::shared_ptr<C2StreamHdrStaticMetadataInfo::input> *staticInfo, |
| 1004 | std::shared_ptr<C2StreamHdrDynamicMetadataInfo::input> *dynamicInfo) { |
| 1005 | c2_status_t err = C2_OK; |
| 1006 | native_handle_t *nativeHandle = UnwrapNativeCodec2GrallocHandle(handle); |
| 1007 | if (nativeHandle == nullptr) { |
| 1008 | // Nothing to do |
| 1009 | return err; |
| 1010 | } |
| 1011 | // TRICKY: UnwrapNativeCodec2GrallocHandle creates a new handle but |
| 1012 | // does not clone the fds. Thus we need to delete the handle |
| 1013 | // without closing it when going out of scope. |
| 1014 | // NativeHandle cannot solve this problem, as it would close and |
| 1015 | // delete the handle, while we need delete only. |
| 1016 | NativeHandleDeleter nhd(nativeHandle); |
| 1017 | sp<IMapper4> mapper = GetMapper4(); |
| 1018 | if (!mapper) { |
| 1019 | // Gralloc4 not supported; nothing to do |
| 1020 | return err; |
| 1021 | } |
| 1022 | Error4 mapperErr = Error4::NONE; |
| 1023 | if (staticInfo) { |
| 1024 | staticInfo->reset(new C2StreamHdrStaticMetadataInfo::input(0u)); |
| 1025 | memset(&(*staticInfo)->mastering, 0, sizeof((*staticInfo)->mastering)); |
| 1026 | (*staticInfo)->maxCll = 0; |
| 1027 | (*staticInfo)->maxFall = 0; |
| 1028 | IMapper4::get_cb cb = [&mapperErr, staticInfo](Error4 err, const hidl_vec<uint8_t> &vec) { |
| 1029 | mapperErr = err; |
| 1030 | if (err != Error4::NONE) { |
| 1031 | return; |
| 1032 | } |
| 1033 | |
| 1034 | std::optional<Smpte2086> smpte2086; |
| 1035 | gralloc4::decodeSmpte2086(vec, &smpte2086); |
| 1036 | if (smpte2086) { |
| 1037 | (*staticInfo)->mastering.red.x = smpte2086->primaryRed.x; |
| 1038 | (*staticInfo)->mastering.red.y = smpte2086->primaryRed.y; |
| 1039 | (*staticInfo)->mastering.green.x = smpte2086->primaryGreen.x; |
| 1040 | (*staticInfo)->mastering.green.y = smpte2086->primaryGreen.y; |
| 1041 | (*staticInfo)->mastering.blue.x = smpte2086->primaryBlue.x; |
| 1042 | (*staticInfo)->mastering.blue.y = smpte2086->primaryBlue.y; |
| 1043 | (*staticInfo)->mastering.white.x = smpte2086->whitePoint.x; |
| 1044 | (*staticInfo)->mastering.white.y = smpte2086->whitePoint.y; |
| 1045 | |
| 1046 | (*staticInfo)->mastering.maxLuminance = smpte2086->maxLuminance; |
| 1047 | (*staticInfo)->mastering.minLuminance = smpte2086->minLuminance; |
| 1048 | } else { |
| 1049 | mapperErr = Error4::BAD_VALUE; |
| 1050 | } |
| 1051 | }; |
| 1052 | Return<void> ret = mapper->get(nativeHandle, MetadataType_Smpte2086, cb); |
| 1053 | if (!ret.isOk()) { |
| 1054 | err = C2_REFUSED; |
| 1055 | } else if (mapperErr != Error4::NONE) { |
| 1056 | err = C2_CORRUPTED; |
| 1057 | } |
| 1058 | cb = [&mapperErr, staticInfo](Error4 err, const hidl_vec<uint8_t> &vec) { |
| 1059 | mapperErr = err; |
| 1060 | if (err != Error4::NONE) { |
| 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | std::optional<Cta861_3> cta861_3; |
| 1065 | gralloc4::decodeCta861_3(vec, &cta861_3); |
| 1066 | if (cta861_3) { |
| 1067 | (*staticInfo)->maxCll = cta861_3->maxContentLightLevel; |
| 1068 | (*staticInfo)->maxFall = cta861_3->maxFrameAverageLightLevel; |
| 1069 | } else { |
| 1070 | mapperErr = Error4::BAD_VALUE; |
| 1071 | } |
| 1072 | }; |
| 1073 | ret = mapper->get(nativeHandle, MetadataType_Cta861_3, cb); |
| 1074 | if (!ret.isOk()) { |
| 1075 | err = C2_REFUSED; |
| 1076 | } else if (mapperErr != Error4::NONE) { |
| 1077 | err = C2_CORRUPTED; |
| 1078 | } |
| 1079 | } |
| 1080 | if (dynamicInfo) { |
| 1081 | dynamicInfo->reset(); |
| 1082 | IMapper4::get_cb cb = [&mapperErr, dynamicInfo](Error4 err, const hidl_vec<uint8_t> &vec) { |
| 1083 | mapperErr = err; |
| 1084 | if (err != Error4::NONE) { |
| 1085 | return; |
| 1086 | } |
| 1087 | if (!dynamicInfo) { |
| 1088 | return; |
| 1089 | } |
| 1090 | *dynamicInfo = C2StreamHdrDynamicMetadataInfo::input::AllocShared( |
| 1091 | vec.size(), 0u, C2Config::HDR_DYNAMIC_METADATA_TYPE_SMPTE_2094_40); |
| 1092 | memcpy((*dynamicInfo)->m.data, vec.data(), vec.size()); |
| 1093 | }; |
| 1094 | Return<void> ret = mapper->get(nativeHandle, MetadataType_Smpte2094_40, cb); |
| 1095 | if (!ret.isOk() || mapperErr != Error4::NONE) { |
| 1096 | dynamicInfo->reset(); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | return err; |
| 1101 | } |
| 1102 | |
| 1103 | c2_status_t SetHdrMetadataToGralloc4Handle( |
| 1104 | const std::shared_ptr<const C2StreamHdrStaticMetadataInfo::output> &staticInfo, |
| 1105 | const std::shared_ptr<const C2StreamHdrDynamicMetadataInfo::output> &dynamicInfo, |
| 1106 | const C2Handle *const handle) { |
| 1107 | c2_status_t err = C2_OK; |
| 1108 | native_handle_t *nativeHandle = UnwrapNativeCodec2GrallocHandle(handle); |
| 1109 | if (nativeHandle == nullptr) { |
| 1110 | // Nothing to do |
| 1111 | return err; |
| 1112 | } |
| 1113 | // TRICKY: UnwrapNativeCodec2GrallocHandle creates a new handle but |
| 1114 | // does not clone the fds. Thus we need to delete the handle |
| 1115 | // without closing it when going out of scope. |
| 1116 | NativeHandleDeleter nhd(nativeHandle); |
| 1117 | sp<IMapper4> mapper = GetMapper4(); |
| 1118 | if (!mapper) { |
| 1119 | // Gralloc4 not supported; nothing to do |
| 1120 | return err; |
| 1121 | } |
| 1122 | if (staticInfo && *staticInfo) { |
| 1123 | std::optional<Smpte2086> smpte2086 = Smpte2086{ |
| 1124 | {staticInfo->mastering.red.x, staticInfo->mastering.red.y}, |
| 1125 | {staticInfo->mastering.green.x, staticInfo->mastering.green.y}, |
| 1126 | {staticInfo->mastering.blue.x, staticInfo->mastering.blue.y}, |
| 1127 | {staticInfo->mastering.white.x, staticInfo->mastering.white.y}, |
| 1128 | staticInfo->mastering.maxLuminance, |
| 1129 | staticInfo->mastering.minLuminance, |
| 1130 | }; |
| 1131 | hidl_vec<uint8_t> vec; |
| 1132 | if (gralloc4::encodeSmpte2086(smpte2086, &vec) == OK) { |
| 1133 | Return<Error4> ret = mapper->set(nativeHandle, MetadataType_Smpte2086, vec); |
| 1134 | if (!ret.isOk()) { |
| 1135 | err = C2_REFUSED; |
| 1136 | } else if (ret != Error4::NONE) { |
| 1137 | err = C2_CORRUPTED; |
| 1138 | } |
| 1139 | } |
| 1140 | std::optional<Cta861_3> cta861_3 = Cta861_3{ |
| 1141 | staticInfo->maxCll, |
| 1142 | staticInfo->maxFall, |
| 1143 | }; |
| 1144 | if (gralloc4::encodeCta861_3(cta861_3, &vec) == OK) { |
| 1145 | Return<Error4> ret = mapper->set(nativeHandle, MetadataType_Cta861_3, vec); |
| 1146 | if (!ret.isOk()) { |
| 1147 | err = C2_REFUSED; |
| 1148 | } else if (ret != Error4::NONE) { |
| 1149 | err = C2_CORRUPTED; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | if (dynamicInfo && *dynamicInfo) { |
| 1154 | hidl_vec<uint8_t> vec; |
| 1155 | vec.resize(dynamicInfo->flexCount()); |
| 1156 | memcpy(vec.data(), dynamicInfo->m.data, dynamicInfo->flexCount()); |
| 1157 | std::optional<IMapper4::MetadataType> metadataType; |
| 1158 | switch (dynamicInfo->m.type_) { |
| 1159 | case C2Config::HDR_DYNAMIC_METADATA_TYPE_SMPTE_2094_10: |
| 1160 | // TODO |
| 1161 | break; |
| 1162 | case C2Config::HDR_DYNAMIC_METADATA_TYPE_SMPTE_2094_40: |
| 1163 | metadataType = MetadataType_Smpte2094_40; |
| 1164 | break; |
| 1165 | } |
| 1166 | if (metadataType) { |
| 1167 | Return<Error4> ret = mapper->set(nativeHandle, *metadataType, vec); |
| 1168 | if (!ret.isOk()) { |
| 1169 | err = C2_REFUSED; |
| 1170 | } else if (ret != Error4::NONE) { |
| 1171 | err = C2_CORRUPTED; |
| 1172 | } |
| 1173 | } else { |
| 1174 | err = C2_BAD_VALUE; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | return err; |
| 1179 | } |
| 1180 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1181 | } // namespace android |