2013-11-01 05:54:14 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* 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 "MacIOSurfaceImage.h"
|
2014-12-22 11:49:00 +03:00
|
|
|
#include "mozilla/layers/CompositableClient.h"
|
|
|
|
#include "mozilla/layers/CompositableForwarder.h"
|
2013-11-18 06:04:38 +04:00
|
|
|
#include "mozilla/layers/MacIOSurfaceTextureClientOGL.h"
|
2015-08-04 00:57:54 +03:00
|
|
|
#include "YCbCrUtils.h"
|
2013-11-01 05:54:14 +04:00
|
|
|
|
2014-01-27 17:25:50 +04:00
|
|
|
using namespace mozilla;
|
2013-11-01 05:54:14 +04:00
|
|
|
using namespace mozilla::layers;
|
2015-08-04 00:57:54 +03:00
|
|
|
using namespace mozilla::gfx;
|
2013-11-01 05:54:14 +04:00
|
|
|
|
|
|
|
TextureClient*
|
2014-02-17 02:23:39 +04:00
|
|
|
MacIOSurfaceImage::GetTextureClient(CompositableClient* aClient)
|
2013-11-01 05:54:14 +04:00
|
|
|
{
|
|
|
|
if (!mTextureClient) {
|
2015-04-29 20:34:00 +03:00
|
|
|
mTextureClient = MacIOSurfaceTextureClientOGL::Create(aClient->GetForwarder(),
|
|
|
|
TextureFlags::DEFAULT,
|
|
|
|
mSurface);
|
2013-11-01 05:54:14 +04:00
|
|
|
}
|
|
|
|
return mTextureClient;
|
|
|
|
}
|
2014-01-27 17:25:50 +04:00
|
|
|
|
2015-08-04 00:57:54 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
2014-01-27 17:25:50 +04:00
|
|
|
MacIOSurfaceImage::GetAsSourceSurface()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> dataSurface;
|
2014-01-27 17:25:50 +04:00
|
|
|
mSurface->Lock();
|
|
|
|
size_t bytesPerRow = mSurface->GetBytesPerRow();
|
|
|
|
size_t ioWidth = mSurface->GetDevicePixelWidth();
|
|
|
|
size_t ioHeight = mSurface->GetDevicePixelHeight();
|
|
|
|
|
2015-08-04 00:57:54 +03:00
|
|
|
SurfaceFormat format = mSurface->GetFormat() == SurfaceFormat::NV12 ? SurfaceFormat::B8G8R8X8 : SurfaceFormat::B8G8R8A8;
|
2014-01-27 17:25:50 +04:00
|
|
|
|
2015-08-04 00:57:54 +03:00
|
|
|
dataSurface = Factory::CreateDataSourceSurface(IntSize(ioWidth, ioHeight), format);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!dataSurface)) {
|
2014-01-27 17:25:50 +04:00
|
|
|
return nullptr;
|
2014-08-27 19:57:43 +04:00
|
|
|
}
|
2014-01-27 17:25:50 +04:00
|
|
|
|
2015-08-04 00:57:54 +03:00
|
|
|
DataSourceSurface::MappedSurface mappedSurface;
|
|
|
|
if (!dataSurface->Map(DataSourceSurface::WRITE, &mappedSurface))
|
2014-01-27 17:25:50 +04:00
|
|
|
return nullptr;
|
|
|
|
|
2015-08-04 00:57:54 +03:00
|
|
|
if (mSurface->GetFormat() == SurfaceFormat::NV12) {
|
|
|
|
if (mSurface->GetDevicePixelWidth() > PlanarYCbCrImage::MAX_DIMENSION ||
|
|
|
|
mSurface->GetDevicePixelHeight() > PlanarYCbCrImage::MAX_DIMENSION) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract and separate the CbCr planes */
|
|
|
|
size_t cbCrStride = mSurface->GetBytesPerRow(1);
|
|
|
|
size_t cbCrWidth = mSurface->GetDevicePixelWidth(1);
|
|
|
|
size_t cbCrHeight = mSurface->GetDevicePixelHeight(1);
|
|
|
|
|
|
|
|
nsAutoArrayPtr<uint8_t> cbPlane(new uint8_t[cbCrWidth * cbCrHeight]);
|
|
|
|
nsAutoArrayPtr<uint8_t> crPlane(new uint8_t[cbCrWidth * cbCrHeight]);
|
|
|
|
|
|
|
|
uint8_t* src = (uint8_t*)mSurface->GetBaseAddressOfPlane(1);
|
|
|
|
uint8_t* cbDest = cbPlane;
|
|
|
|
uint8_t* crDest = crPlane;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < cbCrHeight; i++) {
|
|
|
|
uint8_t* rowSrc = src + cbCrStride * i;
|
|
|
|
for (size_t j = 0; j < cbCrWidth; j++) {
|
|
|
|
*cbDest = *rowSrc;
|
|
|
|
cbDest++;
|
|
|
|
rowSrc++;
|
|
|
|
*crDest = *rowSrc;
|
|
|
|
crDest++;
|
|
|
|
rowSrc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert to RGB */
|
|
|
|
PlanarYCbCrData data;
|
|
|
|
data.mYChannel = (uint8_t*)mSurface->GetBaseAddressOfPlane(0);
|
|
|
|
data.mYStride = mSurface->GetBytesPerRow(0);
|
|
|
|
data.mYSize = IntSize(mSurface->GetDevicePixelWidth(0), mSurface->GetDevicePixelHeight(0));
|
|
|
|
data.mCbChannel = cbPlane;
|
|
|
|
data.mCrChannel = crPlane;
|
|
|
|
data.mCbCrStride = cbCrWidth;
|
|
|
|
data.mCbCrSize = IntSize(cbCrWidth, cbCrHeight);
|
|
|
|
data.mPicSize = data.mYSize;
|
|
|
|
|
|
|
|
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8, IntSize(ioWidth, ioHeight), mappedSurface.mData, mappedSurface.mStride);
|
|
|
|
} else {
|
|
|
|
unsigned char* ioData = (unsigned char*)mSurface->GetBaseAddress();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ioHeight; ++i) {
|
|
|
|
memcpy(mappedSurface.mData + i * mappedSurface.mStride,
|
|
|
|
ioData + i * bytesPerRow,
|
|
|
|
ioWidth * 4);
|
|
|
|
}
|
2014-01-27 17:25:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
dataSurface->Unmap();
|
|
|
|
mSurface->Unlock();
|
|
|
|
|
2015-05-01 16:14:16 +03:00
|
|
|
return dataSurface.forget();
|
2014-01-27 17:25:50 +04:00
|
|
|
}
|