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> |
| 26 | #include <android_runtime/android_hardware_HardwareBuffer.h> |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | #include "hardware/camera3.h" |
| 29 | |
| 30 | using namespace android; |
| 31 | |
| 32 | #define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) ) |
| 33 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 34 | AImage::AImage(AImageReader* reader, int32_t format, uint64_t usage, |
| 35 | BufferItem* buffer, int64_t timestamp, |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 36 | int32_t width, int32_t height, int32_t numPlanes) : |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 37 | mReader(reader), mFormat(format), mUsage(usage), |
| 38 | mBuffer(buffer), mLockedBuffer(nullptr), mTimestamp(timestamp), |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 39 | mWidth(width), mHeight(height), mNumPlanes(numPlanes) { |
| 40 | } |
| 41 | |
| 42 | // Can only be called by free() with mLock hold |
| 43 | AImage::~AImage() { |
| 44 | if (!mIsClosed) { |
| 45 | LOG_ALWAYS_FATAL( |
| 46 | "Error: AImage %p is deleted before returning buffer to AImageReader!", this); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | bool |
| 51 | AImage::isClosed() const { |
| 52 | Mutex::Autolock _l(mLock); |
| 53 | return mIsClosed; |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | AImage::close() { |
| 58 | Mutex::Autolock _l(mLock); |
| 59 | if (mIsClosed) { |
| 60 | return; |
| 61 | } |
| 62 | sp<AImageReader> reader = mReader.promote(); |
| 63 | if (reader == nullptr) { |
| 64 | LOG_ALWAYS_FATAL("Error: AImage not closed before AImageReader close!"); |
| 65 | return; |
| 66 | } |
| 67 | reader->releaseImageLocked(this); |
| 68 | // Should have been set to nullptr in releaseImageLocked |
| 69 | // Set to nullptr here for extra safety only |
| 70 | mBuffer = nullptr; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 71 | mLockedBuffer = nullptr; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 72 | mIsClosed = true; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | AImage::free() { |
| 77 | if (!isClosed()) { |
| 78 | ALOGE("Cannot free AImage before close!"); |
| 79 | return; |
| 80 | } |
| 81 | Mutex::Autolock _l(mLock); |
| 82 | delete this; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | AImage::lockReader() const { |
| 87 | sp<AImageReader> reader = mReader.promote(); |
| 88 | if (reader == nullptr) { |
| 89 | // Reader has been closed |
| 90 | return; |
| 91 | } |
| 92 | reader->mLock.lock(); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | AImage::unlockReader() const { |
| 97 | sp<AImageReader> reader = mReader.promote(); |
| 98 | if (reader == nullptr) { |
| 99 | // Reader has been closed |
| 100 | return; |
| 101 | } |
| 102 | reader->mLock.unlock(); |
| 103 | } |
| 104 | |
| 105 | media_status_t |
| 106 | AImage::getWidth(int32_t* width) const { |
| 107 | if (width == nullptr) { |
| 108 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 109 | } |
| 110 | *width = -1; |
| 111 | if (isClosed()) { |
| 112 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 113 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 114 | } |
| 115 | *width = mWidth; |
| 116 | return AMEDIA_OK; |
| 117 | } |
| 118 | |
| 119 | media_status_t |
| 120 | AImage::getHeight(int32_t* height) const { |
| 121 | if (height == nullptr) { |
| 122 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 123 | } |
| 124 | *height = -1; |
| 125 | if (isClosed()) { |
| 126 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 127 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 128 | } |
| 129 | *height = mHeight; |
| 130 | return AMEDIA_OK; |
| 131 | } |
| 132 | |
| 133 | media_status_t |
| 134 | AImage::getFormat(int32_t* format) const { |
| 135 | if (format == nullptr) { |
| 136 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 137 | } |
| 138 | *format = -1; |
| 139 | if (isClosed()) { |
| 140 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 141 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 142 | } |
| 143 | *format = mFormat; |
| 144 | return AMEDIA_OK; |
| 145 | } |
| 146 | |
| 147 | media_status_t |
| 148 | AImage::getNumPlanes(int32_t* numPlanes) const { |
| 149 | if (numPlanes == nullptr) { |
| 150 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 151 | } |
| 152 | *numPlanes = -1; |
| 153 | if (isClosed()) { |
| 154 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 155 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 156 | } |
| 157 | *numPlanes = mNumPlanes; |
| 158 | return AMEDIA_OK; |
| 159 | } |
| 160 | |
| 161 | media_status_t |
| 162 | AImage::getTimestamp(int64_t* timestamp) const { |
| 163 | if (timestamp == nullptr) { |
| 164 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 165 | } |
| 166 | *timestamp = -1; |
| 167 | if (isClosed()) { |
| 168 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 169 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 170 | } |
| 171 | *timestamp = mTimestamp; |
| 172 | return AMEDIA_OK; |
| 173 | } |
| 174 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 175 | media_status_t AImage::lockImage() { |
| 176 | if (mBuffer == nullptr || mBuffer->mGraphicBuffer == nullptr) { |
| 177 | LOG_ALWAYS_FATAL("%s: AImage %p has no buffer.", __FUNCTION__, this); |
| 178 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 179 | } |
| 180 | |
| 181 | if ((mUsage & AHARDWAREBUFFER_USAGE0_CPU_READ_OFTEN) == 0) { |
| 182 | ALOGE("%s: AImage %p does not have any software read usage bits set, usage=%" PRIu64 "", |
| 183 | __FUNCTION__, this, mUsage); |
| 184 | return AMEDIA_IMGREADER_CANNOT_LOCK_IMAGE; |
| 185 | } |
| 186 | |
| 187 | if (mLockedBuffer != nullptr) { |
| 188 | // Return immediately if the image has already been locked. |
| 189 | return AMEDIA_OK; |
| 190 | } |
| 191 | |
| 192 | auto lockedBuffer = std::make_unique<CpuConsumer::LockedBuffer>(); |
| 193 | |
| 194 | uint64_t producerUsage; |
| 195 | uint64_t consumerUsage; |
| 196 | android_hardware_HardwareBuffer_convertToGrallocUsageBits( |
| 197 | &producerUsage, &consumerUsage, mUsage, 0); |
| 198 | |
| 199 | status_t ret = |
| 200 | lockImageFromBuffer(mBuffer, consumerUsage, mBuffer->mFence->dup(), lockedBuffer.get()); |
| 201 | if (ret != OK) { |
| 202 | ALOGE("%s: AImage %p failed to lock, error=%d", __FUNCTION__, this, ret); |
| 203 | return AMEDIA_IMGREADER_CANNOT_LOCK_IMAGE; |
| 204 | } |
| 205 | |
| 206 | ALOGV("%s: Successfully locked the image %p.", __FUNCTION__, this); |
| 207 | mLockedBuffer = std::move(lockedBuffer); |
| 208 | |
| 209 | return AMEDIA_OK; |
| 210 | } |
| 211 | |
| 212 | media_status_t AImage::unlockImageIfLocked(int* fenceFd) { |
| 213 | if (fenceFd == nullptr) { |
| 214 | LOG_ALWAYS_FATAL("%s: fenceFd cannot be null.", __FUNCTION__); |
| 215 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 216 | } |
| 217 | |
| 218 | if (mBuffer == nullptr || mBuffer->mGraphicBuffer == nullptr) { |
| 219 | LOG_ALWAYS_FATAL("%s: AImage %p has no buffer.", __FUNCTION__, this); |
| 220 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 221 | } |
| 222 | |
| 223 | if (mLockedBuffer == nullptr) { |
| 224 | // This image hasn't been locked yet, no need to unlock. |
| 225 | *fenceFd = -1; |
| 226 | return AMEDIA_OK; |
| 227 | } |
| 228 | |
| 229 | // No fence by default. |
| 230 | int releaseFenceFd = -1; |
| 231 | status_t res = mBuffer->mGraphicBuffer->unlockAsync(&releaseFenceFd); |
| 232 | if (res != OK) { |
| 233 | ALOGE("%s unlock buffer failed on iamge %p.", __FUNCTION__, this); |
| 234 | *fenceFd = -1; |
| 235 | return AMEDIA_IMGREADER_CANNOT_UNLOCK_IMAGE; |
| 236 | } |
| 237 | |
| 238 | *fenceFd = releaseFenceFd; |
| 239 | return AMEDIA_OK; |
| 240 | } |
| 241 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 242 | media_status_t |
| 243 | AImage::getPlanePixelStride(int planeIdx, /*out*/int32_t* pixelStride) const { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 244 | if (mLockedBuffer == nullptr) { |
| 245 | ALOGE("%s: buffer not locked.", __FUNCTION__); |
| 246 | return AMEDIA_IMGREADER_IMAGE_NOT_LOCKED; |
| 247 | } |
| 248 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 249 | if (planeIdx < 0 || planeIdx >= mNumPlanes) { |
| 250 | ALOGE("Error: planeIdx %d out of bound [0,%d]", |
| 251 | planeIdx, mNumPlanes - 1); |
| 252 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 253 | } |
| 254 | if (pixelStride == nullptr) { |
| 255 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 256 | } |
| 257 | if (isClosed()) { |
| 258 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 259 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 260 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 261 | int32_t fmt = mLockedBuffer->flexFormat; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 262 | switch (fmt) { |
| 263 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 264 | *pixelStride = (planeIdx == 0) ? 1 : mLockedBuffer->chromaStep; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 265 | return AMEDIA_OK; |
| 266 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 267 | *pixelStride = (planeIdx == 0) ? 1 : 2; |
| 268 | return AMEDIA_OK; |
| 269 | case HAL_PIXEL_FORMAT_Y8: |
| 270 | *pixelStride = 1; |
| 271 | return AMEDIA_OK; |
| 272 | case HAL_PIXEL_FORMAT_YV12: |
| 273 | *pixelStride = 1; |
| 274 | return AMEDIA_OK; |
| 275 | case HAL_PIXEL_FORMAT_Y16: |
| 276 | case HAL_PIXEL_FORMAT_RAW16: |
| 277 | case HAL_PIXEL_FORMAT_RGB_565: |
| 278 | // Single plane 16bpp data. |
| 279 | *pixelStride = 2; |
| 280 | return AMEDIA_OK; |
| 281 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 282 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 283 | *pixelStride = 4; |
| 284 | return AMEDIA_OK; |
| 285 | case HAL_PIXEL_FORMAT_RGB_888: |
| 286 | // Single plane, 24bpp. |
| 287 | *pixelStride = 3; |
| 288 | return AMEDIA_OK; |
| 289 | case HAL_PIXEL_FORMAT_BLOB: |
| 290 | case HAL_PIXEL_FORMAT_RAW10: |
| 291 | case HAL_PIXEL_FORMAT_RAW12: |
| 292 | case HAL_PIXEL_FORMAT_RAW_OPAQUE: |
| 293 | // Blob is used for JPEG data, RAW10 and RAW12 is used for 10-bit and 12-bit raw data, |
| 294 | // those are single plane data without pixel stride defined |
| 295 | return AMEDIA_ERROR_UNSUPPORTED; |
| 296 | default: |
| 297 | ALOGE("Pixel format: 0x%x is unsupported", fmt); |
| 298 | return AMEDIA_ERROR_UNSUPPORTED; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | media_status_t |
| 303 | AImage::getPlaneRowStride(int planeIdx, /*out*/int32_t* rowStride) const { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 304 | if (mLockedBuffer == nullptr) { |
| 305 | ALOGE("%s: buffer not locked.", __FUNCTION__); |
| 306 | return AMEDIA_IMGREADER_IMAGE_NOT_LOCKED; |
| 307 | } |
| 308 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 309 | if (planeIdx < 0 || planeIdx >= mNumPlanes) { |
| 310 | ALOGE("Error: planeIdx %d out of bound [0,%d]", |
| 311 | planeIdx, mNumPlanes - 1); |
| 312 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 313 | } |
| 314 | if (rowStride == nullptr) { |
| 315 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 316 | } |
| 317 | if (isClosed()) { |
| 318 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 319 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 320 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 321 | int32_t fmt = mLockedBuffer->flexFormat; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 322 | switch (fmt) { |
| 323 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 324 | *rowStride = (planeIdx == 0) ? mLockedBuffer->stride |
| 325 | : mLockedBuffer->chromaStride; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 326 | return AMEDIA_OK; |
| 327 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 328 | *rowStride = mLockedBuffer->width; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 329 | return AMEDIA_OK; |
| 330 | case HAL_PIXEL_FORMAT_YV12: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 331 | if (mLockedBuffer->stride % 16) { |
| 332 | ALOGE("Stride %d is not 16 pixel aligned!", mLockedBuffer->stride); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 333 | return AMEDIA_ERROR_UNKNOWN; |
| 334 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 335 | *rowStride = (planeIdx == 0) ? mLockedBuffer->stride |
| 336 | : ALIGN(mLockedBuffer->stride / 2, 16); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 337 | return AMEDIA_OK; |
| 338 | case HAL_PIXEL_FORMAT_RAW10: |
| 339 | case HAL_PIXEL_FORMAT_RAW12: |
| 340 | // 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^] | 341 | *rowStride = mLockedBuffer->stride; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 342 | return AMEDIA_OK; |
| 343 | case HAL_PIXEL_FORMAT_Y8: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 344 | if (mLockedBuffer->stride % 16) { |
| 345 | ALOGE("Stride %d is not 16 pixel aligned!", |
| 346 | mLockedBuffer->stride); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 347 | return AMEDIA_ERROR_UNKNOWN; |
| 348 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 349 | *rowStride = mLockedBuffer->stride; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 350 | return AMEDIA_OK; |
| 351 | case HAL_PIXEL_FORMAT_Y16: |
| 352 | case HAL_PIXEL_FORMAT_RAW16: |
| 353 | // In native side, strides are specified in pixels, not in bytes. |
| 354 | // Single plane 16bpp bayer data. even width/height, |
| 355 | // row stride multiple of 16 pixels (32 bytes) |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 356 | if (mLockedBuffer->stride % 16) { |
| 357 | ALOGE("Stride %d is not 16 pixel aligned!", |
| 358 | mLockedBuffer->stride); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 359 | return AMEDIA_ERROR_UNKNOWN; |
| 360 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 361 | *rowStride = mLockedBuffer->stride * 2; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 362 | return AMEDIA_OK; |
| 363 | case HAL_PIXEL_FORMAT_RGB_565: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 364 | *rowStride = mLockedBuffer->stride * 2; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 365 | return AMEDIA_OK; |
| 366 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 367 | case HAL_PIXEL_FORMAT_RGBX_8888: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 368 | *rowStride = mLockedBuffer->stride * 4; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 369 | return AMEDIA_OK; |
| 370 | case HAL_PIXEL_FORMAT_RGB_888: |
| 371 | // Single plane, 24bpp. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 372 | *rowStride = mLockedBuffer->stride * 3; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 373 | return AMEDIA_OK; |
| 374 | case HAL_PIXEL_FORMAT_BLOB: |
| 375 | case HAL_PIXEL_FORMAT_RAW_OPAQUE: |
| 376 | // Blob is used for JPEG/Raw opaque data. It is single plane and has 0 row stride and |
| 377 | // no row stride defined |
| 378 | return AMEDIA_ERROR_UNSUPPORTED; |
| 379 | default: |
| 380 | ALOGE("%s Pixel format: 0x%x is unsupported", __FUNCTION__, fmt); |
| 381 | return AMEDIA_ERROR_UNSUPPORTED; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | uint32_t |
| 386 | AImage::getJpegSize() const { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 387 | if (mLockedBuffer == nullptr) { |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 388 | LOG_ALWAYS_FATAL("Error: buffer is null"); |
| 389 | } |
| 390 | |
| 391 | uint32_t size = 0; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 392 | uint32_t width = mLockedBuffer->width; |
| 393 | uint8_t* jpegBuffer = mLockedBuffer->data; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 394 | |
| 395 | // First check for JPEG transport header at the end of the buffer |
| 396 | uint8_t* header = jpegBuffer + (width - sizeof(struct camera3_jpeg_blob)); |
| 397 | struct camera3_jpeg_blob* blob = (struct camera3_jpeg_blob*)(header); |
| 398 | if (blob->jpeg_blob_id == CAMERA3_JPEG_BLOB_ID) { |
| 399 | size = blob->jpeg_size; |
| 400 | ALOGV("%s: Jpeg size = %d", __FUNCTION__, size); |
| 401 | } |
| 402 | |
| 403 | // failed to find size, default to whole buffer |
| 404 | if (size == 0) { |
| 405 | /* |
| 406 | * This is a problem because not including the JPEG header |
| 407 | * means that in certain rare situations a regular JPEG blob |
| 408 | * will be misidentified as having a header, in which case |
| 409 | * we will get a garbage size value. |
| 410 | */ |
| 411 | ALOGW("%s: No JPEG header detected, defaulting to size=width=%d", |
| 412 | __FUNCTION__, width); |
| 413 | size = width; |
| 414 | } |
| 415 | |
| 416 | return size; |
| 417 | } |
| 418 | |
| 419 | media_status_t |
| 420 | 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^] | 421 | if (mLockedBuffer == nullptr) { |
| 422 | ALOGE("%s: buffer not locked.", __FUNCTION__); |
| 423 | return AMEDIA_IMGREADER_IMAGE_NOT_LOCKED; |
| 424 | } |
| 425 | |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 426 | if (planeIdx < 0 || planeIdx >= mNumPlanes) { |
| 427 | ALOGE("Error: planeIdx %d out of bound [0,%d]", |
| 428 | planeIdx, mNumPlanes - 1); |
| 429 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 430 | } |
| 431 | if (data == nullptr || dataLength == nullptr) { |
| 432 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 433 | } |
| 434 | if (isClosed()) { |
| 435 | ALOGE("%s: image %p has been closed!", __FUNCTION__, this); |
| 436 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 437 | } |
| 438 | |
| 439 | uint32_t dataSize, ySize, cSize, cStride; |
| 440 | uint8_t* cb = nullptr; |
| 441 | uint8_t* cr = nullptr; |
| 442 | uint8_t* pData = nullptr; |
| 443 | int bytesPerPixel = 0; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 444 | int32_t fmt = mLockedBuffer->flexFormat; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 445 | |
| 446 | switch (fmt) { |
| 447 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 448 | pData = (planeIdx == 0) ? mLockedBuffer->data |
| 449 | : (planeIdx == 1) ? mLockedBuffer->dataCb |
| 450 | : mLockedBuffer->dataCr; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 451 | // only map until last pixel |
| 452 | if (planeIdx == 0) { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 453 | dataSize = mLockedBuffer->stride * (mLockedBuffer->height - 1) + |
| 454 | mLockedBuffer->width; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 455 | } else { |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 456 | dataSize = |
| 457 | mLockedBuffer->chromaStride * |
| 458 | (mLockedBuffer->height / 2 - 1) + |
| 459 | mLockedBuffer->chromaStep * (mLockedBuffer->width / 2 - 1) + |
| 460 | 1; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 461 | } |
| 462 | break; |
| 463 | // NV21 |
| 464 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 465 | cr = mLockedBuffer->data + |
| 466 | (mLockedBuffer->stride * mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 467 | cb = cr + 1; |
| 468 | // only map until last pixel |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 469 | ySize = mLockedBuffer->width * (mLockedBuffer->height - 1) + |
| 470 | mLockedBuffer->width; |
| 471 | cSize = mLockedBuffer->width * (mLockedBuffer->height / 2 - 1) + |
| 472 | mLockedBuffer->width - 1; |
| 473 | pData = (planeIdx == 0) ? mLockedBuffer->data |
| 474 | : (planeIdx == 1) ? cb : cr; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 475 | dataSize = (planeIdx == 0) ? ySize : cSize; |
| 476 | break; |
| 477 | case HAL_PIXEL_FORMAT_YV12: |
| 478 | // Y and C stride need to be 16 pixel aligned. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 479 | if (mLockedBuffer->stride % 16) { |
| 480 | ALOGE("Stride %d is not 16 pixel aligned!", |
| 481 | mLockedBuffer->stride); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 482 | return AMEDIA_ERROR_UNKNOWN; |
| 483 | } |
| 484 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 485 | ySize = mLockedBuffer->stride * mLockedBuffer->height; |
| 486 | cStride = ALIGN(mLockedBuffer->stride / 2, 16); |
| 487 | cr = mLockedBuffer->data + ySize; |
| 488 | cSize = cStride * mLockedBuffer->height / 2; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 489 | cb = cr + cSize; |
| 490 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 491 | pData = (planeIdx == 0) ? mLockedBuffer->data |
| 492 | : (planeIdx == 1) ? cb : cr; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 493 | dataSize = (planeIdx == 0) ? ySize : cSize; |
| 494 | break; |
| 495 | case HAL_PIXEL_FORMAT_Y8: |
| 496 | // Single plane, 8bpp. |
| 497 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 498 | pData = mLockedBuffer->data; |
| 499 | dataSize = mLockedBuffer->stride * mLockedBuffer->height; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 500 | break; |
| 501 | case HAL_PIXEL_FORMAT_Y16: |
| 502 | bytesPerPixel = 2; |
| 503 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 504 | pData = mLockedBuffer->data; |
| 505 | dataSize = |
| 506 | mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 507 | break; |
| 508 | case HAL_PIXEL_FORMAT_BLOB: |
| 509 | // 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^] | 510 | if (mLockedBuffer->height != 1) { |
| 511 | ALOGE("Jpeg should have height value one but got %d", |
| 512 | mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 513 | return AMEDIA_ERROR_UNKNOWN; |
| 514 | } |
| 515 | |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 516 | pData = mLockedBuffer->data; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 517 | dataSize = getJpegSize(); |
| 518 | break; |
| 519 | case HAL_PIXEL_FORMAT_RAW16: |
| 520 | // Single plane 16bpp bayer data. |
| 521 | bytesPerPixel = 2; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 522 | pData = mLockedBuffer->data; |
| 523 | dataSize = |
| 524 | mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 525 | break; |
| 526 | case HAL_PIXEL_FORMAT_RAW_OPAQUE: |
| 527 | // 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^] | 528 | if (mLockedBuffer->height != 1) { |
| 529 | ALOGE("RAW_OPAQUE should have height value one but got %d", |
| 530 | mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 531 | return AMEDIA_ERROR_UNKNOWN; |
| 532 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 533 | pData = mLockedBuffer->data; |
| 534 | dataSize = mLockedBuffer->width; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 535 | break; |
| 536 | case HAL_PIXEL_FORMAT_RAW10: |
| 537 | // Single plane 10bpp bayer data. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 538 | if (mLockedBuffer->width % 4) { |
| 539 | ALOGE("Width is not multiple of 4 %d", mLockedBuffer->width); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 540 | return AMEDIA_ERROR_UNKNOWN; |
| 541 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 542 | if (mLockedBuffer->height % 2) { |
| 543 | ALOGE("Height is not multiple of 2 %d", mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 544 | return AMEDIA_ERROR_UNKNOWN; |
| 545 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 546 | if (mLockedBuffer->stride < (mLockedBuffer->width * 10 / 8)) { |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 547 | ALOGE("stride (%d) should be at least %d", |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 548 | mLockedBuffer->stride, mLockedBuffer->width * 10 / 8); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 549 | return AMEDIA_ERROR_UNKNOWN; |
| 550 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 551 | pData = mLockedBuffer->data; |
| 552 | dataSize = mLockedBuffer->stride * mLockedBuffer->height; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 553 | break; |
| 554 | case HAL_PIXEL_FORMAT_RAW12: |
| 555 | // Single plane 10bpp bayer data. |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 556 | if (mLockedBuffer->width % 4) { |
| 557 | ALOGE("Width is not multiple of 4 %d", mLockedBuffer->width); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 558 | return AMEDIA_ERROR_UNKNOWN; |
| 559 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 560 | if (mLockedBuffer->height % 2) { |
| 561 | ALOGE("Height is not multiple of 2 %d", mLockedBuffer->height); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 562 | return AMEDIA_ERROR_UNKNOWN; |
| 563 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 564 | if (mLockedBuffer->stride < (mLockedBuffer->width * 12 / 8)) { |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 565 | ALOGE("stride (%d) should be at least %d", |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 566 | mLockedBuffer->stride, mLockedBuffer->width * 12 / 8); |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 567 | return AMEDIA_ERROR_UNKNOWN; |
| 568 | } |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 569 | pData = mLockedBuffer->data; |
| 570 | dataSize = mLockedBuffer->stride * mLockedBuffer->height; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 571 | break; |
| 572 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 573 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 574 | // Single plane, 32bpp. |
| 575 | bytesPerPixel = 4; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 576 | pData = mLockedBuffer->data; |
| 577 | dataSize = |
| 578 | mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 579 | break; |
| 580 | case HAL_PIXEL_FORMAT_RGB_565: |
| 581 | // Single plane, 16bpp. |
| 582 | bytesPerPixel = 2; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 583 | pData = mLockedBuffer->data; |
| 584 | dataSize = |
| 585 | mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 586 | break; |
| 587 | case HAL_PIXEL_FORMAT_RGB_888: |
| 588 | // Single plane, 24bpp. |
| 589 | bytesPerPixel = 3; |
Jiwen 'Steve' Cai | 2f1a473 | 2017-02-04 17:31:55 -0800 | [diff] [blame^] | 590 | pData = mLockedBuffer->data; |
| 591 | dataSize = mLockedBuffer->stride * mLockedBuffer->height * bytesPerPixel; |
Yin-Chia Yeh | c360382 | 2016-01-18 22:11:19 -0800 | [diff] [blame] | 592 | break; |
| 593 | default: |
| 594 | ALOGE("Pixel format: 0x%x is unsupported", fmt); |
| 595 | return AMEDIA_ERROR_UNSUPPORTED; |
| 596 | } |
| 597 | |
| 598 | *data = pData; |
| 599 | *dataLength = dataSize; |
| 600 | return AMEDIA_OK; |
| 601 | } |
| 602 | |
| 603 | EXPORT |
| 604 | void AImage_delete(AImage* image) { |
| 605 | ALOGV("%s", __FUNCTION__); |
| 606 | if (image != nullptr) { |
| 607 | image->lockReader(); |
| 608 | image->close(); |
| 609 | image->unlockReader(); |
| 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 | } |