1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/layers/ipc/SharedRGBImage.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,123 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "SharedRGBImage.h" 1.9 +#include "ImageTypes.h" // for ImageFormat::SHARED_RGB, etc 1.10 +#include "Shmem.h" // for Shmem 1.11 +#include "gfx2DGlue.h" // for ImageFormatToSurfaceFormat, etc 1.12 +#include "gfxPlatform.h" // for gfxPlatform, gfxImageFormat 1.13 +#include "mozilla/layers/ISurfaceAllocator.h" // for ISurfaceAllocator, etc 1.14 +#include "mozilla/layers/ImageClient.h" // for ImageClient 1.15 +#include "mozilla/layers/ImageDataSerializer.h" // for ImageDataSerializer 1.16 +#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc 1.17 +#include "mozilla/layers/TextureClient.h" // for BufferTextureClient, etc 1.18 +#include "mozilla/layers/ImageBridgeChild.h" // for ImageBridgeChild 1.19 +#include "mozilla/mozalloc.h" // for operator delete, etc 1.20 +#include "nsAutoPtr.h" // for nsRefPtr 1.21 +#include "nsDebug.h" // for NS_WARNING, NS_ASSERTION 1.22 +#include "nsISupportsImpl.h" // for Image::AddRef, etc 1.23 +#include "nsRect.h" // for nsIntRect 1.24 +#include "nsSize.h" // for nsIntSize 1.25 + 1.26 +// Just big enough for a 1080p RGBA32 frame 1.27 +#define MAX_FRAME_SIZE (16 * 1024 * 1024) 1.28 + 1.29 +namespace mozilla { 1.30 +namespace layers { 1.31 + 1.32 +already_AddRefed<Image> 1.33 +CreateSharedRGBImage(ImageContainer *aImageContainer, 1.34 + nsIntSize aSize, 1.35 + gfxImageFormat aImageFormat) 1.36 +{ 1.37 + NS_ASSERTION(aImageFormat == gfxImageFormat::ARGB32 || 1.38 + aImageFormat == gfxImageFormat::RGB24 || 1.39 + aImageFormat == gfxImageFormat::RGB16_565, 1.40 + "RGB formats supported only"); 1.41 + 1.42 + if (!aImageContainer) { 1.43 + NS_WARNING("No ImageContainer to allocate SharedRGBImage"); 1.44 + return nullptr; 1.45 + } 1.46 + 1.47 + nsRefPtr<Image> image = aImageContainer->CreateImage(ImageFormat::SHARED_RGB); 1.48 + 1.49 + if (!image) { 1.50 + NS_WARNING("Failed to create SharedRGBImage"); 1.51 + return nullptr; 1.52 + } 1.53 + 1.54 + nsRefPtr<SharedRGBImage> rgbImage = static_cast<SharedRGBImage*>(image.get()); 1.55 + if (!rgbImage->Allocate(gfx::ToIntSize(aSize), 1.56 + gfx::ImageFormatToSurfaceFormat(aImageFormat))) { 1.57 + NS_WARNING("Failed to allocate a shared image"); 1.58 + return nullptr; 1.59 + } 1.60 + return image.forget(); 1.61 +} 1.62 + 1.63 +SharedRGBImage::SharedRGBImage(ImageClient* aCompositable) 1.64 +: Image(nullptr, ImageFormat::SHARED_RGB) 1.65 +, mCompositable(aCompositable) 1.66 +{ 1.67 + MOZ_COUNT_CTOR(SharedRGBImage); 1.68 +} 1.69 + 1.70 +SharedRGBImage::~SharedRGBImage() 1.71 +{ 1.72 + MOZ_COUNT_DTOR(SharedRGBImage); 1.73 + 1.74 + if (mCompositable->GetAsyncID() != 0 && 1.75 + !InImageBridgeChildThread()) { 1.76 + ImageBridgeChild::DispatchReleaseTextureClient(mTextureClient.forget().drop()); 1.77 + ImageBridgeChild::DispatchReleaseImageClient(mCompositable.forget().drop()); 1.78 + } 1.79 +} 1.80 + 1.81 +bool 1.82 +SharedRGBImage::Allocate(gfx::IntSize aSize, gfx::SurfaceFormat aFormat) 1.83 +{ 1.84 + mSize = aSize; 1.85 + mTextureClient = mCompositable->CreateBufferTextureClient(aFormat); 1.86 + return mTextureClient->AllocateForSurface(aSize); 1.87 +} 1.88 + 1.89 +uint8_t* 1.90 +SharedRGBImage::GetBuffer() 1.91 +{ 1.92 + if (!mTextureClient) { 1.93 + return nullptr; 1.94 + } 1.95 + 1.96 + ImageDataSerializer serializer(mTextureClient->GetBuffer(), mTextureClient->GetBufferSize()); 1.97 + return serializer.GetData(); 1.98 +} 1.99 + 1.100 +gfx::IntSize 1.101 +SharedRGBImage::GetSize() 1.102 +{ 1.103 + return mSize; 1.104 +} 1.105 + 1.106 +size_t 1.107 +SharedRGBImage::GetBufferSize() 1.108 +{ 1.109 + return mTextureClient ? mTextureClient->GetBufferSize() 1.110 + : 0; 1.111 +} 1.112 + 1.113 +TextureClient* 1.114 +SharedRGBImage::GetTextureClient(CompositableClient* aClient) 1.115 +{ 1.116 + return mTextureClient.get(); 1.117 +} 1.118 + 1.119 +TemporaryRef<gfx::SourceSurface> 1.120 +SharedRGBImage::GetAsSourceSurface() 1.121 +{ 1.122 + return nullptr; 1.123 +} 1.124 + 1.125 +} // namespace layers 1.126 +} // namespace mozilla