michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "SharedRGBImage.h" michael@0: #include "ImageTypes.h" // for ImageFormat::SHARED_RGB, etc michael@0: #include "Shmem.h" // for Shmem michael@0: #include "gfx2DGlue.h" // for ImageFormatToSurfaceFormat, etc michael@0: #include "gfxPlatform.h" // for gfxPlatform, gfxImageFormat michael@0: #include "mozilla/layers/ISurfaceAllocator.h" // for ISurfaceAllocator, etc michael@0: #include "mozilla/layers/ImageClient.h" // for ImageClient michael@0: #include "mozilla/layers/ImageDataSerializer.h" // for ImageDataSerializer michael@0: #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc michael@0: #include "mozilla/layers/TextureClient.h" // for BufferTextureClient, etc michael@0: #include "mozilla/layers/ImageBridgeChild.h" // for ImageBridgeChild michael@0: #include "mozilla/mozalloc.h" // for operator delete, etc michael@0: #include "nsAutoPtr.h" // for nsRefPtr michael@0: #include "nsDebug.h" // for NS_WARNING, NS_ASSERTION michael@0: #include "nsISupportsImpl.h" // for Image::AddRef, etc michael@0: #include "nsRect.h" // for nsIntRect michael@0: #include "nsSize.h" // for nsIntSize michael@0: michael@0: // Just big enough for a 1080p RGBA32 frame michael@0: #define MAX_FRAME_SIZE (16 * 1024 * 1024) michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: already_AddRefed michael@0: CreateSharedRGBImage(ImageContainer *aImageContainer, michael@0: nsIntSize aSize, michael@0: gfxImageFormat aImageFormat) michael@0: { michael@0: NS_ASSERTION(aImageFormat == gfxImageFormat::ARGB32 || michael@0: aImageFormat == gfxImageFormat::RGB24 || michael@0: aImageFormat == gfxImageFormat::RGB16_565, michael@0: "RGB formats supported only"); michael@0: michael@0: if (!aImageContainer) { michael@0: NS_WARNING("No ImageContainer to allocate SharedRGBImage"); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsRefPtr image = aImageContainer->CreateImage(ImageFormat::SHARED_RGB); michael@0: michael@0: if (!image) { michael@0: NS_WARNING("Failed to create SharedRGBImage"); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsRefPtr rgbImage = static_cast(image.get()); michael@0: if (!rgbImage->Allocate(gfx::ToIntSize(aSize), michael@0: gfx::ImageFormatToSurfaceFormat(aImageFormat))) { michael@0: NS_WARNING("Failed to allocate a shared image"); michael@0: return nullptr; michael@0: } michael@0: return image.forget(); michael@0: } michael@0: michael@0: SharedRGBImage::SharedRGBImage(ImageClient* aCompositable) michael@0: : Image(nullptr, ImageFormat::SHARED_RGB) michael@0: , mCompositable(aCompositable) michael@0: { michael@0: MOZ_COUNT_CTOR(SharedRGBImage); michael@0: } michael@0: michael@0: SharedRGBImage::~SharedRGBImage() michael@0: { michael@0: MOZ_COUNT_DTOR(SharedRGBImage); michael@0: michael@0: if (mCompositable->GetAsyncID() != 0 && michael@0: !InImageBridgeChildThread()) { michael@0: ImageBridgeChild::DispatchReleaseTextureClient(mTextureClient.forget().drop()); michael@0: ImageBridgeChild::DispatchReleaseImageClient(mCompositable.forget().drop()); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: SharedRGBImage::Allocate(gfx::IntSize aSize, gfx::SurfaceFormat aFormat) michael@0: { michael@0: mSize = aSize; michael@0: mTextureClient = mCompositable->CreateBufferTextureClient(aFormat); michael@0: return mTextureClient->AllocateForSurface(aSize); michael@0: } michael@0: michael@0: uint8_t* michael@0: SharedRGBImage::GetBuffer() michael@0: { michael@0: if (!mTextureClient) { michael@0: return nullptr; michael@0: } michael@0: michael@0: ImageDataSerializer serializer(mTextureClient->GetBuffer(), mTextureClient->GetBufferSize()); michael@0: return serializer.GetData(); michael@0: } michael@0: michael@0: gfx::IntSize michael@0: SharedRGBImage::GetSize() michael@0: { michael@0: return mSize; michael@0: } michael@0: michael@0: size_t michael@0: SharedRGBImage::GetBufferSize() michael@0: { michael@0: return mTextureClient ? mTextureClient->GetBufferSize() michael@0: : 0; michael@0: } michael@0: michael@0: TextureClient* michael@0: SharedRGBImage::GetTextureClient(CompositableClient* aClient) michael@0: { michael@0: return mTextureClient.get(); michael@0: } michael@0: michael@0: TemporaryRef michael@0: SharedRGBImage::GetAsSourceSurface() michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: } // namespace layers michael@0: } // namespace mozilla