Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <inttypes.h> |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "NdkImage" |
| 21 | |
| 22 | #include "NdkImagePriv.h" |
| 23 | #include "NdkImageReaderPriv.h" |
| 24 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 25 | #include <android_media_Utils.h> |
Jooyung Han | fe30459 | 2019-02-21 15:12:59 +0900 | [diff] [blame] | 26 | #include <private/android/AHardwareBufferHelpers.h> |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 28 | |
| 29 | using namespace android; |
| 30 | |
| 31 | #define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) ) |
| 32 | |
Jiwen 'Steve' Cai | e31bc87 | 2017-04-21 17:13:18 -0700 | [diff] [blame] | 33 | AImage::AImage(AImageReader* reader, int32_t format, uint64_t usage, BufferItem* buffer, |
| 34 | int64_t timestamp, int32_t width, int32_t height, int32_t numPlanes) : |
| 35 | mReader(reader), mFormat(format), mUsage(usage), mBuffer(buffer), mLockedBuffer(nullptr), |
| 36 | mTimestamp(timestamp), mWidth(width), mHeight(height), mNumPlanes(numPlanes) { |
Jayant Chowdhary | 9ddd6e8 | 2019-07-16 11:04:06 -0700 | [diff] [blame] | 37 | LOG_FATAL_IF(reader == nullptr, "AImageReader shouldn't be null while creating AImage"); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 40 | AImage::~AImage() { |
Yin-Chia Yeh | 4199198 | 2017-11-15 15:53:18 -0800 | [diff] [blame] | 41 | Mutex::Autolock _l(mLock); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 42 | if (!mIsClosed) { |
| 43 | LOG_ALWAYS_FATAL( |
| 44 | "Error: AImage %p is deleted before returning buffer to AImageReader!", this); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | bool |
| 49 | AImage::isClosed() const { |
| 50 | Mutex::Autolock _l(mLock); |
| 51 | return mIsClosed; |
| 52 | } |
| 53 | |
| 54 | void |
Jiwen 'Steve' Cai | e168996 | 2017-02-20 16:59:05 -0800 | [diff] [blame] | 55 | AImage::close(int releaseFenceFd) { |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 56 | Mutex::Autolock _l(mLock); |
| 57 | if (mIsClosed) { |
| 58 | return; |
| 59 | } |
Jayant Chowdhary | 3a4af23 | 2020-12-15 11:01:03 -0800 | [diff] [blame] | 60 | if (mReader->mIsOpen) { |
Jayant Chowdhary | 9ddd6e8 | 2019-07-16 11:04:06 -0700 | [diff] [blame] | 61 | mReader->releaseImageLocked(this, releaseFenceFd); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 62 | } |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 63 | // Should have been set to nullptr in releaseImageLocked |
| 64 | // Set to nullptr here for extra safety only |
| 65 | mBuffer = nullptr; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 66 | mLockedBuffer = nullptr; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 67 | mIsClosed = true; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | AImage::free() { |
| 72 | if (!isClosed()) { |
| 73 | ALOGE("Cannot free AImage before close!"); |
| 74 | return; |
| 75 | } |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 76 | delete this; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | AImage::lockReader() const { |
Jayant Chowdhary | 9ddd6e8 | 2019-07-16 11:04:06 -0700 | [diff] [blame] | 81 | mReader->mLock.lock(); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | AImage::unlockReader() const { |
Jayant Chowdhary | 9ddd6e8 | 2019-07-16 11:04:06 -0700 | [diff] [blame] | 86 | mReader->mLock.unlock(); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | media_status_t |
| 90 | AImage::getWidth(int32_t* width) const { |
| 91 | if (width == nullptr) { |
| 92 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 93 | } |
| 94 | *width = -1; |
| 95 | if (isClosed()) { |
| 96 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 97 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 98 | } |
| 99 | *width = mWidth; |
| 100 | return AMEDIA_OK; |
| 101 | } |
| 102 | |
| 103 | media_status_t |
| 104 | AImage::getHeight(int32_t* height) const { |
| 105 | if (height == nullptr) { |
| 106 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 107 | } |
| 108 | *height = -1; |
| 109 | if (isClosed()) { |
| 110 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 111 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 112 | } |
| 113 | *height = mHeight; |
| 114 | return AMEDIA_OK; |
| 115 | } |
| 116 | |
| 117 | media_status_t |
| 118 | AImage::getFormat(int32_t* format) const { |
| 119 | if (format == nullptr) { |
| 120 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 121 | } |
| 122 | *format = -1; |
| 123 | if (isClosed()) { |
| 124 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 125 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 126 | } |
| 127 | *format = mFormat; |
| 128 | return AMEDIA_OK; |
| 129 | } |
| 130 | |
| 131 | media_status_t |
| 132 | AImage::getNumPlanes(int32_t* numPlanes) const { |
| 133 | if (numPlanes == nullptr) { |
| 134 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 135 | } |
| 136 | *numPlanes = -1; |
| 137 | if (isClosed()) { |
| 138 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 139 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 140 | } |
| 141 | *numPlanes = mNumPlanes; |
| 142 | return AMEDIA_OK; |
| 143 | } |
| 144 | |
| 145 | media_status_t |
| 146 | AImage::getTimestamp(int64_t* timestamp) const { |
| 147 | if (timestamp == nullptr) { |
| 148 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 149 | } |
| 150 | *timestamp = -1; |
| 151 | if (isClosed()) { |
| 152 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 153 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 154 | } |
| 155 | *timestamp = mTimestamp; |
| 156 | return AMEDIA_OK; |
| 157 | } |
| 158 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 159 | media_status_t AImage::lockImage() { |
| 160 | if (mBuffer == nullptr || mBuffer->mGraphicBuffer == nullptr) { |
| 161 | LOG_ALWAYS_FATAL("%s: AImage %p has no buffer.", __FUNCTION__, this); |
| 162 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 163 | } |
| 164 | |
Jiwen 'Steve' Cai | e31bc87 | 2017-04-21 17:13:18 -0700 | [diff] [blame] | 165 | if ((mUsage & AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN) == 0) { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 166 | ALOGE("%s: AImage %p does not have any software read usage bits set, usage=%" PRIu64 "", |
Jiwen 'Steve' Cai | e31bc87 | 2017-04-21 17:13:18 -0700 | [diff] [blame] | 167 | __FUNCTION__, this, mUsage); |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 168 | return AMEDIA_IMGREADER_CANNOT_LOCK_IMAGE; |
| 169 | } |
| 170 | |
| 171 | if (mLockedBuffer != nullptr) { |
| 172 | // Return immediately if the image has already been locked. |
| 173 | return AMEDIA_OK; |
| 174 | } |
| 175 | |
| 176 | auto lockedBuffer = std::make_unique<CpuConsumer::LockedBuffer>(); |
| 177 | |
Jooyung Han | fe30459 | 2019-02-21 15:12:59 +0900 | [diff] [blame] | 178 | uint64_t grallocUsage = AHardwareBuffer_convertToGrallocUsageBits(mUsage); |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 179 | |
| 180 | status_t ret = |
Jiwen 'Steve' Cai | e31bc87 | 2017-04-21 17:13:18 -0700 | [diff] [blame] | 181 | lockImageFromBuffer(mBuffer, grallocUsage, mBuffer->mFence->dup(), lockedBuffer.get()); |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 182 | if (ret != OK) { |
| 183 | ALOGE("%s: AImage %p failed to lock, error=%d", __FUNCTION__, this, ret); |
| 184 | return AMEDIA_IMGREADER_CANNOT_LOCK_IMAGE; |
| 185 | } |
| 186 | |
| 187 | ALOGV("%s: Successfully locked the image %p.", __FUNCTION__, this); |
| 188 | mLockedBuffer = std::move(lockedBuffer); |
| 189 | |
| 190 | return AMEDIA_OK; |
| 191 | } |
| 192 | |
| 193 | media_status_t AImage::unlockImageIfLocked(int* fenceFd) { |
| 194 | if (fenceFd == nullptr) { |
| 195 | LOG_ALWAYS_FATAL("%s: fenceFd cannot be null.", __FUNCTION__); |
| 196 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 197 | } |
| 198 | |
| 199 | if (mBuffer == nullptr || mBuffer->mGraphicBuffer == nullptr) { |
| 200 | LOG_ALWAYS_FATAL("%s: AImage %p has no buffer.", __FUNCTION__, this); |
| 201 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 202 | } |
| 203 | |
| 204 | if (mLockedBuffer == nullptr) { |
| 205 | // This image hasn't been locked yet, no need to unlock. |
| 206 | *fenceFd = -1; |
| 207 | return AMEDIA_OK; |
| 208 | } |
| 209 | |
| 210 | // No fence by default. |
| 211 | int releaseFenceFd = -1; |
| 212 | status_t res = mBuffer->mGraphicBuffer->unlockAsync(&releaseFenceFd); |
| 213 | if (res != OK) { |
| 214 | ALOGE("%s unlock buffer failed on iamge %p.", __FUNCTION__, this); |
| 215 | *fenceFd = -1; |
| 216 | return AMEDIA_IMGREADER_CANNOT_UNLOCK_IMAGE; |
| 217 | } |
| 218 | |
| 219 | *fenceFd = releaseFenceFd; |
| 220 | return AMEDIA_OK; |
| 221 | } |
| 222 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 223 | media_status_t |
| 224 | AImage::getPlanePixelStride(int planeIdx, /*out*/int32_t* pixelStride) const { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 225 | if (mLockedBuffer == nullptr) { |
| 226 | ALOGE("%s: buffer not locked.", __FUNCTION__); |
| 227 | return AMEDIA_IMGREADER_IMAGE_NOT_LOCKED; |
| 228 | } |
| 229 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 230 | if (planeIdx < 0 || planeIdx >= mNumPlanes) { |
| 231 | ALOGE("Error: planeIdx %d out of bound [0,%d]", |
| 232 | planeIdx, mNumPlanes - 1); |
| 233 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 234 | } |
| 235 | if (pixelStride == nullptr) { |
| 236 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 237 | } |
| 238 | if (isClosed()) { |
| 239 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 240 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 241 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 242 | int32_t fmt = mLockedBuffer->flexFormat; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 243 | switch (fmt) { |
| 244 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 245 | *pixelStride = (planeIdx == 0) ? 1 : mLockedBuffer->chromaStep; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 246 | return AMEDIA_OK; |
| 247 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 248 | *pixelStride = (planeIdx == 0) ? 1 : 2; |
| 249 | return AMEDIA_OK; |
| 250 | case HAL_PIXEL_FORMAT_Y8: |
| 251 | *pixelStride = 1; |
| 252 | return AMEDIA_OK; |
| 253 | case HAL_PIXEL_FORMAT_YV12: |
| 254 | *pixelStride = 1; |
| 255 | return AMEDIA_OK; |
| 256 | case HAL_PIXEL_FORMAT_Y16: |
| 257 | case HAL_PIXEL_FORMAT_RAW16: |
| 258 | case HAL_PIXEL_FORMAT_RGB_565: |
| 259 | // Single plane 16bpp data. |
| 260 | *pixelStride = 2; |
| 261 | return AMEDIA_OK; |
| 262 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 263 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 264 | *pixelStride = 4; |
| 265 | return AMEDIA_OK; |
| 266 | case HAL_PIXEL_FORMAT_RGB_888: |
| 267 | // Single plane, 24bpp. |
| 268 | *pixelStride = 3; |
| 269 | return AMEDIA_OK; |
| 270 | case HAL_PIXEL_FORMAT_BLOB: |
| 271 | case HAL_PIXEL_FORMAT_RAW10: |
| 272 | case HAL_PIXEL_FORMAT_RAW12: |
| 273 | case HAL_PIXEL_FORMAT_RAW_OPAQUE: |
| 274 | // Blob is used for JPEG data, RAW10 and RAW12 is used for 10-bit and 12-bit raw data, |
| 275 | // those are single plane data without pixel stride defined |
| 276 | return AMEDIA_ERROR_UNSUPPORTED; |
| 277 | default: |
| 278 | ALOGE("Pixel format: 0x%x is unsupported", fmt); |
| 279 | return AMEDIA_ERROR_UNSUPPORTED; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | media_status_t |
| 284 | AImage::getPlaneRowStride(int planeIdx, /*out*/int32_t* rowStride) const { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 285 | if (mLockedBuffer == nullptr) { |
| 286 | ALOGE("%s: buffer not locked.", __FUNCTION__); |
| 287 | return AMEDIA_IMGREADER_IMAGE_NOT_LOCKED; |
| 288 | } |
| 289 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 290 | if (planeIdx < 0 || planeIdx >= mNumPlanes) { |
| 291 | ALOGE("Error: planeIdx %d out of bound [0,%d]", |
| 292 | planeIdx, mNumPlanes - 1); |
| 293 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 294 | } |
| 295 | if (rowStride == nullptr) { |
| 296 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 297 | } |
| 298 | if (isClosed()) { |
| 299 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 300 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 301 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 302 | int32_t fmt = mLockedBuffer->flexFormat; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 303 | switch (fmt) { |
| 304 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 305 | *rowStride = (planeIdx == 0) ? mLockedBuffer->stride |
| 306 | : mLockedBuffer->chromaStride; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 307 | return AMEDIA_OK; |
| 308 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 309 | *rowStride = mLockedBuffer->width; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 310 | return AMEDIA_OK; |
| 311 | case HAL_PIXEL_FORMAT_YV12: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 312 | if (mLockedBuffer->stride % 16) { |
| 313 | ALOGE("Stride %d is not 16 pixel aligned!", mLockedBuffer->stride); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 314 | return AMEDIA_ERROR_UNKNOWN; |
| 315 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 316 | *rowStride = (planeIdx == 0) ? mLockedBuffer->stride |
| 317 | : ALIGN(mLockedBuffer->stride / 2, 16); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 318 | return AMEDIA_OK; |
| 319 | case HAL_PIXEL_FORMAT_RAW10: |
| 320 | case HAL_PIXEL_FORMAT_RAW12: |
| 321 | // RAW10 and RAW12 are used for 10-bit and 12-bit raw data, they are single plane |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 322 | *rowStride = mLockedBuffer->stride; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 323 | return AMEDIA_OK; |
| 324 | case HAL_PIXEL_FORMAT_Y8: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 325 | if (mLockedBuffer->stride % 16) { |
| 326 | ALOGE("Stride %d is not 16 pixel aligned!", |
| 327 | mLockedBuffer->stride); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 328 | return AMEDIA_ERROR_UNKNOWN; |
| 329 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 330 | *rowStride = mLockedBuffer->stride; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 331 | return AMEDIA_OK; |
| 332 | case HAL_PIXEL_FORMAT_Y16: |
| 333 | case HAL_PIXEL_FORMAT_RAW16: |
| 334 | // In native side, strides are specified in pixels, not in bytes. |
| 335 | // Single plane 16bpp bayer data. even width/height, |
| 336 | // row stride multiple of 16 pixels (32 bytes) |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 337 | if (mLockedBuffer->stride % 16) { |
| 338 | ALOGE("Stride %d is not 16 pixel aligned!", |
| 339 | mLockedBuffer->stride); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 340 | return AMEDIA_ERROR_UNKNOWN; |
| 341 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 342 | *rowStride = mLockedBuffer->stride * 2; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 343 | return AMEDIA_OK; |
| 344 | case HAL_PIXEL_FORMAT_RGB_565: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 345 | *rowStride = mLockedBuffer->stride * 2; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 346 | return AMEDIA_OK; |
| 347 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 348 | case HAL_PIXEL_FORMAT_RGBX_8888: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 349 | *rowStride = mLockedBuffer->stride * 4; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 350 | return AMEDIA_OK; |
| 351 | case HAL_PIXEL_FORMAT_RGB_888: |
| 352 | // Single plane, 24bpp. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 353 | *rowStride = mLockedBuffer->stride * 3; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 354 | return AMEDIA_OK; |
| 355 | case HAL_PIXEL_FORMAT_BLOB: |
| 356 | case HAL_PIXEL_FORMAT_RAW_OPAQUE: |
| 357 | // Blob is used for JPEG/Raw opaque data. It is single plane and has 0 row stride and |
| 358 | // no row stride defined |
| 359 | return AMEDIA_ERROR_UNSUPPORTED; |
| 360 | default: |
| 361 | ALOGE("%s Pixel format: 0x%x is unsupported", __FUNCTION__, fmt); |
| 362 | return AMEDIA_ERROR_UNSUPPORTED; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | uint32_t |
| 367 | AImage::getJpegSize() const { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 368 | if (mLockedBuffer == nullptr) { |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 369 | LOG_ALWAYS_FATAL("Error: buffer is null"); |
| 370 | } |
| 371 | |
| 372 | uint32_t size = 0; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 373 | uint32_t width = mLockedBuffer->width; |
| 374 | uint8_t* jpegBuffer = mLockedBuffer->data; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 375 | |
| 376 | // First check for JPEG transport header at the end of the buffer |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame^] | 377 | uint8_t* header = jpegBuffer + (width - sizeof(struct camera3_jpeg_blob_v2)); |
| 378 | struct camera3_jpeg_blob_v2* blob = (struct camera3_jpeg_blob_v2*)(header); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 379 | if (blob->jpeg_blob_id == CAMERA3_JPEG_BLOB_ID) { |
| 380 | size = blob->jpeg_size; |
| 381 | ALOGV("%s: Jpeg size = %d", __FUNCTION__, size); |
| 382 | } |
| 383 | |
| 384 | // failed to find size, default to whole buffer |
| 385 | if (size == 0) { |
| 386 | /* |
| 387 | * This is a problem because not including the JPEG header |
| 388 | * means that in certain rare situations a regular JPEG blob |
| 389 | * will be misidentified as having a header, in which case |
| 390 | * we will get a garbage size value. |
| 391 | */ |
| 392 | ALOGW("%s: No JPEG header detected, defaulting to size=width=%d", |
| 393 | __FUNCTION__, width); |
| 394 | size = width; |
| 395 | } |
| 396 | |
| 397 | return size; |
| 398 | } |
| 399 | |
| 400 | media_status_t |
| 401 | AImage::getPlaneData(int planeIdx,/*out*/uint8_t** data, /*out*/int* dataLength) const { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 402 | if (mLockedBuffer == nullptr) { |
| 403 | ALOGE("%s: buffer not locked.", __FUNCTION__); |
| 404 | return AMEDIA_IMGREADER_IMAGE_NOT_LOCKED; |
| 405 | } |
| 406 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 407 | if (planeIdx < 0 || planeIdx >= mNumPlanes) { |
| 408 | ALOGE("Error: planeIdx %d out of bound [0,%d]", |
| 409 | planeIdx, mNumPlanes - 1); |
| 410 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 411 | } |
| 412 | if (data == nullptr || dataLength == nullptr) { |
| 413 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 414 | } |
| 415 | if (isClosed()) { |
| 416 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 417 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 418 | } |
| 419 | |
| 420 | uint32_t dataSize, ySize, cSize, cStride; |
| 421 | uint8_t* cb = nullptr; |
| 422 | uint8_t* cr = nullptr; |
| 423 | uint8_t* pData = nullptr; |
| 424 | int bytesPerPixel = 0; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 425 | int32_t fmt = mLockedBuffer->flexFormat; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 426 | |
| 427 | switch (fmt) { |
| 428 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 429 | pData = (planeIdx == 0) ? mLockedBuffer->data |
| 430 | : (planeIdx == 1) ? mLockedBuffer->dataCb |
| 431 | : mLockedBuffer->dataCr; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 432 | // only map until last pixel |
| 433 | if (planeIdx == 0) { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 434 | dataSize = mLockedBuffer->stride * (mLockedBuffer->height - 1) + |
| 435 | mLockedBuffer->width; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 436 | } else { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 437 | dataSize = |
| 438 | mLockedBuffer->chromaStride * |
| 439 | (mLockedBuffer->height / 2 - 1) + |
| 440 | mLockedBuffer->chromaStep * (mLockedBuffer->width / 2 - 1) + |
| 441 | 1; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 442 | } |
| 443 | break; |
| 444 | // NV21 |
| 445 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 446 | cr = mLockedBuffer->data + |
| 447 | (mLockedBuffer->stride * mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 448 | cb = cr + 1; |
| 449 | // only map until last pixel |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 450 | ySize = mLockedBuffer->width * (mLockedBuffer->height - 1) + |
| 451 | mLockedBuffer->width; |
| 452 | cSize = mLockedBuffer->width * (mLockedBuffer->height / 2 - 1) + |
| 453 | mLockedBuffer->width - 1; |
| 454 | pData = (planeIdx == 0) ? mLockedBuffer->data |
| 455 | : (planeIdx == 1) ? cb : cr; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 456 | dataSize = (planeIdx == 0) ? ySize : cSize; |
| 457 | break; |
| 458 | case HAL_PIXEL_FORMAT_YV12: |
| 459 | // Y and C stride need to be 16 pixel aligned. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 460 | if (mLockedBuffer->stride % 16) { |
| 461 | ALOGE("Stride %d is not 16 pixel aligned!", |
| 462 | mLockedBuffer->stride); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 463 | return AMEDIA_ERROR_UNKNOWN; |
| 464 | } |
| 465 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 466 | ySize = mLockedBuffer->stride * mLockedBuffer->height; |
| 467 | cStride = ALIGN(mLockedBuffer->stride / 2, 16); |
| 468 | cr = mLockedBuffer->data + ySize; |
| 469 | cSize = cStride * mLockedBuffer->height / 2; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 470 | cb = cr + cSize; |
| 471 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 472 | pData = (planeIdx == 0) ? mLockedBuffer->data |
| 473 | : (planeIdx == 1) ? cb : cr; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 474 | dataSize = (planeIdx == 0) ? ySize : cSize; |
| 475 | break; |
| 476 | case HAL_PIXEL_FORMAT_Y8: |
| 477 | // Single plane, 8bpp. |
| 478 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 479 | pData = mLockedBuffer->data; |
| 480 | dataSize = mLockedBuffer->stride * mLockedBuffer->height; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 481 | break; |
| 482 | case HAL_PIXEL_FORMAT_Y16: |
| 483 | bytesPerPixel = 2; |
| 484 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 485 | pData = mLockedBuffer->data; |
| 486 | dataSize = |
| 487 | mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 488 | break; |
| 489 | case HAL_PIXEL_FORMAT_BLOB: |
| 490 | // Used for JPEG data, height must be 1, width == size, single plane. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 491 | if (mLockedBuffer->height != 1) { |
| 492 | ALOGE("Jpeg should have height value one but got %d", |
| 493 | mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 494 | return AMEDIA_ERROR_UNKNOWN; |
| 495 | } |
| 496 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 497 | pData = mLockedBuffer->data; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 498 | dataSize = getJpegSize(); |
| 499 | break; |
| 500 | case HAL_PIXEL_FORMAT_RAW16: |
| 501 | // Single plane 16bpp bayer data. |
| 502 | bytesPerPixel = 2; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 503 | pData = mLockedBuffer->data; |
| 504 | dataSize = |
| 505 | mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 506 | break; |
| 507 | case HAL_PIXEL_FORMAT_RAW_OPAQUE: |
| 508 | // Used for RAW_OPAQUE data, height must be 1, width == size, single plane. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 509 | if (mLockedBuffer->height != 1) { |
| 510 | ALOGE("RAW_OPAQUE should have height value one but got %d", |
| 511 | mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 512 | return AMEDIA_ERROR_UNKNOWN; |
| 513 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 514 | pData = mLockedBuffer->data; |
| 515 | dataSize = mLockedBuffer->width; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 516 | break; |
| 517 | case HAL_PIXEL_FORMAT_RAW10: |
| 518 | // Single plane 10bpp bayer data. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 519 | if (mLockedBuffer->width % 4) { |
| 520 | ALOGE("Width is not multiple of 4 %d", mLockedBuffer->width); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 521 | return AMEDIA_ERROR_UNKNOWN; |
| 522 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 523 | if (mLockedBuffer->height % 2) { |
| 524 | ALOGE("Height is not multiple of 2 %d", mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 525 | return AMEDIA_ERROR_UNKNOWN; |
| 526 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 527 | if (mLockedBuffer->stride < (mLockedBuffer->width * 10 / 8)) { |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 528 | ALOGE("stride (%d) should be at least %d", |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 529 | mLockedBuffer->stride, mLockedBuffer->width * 10 / 8); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 530 | return AMEDIA_ERROR_UNKNOWN; |
| 531 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 532 | pData = mLockedBuffer->data; |
| 533 | dataSize = mLockedBuffer->stride * mLockedBuffer->height; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 534 | break; |
| 535 | case HAL_PIXEL_FORMAT_RAW12: |
| 536 | // Single plane 10bpp bayer data. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 537 | if (mLockedBuffer->width % 4) { |
| 538 | ALOGE("Width is not multiple of 4 %d", mLockedBuffer->width); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 539 | return AMEDIA_ERROR_UNKNOWN; |
| 540 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 541 | if (mLockedBuffer->height % 2) { |
| 542 | ALOGE("Height is not multiple of 2 %d", mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 543 | return AMEDIA_ERROR_UNKNOWN; |
| 544 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 545 | if (mLockedBuffer->stride < (mLockedBuffer->width * 12 / 8)) { |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 546 | ALOGE("stride (%d) should be at least %d", |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 547 | mLockedBuffer->stride, mLockedBuffer->width * 12 / 8); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 548 | return AMEDIA_ERROR_UNKNOWN; |
| 549 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 550 | pData = mLockedBuffer->data; |
| 551 | dataSize = mLockedBuffer->stride * mLockedBuffer->height; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 552 | break; |
| 553 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 554 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 555 | // Single plane, 32bpp. |
| 556 | bytesPerPixel = 4; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 557 | pData = mLockedBuffer->data; |
| 558 | dataSize = |
| 559 | mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 560 | break; |
| 561 | case HAL_PIXEL_FORMAT_RGB_565: |
| 562 | // Single plane, 16bpp. |
| 563 | bytesPerPixel = 2; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 564 | pData = mLockedBuffer->data; |
| 565 | dataSize = |
| 566 | mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 567 | break; |
| 568 | case HAL_PIXEL_FORMAT_RGB_888: |
| 569 | // Single plane, 24bpp. |
| 570 | bytesPerPixel = 3; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 571 | pData = mLockedBuffer->data; |
| 572 | dataSize = mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 573 | break; |
| 574 | default: |
| 575 | ALOGE("Pixel format: 0x%x is unsupported", fmt); |
| 576 | return AMEDIA_ERROR_UNSUPPORTED; |
| 577 | } |
| 578 | |
| 579 | *data = pData; |
| 580 | *dataLength = dataSize; |
| 581 | return AMEDIA_OK; |
| 582 | } |
| 583 | |
Jiwen 'Steve' Cai | e168996 | 2017-02-20 16:59:05 -0800 | [diff] [blame] | 584 | media_status_t |
| 585 | AImage::getHardwareBuffer(/*out*/AHardwareBuffer** buffer) const { |
| 586 | if (mBuffer == nullptr || mBuffer->mGraphicBuffer == nullptr) { |
| 587 | ALOGE("%s: AImage %p has no buffer.", __FUNCTION__, this); |
| 588 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 589 | } |
| 590 | |
| 591 | // TODO(jwcai) Someone from Android graphics team stating this should just be a static_cast. |
| 592 | *buffer = reinterpret_cast<AHardwareBuffer*>(mBuffer->mGraphicBuffer.get()); |
| 593 | return AMEDIA_OK; |
| 594 | } |
| 595 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 596 | EXPORT |
| 597 | void AImage_delete(AImage* image) { |
| 598 | ALOGV("%s", __FUNCTION__); |
Jiwen 'Steve' Cai | e168996 | 2017-02-20 16:59:05 -0800 | [diff] [blame] | 599 | AImage_deleteAsync(image, -1); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | EXPORT |
| 604 | void AImage_deleteAsync(AImage* image, int releaseFenceFd) { |
| 605 | ALOGV("%s", __FUNCTION__); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 606 | if (image != nullptr) { |
Yin-Chia Yeh | b29fce7 | 2017-11-06 17:16:22 -0800 | [diff] [blame] | 607 | image->lockReader(); |
Jiwen 'Steve' Cai | e168996 | 2017-02-20 16:59:05 -0800 | [diff] [blame] | 608 | image->close(releaseFenceFd); |
Yin-Chia Yeh | b29fce7 | 2017-11-06 17:16:22 -0800 | [diff] [blame] | 609 | image->unlockReader(); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 610 | if (!image->isClosed()) { |
| 611 | LOG_ALWAYS_FATAL("Image close failed!"); |
| 612 | } |
| 613 | image->free(); |
| 614 | } |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | EXPORT |
| 619 | media_status_t AImage_getWidth(const AImage* image, /*out*/int32_t* width) { |
| 620 | ALOGV("%s", __FUNCTION__); |
| 621 | if (image == nullptr || width == nullptr) { |
| 622 | ALOGE("%s: bad argument. image %p width %p", |
| 623 | __FUNCTION__, image, width); |
| 624 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 625 | } |
| 626 | return image->getWidth(width); |
| 627 | } |
| 628 | |
| 629 | EXPORT |
| 630 | media_status_t AImage_getHeight(const AImage* image, /*out*/int32_t* height) { |
| 631 | ALOGV("%s", __FUNCTION__); |
| 632 | if (image == nullptr || height == nullptr) { |
| 633 | ALOGE("%s: bad argument. image %p height %p", |
| 634 | __FUNCTION__, image, height); |
| 635 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 636 | } |
| 637 | return image->getHeight(height); |
| 638 | } |
| 639 | |
| 640 | EXPORT |
| 641 | media_status_t AImage_getFormat(const AImage* image, /*out*/int32_t* format) { |
| 642 | ALOGV("%s", __FUNCTION__); |
| 643 | if (image == nullptr || format == nullptr) { |
| 644 | ALOGE("%s: bad argument. image %p format %p", |
| 645 | __FUNCTION__, image, format); |
| 646 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 647 | } |
| 648 | return image->getFormat(format); |
| 649 | } |
| 650 | |
| 651 | EXPORT |
| 652 | media_status_t AImage_getCropRect(const AImage* image, /*out*/AImageCropRect* rect) { |
| 653 | ALOGV("%s", __FUNCTION__); |
| 654 | if (image == nullptr || rect == nullptr) { |
| 655 | ALOGE("%s: bad argument. image %p rect %p", |
| 656 | __FUNCTION__, image, rect); |
| 657 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 658 | } |
| 659 | // For now AImage only supports camera outputs where cropRect is always full window |
| 660 | int32_t width = -1; |
| 661 | media_status_t ret = image->getWidth(&width); |
| 662 | if (ret != AMEDIA_OK) { |
| 663 | return ret; |
| 664 | } |
| 665 | int32_t height = -1; |
| 666 | ret = image->getHeight(&height); |
| 667 | if (ret != AMEDIA_OK) { |
| 668 | return ret; |
| 669 | } |
| 670 | rect->left = 0; |
| 671 | rect->top = 0; |
| 672 | rect->right = width; |
| 673 | rect->bottom = height; |
| 674 | return AMEDIA_OK; |
| 675 | } |
| 676 | |
| 677 | EXPORT |
| 678 | media_status_t AImage_getTimestamp(const AImage* image, /*out*/int64_t* timestampNs) { |
| 679 | ALOGV("%s", __FUNCTION__); |
| 680 | if (image == nullptr || timestampNs == nullptr) { |
| 681 | ALOGE("%s: bad argument. image %p timestampNs %p", |
| 682 | __FUNCTION__, image, timestampNs); |
| 683 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 684 | } |
| 685 | return image->getTimestamp(timestampNs); |
| 686 | } |
| 687 | |
| 688 | EXPORT |
| 689 | media_status_t AImage_getNumberOfPlanes(const AImage* image, /*out*/int32_t* numPlanes) { |
| 690 | ALOGV("%s", __FUNCTION__); |
| 691 | if (image == nullptr || numPlanes == nullptr) { |
| 692 | ALOGE("%s: bad argument. image %p numPlanes %p", |
| 693 | __FUNCTION__, image, numPlanes); |
| 694 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 695 | } |
| 696 | return image->getNumPlanes(numPlanes); |
| 697 | } |
| 698 | |
| 699 | EXPORT |
| 700 | media_status_t AImage_getPlanePixelStride( |
| 701 | const AImage* image, int planeIdx, /*out*/int32_t* pixelStride) { |
| 702 | ALOGV("%s", __FUNCTION__); |
| 703 | if (image == nullptr || pixelStride == nullptr) { |
| 704 | ALOGE("%s: bad argument. image %p pixelStride %p", |
| 705 | __FUNCTION__, image, pixelStride); |
| 706 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 707 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 708 | media_status_t ret = const_cast<AImage*>(image)->lockImage(); |
| 709 | if (ret != AMEDIA_OK) { |
| 710 | ALOGE("%s: failed to lock buffer for CPU access. image %p, error=%d.", |
| 711 | __FUNCTION__, image, ret); |
| 712 | return ret; |
| 713 | } |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 714 | return image->getPlanePixelStride(planeIdx, pixelStride); |
| 715 | } |
| 716 | |
| 717 | EXPORT |
| 718 | media_status_t AImage_getPlaneRowStride( |
| 719 | const AImage* image, int planeIdx, /*out*/int32_t* rowStride) { |
| 720 | ALOGV("%s", __FUNCTION__); |
| 721 | if (image == nullptr || rowStride == nullptr) { |
| 722 | ALOGE("%s: bad argument. image %p rowStride %p", |
| 723 | __FUNCTION__, image, rowStride); |
| 724 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 725 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 726 | media_status_t ret = const_cast<AImage*>(image)->lockImage(); |
| 727 | if (ret != AMEDIA_OK) { |
| 728 | ALOGE("%s: failed to lock buffer for CPU access. image %p, error=%d.", |
| 729 | __FUNCTION__, image, ret); |
| 730 | return ret; |
| 731 | } |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 732 | return image->getPlaneRowStride(planeIdx, rowStride); |
| 733 | } |
| 734 | |
| 735 | EXPORT |
| 736 | media_status_t AImage_getPlaneData( |
| 737 | const AImage* image, int planeIdx, |
| 738 | /*out*/uint8_t** data, /*out*/int* dataLength) { |
| 739 | ALOGV("%s", __FUNCTION__); |
| 740 | if (image == nullptr || data == nullptr || dataLength == nullptr) { |
| 741 | ALOGE("%s: bad argument. image %p data %p dataLength %p", |
| 742 | __FUNCTION__, image, data, dataLength); |
| 743 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 744 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame] | 745 | media_status_t ret = const_cast<AImage*>(image)->lockImage(); |
| 746 | if (ret != AMEDIA_OK) { |
| 747 | ALOGE("%s: failed to lock buffer for CPU access. image %p, error=%d.", |
| 748 | __FUNCTION__, image, ret); |
| 749 | return ret; |
| 750 | } |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 751 | return image->getPlaneData(planeIdx, data, dataLength); |
| 752 | } |
Jiwen 'Steve' Cai | e168996 | 2017-02-20 16:59:05 -0800 | [diff] [blame] | 753 | |
| 754 | EXPORT |
| 755 | media_status_t AImage_getHardwareBuffer( |
| 756 | const AImage* image, /*out*/AHardwareBuffer** buffer) { |
| 757 | ALOGV("%s", __FUNCTION__); |
| 758 | |
| 759 | if (image == nullptr || buffer == nullptr) { |
| 760 | ALOGE("%s: bad argument. image %p buffer %p", __FUNCTION__, image, buffer); |
| 761 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 762 | } |
| 763 | return image->getHardwareBuffer(buffer); |
| 764 | } |