2012-08-21 14:22:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "GrallocImages.h"
|
2013-08-12 03:17:23 +04:00
|
|
|
#include <stddef.h> // for size_t
|
|
|
|
#include <stdint.h> // for int8_t, uint8_t, uint32_t, etc
|
|
|
|
#include "nsDebug.h" // for NS_WARNING, NS_PRECONDITION
|
|
|
|
#include "mozilla/layers/ImageBridgeChild.h"
|
2013-08-09 19:23:46 +04:00
|
|
|
#include "mozilla/layers/GrallocTextureClient.h"
|
|
|
|
#include "gfx2DGlue.h"
|
2014-03-07 01:58:02 +04:00
|
|
|
#include "YCbCrUtils.h" // for YCbCr conversions
|
2012-08-21 14:22:58 +04:00
|
|
|
|
2013-07-25 06:13:35 +04:00
|
|
|
#include <ColorConverter.h>
|
2014-03-31 19:24:28 +04:00
|
|
|
#include <OMX_IVCommon.h>
|
|
|
|
|
2013-07-25 06:13:35 +04:00
|
|
|
|
2012-08-21 14:22:58 +04:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
using namespace android;
|
|
|
|
|
2013-07-25 06:13:35 +04:00
|
|
|
#define ALIGN(x, align) ((x + align - 1) & ~(align - 1))
|
|
|
|
|
2012-08-21 14:22:58 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2014-11-25 03:54:33 +03:00
|
|
|
int32_t GrallocImage::sColorIdMap[] = {
|
2013-07-25 06:13:35 +04:00
|
|
|
HAL_PIXEL_FORMAT_YCbCr_420_P, OMX_COLOR_FormatYUV420Planar,
|
|
|
|
HAL_PIXEL_FORMAT_YCbCr_422_P, OMX_COLOR_FormatYUV422Planar,
|
|
|
|
HAL_PIXEL_FORMAT_YCbCr_420_SP, OMX_COLOR_FormatYUV420SemiPlanar,
|
|
|
|
HAL_PIXEL_FORMAT_YCrCb_420_SP, -1,
|
|
|
|
HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO, -1,
|
2013-09-12 13:40:26 +04:00
|
|
|
HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED, HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED,
|
2015-03-17 22:46:50 +03:00
|
|
|
HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS, HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS,
|
2013-07-25 06:13:35 +04:00
|
|
|
HAL_PIXEL_FORMAT_YV12, OMX_COLOR_FormatYUV420Planar,
|
2015-07-02 23:53:06 +03:00
|
|
|
HAL_PIXEL_FORMAT_RGBA_8888, -1,
|
2013-07-25 06:13:35 +04:00
|
|
|
0, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GraphicBufferAutoUnlock {
|
|
|
|
android::sp<GraphicBuffer> mGraphicBuffer;
|
|
|
|
|
|
|
|
GraphicBufferAutoUnlock(android::sp<GraphicBuffer>& aGraphicBuffer)
|
|
|
|
: mGraphicBuffer(aGraphicBuffer) { }
|
|
|
|
|
|
|
|
~GraphicBufferAutoUnlock() { mGraphicBuffer->unlock(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
GrallocImage::GrallocImage()
|
2015-11-06 21:55:31 +03:00
|
|
|
: RecyclingPlanarYCbCrImage(nullptr)
|
2012-08-21 14:22:58 +04:00
|
|
|
{
|
2014-01-31 02:58:49 +04:00
|
|
|
mFormat = ImageFormat::GRALLOC_PLANAR_YCBCR;
|
2012-08-21 14:22:58 +04:00
|
|
|
}
|
|
|
|
|
2013-07-25 06:13:35 +04:00
|
|
|
GrallocImage::~GrallocImage()
|
2012-08-21 14:22:58 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-03 14:24:26 +03:00
|
|
|
bool
|
2013-07-25 06:13:35 +04:00
|
|
|
GrallocImage::SetData(const Data& aData)
|
2012-08-21 14:22:58 +04:00
|
|
|
{
|
2014-03-31 19:24:28 +04:00
|
|
|
MOZ_ASSERT(!mTextureClient, "TextureClient is already set");
|
2012-08-21 14:22:58 +04:00
|
|
|
NS_PRECONDITION(aData.mYSize.width % 2 == 0, "Image should have even width");
|
|
|
|
NS_PRECONDITION(aData.mYSize.height % 2 == 0, "Image should have even height");
|
|
|
|
NS_PRECONDITION(aData.mYStride % 16 == 0, "Image should have stride of multiple of 16 pixels");
|
|
|
|
|
|
|
|
mData = aData;
|
|
|
|
mSize = aData.mPicSize;
|
|
|
|
|
2014-04-01 17:33:49 +04:00
|
|
|
if (gfxPlatform::GetPlatform()->IsInGonkEmulator()) {
|
2014-03-31 19:24:28 +04:00
|
|
|
// Emulator does not support HAL_PIXEL_FORMAT_YV12.
|
2015-11-03 14:24:26 +03:00
|
|
|
return false;
|
2012-08-21 14:22:58 +04:00
|
|
|
}
|
|
|
|
|
2015-10-15 18:53:37 +03:00
|
|
|
ISurfaceAllocator* allocator = ImageBridgeChild::GetSingleton();
|
|
|
|
GrallocTextureData* texData = GrallocTextureData::Create(mData.mYSize, HAL_PIXEL_FORMAT_YV12,
|
|
|
|
gfx::BackendType::NONE,
|
|
|
|
GraphicBuffer::USAGE_SW_READ_OFTEN |
|
|
|
|
GraphicBuffer::USAGE_SW_WRITE_OFTEN |
|
|
|
|
GraphicBuffer::USAGE_HW_TEXTURE,
|
|
|
|
allocator
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!texData) {
|
2015-11-03 14:24:26 +03:00
|
|
|
return false;
|
2012-08-21 14:22:58 +04:00
|
|
|
}
|
|
|
|
|
2015-11-20 16:25:00 +03:00
|
|
|
mTextureClient = new TextureClient(texData, TextureFlags::DEFAULT, allocator);
|
2015-10-15 18:53:37 +03:00
|
|
|
sp<GraphicBuffer> graphicBuffer = texData->GetGraphicBuffer();
|
2014-03-31 19:24:28 +04:00
|
|
|
|
2012-08-21 14:22:58 +04:00
|
|
|
void* vaddr;
|
|
|
|
if (graphicBuffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,
|
|
|
|
&vaddr) != OK) {
|
2015-11-03 14:24:26 +03:00
|
|
|
return false;
|
2012-08-21 14:22:58 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* yChannel = static_cast<uint8_t*>(vaddr);
|
2013-12-20 20:46:29 +04:00
|
|
|
gfx::IntSize ySize = aData.mYSize;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t yStride = graphicBuffer->getStride();
|
2012-08-21 14:22:58 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* vChannel = yChannel + (yStride * ySize.height);
|
2013-12-20 20:46:29 +04:00
|
|
|
gfx::IntSize uvSize = gfx::IntSize(ySize.width / 2,
|
2012-08-21 14:22:58 +04:00
|
|
|
ySize.height / 2);
|
|
|
|
// Align to 16 bytes boundary
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t uvStride = ((yStride / 2) + 15) & ~0x0F;
|
|
|
|
uint8_t* uChannel = vChannel + (uvStride * uvSize.height);
|
2012-08-21 14:22:58 +04:00
|
|
|
|
|
|
|
// Memory outside of the image width may not writable. If the stride
|
|
|
|
// equals to the image width then we can use only one copy.
|
|
|
|
if (yStride == mData.mYStride &&
|
|
|
|
yStride == ySize.width) {
|
2012-09-18 21:00:45 +04:00
|
|
|
memcpy(yChannel, mData.mYChannel, yStride * ySize.height);
|
2012-08-21 14:22:58 +04:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < ySize.height; i++) {
|
|
|
|
memcpy(yChannel + i * yStride,
|
|
|
|
mData.mYChannel + i * mData.mYStride,
|
|
|
|
ySize.width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uvStride == mData.mCbCrStride &&
|
|
|
|
uvStride == uvSize.width) {
|
2012-09-18 21:00:45 +04:00
|
|
|
memcpy(uChannel, mData.mCbChannel, uvStride * uvSize.height);
|
|
|
|
memcpy(vChannel, mData.mCrChannel, uvStride * uvSize.height);
|
2012-08-21 14:22:58 +04:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < uvSize.height; i++) {
|
|
|
|
memcpy(uChannel + i * uvStride,
|
|
|
|
mData.mCbChannel + i * mData.mCbCrStride,
|
|
|
|
uvSize.width);
|
|
|
|
memcpy(vChannel + i * uvStride,
|
|
|
|
mData.mCrChannel + i * mData.mCbCrStride,
|
|
|
|
uvSize.width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
graphicBuffer->unlock();
|
2014-10-10 17:21:43 +04:00
|
|
|
// Initialze the channels' addresses.
|
|
|
|
// Do not cache the addresses when gralloc buffer is not locked.
|
|
|
|
// gralloc hal could map gralloc buffer only when the buffer is locked,
|
|
|
|
// though some gralloc hals implementation maps it when it is allocated.
|
|
|
|
mData.mYChannel = nullptr;
|
|
|
|
mData.mCrChannel = nullptr;
|
|
|
|
mData.mCbChannel = nullptr;
|
2015-11-03 14:24:26 +03:00
|
|
|
return true;
|
2012-08-21 14:22:58 +04:00
|
|
|
}
|
|
|
|
|
2015-11-17 11:09:01 +03:00
|
|
|
void
|
|
|
|
GrallocImage::SetData(TextureClient* aGraphicBuffer, const gfx::IntSize& aSize)
|
2013-07-25 06:13:35 +04:00
|
|
|
{
|
2015-10-15 18:53:37 +03:00
|
|
|
mTextureClient = aGraphicBuffer;
|
2015-11-17 11:09:01 +03:00
|
|
|
mSize = aSize;
|
2013-07-25 06:13:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts YVU420 semi planar frames to RGB565, possibly taking different
|
|
|
|
* stride values.
|
|
|
|
* Needed because the Android ColorConverter class assumes that the Y and UV
|
|
|
|
* channels have equal stride.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ConvertYVU420SPToRGB565(void *aYData, uint32_t aYStride,
|
2014-07-04 04:38:00 +04:00
|
|
|
void *aUData, void *aVData, uint32_t aUVStride,
|
2013-07-25 06:13:35 +04:00
|
|
|
void *aOut,
|
|
|
|
uint32_t aWidth, uint32_t aHeight)
|
|
|
|
{
|
|
|
|
uint8_t *y = (uint8_t*)aYData;
|
2014-07-04 04:38:00 +04:00
|
|
|
bool isCbCr;
|
|
|
|
int8_t *uv;
|
|
|
|
|
|
|
|
if (aUData < aVData) {
|
|
|
|
// The color format is YCbCr
|
|
|
|
isCbCr = true;
|
|
|
|
uv = (int8_t*)aUData;
|
|
|
|
} else {
|
|
|
|
// The color format is YCrCb
|
|
|
|
isCbCr = false;
|
|
|
|
uv = (int8_t*)aVData;
|
|
|
|
}
|
2013-07-25 06:13:35 +04:00
|
|
|
|
|
|
|
uint16_t *rgb = (uint16_t*)aOut;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < aHeight; i++) {
|
|
|
|
for (size_t j = 0; j < aWidth; j++) {
|
2014-07-04 04:38:00 +04:00
|
|
|
int8_t d, e;
|
|
|
|
|
|
|
|
if (isCbCr) {
|
|
|
|
d = uv[j & ~1] - 128;
|
|
|
|
e = uv[j | 1] - 128;
|
|
|
|
} else {
|
|
|
|
d = uv[j | 1] - 128;
|
|
|
|
e = uv[j & ~1] - 128;
|
|
|
|
}
|
2013-07-25 06:13:35 +04:00
|
|
|
|
|
|
|
// Constants taken from https://en.wikipedia.org/wiki/YUV
|
|
|
|
int32_t r = (298 * y[j] + 409 * e + 128) >> 11;
|
|
|
|
int32_t g = (298 * y[j] - 100 * d - 208 * e + 128) >> 10;
|
|
|
|
int32_t b = (298 * y[j] + 516 * d + 128) >> 11;
|
|
|
|
|
|
|
|
r = r > 0x1f ? 0x1f : r < 0 ? 0 : r;
|
|
|
|
g = g > 0x3f ? 0x3f : g < 0 ? 0 : g;
|
|
|
|
b = b > 0x1f ? 0x1f : b < 0 ? 0 : b;
|
|
|
|
|
|
|
|
*rgb++ = (uint16_t)(r << 11 | g << 5 | b);
|
|
|
|
}
|
|
|
|
|
|
|
|
y += aYStride;
|
|
|
|
if (i % 2) {
|
|
|
|
uv += aUVStride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
/**
|
|
|
|
* Converts the format of vendor-specific YVU420(planar and semi-planar)
|
|
|
|
* with the help of GraphicBuffer::lockYCbCr. In this way, we can convert
|
|
|
|
* the YUV color format without awaring actual definition/enumeration
|
|
|
|
* of vendor formats.
|
|
|
|
*/
|
|
|
|
static status_t
|
|
|
|
ConvertVendorYUVFormatToRGB565(android::sp<GraphicBuffer>& aBuffer,
|
|
|
|
gfx::DataSourceSurface *aSurface,
|
|
|
|
gfx::DataSourceSurface::MappedSurface *aMappedSurface)
|
2014-01-28 18:13:58 +04:00
|
|
|
{
|
2014-07-04 04:38:00 +04:00
|
|
|
status_t rv = BAD_VALUE;
|
2014-01-28 18:13:58 +04:00
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
#if ANDROID_VERSION >= 18
|
|
|
|
android_ycbcr ycbcr;
|
2014-01-28 18:13:58 +04:00
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
// Check if the vendor provides explicit addresses of Y/Cb/Cr buffer from lockYCbCr
|
|
|
|
rv = aBuffer->lockYCbCr(android::GraphicBuffer::USAGE_SW_READ_OFTEN, &ycbcr);
|
|
|
|
|
|
|
|
if (rv != OK) {
|
|
|
|
NS_WARNING("Couldn't lock graphic buffer using lockYCbCr()");
|
|
|
|
return rv;
|
2014-01-28 18:13:58 +04:00
|
|
|
}
|
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
GraphicBufferAutoUnlock unlock(aBuffer);
|
2014-01-28 18:13:58 +04:00
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
uint32_t width = aSurface->GetSize().width;
|
|
|
|
uint32_t height = aSurface->GetSize().height;
|
2014-01-28 18:13:58 +04:00
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
if (ycbcr.chroma_step == 2) {
|
|
|
|
// From the system/core/include/system/graphics.h
|
|
|
|
// @chroma_step is the distance in bytes from one chroma pixel value to
|
|
|
|
// the next. This is 2 bytes for semiplanar (because chroma values are
|
|
|
|
// interleaved and each chroma value is one byte) and 1 for planar.
|
|
|
|
ConvertYVU420SPToRGB565(ycbcr.y, ycbcr.ystride,
|
|
|
|
ycbcr.cb, ycbcr.cr, ycbcr.cstride,
|
|
|
|
aMappedSurface->mData,
|
|
|
|
width, height);
|
|
|
|
} else {
|
|
|
|
layers::PlanarYCbCrData ycbcrData;
|
|
|
|
ycbcrData.mYChannel = static_cast<uint8_t*>(ycbcr.y);
|
|
|
|
ycbcrData.mYStride = ycbcr.ystride;
|
|
|
|
ycbcrData.mYSize = aSurface->GetSize();
|
|
|
|
ycbcrData.mCbChannel = static_cast<uint8_t*>(ycbcr.cb);
|
|
|
|
ycbcrData.mCrChannel = static_cast<uint8_t*>(ycbcr.cr);
|
|
|
|
ycbcrData.mCbCrStride = ycbcr.cstride;
|
|
|
|
ycbcrData.mCbCrSize = aSurface->GetSize() / 2;
|
|
|
|
ycbcrData.mPicSize = aSurface->GetSize();
|
|
|
|
|
|
|
|
gfx::ConvertYCbCrToRGB(ycbcrData,
|
|
|
|
aSurface->GetFormat(),
|
|
|
|
aSurface->GetSize(),
|
|
|
|
aMappedSurface->mData,
|
|
|
|
aMappedSurface->mStride);
|
2014-01-28 18:13:58 +04:00
|
|
|
}
|
2014-07-04 04:38:00 +04:00
|
|
|
#endif
|
2014-01-28 18:13:58 +04:00
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
ConvertOmxYUVFormatToRGB565(android::sp<GraphicBuffer>& aBuffer,
|
|
|
|
gfx::DataSourceSurface *aSurface,
|
|
|
|
gfx::DataSourceSurface::MappedSurface *aMappedSurface,
|
2015-03-20 05:25:12 +03:00
|
|
|
const layers::PlanarYCbCrData& aYcbcrData)
|
2014-07-04 04:38:00 +04:00
|
|
|
{
|
2015-03-20 05:25:12 +03:00
|
|
|
uint32_t omxFormat =
|
|
|
|
GrallocImage::GetOmxFormat(aBuffer->getPixelFormat());
|
|
|
|
if (!omxFormat) {
|
2014-01-28 18:13:58 +04:00
|
|
|
NS_WARNING("Unknown color format");
|
2014-07-04 04:38:00 +04:00
|
|
|
return BAD_VALUE;
|
2014-01-28 18:13:58 +04:00
|
|
|
}
|
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
status_t rv;
|
|
|
|
uint8_t *buffer;
|
|
|
|
|
|
|
|
rv = aBuffer->lock(android::GraphicBuffer::USAGE_SW_READ_OFTEN,
|
|
|
|
reinterpret_cast<void **>(&buffer));
|
|
|
|
if (rv != OK) {
|
|
|
|
NS_WARNING("Couldn't lock graphic buffer");
|
|
|
|
return BAD_VALUE;
|
2014-06-17 03:47:28 +04:00
|
|
|
}
|
2014-01-28 18:13:58 +04:00
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
GraphicBufferAutoUnlock unlock(aBuffer);
|
2014-01-28 18:13:58 +04:00
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
uint32_t format = aBuffer->getPixelFormat();
|
|
|
|
uint32_t width = aSurface->GetSize().width;
|
|
|
|
uint32_t height = aSurface->GetSize().height;
|
2015-05-05 01:14:34 +03:00
|
|
|
uint32_t stride = aBuffer->getStride();
|
2014-01-28 18:13:58 +04:00
|
|
|
|
2014-07-04 04:38:00 +04:00
|
|
|
if (format == GrallocImage::HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO) {
|
2014-01-28 18:13:58 +04:00
|
|
|
// The Adreno hardware decoder aligns image dimensions to a multiple of 32,
|
|
|
|
// so we have to account for that here
|
|
|
|
uint32_t alignedWidth = ALIGN(width, 32);
|
|
|
|
uint32_t alignedHeight = ALIGN(height, 32);
|
|
|
|
uint32_t uvOffset = ALIGN(alignedHeight * alignedWidth, 4096);
|
|
|
|
uint32_t uvStride = 2 * ALIGN(width / 2, 32);
|
|
|
|
ConvertYVU420SPToRGB565(buffer, alignedWidth,
|
2014-07-04 04:38:00 +04:00
|
|
|
buffer + uvOffset + 1,
|
|
|
|
buffer + uvOffset,
|
|
|
|
uvStride,
|
|
|
|
aMappedSurface->mData,
|
2014-01-28 18:13:58 +04:00
|
|
|
width, height);
|
2014-07-04 04:38:00 +04:00
|
|
|
return OK;
|
2014-03-07 17:21:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format == HAL_PIXEL_FORMAT_YCrCb_420_SP) {
|
2015-05-05 01:14:34 +03:00
|
|
|
uint32_t uvOffset = height * stride;
|
|
|
|
ConvertYVU420SPToRGB565(buffer, stride,
|
2014-07-04 04:38:00 +04:00
|
|
|
buffer + uvOffset + 1,
|
|
|
|
buffer + uvOffset,
|
2015-05-05 01:14:34 +03:00
|
|
|
stride,
|
2014-07-04 04:38:00 +04:00
|
|
|
aMappedSurface->mData,
|
2014-01-28 18:13:58 +04:00
|
|
|
width, height);
|
2014-07-04 04:38:00 +04:00
|
|
|
return OK;
|
2014-03-07 17:21:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format == HAL_PIXEL_FORMAT_YV12) {
|
2014-09-01 09:52:03 +04:00
|
|
|
// Depend on platforms, it is possible for HW decoder to output YV12 format.
|
|
|
|
// It means the mData won't be configured during the SetData API because the
|
|
|
|
// yuv data has already stored in GraphicBuffer. Here we try to confgiure the
|
|
|
|
// mData if it doesn't contain valid configuration.
|
|
|
|
layers::PlanarYCbCrData ycbcrData = aYcbcrData;
|
|
|
|
if (!ycbcrData.mYChannel) {
|
|
|
|
ycbcrData.mYChannel = buffer;
|
|
|
|
ycbcrData.mYSkip = 0;
|
|
|
|
ycbcrData.mYStride = aBuffer->getStride();
|
|
|
|
ycbcrData.mYSize = aSurface->GetSize();
|
|
|
|
ycbcrData.mCbSkip = 0;
|
|
|
|
ycbcrData.mCbCrSize = aSurface->GetSize() / 2;
|
|
|
|
ycbcrData.mPicSize = aSurface->GetSize();
|
2014-10-09 00:34:52 +04:00
|
|
|
ycbcrData.mCrChannel = buffer + ycbcrData.mYStride * aBuffer->getHeight();
|
2014-09-01 09:52:03 +04:00
|
|
|
ycbcrData.mCrSkip = 0;
|
|
|
|
// Align to 16 bytes boundary
|
2014-10-09 00:34:52 +04:00
|
|
|
ycbcrData.mCbCrStride = ALIGN(ycbcrData.mYStride / 2, 16);
|
|
|
|
ycbcrData.mCbChannel = ycbcrData.mCrChannel + (ycbcrData.mCbCrStride * aBuffer->getHeight() / 2);
|
2014-10-10 17:21:43 +04:00
|
|
|
} else {
|
|
|
|
// Update channels' address.
|
|
|
|
// Gralloc buffer could map gralloc buffer only when the buffer is locked.
|
|
|
|
ycbcrData.mYChannel = buffer;
|
|
|
|
ycbcrData.mCrChannel = buffer + ycbcrData.mYStride * aBuffer->getHeight();
|
|
|
|
ycbcrData.mCbChannel = ycbcrData.mCrChannel + (ycbcrData.mCbCrStride * aBuffer->getHeight() / 2);
|
2014-09-01 09:52:03 +04:00
|
|
|
}
|
|
|
|
gfx::ConvertYCbCrToRGB(ycbcrData,
|
2014-07-04 04:38:00 +04:00
|
|
|
aSurface->GetFormat(),
|
|
|
|
aSurface->GetSize(),
|
2014-09-01 09:52:03 +04:00
|
|
|
aMappedSurface->mData,
|
|
|
|
aMappedSurface->mStride);
|
2014-07-04 04:38:00 +04:00
|
|
|
return OK;
|
2014-01-28 18:13:58 +04:00
|
|
|
}
|
|
|
|
|
2015-07-02 23:53:06 +03:00
|
|
|
if (format == HAL_PIXEL_FORMAT_RGBA_8888) {
|
|
|
|
uint32_t* src = (uint32_t*)(buffer);
|
|
|
|
uint16_t* dest = (uint16_t*)(aMappedSurface->mData);
|
|
|
|
|
|
|
|
// Convert RGBA8888 to RGB565
|
|
|
|
for (size_t i = 0; i < width * height; i++) {
|
|
|
|
uint32_t r = ((*src >> 0 ) & 0xFF);
|
|
|
|
uint32_t g = ((*src >> 8 ) & 0xFF);
|
|
|
|
uint32_t b = ((*src >> 16) & 0xFF);
|
|
|
|
*dest++ = ((r >> 3) << 11) | ((g >> 2) << 5) | ((b >> 3) << 0);
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-03-20 05:25:12 +03:00
|
|
|
android::ColorConverter colorConverter((OMX_COLOR_FORMATTYPE)omxFormat,
|
2014-01-28 18:13:58 +04:00
|
|
|
OMX_COLOR_Format16bitRGB565);
|
|
|
|
if (!colorConverter.isValid()) {
|
|
|
|
NS_WARNING("Invalid color conversion");
|
2014-07-04 04:38:00 +04:00
|
|
|
return BAD_VALUE;
|
2014-01-28 18:13:58 +04:00
|
|
|
}
|
|
|
|
|
2015-10-23 09:01:31 +03:00
|
|
|
uint32_t pixelStride = aMappedSurface->mStride/gfx::BytesPerPixel(gfx::SurfaceFormat::R5G6B5_UINT16);
|
2014-01-28 18:13:58 +04:00
|
|
|
rv = colorConverter.convert(buffer, width, height,
|
|
|
|
0, 0, width - 1, height - 1 /* source crop */,
|
2014-09-15 17:30:40 +04:00
|
|
|
aMappedSurface->mData, pixelStride, height,
|
2014-01-28 18:13:58 +04:00
|
|
|
0, 0, width - 1, height - 1 /* dest crop */);
|
2014-07-04 04:38:00 +04:00
|
|
|
if (rv) {
|
|
|
|
NS_WARNING("OMX color conversion failed");
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-10-27 08:43:07 +03:00
|
|
|
already_AddRefed<gfx::DataSourceSurface>
|
|
|
|
GetDataSourceSurfaceFrom(android::sp<android::GraphicBuffer>& aGraphicBuffer,
|
|
|
|
gfx::IntSize aSize,
|
|
|
|
const layers::PlanarYCbCrData& aYcbcrData)
|
2014-07-04 04:38:00 +04:00
|
|
|
{
|
2015-10-27 08:43:07 +03:00
|
|
|
MOZ_ASSERT(aGraphicBuffer.get());
|
2014-07-04 04:38:00 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfx::DataSourceSurface> surface =
|
2015-10-29 05:33:16 +03:00
|
|
|
gfx::Factory::CreateDataSourceSurface(aSize, gfx::SurfaceFormat::R5G6B5_UINT16);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!surface)) {
|
2014-07-04 04:38:00 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::DataSourceSurface::MappedSurface mappedSurface;
|
|
|
|
if (!surface->Map(gfx::DataSourceSurface::WRITE, &mappedSurface)) {
|
|
|
|
NS_WARNING("Could not map DataSourceSurface");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t rv;
|
2015-10-27 08:43:07 +03:00
|
|
|
rv = ConvertOmxYUVFormatToRGB565(aGraphicBuffer, surface, &mappedSurface, aYcbcrData);
|
2015-03-20 05:25:12 +03:00
|
|
|
if (rv == OK) {
|
2014-07-04 04:38:00 +04:00
|
|
|
surface->Unmap();
|
2015-05-01 16:14:16 +03:00
|
|
|
return surface.forget();
|
2014-07-04 04:38:00 +04:00
|
|
|
}
|
|
|
|
|
2015-10-27 08:43:07 +03:00
|
|
|
rv = ConvertVendorYUVFormatToRGB565(aGraphicBuffer, surface, &mappedSurface);
|
2014-01-28 18:13:58 +04:00
|
|
|
surface->Unmap();
|
2014-07-04 04:38:00 +04:00
|
|
|
if (rv != OK) {
|
2015-03-20 05:25:12 +03:00
|
|
|
NS_WARNING("Unknown color format");
|
2014-01-28 18:13:58 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-01 16:14:16 +03:00
|
|
|
return surface.forget();
|
2014-01-28 18:13:58 +04:00
|
|
|
}
|
|
|
|
|
2015-10-27 08:43:07 +03:00
|
|
|
already_AddRefed<gfx::SourceSurface>
|
|
|
|
GrallocImage::GetAsSourceSurface()
|
|
|
|
{
|
|
|
|
if (!mTextureClient) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:53:37 +03:00
|
|
|
android::sp<GraphicBuffer> graphicBuffer = GetGraphicBuffer();
|
2015-10-27 08:43:07 +03:00
|
|
|
|
|
|
|
RefPtr<gfx::DataSourceSurface> surface =
|
|
|
|
GetDataSourceSurfaceFrom(graphicBuffer, mSize, mData);
|
|
|
|
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2014-04-11 17:13:12 +04:00
|
|
|
android::sp<android::GraphicBuffer>
|
|
|
|
GrallocImage::GetGraphicBuffer() const
|
|
|
|
{
|
|
|
|
if (!mTextureClient) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-15 18:53:37 +03:00
|
|
|
return static_cast<GrallocTextureData*>(mTextureClient->GetInternalData())->GetGraphicBuffer();
|
2014-04-11 17:13:12 +04:00
|
|
|
}
|
|
|
|
|
2014-03-31 19:24:28 +04:00
|
|
|
void*
|
|
|
|
GrallocImage::GetNativeBuffer()
|
|
|
|
{
|
|
|
|
if (!mTextureClient) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-15 18:53:37 +03:00
|
|
|
android::sp<android::GraphicBuffer> graphicBuffer = GetGraphicBuffer();
|
2014-03-31 19:24:28 +04:00
|
|
|
if (!graphicBuffer.get()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return graphicBuffer->getNativeBuffer();
|
|
|
|
}
|
|
|
|
|
2013-08-09 19:23:46 +04:00
|
|
|
TextureClient*
|
2014-02-17 02:23:39 +04:00
|
|
|
GrallocImage::GetTextureClient(CompositableClient* aClient)
|
2013-08-09 19:23:46 +04:00
|
|
|
{
|
|
|
|
return mTextureClient;
|
|
|
|
}
|
|
|
|
|
2012-08-21 14:22:58 +04:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|