2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-08-19 23:33:25 +04:00
|
|
|
* 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 "ImageContainer.h"
|
2013-08-12 03:17:23 +04:00
|
|
|
#include <string.h> // for memcpy, memset
|
2014-09-17 17:13:29 +04:00
|
|
|
#include "GLImages.h" // for SurfaceTextureImage
|
2013-12-13 21:32:02 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "gfxPlatform.h" // for gfxPlatform
|
|
|
|
#include "gfxUtils.h" // for gfxUtils
|
2016-03-10 05:35:27 +03:00
|
|
|
#include "libyuv.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h" // for already_AddRefed
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "mozilla/ipc/CrossProcessMutex.h" // for CrossProcessMutex, etc
|
|
|
|
#include "mozilla/layers/CompositorTypes.h"
|
|
|
|
#include "mozilla/layers/ImageBridgeChild.h" // for ImageBridgeChild
|
|
|
|
#include "mozilla/layers/ImageClient.h" // for ImageClient
|
2018-11-14 21:05:28 +03:00
|
|
|
#include "mozilla/layers/ImageDataSerializer.h" // for SurfaceDescriptorBuffer
|
2015-07-03 10:39:09 +03:00
|
|
|
#include "mozilla/layers/LayersMessages.h"
|
2015-11-17 11:09:01 +03:00
|
|
|
#include "mozilla/layers/SharedPlanarYCbCrImage.h"
|
2018-10-02 20:28:22 +03:00
|
|
|
#include "mozilla/layers/SharedSurfacesChild.h" // for SharedSurfacesAnimation
|
2015-11-17 11:09:01 +03:00
|
|
|
#include "mozilla/layers/SharedRGBImage.h"
|
2016-06-30 05:12:31 +03:00
|
|
|
#include "mozilla/layers/TextureClientRecycleAllocator.h"
|
2016-08-25 06:48:35 +03:00
|
|
|
#include "mozilla/gfx/gfxVars.h"
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "nsISupportsUtils.h" // for NS_IF_ADDREF
|
2013-12-13 21:32:02 +04:00
|
|
|
#include "YCbCrUtils.h" // for YCbCr conversions
|
2014-01-15 19:06:43 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2014-02-17 02:23:39 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2017-03-30 05:52:12 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2012-08-19 23:33:25 +04:00
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
# include "mozilla/gfx/QuartzSupport.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
# include "gfxWindowsPlatform.h"
|
|
|
|
# include <d3d10_1.h>
|
2017-05-04 00:23:45 +03:00
|
|
|
# include "mozilla/gfx/DeviceManagerDx.h"
|
|
|
|
# include "mozilla/layers/D3D11YCbCrImage.h"
|
2012-08-19 23:33:25 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2014-05-22 14:11:45 +04:00
|
|
|
using namespace mozilla::gfx;
|
2019-04-22 22:58:52 +03:00
|
|
|
using namespace mozilla::ipc;
|
2013-08-12 03:17:23 +04:00
|
|
|
|
2013-08-12 13:51:49 +04:00
|
|
|
Atomic<int32_t> Image::sSerialCounter(0);
|
2012-08-23 04:08:26 +04:00
|
|
|
|
2015-05-12 03:56:09 +03:00
|
|
|
Atomic<uint32_t> ImageContainer::sGenerationCounter(0);
|
|
|
|
|
2018-11-14 21:05:28 +03:00
|
|
|
static void CopyPlane(uint8_t* aDst, const uint8_t* aSrc,
|
|
|
|
const gfx::IntSize& aSize, int32_t aStride,
|
|
|
|
int32_t aSkip);
|
|
|
|
|
2015-11-17 11:09:01 +03:00
|
|
|
RefPtr<PlanarYCbCrImage> ImageFactory::CreatePlanarYCbCrImage(
|
|
|
|
const gfx::IntSize& aScaleHint, BufferRecycleBin* aRecycleBin) {
|
|
|
|
return new RecyclingPlanarYCbCrImage(aRecycleBin);
|
|
|
|
}
|
|
|
|
|
2012-08-19 23:33:25 +04:00
|
|
|
BufferRecycleBin::BufferRecycleBin()
|
|
|
|
: mLock("mozilla.layers.BufferRecycleBin.mLock")
|
2016-01-19 22:14:22 +03:00
|
|
|
// This member is only valid when the bin is not empty and will be
|
|
|
|
// properly initialized in RecycleBuffer, but initializing it here avoids
|
|
|
|
// static analysis noise.
|
|
|
|
,
|
|
|
|
mRecycledBufferSize(0) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-20 20:16:04 +03:00
|
|
|
void BufferRecycleBin::RecycleBuffer(UniquePtr<uint8_t[]> aBuffer,
|
|
|
|
uint32_t aSize) {
|
2012-08-19 23:33:25 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (!mRecycledBuffers.IsEmpty() && aSize != mRecycledBufferSize) {
|
|
|
|
mRecycledBuffers.Clear();
|
|
|
|
}
|
|
|
|
mRecycledBufferSize = aSize;
|
2018-05-30 22:15:35 +03:00
|
|
|
mRecycledBuffers.AppendElement(std::move(aBuffer));
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
UniquePtr<uint8_t[]> BufferRecycleBin::GetBuffer(uint32_t aSize) {
|
2012-08-19 23:33:25 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2017-10-28 01:30:13 +03:00
|
|
|
if (mRecycledBuffers.IsEmpty() || mRecycledBufferSize != aSize) {
|
|
|
|
return UniquePtr<uint8_t[]>(new (fallible) uint8_t[aSize]);
|
|
|
|
}
|
2012-08-19 23:33:25 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t last = mRecycledBuffers.Length() - 1;
|
2018-05-30 22:15:35 +03:00
|
|
|
UniquePtr<uint8_t[]> result = std::move(mRecycledBuffers[last]);
|
2012-08-19 23:33:25 +04:00
|
|
|
mRecycledBuffers.RemoveElementAt(last);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-28 04:25:01 +03:00
|
|
|
void BufferRecycleBin::ClearRecycledBuffers() {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (!mRecycledBuffers.IsEmpty()) {
|
|
|
|
mRecycledBuffers.Clear();
|
|
|
|
}
|
|
|
|
mRecycledBufferSize = 0;
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:51:40 +03:00
|
|
|
ImageContainerListener::ImageContainerListener(ImageContainer* aImageContainer)
|
|
|
|
: mLock("mozilla.layers.ImageContainerListener.mLock"),
|
|
|
|
mImageContainer(aImageContainer) {}
|
|
|
|
|
|
|
|
ImageContainerListener::~ImageContainerListener() {}
|
|
|
|
|
|
|
|
void ImageContainerListener::NotifyComposite(
|
|
|
|
const ImageCompositeNotification& aNotification) {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (mImageContainer) {
|
|
|
|
mImageContainer->NotifyComposite(aNotification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:09 +03:00
|
|
|
void ImageContainerListener::NotifyDropped(uint32_t aDropped) {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (mImageContainer) {
|
|
|
|
mImageContainer->NotifyDropped(aDropped);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:51:40 +03:00
|
|
|
void ImageContainerListener::ClearImageContainer() {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
mImageContainer = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-09-26 03:58:37 +03:00
|
|
|
void ImageContainerListener::DropImageClient() {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (mImageContainer) {
|
|
|
|
mImageContainer->DropImageClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 14:52:02 +03:00
|
|
|
already_AddRefed<ImageClient> ImageContainer::GetImageClient() {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock mon(mRecursiveMutex);
|
2017-07-15 14:52:02 +03:00
|
|
|
EnsureImageClient();
|
|
|
|
RefPtr<ImageClient> imageClient = mImageClient;
|
|
|
|
return imageClient.forget();
|
|
|
|
}
|
|
|
|
|
2017-09-22 02:21:09 +03:00
|
|
|
void ImageContainer::DropImageClient() {
|
|
|
|
RecursiveMutexAutoLock mon(mRecursiveMutex);
|
|
|
|
if (mImageClient) {
|
|
|
|
mImageClient->ClearCachedResources();
|
|
|
|
mImageClient = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 10:51:51 +03:00
|
|
|
void ImageContainer::EnsureImageClient() {
|
2016-10-03 11:20:42 +03:00
|
|
|
// If we're not forcing a new ImageClient, then we can skip this if we don't
|
|
|
|
// have an existing ImageClient, or if the existing one belongs to an IPC
|
|
|
|
// actor that is still open.
|
2017-02-02 10:51:51 +03:00
|
|
|
if (!mIsAsync) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mImageClient &&
|
|
|
|
mImageClient->GetForwarder()->GetLayersIPCActor()->IPCOpen()) {
|
2016-10-03 11:20:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ImageBridgeChild> imageBridge = ImageBridgeChild::GetSingleton();
|
|
|
|
if (imageBridge) {
|
2017-01-04 18:19:30 +03:00
|
|
|
mImageClient =
|
|
|
|
imageBridge->CreateImageClient(CompositableType::IMAGE, this);
|
2016-10-29 01:48:35 +03:00
|
|
|
if (mImageClient) {
|
2017-01-18 05:47:05 +03:00
|
|
|
mAsyncContainerHandle = mImageClient->GetAsyncHandle();
|
2017-02-02 10:51:51 +03:00
|
|
|
} else {
|
|
|
|
// It's okay to drop the async container handle since the ImageBridgeChild
|
|
|
|
// is going to die anyway.
|
|
|
|
mAsyncContainerHandle = CompositableHandle();
|
2016-10-29 01:48:35 +03:00
|
|
|
}
|
2016-10-03 11:20:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:28:22 +03:00
|
|
|
SharedSurfacesAnimation* ImageContainer::EnsureSharedSurfacesAnimation() {
|
|
|
|
if (!mSharedAnimation) {
|
|
|
|
mSharedAnimation = new SharedSurfacesAnimation();
|
|
|
|
}
|
|
|
|
return mSharedAnimation;
|
|
|
|
}
|
|
|
|
|
2015-07-06 06:02:26 +03:00
|
|
|
ImageContainer::ImageContainer(Mode flag)
|
2017-07-04 20:47:42 +03:00
|
|
|
: mRecursiveMutex("ImageContainer.mRecursiveMutex"),
|
2015-05-12 03:56:09 +03:00
|
|
|
mGenerationCounter(++sGenerationCounter),
|
2012-08-19 23:33:25 +04:00
|
|
|
mPaintCount(0),
|
2015-05-11 05:07:07 +03:00
|
|
|
mDroppedImageCount(0),
|
2012-08-19 23:33:25 +04:00
|
|
|
mImageFactory(new ImageFactory()),
|
|
|
|
mRecycleBin(new BufferRecycleBin()),
|
2017-02-02 10:51:51 +03:00
|
|
|
mIsAsync(flag == ASYNCHRONOUS),
|
2016-09-07 01:20:41 +03:00
|
|
|
mCurrentProducerID(-1) {
|
2016-10-03 11:20:42 +03:00
|
|
|
if (flag == ASYNCHRONOUS) {
|
2017-09-26 03:58:37 +03:00
|
|
|
mNotifyCompositeListener = new ImageContainerListener(this);
|
2017-02-02 10:51:51 +03:00
|
|
|
EnsureImageClient();
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
2016-07-18 11:54:01 +03:00
|
|
|
}
|
|
|
|
|
2017-01-18 05:47:05 +03:00
|
|
|
ImageContainer::ImageContainer(const CompositableHandle& aHandle)
|
2017-07-04 20:47:42 +03:00
|
|
|
: mRecursiveMutex("ImageContainer.mRecursiveMutex"),
|
2016-07-18 11:54:01 +03:00
|
|
|
mGenerationCounter(++sGenerationCounter),
|
|
|
|
mPaintCount(0),
|
|
|
|
mDroppedImageCount(0),
|
|
|
|
mImageFactory(nullptr),
|
|
|
|
mRecycleBin(nullptr),
|
2017-02-02 10:51:51 +03:00
|
|
|
mIsAsync(true),
|
2017-01-18 05:47:05 +03:00
|
|
|
mAsyncContainerHandle(aHandle),
|
2016-09-07 01:20:41 +03:00
|
|
|
mCurrentProducerID(-1) {
|
2017-01-18 05:47:05 +03:00
|
|
|
MOZ_ASSERT(mAsyncContainerHandle);
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageContainer::~ImageContainer() {
|
2017-01-14 01:51:40 +03:00
|
|
|
if (mNotifyCompositeListener) {
|
|
|
|
mNotifyCompositeListener->ClearImageContainer();
|
|
|
|
}
|
2017-01-18 05:47:05 +03:00
|
|
|
if (mAsyncContainerHandle) {
|
2017-01-04 18:19:31 +03:00
|
|
|
if (RefPtr<ImageBridgeChild> imageBridge =
|
|
|
|
ImageBridgeChild::GetSingleton()) {
|
2017-01-18 05:47:05 +03:00
|
|
|
imageBridge->ForgetImageContainer(mAsyncContainerHandle);
|
2017-01-04 18:19:31 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-03 15:19:14 +03:00
|
|
|
if (mSharedAnimation) {
|
|
|
|
mSharedAnimation->Destroy();
|
|
|
|
}
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2015-11-17 11:09:01 +03:00
|
|
|
RefPtr<PlanarYCbCrImage> ImageContainer::CreatePlanarYCbCrImage() {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2017-02-02 10:51:51 +03:00
|
|
|
EnsureImageClient();
|
2015-11-17 11:09:01 +03:00
|
|
|
if (mImageClient && mImageClient->AsImageClientSingle()) {
|
|
|
|
return new SharedPlanarYCbCrImage(mImageClient);
|
|
|
|
}
|
|
|
|
return mImageFactory->CreatePlanarYCbCrImage(mScaleHint, mRecycleBin);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SharedRGBImage> ImageContainer::CreateSharedRGBImage() {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2017-02-02 10:51:51 +03:00
|
|
|
EnsureImageClient();
|
2015-11-17 11:09:01 +03:00
|
|
|
if (!mImageClient || !mImageClient->AsImageClientSingle()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return new SharedRGBImage(mImageClient);
|
|
|
|
}
|
|
|
|
|
2015-05-25 05:33:35 +03:00
|
|
|
void ImageContainer::SetCurrentImageInternal(
|
|
|
|
const nsTArray<NonOwningImage>& aImages) {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2012-08-19 23:33:25 +04:00
|
|
|
|
2015-05-25 05:33:35 +03:00
|
|
|
mGenerationCounter = ++sGenerationCounter;
|
|
|
|
|
|
|
|
if (!aImages.IsEmpty()) {
|
|
|
|
NS_ASSERTION(mCurrentImages.IsEmpty() ||
|
|
|
|
mCurrentImages[0].mProducerID != aImages[0].mProducerID ||
|
|
|
|
mCurrentImages[0].mFrameID <= aImages[0].mFrameID,
|
|
|
|
"frame IDs shouldn't go backwards");
|
|
|
|
if (aImages[0].mProducerID != mCurrentProducerID) {
|
|
|
|
mCurrentProducerID = aImages[0].mProducerID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<OwningImage> newImages;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aImages.Length(); ++i) {
|
|
|
|
NS_ASSERTION(aImages[i].mImage, "image can't be null");
|
|
|
|
NS_ASSERTION(!aImages[i].mTimeStamp.IsNull() || aImages.Length() == 1,
|
|
|
|
"Multiple images require timestamps");
|
|
|
|
if (i > 0) {
|
|
|
|
NS_ASSERTION(aImages[i].mTimeStamp >= aImages[i - 1].mTimeStamp,
|
|
|
|
"Timestamps must not decrease");
|
|
|
|
NS_ASSERTION(aImages[i].mFrameID > aImages[i - 1].mFrameID,
|
|
|
|
"FrameIDs must increase");
|
|
|
|
NS_ASSERTION(aImages[i].mProducerID == aImages[i - 1].mProducerID,
|
|
|
|
"ProducerIDs must be the same");
|
|
|
|
}
|
|
|
|
OwningImage* img = newImages.AppendElement();
|
|
|
|
img->mImage = aImages[i].mImage;
|
|
|
|
img->mTimeStamp = aImages[i].mTimeStamp;
|
|
|
|
img->mFrameID = aImages[i].mFrameID;
|
|
|
|
img->mProducerID = aImages[i].mProducerID;
|
2018-07-17 00:19:09 +03:00
|
|
|
for (const auto& oldImg : mCurrentImages) {
|
2015-05-25 05:33:35 +03:00
|
|
|
if (oldImg.mFrameID == img->mFrameID &&
|
|
|
|
oldImg.mProducerID == img->mProducerID) {
|
|
|
|
img->mComposited = oldImg.mComposited;
|
|
|
|
break;
|
|
|
|
}
|
2015-05-11 05:07:07 +03:00
|
|
|
}
|
2015-05-12 03:56:09 +03:00
|
|
|
}
|
2015-05-25 05:33:35 +03:00
|
|
|
|
|
|
|
mCurrentImages.SwapElements(newImages);
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2015-03-26 04:17:13 +03:00
|
|
|
void ImageContainer::ClearImagesFromImageBridge() {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2015-05-25 05:33:35 +03:00
|
|
|
SetCurrentImageInternal(nsTArray<NonOwningImage>());
|
2013-09-12 18:50:28 +04:00
|
|
|
}
|
|
|
|
|
2015-07-07 00:58:18 +03:00
|
|
|
void ImageContainer::SetCurrentImages(const nsTArray<NonOwningImage>& aImages) {
|
|
|
|
MOZ_ASSERT(!aImages.IsEmpty());
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2017-07-15 14:52:02 +03:00
|
|
|
if (mIsAsync) {
|
2016-09-14 02:30:56 +03:00
|
|
|
if (RefPtr<ImageBridgeChild> imageBridge =
|
|
|
|
ImageBridgeChild::GetSingleton()) {
|
2017-07-15 14:52:02 +03:00
|
|
|
imageBridge->UpdateImageClient(this);
|
2016-09-14 02:30:56 +03:00
|
|
|
}
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
2015-05-25 05:33:35 +03:00
|
|
|
SetCurrentImageInternal(aImages);
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 19:09:15 +04:00
|
|
|
void ImageContainer::ClearAllImages() {
|
2016-07-18 11:54:01 +03:00
|
|
|
if (mImageClient) {
|
2015-03-26 04:17:13 +03:00
|
|
|
// Let ImageClient release all TextureClients. This doesn't return
|
|
|
|
// until ImageBridge has called ClearCurrentImageFromImageBridge.
|
2016-09-14 02:30:56 +03:00
|
|
|
if (RefPtr<ImageBridgeChild> imageBridge =
|
|
|
|
ImageBridgeChild::GetSingleton()) {
|
|
|
|
imageBridge->FlushAllImages(mImageClient, this);
|
|
|
|
}
|
2013-10-17 19:09:15 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-12-12 05:44:45 +04:00
|
|
|
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2015-05-25 05:33:35 +03:00
|
|
|
SetCurrentImageInternal(nsTArray<NonOwningImage>());
|
2013-10-17 19:09:15 +04:00
|
|
|
}
|
|
|
|
|
2016-06-28 04:25:01 +03:00
|
|
|
void ImageContainer::ClearCachedResources() {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2016-06-28 04:25:01 +03:00
|
|
|
if (mImageClient && mImageClient->AsImageClientSingle()) {
|
2016-06-30 05:12:31 +03:00
|
|
|
if (!mImageClient->HasTextureClientRecycler()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mImageClient->GetTextureClientRecycler()->ShrinkToMinimumSize();
|
2016-06-28 04:25:01 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return mRecycleBin->ClearRecycledBuffers();
|
|
|
|
}
|
|
|
|
|
2012-08-19 23:33:25 +04:00
|
|
|
void ImageContainer::SetCurrentImageInTransaction(Image* aImage) {
|
2016-01-12 07:14:09 +03:00
|
|
|
AutoTArray<NonOwningImage, 1> images;
|
|
|
|
images.AppendElement(NonOwningImage(aImage));
|
|
|
|
SetCurrentImagesInTransaction(images);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageContainer::SetCurrentImagesInTransaction(
|
|
|
|
const nsTArray<NonOwningImage>& aImages) {
|
2012-08-19 23:33:25 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
Bug 825928: Land layers refactoring. r=jrmuizel,bas,nical,mattwoodrow,roc,nrc,benwa,bjacob,jgilbert,kchen CLOSED TREE
Please contact Bas Schouten <bschouten@mozilla.com>, Nicolas Silva <nsilva@mozilla.com> or Nicholas Cameron <ncameron@mozilla.com> with general questions. Below is a rough list of authors to contact with specific questions.
Authors:
gfx/layers/Compositor.* gfx/layers/Effects.h - Compositor Interface - bas,nrc,nical
gfx/layers/d3d* - D3D9/D3D10 - bas
gfx/layers/ThebesLayer* - ThebesLayers - nrc,bas
gfx/layers/composite/* - CompositeLayers - nrc,nical
gfx/layers/client/* - Client - nrc,nical,bas
gfx/layers/*Image* - nical
gfx/layers/ipc ipc - IPC - nical
gfx/layers/opengl - CompositorOGL - nrc,nical
gfx/2d - bas,nrc
gfx/gl - GLContext - bjacob
dom/* layout/* - DOM - mattwoodrow
2013-04-10 13:20:52 +04:00
|
|
|
NS_ASSERTION(!mImageClient,
|
|
|
|
"Should use async image transfer with ImageBridge.");
|
|
|
|
|
2016-01-12 07:14:09 +03:00
|
|
|
SetCurrentImageInternal(aImages);
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2015-03-26 04:08:30 +03:00
|
|
|
bool ImageContainer::IsAsync() const { return mIsAsync; }
|
2012-08-19 23:33:25 +04:00
|
|
|
|
2017-01-18 05:47:05 +03:00
|
|
|
CompositableHandle ImageContainer::GetAsyncContainerHandle() {
|
2017-02-02 10:51:51 +03:00
|
|
|
NS_ASSERTION(IsAsync(),
|
|
|
|
"Shared image ID is only relevant to async ImageContainers");
|
|
|
|
NS_ASSERTION(mAsyncContainerHandle, "Should have a shared image ID");
|
2017-09-26 03:58:37 +03:00
|
|
|
RecursiveMutexAutoLock mon(mRecursiveMutex);
|
2017-02-02 10:51:51 +03:00
|
|
|
EnsureImageClient();
|
2017-01-18 05:47:05 +03:00
|
|
|
return mAsyncContainerHandle;
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageContainer::HasCurrentImage() {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2012-08-19 23:33:25 +04:00
|
|
|
|
2015-05-25 05:33:35 +03:00
|
|
|
return !mCurrentImages.IsEmpty();
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2015-05-12 03:56:09 +03:00
|
|
|
void ImageContainer::GetCurrentImages(nsTArray<OwningImage>* aImages,
|
|
|
|
uint32_t* aGenerationCounter) {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2014-01-15 19:06:43 +04:00
|
|
|
|
2015-05-25 05:33:35 +03:00
|
|
|
*aImages = mCurrentImages;
|
2015-05-12 03:56:09 +03:00
|
|
|
if (aGenerationCounter) {
|
|
|
|
*aGenerationCounter = mGenerationCounter;
|
|
|
|
}
|
2014-01-15 19:06:43 +04:00
|
|
|
}
|
|
|
|
|
2012-08-19 23:33:25 +04:00
|
|
|
gfx::IntSize ImageContainer::GetCurrentSize() {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2012-08-19 23:33:25 +04:00
|
|
|
|
2015-05-25 05:33:35 +03:00
|
|
|
if (mCurrentImages.IsEmpty()) {
|
2013-12-31 13:06:12 +04:00
|
|
|
return gfx::IntSize(0, 0);
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2015-05-25 05:33:35 +03:00
|
|
|
return mCurrentImages[0].mImage->GetSize();
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2017-01-04 18:19:30 +03:00
|
|
|
void ImageContainer::NotifyComposite(
|
|
|
|
const ImageCompositeNotification& aNotification) {
|
2017-07-04 20:47:42 +03:00
|
|
|
RecursiveMutexAutoLock lock(mRecursiveMutex);
|
2015-07-03 10:39:09 +03:00
|
|
|
|
2015-06-15 05:45:59 +03:00
|
|
|
// An image composition notification is sent the first time a particular
|
|
|
|
// image is composited by an ImageHost. Thus, every time we receive such
|
|
|
|
// a notification, a new image has been painted.
|
|
|
|
++mPaintCount;
|
|
|
|
|
2015-05-25 05:33:35 +03:00
|
|
|
if (aNotification.producerID() == mCurrentProducerID) {
|
|
|
|
for (auto& img : mCurrentImages) {
|
|
|
|
if (img.mFrameID == aNotification.frameID()) {
|
|
|
|
img.mComposited = true;
|
2015-05-11 05:07:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 10:39:09 +03:00
|
|
|
if (!aNotification.imageTimeStamp().IsNull()) {
|
2018-07-17 00:19:09 +03:00
|
|
|
mPaintDelay = aNotification.firstCompositeTimeStamp() -
|
|
|
|
aNotification.imageTimeStamp();
|
2015-07-03 10:39:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:09 +03:00
|
|
|
void ImageContainer::NotifyDropped(uint32_t aDropped) {
|
|
|
|
mDroppedImageCount += aDropped;
|
|
|
|
}
|
|
|
|
|
2017-05-04 00:23:45 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
D3D11YCbCrRecycleAllocator* ImageContainer::GetD3D11YCbCrRecycleAllocator(
|
|
|
|
KnowsCompositor* aAllocator) {
|
|
|
|
if (mD3D11YCbCrRecycleAllocator &&
|
|
|
|
aAllocator == mD3D11YCbCrRecycleAllocator->GetAllocator()) {
|
|
|
|
return mD3D11YCbCrRecycleAllocator;
|
|
|
|
}
|
|
|
|
|
2018-05-31 03:35:24 +03:00
|
|
|
if (!aAllocator->SupportsD3D11() ||
|
|
|
|
!gfx::DeviceManagerDx::Get()->GetImageDevice()) {
|
2017-05-04 00:23:45 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-31 03:35:24 +03:00
|
|
|
mD3D11YCbCrRecycleAllocator = new D3D11YCbCrRecycleAllocator(aAllocator);
|
2017-05-04 00:23:45 +03:00
|
|
|
return mD3D11YCbCrRecycleAllocator;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-06 21:55:31 +03:00
|
|
|
PlanarYCbCrImage::PlanarYCbCrImage()
|
2014-01-31 02:58:49 +04:00
|
|
|
: Image(nullptr, ImageFormat::PLANAR_YCBCR),
|
2016-01-08 07:57:38 +03:00
|
|
|
mOffscreenFormat(SurfaceFormat::UNKNOWN),
|
2015-11-06 21:55:31 +03:00
|
|
|
mBufferSize(0) {}
|
2012-08-19 23:33:25 +04:00
|
|
|
|
2018-11-14 21:05:28 +03:00
|
|
|
nsresult PlanarYCbCrImage::BuildSurfaceDescriptorBuffer(
|
|
|
|
SurfaceDescriptorBuffer& aSdBuffer) {
|
|
|
|
const PlanarYCbCrData* pdata = GetData();
|
|
|
|
MOZ_ASSERT(pdata, "must have PlanarYCbCrData");
|
|
|
|
MOZ_ASSERT(pdata->mYSkip == 0 && pdata->mCbSkip == 0 && pdata->mCrSkip == 0,
|
|
|
|
"YCbCrDescriptor doesn't hold skip values");
|
|
|
|
MOZ_ASSERT(pdata->mPicX == 0 && pdata->mPicY == 0,
|
|
|
|
"YCbCrDescriptor doesn't hold picx or picy");
|
|
|
|
|
|
|
|
uint32_t yOffset;
|
|
|
|
uint32_t cbOffset;
|
|
|
|
uint32_t crOffset;
|
|
|
|
ImageDataSerializer::ComputeYCbCrOffsets(
|
|
|
|
pdata->mYStride, pdata->mYSize.height, pdata->mCbCrStride,
|
|
|
|
pdata->mCbCrSize.height, yOffset, cbOffset, crOffset);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-14 21:05:28 +03:00
|
|
|
aSdBuffer.desc() = YCbCrDescriptor(
|
|
|
|
pdata->mYSize, pdata->mYStride, pdata->mCbCrSize, pdata->mCbCrStride,
|
|
|
|
yOffset, cbOffset, crOffset, pdata->mStereoMode, pdata->mColorDepth,
|
2019-07-26 11:45:31 +03:00
|
|
|
pdata->mYUVColorSpace, pdata->mColorRange,
|
2018-11-14 21:05:28 +03:00
|
|
|
/*hasIntermediateBuffer*/ false);
|
|
|
|
|
|
|
|
uint8_t* buffer = nullptr;
|
|
|
|
const MemoryOrShmem& memOrShmem = aSdBuffer.data();
|
|
|
|
switch (memOrShmem.type()) {
|
|
|
|
case MemoryOrShmem::Tuintptr_t:
|
|
|
|
buffer = reinterpret_cast<uint8_t*>(memOrShmem.get_uintptr_t());
|
|
|
|
break;
|
|
|
|
case MemoryOrShmem::TShmem:
|
|
|
|
buffer = memOrShmem.get_Shmem().get<uint8_t>();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unknown MemoryOrShmem type");
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(buffer, "no valid buffer available to copy image data");
|
|
|
|
if (!buffer) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyPlane(buffer + yOffset, pdata->mYChannel, pdata->mYSize, pdata->mYStride,
|
|
|
|
pdata->mYSkip);
|
|
|
|
CopyPlane(buffer + cbOffset, pdata->mCbChannel, pdata->mCbCrSize,
|
|
|
|
pdata->mCbCrStride, pdata->mCbSkip);
|
|
|
|
CopyPlane(buffer + crOffset, pdata->mCrChannel, pdata->mCbCrSize,
|
|
|
|
pdata->mCbCrStride, pdata->mCrSkip);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-06 21:55:31 +03:00
|
|
|
RecyclingPlanarYCbCrImage::~RecyclingPlanarYCbCrImage() {
|
2012-08-19 23:33:25 +04:00
|
|
|
if (mBuffer) {
|
2018-05-30 22:15:35 +03:00
|
|
|
mRecycleBin->RecycleBuffer(std::move(mBuffer), mBufferSize);
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 21:55:31 +03:00
|
|
|
size_t RecyclingPlanarYCbCrImage::SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
2014-03-20 01:33:12 +04:00
|
|
|
// Ignoring:
|
|
|
|
// - mData - just wraps mBuffer
|
|
|
|
// - Surfaces should be reported under gfx-surfaces-*:
|
|
|
|
// - mSourceSurface
|
|
|
|
// - Base class:
|
|
|
|
// - mImplData is not used
|
|
|
|
// Not owned:
|
|
|
|
// - mRecycleBin
|
2015-10-20 20:16:04 +03:00
|
|
|
size_t size = aMallocSizeOf(mBuffer.get());
|
2014-03-20 01:33:12 +04:00
|
|
|
|
|
|
|
// Could add in the future:
|
|
|
|
// - mBackendData (from base class)
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2015-11-06 21:55:31 +03:00
|
|
|
UniquePtr<uint8_t[]> RecyclingPlanarYCbCrImage::AllocateBuffer(uint32_t aSize) {
|
2014-07-12 00:50:23 +04:00
|
|
|
return mRecycleBin->GetBuffer(aSize);
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2012-09-04 21:10:06 +04:00
|
|
|
static void CopyPlane(uint8_t* aDst, const uint8_t* aSrc,
|
2013-12-20 20:46:29 +04:00
|
|
|
const gfx::IntSize& aSize, int32_t aStride,
|
|
|
|
int32_t aSkip) {
|
2018-02-08 03:09:32 +03:00
|
|
|
int32_t height = aSize.height;
|
|
|
|
int32_t width = aSize.width;
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(width <= aStride);
|
|
|
|
|
2012-08-26 07:22:51 +04:00
|
|
|
if (!aSkip) {
|
2012-08-23 21:01:14 +04:00
|
|
|
// Fast path: planar input.
|
2018-02-08 03:09:32 +03:00
|
|
|
memcpy(aDst, aSrc, height * aStride);
|
2012-08-23 21:01:14 +04:00
|
|
|
} else {
|
|
|
|
for (int y = 0; y < height; ++y) {
|
2012-09-04 21:10:06 +04:00
|
|
|
const uint8_t* src = aSrc;
|
2012-08-23 21:01:14 +04:00
|
|
|
uint8_t* dst = aDst;
|
2012-08-26 07:22:51 +04:00
|
|
|
// Slow path
|
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
*dst++ = *src++;
|
|
|
|
src += aSkip;
|
2012-08-23 21:01:14 +04:00
|
|
|
}
|
2012-09-04 21:10:06 +04:00
|
|
|
aSrc += aStride;
|
|
|
|
aDst += aStride;
|
2012-08-23 21:01:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 21:55:31 +03:00
|
|
|
bool RecyclingPlanarYCbCrImage::CopyData(const Data& aData) {
|
2012-08-19 23:33:25 +04:00
|
|
|
// update buffer size
|
2017-03-30 05:52:12 +03:00
|
|
|
// Use uint32_t throughout to match AllocateBuffer's param and mBufferSize
|
|
|
|
const auto checkedSize =
|
2018-02-08 03:09:32 +03:00
|
|
|
CheckedInt<uint32_t>(aData.mCbCrStride) * aData.mCbCrSize.height * 2 +
|
|
|
|
CheckedInt<uint32_t>(aData.mYStride) * aData.mYSize.height;
|
2017-03-30 05:52:12 +03:00
|
|
|
|
|
|
|
if (!checkedSize.isValid()) return false;
|
|
|
|
|
|
|
|
const auto size = checkedSize.value();
|
2012-08-23 21:01:14 +04:00
|
|
|
|
2012-08-19 23:33:25 +04:00
|
|
|
// get new buffer
|
2013-12-11 10:58:51 +04:00
|
|
|
mBuffer = AllocateBuffer(size);
|
2015-11-03 14:24:26 +03:00
|
|
|
if (!mBuffer) return false;
|
2012-08-19 23:33:25 +04:00
|
|
|
|
2013-12-11 10:58:51 +04:00
|
|
|
// update buffer size
|
|
|
|
mBufferSize = size;
|
|
|
|
|
2018-02-08 03:09:32 +03:00
|
|
|
mData = aData;
|
2015-10-20 20:16:04 +03:00
|
|
|
mData.mYChannel = mBuffer.get();
|
2012-08-23 21:01:14 +04:00
|
|
|
mData.mCbChannel = mData.mYChannel + mData.mYStride * mData.mYSize.height;
|
|
|
|
mData.mCrChannel =
|
|
|
|
mData.mCbChannel + mData.mCbCrStride * mData.mCbCrSize.height;
|
2018-02-08 03:09:32 +03:00
|
|
|
mData.mYSkip = mData.mCbSkip = mData.mCrSkip = 0;
|
2012-08-23 21:01:14 +04:00
|
|
|
|
|
|
|
CopyPlane(mData.mYChannel, aData.mYChannel, aData.mYSize, aData.mYStride,
|
2018-02-08 03:09:32 +03:00
|
|
|
aData.mYSkip);
|
2012-08-23 21:01:14 +04:00
|
|
|
CopyPlane(mData.mCbChannel, aData.mCbChannel, aData.mCbCrSize,
|
2018-02-08 03:09:32 +03:00
|
|
|
aData.mCbCrStride, aData.mCbSkip);
|
2012-08-23 21:01:14 +04:00
|
|
|
CopyPlane(mData.mCrChannel, aData.mCrChannel, aData.mCbCrSize,
|
2018-02-08 03:09:32 +03:00
|
|
|
aData.mCbCrStride, aData.mCrSkip);
|
2012-08-23 21:01:14 +04:00
|
|
|
|
|
|
|
mSize = aData.mPicSize;
|
2016-05-23 03:37:43 +03:00
|
|
|
mOrigin = gfx::IntPoint(aData.mPicX, aData.mPicY);
|
2015-11-03 14:24:26 +03:00
|
|
|
return true;
|
2012-08-19 23:33:25 +04:00
|
|
|
}
|
|
|
|
|
2017-11-30 21:03:02 +03:00
|
|
|
gfxImageFormat PlanarYCbCrImage::GetOffscreenFormat() const {
|
2016-01-08 07:57:38 +03:00
|
|
|
return mOffscreenFormat == SurfaceFormat::UNKNOWN ? gfxVars::OffscreenFormat()
|
2013-01-24 03:08:16 +04:00
|
|
|
: mOffscreenFormat;
|
|
|
|
}
|
|
|
|
|
2017-10-04 04:26:45 +03:00
|
|
|
bool PlanarYCbCrImage::AdoptData(const Data& aData) {
|
2013-03-14 00:11:15 +04:00
|
|
|
mData = aData;
|
|
|
|
mSize = aData.mPicSize;
|
2016-05-23 03:37:43 +03:00
|
|
|
mOrigin = gfx::IntPoint(aData.mPicX, aData.mPicY);
|
2015-11-03 14:24:26 +03:00
|
|
|
return true;
|
2013-03-14 00:11:15 +04:00
|
|
|
}
|
|
|
|
|
2014-01-21 20:49:53 +04:00
|
|
|
already_AddRefed<gfx::SourceSurface> PlanarYCbCrImage::GetAsSourceSurface() {
|
|
|
|
if (mSourceSurface) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfx::SourceSurface> surface(mSourceSurface);
|
2015-04-30 22:20:30 +03:00
|
|
|
return surface.forget();
|
2014-01-21 20:49:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::IntSize size(mSize);
|
|
|
|
gfx::SurfaceFormat format =
|
|
|
|
gfx::ImageFormatToSurfaceFormat(GetOffscreenFormat());
|
|
|
|
gfx::GetYCbCrToRGBDestFormatAndSize(mData, format, size);
|
|
|
|
if (mSize.width > PlanarYCbCrImage::MAX_DIMENSION ||
|
|
|
|
mSize.height > PlanarYCbCrImage::MAX_DIMENSION) {
|
|
|
|
NS_ERROR("Illegal image dest width or height");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfx::DataSourceSurface> surface =
|
|
|
|
gfx::Factory::CreateDataSourceSurface(size, format);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!surface)) {
|
2014-06-17 03:47:28 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-01-21 20:49:53 +04:00
|
|
|
|
2015-06-10 14:01:00 +03:00
|
|
|
DataSourceSurface::ScopedMap mapping(surface, DataSourceSurface::WRITE);
|
|
|
|
if (NS_WARN_IF(!mapping.IsMapped())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::ConvertYCbCrToRGB(mData, format, size, mapping.GetData(),
|
|
|
|
mapping.GetStride());
|
2014-01-21 20:49:53 +04:00
|
|
|
|
|
|
|
mSourceSurface = surface;
|
|
|
|
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2016-03-10 05:35:27 +03:00
|
|
|
NVImage::NVImage() : Image(nullptr, ImageFormat::NV_IMAGE), mBufferSize(0) {}
|
|
|
|
|
2016-11-15 12:04:18 +03:00
|
|
|
NVImage::~NVImage() = default;
|
2016-03-10 05:35:27 +03:00
|
|
|
|
2017-11-30 21:03:02 +03:00
|
|
|
IntSize NVImage::GetSize() const { return mSize; }
|
2016-03-10 05:35:27 +03:00
|
|
|
|
2017-11-30 21:03:02 +03:00
|
|
|
IntRect NVImage::GetPictureRect() const { return mData.GetPictureRect(); }
|
2016-03-10 05:35:27 +03:00
|
|
|
|
|
|
|
already_AddRefed<SourceSurface> NVImage::GetAsSourceSurface() {
|
|
|
|
if (mSourceSurface) {
|
|
|
|
RefPtr<gfx::SourceSurface> surface(mSourceSurface);
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the current NV12 or NV21 data to YUV420P so that we can follow the
|
|
|
|
// logics in PlanarYCbCrImage::GetAsSourceSurface().
|
|
|
|
const int bufferLength = mData.mYSize.height * mData.mYStride +
|
|
|
|
mData.mCbCrSize.height * mData.mCbCrSize.width * 2;
|
2016-11-15 12:00:08 +03:00
|
|
|
auto* buffer = new uint8_t[bufferLength];
|
2016-03-10 05:35:27 +03:00
|
|
|
|
|
|
|
Data aData = mData;
|
|
|
|
aData.mCbCrStride = aData.mCbCrSize.width;
|
|
|
|
aData.mCbSkip = 0;
|
|
|
|
aData.mCrSkip = 0;
|
|
|
|
aData.mYChannel = buffer;
|
|
|
|
aData.mCbChannel = aData.mYChannel + aData.mYSize.height * aData.mYStride;
|
|
|
|
aData.mCrChannel =
|
|
|
|
aData.mCbChannel + aData.mCbCrSize.height * aData.mCbCrStride;
|
|
|
|
|
|
|
|
if (mData.mCbChannel < mData.mCrChannel) { // NV12
|
|
|
|
libyuv::NV12ToI420(mData.mYChannel, mData.mYStride, mData.mCbChannel,
|
|
|
|
mData.mCbCrStride, aData.mYChannel, aData.mYStride,
|
|
|
|
aData.mCbChannel, aData.mCbCrStride, aData.mCrChannel,
|
|
|
|
aData.mCbCrStride, aData.mYSize.width,
|
|
|
|
aData.mYSize.height);
|
|
|
|
} else { // NV21
|
|
|
|
libyuv::NV21ToI420(mData.mYChannel, mData.mYStride, mData.mCrChannel,
|
|
|
|
mData.mCbCrStride, aData.mYChannel, aData.mYStride,
|
|
|
|
aData.mCbChannel, aData.mCbCrStride, aData.mCrChannel,
|
|
|
|
aData.mCbCrStride, aData.mYSize.width,
|
|
|
|
aData.mYSize.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The logics in PlanarYCbCrImage::GetAsSourceSurface().
|
|
|
|
gfx::IntSize size(mSize);
|
|
|
|
gfx::SurfaceFormat format = gfx::ImageFormatToSurfaceFormat(
|
|
|
|
gfxPlatform::GetPlatform()->GetOffscreenFormat());
|
|
|
|
gfx::GetYCbCrToRGBDestFormatAndSize(aData, format, size);
|
|
|
|
if (mSize.width > PlanarYCbCrImage::MAX_DIMENSION ||
|
|
|
|
mSize.height > PlanarYCbCrImage::MAX_DIMENSION) {
|
|
|
|
NS_ERROR("Illegal image dest width or height");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<gfx::DataSourceSurface> surface =
|
|
|
|
gfx::Factory::CreateDataSourceSurface(size, format);
|
|
|
|
if (NS_WARN_IF(!surface)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface::ScopedMap mapping(surface, DataSourceSurface::WRITE);
|
|
|
|
if (NS_WARN_IF(!mapping.IsMapped())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::ConvertYCbCrToRGB(aData, format, size, mapping.GetData(),
|
|
|
|
mapping.GetStride());
|
|
|
|
|
|
|
|
mSourceSurface = surface;
|
|
|
|
|
|
|
|
// Release the temporary buffer.
|
|
|
|
delete[] buffer;
|
|
|
|
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2017-11-30 21:03:02 +03:00
|
|
|
bool NVImage::IsValid() const { return !!mBufferSize; }
|
2016-03-10 05:35:27 +03:00
|
|
|
|
|
|
|
uint32_t NVImage::GetBufferSize() const { return mBufferSize; }
|
|
|
|
|
|
|
|
NVImage* NVImage::AsNVImage() { return this; };
|
|
|
|
|
|
|
|
bool NVImage::SetData(const Data& aData) {
|
|
|
|
MOZ_ASSERT(aData.mCbSkip == 1 && aData.mCrSkip == 1);
|
|
|
|
MOZ_ASSERT((int)std::abs(aData.mCbChannel - aData.mCrChannel) == 1);
|
|
|
|
|
|
|
|
// Calculate buffer size
|
2017-03-30 05:52:12 +03:00
|
|
|
// Use uint32_t throughout to match AllocateBuffer's param and mBufferSize
|
|
|
|
const auto checkedSize =
|
|
|
|
CheckedInt<uint32_t>(aData.mYSize.height) * aData.mYStride +
|
|
|
|
CheckedInt<uint32_t>(aData.mCbCrSize.height) * aData.mCbCrStride;
|
|
|
|
|
|
|
|
if (!checkedSize.isValid()) return false;
|
|
|
|
|
|
|
|
const auto size = checkedSize.value();
|
2016-03-10 05:35:27 +03:00
|
|
|
|
|
|
|
// Allocate a new buffer.
|
|
|
|
mBuffer = AllocateBuffer(size);
|
|
|
|
if (!mBuffer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update mBufferSize.
|
|
|
|
mBufferSize = size;
|
|
|
|
|
|
|
|
// Update mData.
|
|
|
|
mData = aData;
|
|
|
|
mData.mYChannel = mBuffer.get();
|
|
|
|
mData.mCbChannel = mData.mYChannel + (aData.mCbChannel - aData.mYChannel);
|
|
|
|
mData.mCrChannel = mData.mYChannel + (aData.mCrChannel - aData.mYChannel);
|
|
|
|
|
|
|
|
// Update mSize.
|
|
|
|
mSize = aData.mPicSize;
|
|
|
|
|
|
|
|
// Copy the input data into mBuffer.
|
|
|
|
// This copies the y-channel and the interleaving CbCr-channel.
|
|
|
|
memcpy(mData.mYChannel, aData.mYChannel, mBufferSize);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NVImage::Data* NVImage::GetData() const { return &mData; }
|
|
|
|
|
|
|
|
UniquePtr<uint8_t> NVImage::AllocateBuffer(uint32_t aSize) {
|
|
|
|
UniquePtr<uint8_t> buffer(new uint8_t[aSize]);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2017-11-30 21:03:02 +03:00
|
|
|
SourceSurfaceImage::SourceSurfaceImage(const gfx::IntSize& aSize,
|
|
|
|
gfx::SourceSurface* aSourceSurface)
|
|
|
|
: Image(nullptr, ImageFormat::CAIRO_SURFACE),
|
|
|
|
mSize(aSize),
|
|
|
|
mSourceSurface(aSourceSurface),
|
|
|
|
mTextureFlags(TextureFlags::DEFAULT) {}
|
2014-02-17 02:23:39 +04:00
|
|
|
|
2016-02-27 08:01:50 +03:00
|
|
|
SourceSurfaceImage::SourceSurfaceImage(gfx::SourceSurface* aSourceSurface)
|
2017-11-30 21:03:02 +03:00
|
|
|
: Image(nullptr, ImageFormat::CAIRO_SURFACE),
|
|
|
|
mSize(aSourceSurface->GetSize()),
|
|
|
|
mSourceSurface(aSourceSurface),
|
|
|
|
mTextureFlags(TextureFlags::DEFAULT) {}
|
2016-02-27 08:01:50 +03:00
|
|
|
|
2016-11-15 12:04:18 +03:00
|
|
|
SourceSurfaceImage::~SourceSurfaceImage() = default;
|
2014-02-17 02:23:39 +04:00
|
|
|
|
2016-09-27 06:22:20 +03:00
|
|
|
TextureClient* SourceSurfaceImage::GetTextureClient(
|
2019-06-07 15:27:05 +03:00
|
|
|
KnowsCompositor* aKnowsCompositor) {
|
|
|
|
if (!aKnowsCompositor) {
|
2014-05-06 16:34:19 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:27:05 +03:00
|
|
|
auto entry = mTextureClients.LookupForAdd(aKnowsCompositor->GetSerial());
|
2017-06-14 18:27:25 +03:00
|
|
|
if (entry) {
|
|
|
|
return entry.Data();
|
2014-02-17 02:23:39 +04:00
|
|
|
}
|
|
|
|
|
2017-06-14 18:27:25 +03:00
|
|
|
RefPtr<TextureClient> textureClient;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface = GetAsSourceSurface();
|
2014-02-17 02:23:39 +04:00
|
|
|
MOZ_ASSERT(surface);
|
2017-06-14 18:27:25 +03:00
|
|
|
if (surface) {
|
2014-11-13 18:53:49 +03:00
|
|
|
// gfx::BackendType::NONE means default to content backend
|
2017-06-14 18:27:25 +03:00
|
|
|
textureClient = TextureClient::CreateFromSurface(
|
2019-06-07 15:27:05 +03:00
|
|
|
aKnowsCompositor, surface, BackendSelector::Content, mTextureFlags,
|
2017-06-14 18:27:25 +03:00
|
|
|
ALLOC_DEFAULT);
|
2014-11-13 18:53:49 +03:00
|
|
|
}
|
2017-06-14 18:27:25 +03:00
|
|
|
if (textureClient) {
|
2019-06-07 15:27:05 +03:00
|
|
|
textureClient->SyncWithObject(aKnowsCompositor->GetSyncObject());
|
2017-06-14 18:27:25 +03:00
|
|
|
entry.OrInsert([&textureClient]() { return textureClient; });
|
|
|
|
return textureClient;
|
2014-05-06 16:34:19 +04:00
|
|
|
}
|
2015-07-27 18:47:29 +03:00
|
|
|
|
2017-06-14 18:27:25 +03:00
|
|
|
// Remove the speculatively added entry.
|
2018-03-28 19:58:49 +03:00
|
|
|
entry.OrRemove();
|
2017-06-14 18:27:25 +03:00
|
|
|
return nullptr;
|
2014-02-17 02:23:39 +04:00
|
|
|
}
|
|
|
|
|
2015-05-25 05:33:35 +03:00
|
|
|
ImageContainer::ProducerID ImageContainer::AllocateProducerID() {
|
2015-10-11 19:13:10 +03:00
|
|
|
// Callable on all threads.
|
|
|
|
static Atomic<ImageContainer::ProducerID> sProducerID(0u);
|
2015-05-25 05:33:35 +03:00
|
|
|
return ++sProducerID;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|