diff options
author | Zhijun He <zhijunhe@google.com> | 2015-07-07 01:07:16 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-07-07 01:07:17 +0000 |
commit | 1f52fce6b9535b0c83e703d0141f2f80a4a2ebfa (patch) | |
tree | 6bfda2494a988506735854f5eb4796bd583c5e2b /media/java/android | |
parent | fe45b29a1cce45b47f648cbff2b7d65890f1d638 (diff) | |
parent | bc0b63667960896929fdfcc0d95491ff8f1ef00a (diff) | |
download | frameworks_base-1f52fce6b9535b0c83e703d0141f2f80a4a2ebfa.zip frameworks_base-1f52fce6b9535b0c83e703d0141f2f80a4a2ebfa.tar.gz frameworks_base-1f52fce6b9535b0c83e703d0141f2f80a4a2ebfa.tar.bz2 |
Merge "ImageUtils: fix image copy issue" into mnc-dev
Diffstat (limited to 'media/java/android')
-rw-r--r-- | media/java/android/media/ImageUtils.java | 84 |
1 files changed, 82 insertions, 2 deletions
diff --git a/media/java/android/media/ImageUtils.java b/media/java/android/media/ImageUtils.java index 2763d1d..ba3949a 100644 --- a/media/java/android/media/ImageUtils.java +++ b/media/java/android/media/ImageUtils.java @@ -21,6 +21,8 @@ import android.graphics.PixelFormat; import android.media.Image.Plane; import android.util.Size; +import libcore.io.Memory; + import java.nio.ByteBuffer; /** @@ -109,12 +111,50 @@ class ImageUtils { ByteBuffer srcBuffer = null; ByteBuffer dstBuffer = null; for (int i = 0; i < srcPlanes.length; i++) { + int srcRowStride = srcPlanes[i].getRowStride(); + int dstRowStride = dstPlanes[i].getRowStride(); srcBuffer = srcPlanes[i].getBuffer(); + dstBuffer = dstPlanes[i].getBuffer(); + if (!(srcBuffer.isDirect() && dstBuffer.isDirect())) { + throw new IllegalArgumentException("Source and destination ByteBuffers must be" + + " direct byteBuffer!"); + } + if (srcPlanes[i].getPixelStride() != dstPlanes[i].getPixelStride()) { + throw new IllegalArgumentException("Source plane image pixel stride " + + srcPlanes[i].getPixelStride() + + " must be same as destination image pixel stride " + + dstPlanes[i].getPixelStride()); + } + int srcPos = srcBuffer.position(); srcBuffer.rewind(); - dstBuffer = dstPlanes[i].getBuffer(); dstBuffer.rewind(); - dstBuffer.put(srcBuffer); + if (srcRowStride == dstRowStride) { + // Fast path, just copy the content if the byteBuffer all together. + dstBuffer.put(srcBuffer); + } else { + // Source and destination images may have different alignment requirements, + // therefore may have different strides. Copy row by row for such case. + int srcOffset = srcBuffer.position(); + int dstOffset = dstBuffer.position(); + Size effectivePlaneSize = getEffectivePlaneSizeForImage(src, i); + int srcByteCount = effectivePlaneSize.getWidth() * srcPlanes[i].getPixelStride(); + for (int row = 0; row < effectivePlaneSize.getHeight(); row++) { + if (row == effectivePlaneSize.getHeight() - 1) { + // Special case for NV21 backed YUV420_888: need handle the last row + // carefully to avoid memory corruption. Check if we have enough bytes to + // copy. + int remainingBytes = srcBuffer.remaining() - srcOffset; + if (srcByteCount > remainingBytes) { + srcByteCount = remainingBytes; + } + } + directByteBufferCopy(srcBuffer, srcOffset, dstBuffer, dstOffset, srcByteCount); + srcOffset += srcRowStride; + dstOffset += dstRowStride; + } + } + srcBuffer.position(srcPos); dstBuffer.rewind(); } @@ -175,4 +215,44 @@ class ImageUtils { return (int)(width * height * estimatedBytePerPixel * numImages); } + + private static Size getEffectivePlaneSizeForImage(Image image, int planeIdx) { + switch (image.getFormat()) { + case ImageFormat.YV12: + case ImageFormat.YUV_420_888: + case ImageFormat.NV21: + if (planeIdx == 0) { + return new Size(image.getWidth(), image.getHeight()); + } else { + return new Size(image.getWidth() / 2, image.getHeight() / 2); + } + case ImageFormat.NV16: + if (planeIdx == 0) { + return new Size(image.getWidth(), image.getHeight()); + } else { + return new Size(image.getWidth(), image.getHeight() / 2); + } + case PixelFormat.RGB_565: + case PixelFormat.RGBA_8888: + case PixelFormat.RGBX_8888: + case PixelFormat.RGB_888: + case ImageFormat.JPEG: + case ImageFormat.YUY2: + case ImageFormat.Y8: + case ImageFormat.Y16: + case ImageFormat.RAW_SENSOR: + case ImageFormat.RAW10: + return new Size(image.getWidth(), image.getHeight()); + case ImageFormat.PRIVATE: + return new Size(0, 0); + default: + throw new UnsupportedOperationException( + String.format("Invalid image format %d", image.getFormat())); + } + } + + private static void directByteBufferCopy(ByteBuffer srcBuffer, int srcOffset, + ByteBuffer dstBuffer, int dstOffset, int srcByteCount) { + Memory.memmove(dstBuffer, dstOffset, srcBuffer, srcOffset, srcByteCount); + } } |