Files
palemoon27/gfx/layers/ImageContainer.cpp
T
roytam1 96836f9b35 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1149185 - override GetMessageLoop() r=nical (42bec9405)
- Add a "blacklisted" property to d3d11 telemetry. (bug 1187453, r=mattwoodrow) (83bcb0e8e)
- Don't create a D3D11 compositor device on content processes. (bug 1183910 part 8, r=bas,mattwoodrow) (c03d496a6)
- Bug 1168889 - ContentClientSingleBuffered needs to upload during the transaction. r=nical (35171fe14)
- Bug 1176077 - Only recompute PaintedLayer item visibility when display list has changed. r=mattwoodrow (0ea691594)
- Bug 1179298. ClientTiledPaintedLayer should allow empty transactions to succeed if it doesn't need to draw anything. r=mattwoodrow (4aaad97b2)
- Bug 1169200 - Send OpRemoveTextureAsync before actor destroyed r=nical (86007156c)
- Bug 1158838 - Add some gfxCriticalError logging. r=milan (32bf80848)
- Bug 1129871 - Avoid copying TileClient when not necessary. r=BenWa (1ac30bcc2)
- Bug 1186911 - Fix progressive paint when using tiled-drawtarget. r=nical (87649fb8d)
- Bug 1118876 - Add edge padding with DrawTargetTiled. r=jrmuizel (64b12cf6a)
- Bug 1157677 - Fix uninitialized members warning in ClientTiledLayerBuffer. r=nical (d11d0da30)
- Bug 1151071 - Make sure low-resolution tiles are transparent. r=mattwoodrow (164446378)
- Bug 1170390 - Use Optimal2DFormatForContent over OptimalFormatForContent to avoid enum conversions. r=jrmuizel (db7ee2c31)
- Bug 1180326 - Part 1: Abstract parts of the client side tiling that we need. r=jrmuizel (0ad7651fe)
- Bug 1179987 - Remove painted region from SurfaceDescriptorTiles since it isn't used any more. r=nical (653e9389e)
- Bug 1180326 - Part 2: Add support for variable tile sizes. r=jrmuizel (081349a5e)
- Bug 1180326 - Part 3: Move repeated code into GetBackBuffer. r=jrmuizel (2e9091ae1)
- Bug 1180326 - Part 4: Add new content client. r=jrmuizel (7362d805b)
- Bug 1180326 - Part 5: Support HWC with tiling when we only have 1 tile. r=jrmuizel (09ce93b7e)
- Bug 1152461 - Don't use tiled layers for overflow:hidden scroll frames. r=tn (804a683a1)
- Bug 961887 - Add some reftest fuzz. (dcd707004)
- Bug 1186061 patch 1 - Add feature to reftest harness to allow skipping the flush. r=mattwoodrow (8cefe9640)
- Bug 1176969 - Disable OMT animation for any frame in a preserve-3d scene rather than only frames whose parent and child are in a preserve-3d scene. r=mattwoodrow (693d0a518)
- Bug 1176969 followup - Annotate new reftests as intermittently fuzzy on Mac. (0269d5d8f)
- Bug 1176969 followup - Increase error amounts for intermittent fuzzy-if(cocoaWidget). (116039ed0)
- Bug 1176969 followup - Mark test as random for now until I have a chance to rewrite it. (8ba561f83)
- Bug 1176969 followup - Reorganize the timing of the reftest to make it more reliable. (f3ffc93cd)
- Bug 1186061 patch 2 - Disable compositor thread animation of transforms when backface-visibility is hidden. r=mattwoodrow (e67bed9fb)
- Bug 1169666 - Mark reftests failing due to scrollbar transparency random on GTK3. r=karlt (0c6080d55)
- Bug 1180326 - Part 6: Use SingleTiledContentClient for non-scrollable layers on b2g and OSX. r=jrmuizel (079951f0a)
- Bug 1189261 - Mark tile invalid region in coordinates relative to the tile. r=jrmuizel (7e696c911)
- Bug 1187619 - Only optmimize FrameLayerBuilder visibility calculations if correct. r=mattwoodrow (05228ad31)
- Bug 1190950: Check mappings and surfaces for UpdateFromSurface more robustly. r=mattwoodrow (c00adba10)
- Remove the backend flag to TextureClient::CreateForDrawing. (bug 1183910 part 9, r=mattwoodrow) (5df1d964c)
- Bug 1135935 - Part 1: Don't implement ISurfaceAllocator for the texture recycler. r=sotaro (bcba93d3d)
- Bug 1135935 - Part 2: Merge TextureClientRecycleAllocator into base class. r=sotaro (8f4e7b6b1)
- Bug 1135935 - Part 3: Allow overriding of allocations for texture client recycling. r=sotaro (35d7499e9)
- Bug 1135935 - Part 4: Move IDirect3DTexture9 allocations and ownership into the TextureClient. r=jrmuizel (e62cf9acf)
- Bug 1135935 - Part 5: Add D3D9 texture recycler. r=jrmuizel (8ab85b064)
- Bug 1135935 - Part 6: Wait for the compositor to stop using textures before recycling them. r=jrmuizel (ae0c45cfd)
2021-10-12 09:46:43 +08:00

617 lines
16 KiB
C++

/* -*- 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 "ImageContainer.h"
#include <string.h> // for memcpy, memset
#include "GLImages.h" // for SurfaceTextureImage
#include "gfx2DGlue.h"
#include "gfxPlatform.h" // for gfxPlatform
#include "gfxUtils.h" // for gfxUtils
#include "mozilla/RefPtr.h" // for already_AddRefed
#include "mozilla/ipc/CrossProcessMutex.h" // for CrossProcessMutex, etc
#include "mozilla/layers/CompositorTypes.h"
#include "mozilla/layers/ImageBridgeChild.h" // for ImageBridgeChild
#include "mozilla/layers/PImageContainerChild.h"
#include "mozilla/layers/ImageClient.h" // for ImageClient
#include "nsISupportsUtils.h" // for NS_IF_ADDREF
#include "YCbCrUtils.h" // for YCbCr conversions
#ifdef MOZ_WIDGET_GONK
#include "GrallocImages.h"
#endif
#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_CAMERA) && defined(MOZ_WEBRTC)
#include "GonkCameraImage.h"
#endif
#include "gfx2DGlue.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/CheckedInt.h"
#ifdef XP_MACOSX
#include "mozilla/gfx/QuartzSupport.h"
#include "MacIOSurfaceImage.h"
#endif
#ifdef XP_WIN
#include "gfxWindowsPlatform.h"
#include <d3d10_1.h>
#include "D3D9SurfaceImage.h"
#include "D3D11ShareHandleImage.h"
#endif
namespace mozilla {
namespace layers {
using namespace mozilla::ipc;
using namespace android;
using namespace mozilla::gfx;
Atomic<int32_t> Image::sSerialCounter(0);
Atomic<uint32_t> ImageContainer::sGenerationCounter(0);
already_AddRefed<Image>
ImageFactory::CreateImage(ImageFormat aFormat,
const gfx::IntSize &,
BufferRecycleBin *aRecycleBin)
{
nsRefPtr<Image> img;
#ifdef MOZ_WIDGET_GONK
if (aFormat == ImageFormat::GRALLOC_PLANAR_YCBCR) {
img = new GrallocImage();
return img.forget();
}
if (aFormat == ImageFormat::OVERLAY_IMAGE) {
img = new OverlayImage();
return img.forget();
}
#endif
#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_CAMERA) && defined(MOZ_WEBRTC)
if (aFormat == ImageFormat::GONK_CAMERA_IMAGE) {
img = new GonkCameraImage();
return img.forget();
}
#endif
if (aFormat == ImageFormat::PLANAR_YCBCR) {
img = new PlanarYCbCrImage(aRecycleBin);
return img.forget();
}
if (aFormat == ImageFormat::CAIRO_SURFACE) {
img = new CairoImage();
return img.forget();
}
#ifdef MOZ_WIDGET_ANDROID
if (aFormat == ImageFormat::SURFACE_TEXTURE) {
img = new SurfaceTextureImage();
return img.forget();
}
#endif
if (aFormat == ImageFormat::EGLIMAGE) {
img = new EGLImageImage();
return img.forget();
}
#ifdef XP_MACOSX
if (aFormat == ImageFormat::MAC_IOSURFACE) {
img = new MacIOSurfaceImage();
return img.forget();
}
#endif
#ifdef XP_WIN
if (aFormat == ImageFormat::D3D11_SHARE_HANDLE_TEXTURE) {
img = new D3D11ShareHandleImage();
return img.forget();
}
if (aFormat == ImageFormat::D3D9_RGB32_TEXTURE) {
img = new D3D9SurfaceImage();
return img.forget();
}
#endif
return nullptr;
}
BufferRecycleBin::BufferRecycleBin()
: mLock("mozilla.layers.BufferRecycleBin.mLock")
{
}
void
BufferRecycleBin::RecycleBuffer(uint8_t* aBuffer, uint32_t aSize)
{
MutexAutoLock lock(mLock);
if (!mRecycledBuffers.IsEmpty() && aSize != mRecycledBufferSize) {
mRecycledBuffers.Clear();
}
mRecycledBufferSize = aSize;
mRecycledBuffers.AppendElement(aBuffer);
}
uint8_t*
BufferRecycleBin::GetBuffer(uint32_t aSize)
{
MutexAutoLock lock(mLock);
if (mRecycledBuffers.IsEmpty() || mRecycledBufferSize != aSize)
return new uint8_t[aSize];
uint32_t last = mRecycledBuffers.Length() - 1;
uint8_t* result = mRecycledBuffers[last].forget();
mRecycledBuffers.RemoveElementAt(last);
return result;
}
/**
* The child side of PImageContainer. It's best to avoid ImageContainer filling
* this role since IPDL objects should be associated with a single thread and
* ImageContainer definitely isn't. This object belongs to (and is always
* destroyed on) the ImageBridge thread, except when we need to destroy it
* during shutdown.
* An ImageContainer owns one of these; we have a weak reference to our
* ImageContainer.
*/
class ImageContainerChild : public PImageContainerChild {
public:
explicit ImageContainerChild(ImageContainer* aImageContainer)
: mLock("ImageContainerChild"), mImageContainer(aImageContainer) {}
void ForgetImageContainer()
{
MutexAutoLock lock(mLock);
mImageContainer = nullptr;
}
// This protects mImageContainer. This is always taken before the
// mImageContainer's monitor (when both need to be held).
Mutex mLock;
ImageContainer* mImageContainer;
};
ImageContainer::ImageContainer(Mode flag)
: mReentrantMonitor("ImageContainer.mReentrantMonitor"),
mGenerationCounter(++sGenerationCounter),
mPaintCount(0),
mPreviousImagePainted(false),
mImageFactory(new ImageFactory()),
mRecycleBin(new BufferRecycleBin()),
mImageClient(nullptr),
mIPDLChild(nullptr)
{
if (ImageBridgeChild::IsCreated()) {
// the refcount of this ImageClient is 1. we don't use a RefPtr here because the refcount
// of this class must be done on the ImageBridge thread.
switch (flag) {
case SYNCHRONOUS:
break;
case ASYNCHRONOUS:
mIPDLChild = new ImageContainerChild(this);
mImageClient = ImageBridgeChild::GetSingleton()->CreateImageClient(CompositableType::IMAGE, this).take();
MOZ_ASSERT(mImageClient);
break;
case ASYNCHRONOUS_OVERLAY:
mIPDLChild = new ImageContainerChild(this);
mImageClient = ImageBridgeChild::GetSingleton()->CreateImageClient(CompositableType::IMAGE_OVERLAY, this).take();
MOZ_ASSERT(mImageClient);
break;
default:
MOZ_ASSERT(false, "This flag is invalid.");
break;
}
}
}
ImageContainer::~ImageContainer()
{
if (IsAsync()) {
mIPDLChild->ForgetImageContainer();
ImageBridgeChild::DispatchReleaseImageClient(mImageClient, mIPDLChild);
}
}
already_AddRefed<Image>
ImageContainer::CreateImage(ImageFormat aFormat)
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
#ifdef MOZ_WIDGET_GONK
if (aFormat == ImageFormat::OVERLAY_IMAGE) {
if (mImageClient && mImageClient->GetTextureInfo().mCompositableType != CompositableType::IMAGE_OVERLAY) {
// If this ImageContainer is async but the image type mismatch, fix it here
if (ImageBridgeChild::IsCreated()) {
ImageBridgeChild::DispatchReleaseImageClient(mImageClient);
mImageClient = ImageBridgeChild::GetSingleton()->CreateImageClient(
CompositableType::IMAGE_OVERLAY, this).take();
}
}
}
#endif
if (mImageClient) {
nsRefPtr<Image> img = mImageClient->CreateImage(aFormat);
if (img) {
return img.forget();
}
}
return mImageFactory->CreateImage(aFormat, mScaleHint, mRecycleBin);
}
void
ImageContainer::SetCurrentImageInternal(Image *aImage)
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
if (mActiveImage != aImage) {
mGenerationCounter = ++sGenerationCounter;
}
mActiveImage = aImage;
CurrentImageChanged();
}
void
ImageContainer::ClearCurrentImage()
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
SetCurrentImageInternal(nullptr);
}
void
ImageContainer::SetCurrentImage(Image *aImage)
{
if (!aImage) {
ClearAllImages();
return;
}
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
if (IsAsync()) {
ImageBridgeChild::DispatchImageClientUpdate(mImageClient, this);
}
SetCurrentImageInternal(aImage);
}
void
ImageContainer::ClearAllImages()
{
if (IsAsync()) {
// Let ImageClient release all TextureClients.
ImageBridgeChild::FlushAllImages(mImageClient, this, false);
return;
}
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
SetCurrentImageInternal(nullptr);
}
void
ImageContainer::ClearAllImagesExceptFront()
{
if (IsAsync()) {
// Let ImageClient release all TextureClients except front one.
ImageBridgeChild::FlushAllImages(mImageClient, this, true);
}
}
void
ImageContainer::SetCurrentImageInTransaction(Image *aImage)
{
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
NS_ASSERTION(!mImageClient, "Should use async image transfer with ImageBridge.");
SetCurrentImageInternal(aImage);
}
bool ImageContainer::IsAsync() const {
return mImageClient != nullptr;
}
uint64_t ImageContainer::GetAsyncContainerID() const
{
NS_ASSERTION(IsAsync(),"Shared image ID is only relevant to async ImageContainers");
if (IsAsync()) {
return mImageClient->GetAsyncID();
} else {
return 0; // zero is always an invalid AsyncID
}
}
bool
ImageContainer::HasCurrentImage()
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
return !!mActiveImage.get();
}
void
ImageContainer::GetCurrentImages(nsTArray<OwningImage>* aImages,
uint32_t* aGenerationCounter)
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
if (mActiveImage) {
OwningImage* img = aImages->AppendElement();
img->mImage = mActiveImage;
img->mFrameID = mGenerationCounter;
img->mProducerID = 0;
}
if (aGenerationCounter) {
*aGenerationCounter = mGenerationCounter;
}
}
gfx::IntSize
ImageContainer::GetCurrentSize()
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
if (!mActiveImage) {
return gfx::IntSize(0, 0);
}
return mActiveImage->GetSize();
}
PlanarYCbCrImage::PlanarYCbCrImage(BufferRecycleBin *aRecycleBin)
: Image(nullptr, ImageFormat::PLANAR_YCBCR)
, mBufferSize(0)
, mOffscreenFormat(gfxImageFormat::Unknown)
, mRecycleBin(aRecycleBin)
{
}
PlanarYCbCrImage::~PlanarYCbCrImage()
{
if (mBuffer) {
mRecycleBin->RecycleBuffer(mBuffer.forget(), mBufferSize);
}
}
size_t
PlanarYCbCrImage::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
{
// Ignoring:
// - mData - just wraps mBuffer
// - Surfaces should be reported under gfx-surfaces-*:
// - mSourceSurface
// - Base class:
// - mImplData is not used
// Not owned:
// - mRecycleBin
size_t size = mBuffer.SizeOfExcludingThis(aMallocSizeOf);
// Could add in the future:
// - mBackendData (from base class)
return size;
}
uint8_t*
PlanarYCbCrImage::AllocateBuffer(uint32_t aSize)
{
return mRecycleBin->GetBuffer(aSize);
}
static void
CopyPlane(uint8_t *aDst, const uint8_t *aSrc,
const gfx::IntSize &aSize, int32_t aStride, int32_t aSkip)
{
int32_t height = aSize.height;
int32_t width = aSize.width;
MOZ_RELEASE_ASSERT(width <= aStride);
if (!aSkip) {
// Fast path: planar input.
memcpy(aDst, aSrc, height * aStride);
} else {
for (int y = 0; y < height; ++y) {
const uint8_t *src = aSrc;
uint8_t *dst = aDst;
// Slow path
for (int x = 0; x < width; ++x) {
*dst++ = *src++;
src += aSkip;
}
aSrc += aStride;
aDst += aStride;
}
}
}
void
PlanarYCbCrImage::CopyData(const Data& aData)
{
// update buffer size
// Use uint32_t throughout to match AllocateBuffer's param and mBufferSize
const auto checkedSize =
CheckedInt<uint32_t>(aData.mCbCrStride) * aData.mCbCrSize.height * 2 +
CheckedInt<uint32_t>(aData.mYStride) * aData.mYSize.height;
if (!checkedSize.isValid())
return;
const auto size = checkedSize.value();
// get new buffer
mBuffer = AllocateBuffer(size);
if (!mBuffer)
return;
// update buffer size
mBufferSize = size;
mData = aData;
mData.mYChannel = mBuffer;
mData.mCbChannel = mData.mYChannel + mData.mYStride * mData.mYSize.height;
mData.mCrChannel = mData.mCbChannel + mData.mCbCrStride * mData.mCbCrSize.height;
mData.mYSkip = mData.mCbSkip = mData.mCrSkip = 0;
CopyPlane(mData.mYChannel, aData.mYChannel,
aData.mYSize, aData.mYStride, aData.mYSkip);
CopyPlane(mData.mCbChannel, aData.mCbChannel,
aData.mCbCrSize, aData.mCbCrStride, aData.mCbSkip);
CopyPlane(mData.mCrChannel, aData.mCrChannel,
aData.mCbCrSize, aData.mCbCrStride, aData.mCrSkip);
mSize = aData.mPicSize;
}
void
PlanarYCbCrImage::SetData(const Data &aData)
{
CopyData(aData);
}
gfxImageFormat
PlanarYCbCrImage::GetOffscreenFormat()
{
return mOffscreenFormat == gfxImageFormat::Unknown ?
gfxPlatform::GetPlatform()->GetOffscreenFormat() :
mOffscreenFormat;
}
void
PlanarYCbCrImage::SetDataNoCopy(const Data &aData)
{
mData = aData;
mSize = aData.mPicSize;
}
uint8_t*
PlanarYCbCrImage::AllocateAndGetNewBuffer(uint32_t aSize)
{
// get new buffer
mBuffer = AllocateBuffer(aSize);
if (mBuffer) {
// update buffer size
mBufferSize = aSize;
}
return mBuffer;
}
already_AddRefed<gfx::SourceSurface>
PlanarYCbCrImage::GetAsSourceSurface()
{
if (mSourceSurface) {
RefPtr<gfx::SourceSurface> surface(mSourceSurface);
return surface.forget();
}
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;
}
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(mData, format, size, mapping.GetData(), mapping.GetStride());
mSourceSurface = surface;
return surface.forget();
}
CairoImage::CairoImage()
: Image(nullptr, ImageFormat::CAIRO_SURFACE)
{}
CairoImage::~CairoImage()
{
}
TextureClient*
CairoImage::GetTextureClient(CompositableClient *aClient)
{
if (!aClient) {
return nullptr;
}
CompositableForwarder* forwarder = aClient->GetForwarder();
RefPtr<TextureClient> textureClient = mTextureClients.Get(forwarder->GetSerial());
if (textureClient) {
return textureClient;
}
RefPtr<SourceSurface> surface = GetAsSourceSurface();
MOZ_ASSERT(surface);
if (!surface) {
return nullptr;
}
// XXX windows' TextureClients do not hold ISurfaceAllocator,
// recycler does not work on windows.
#ifndef XP_WIN
// XXX only gonk ensure when TextureClient is recycled,
// TextureHost is not used by CompositableHost.
#ifdef MOZ_WIDGET_GONK
RefPtr<TextureClientRecycleAllocator> recycler =
aClient->GetTextureClientRecycler();
if (recycler) {
textureClient =
recycler->CreateOrRecycle(surface->GetFormat(),
surface->GetSize(),
BackendSelector::Content,
aClient->GetTextureFlags());
}
#endif
#endif
if (!textureClient) {
// gfx::BackendType::NONE means default to content backend
textureClient = aClient->CreateTextureClientForDrawing(surface->GetFormat(),
surface->GetSize(),
BackendSelector::Content,
TextureFlags::DEFAULT);
}
if (!textureClient) {
return nullptr;
}
if (!textureClient->Lock(OpenMode::OPEN_WRITE_ONLY)) {
return nullptr;
}
TextureClientAutoUnlock autoUnlock(textureClient);
textureClient->UpdateFromSurface(surface);
textureClient->SyncWithObject(forwarder->GetSyncObject());
mTextureClients.Put(forwarder->GetSerial(), textureClient);
return textureClient;
}
PImageContainerChild*
ImageContainer::GetPImageContainerChild()
{
return mIPDLChild;
}
/* static */ void
ImageContainer::NotifyComposite(const ImageCompositeNotification& aNotification)
{
ImageContainerChild* child =
static_cast<ImageContainerChild*>(aNotification.imageContainerChild());
if (child) {
MutexAutoLock lock(child->mLock);
if (child->mImageContainer) {
child->mImageContainer->NotifyCompositeInternal(aNotification);
}
}
}
} // namespace layers
} // namespace mozilla