|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "SharedRGBImage.h" |
|
6 #include "ImageTypes.h" // for ImageFormat::SHARED_RGB, etc |
|
7 #include "Shmem.h" // for Shmem |
|
8 #include "gfx2DGlue.h" // for ImageFormatToSurfaceFormat, etc |
|
9 #include "gfxPlatform.h" // for gfxPlatform, gfxImageFormat |
|
10 #include "mozilla/layers/ISurfaceAllocator.h" // for ISurfaceAllocator, etc |
|
11 #include "mozilla/layers/ImageClient.h" // for ImageClient |
|
12 #include "mozilla/layers/ImageDataSerializer.h" // for ImageDataSerializer |
|
13 #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc |
|
14 #include "mozilla/layers/TextureClient.h" // for BufferTextureClient, etc |
|
15 #include "mozilla/layers/ImageBridgeChild.h" // for ImageBridgeChild |
|
16 #include "mozilla/mozalloc.h" // for operator delete, etc |
|
17 #include "nsAutoPtr.h" // for nsRefPtr |
|
18 #include "nsDebug.h" // for NS_WARNING, NS_ASSERTION |
|
19 #include "nsISupportsImpl.h" // for Image::AddRef, etc |
|
20 #include "nsRect.h" // for nsIntRect |
|
21 #include "nsSize.h" // for nsIntSize |
|
22 |
|
23 // Just big enough for a 1080p RGBA32 frame |
|
24 #define MAX_FRAME_SIZE (16 * 1024 * 1024) |
|
25 |
|
26 namespace mozilla { |
|
27 namespace layers { |
|
28 |
|
29 already_AddRefed<Image> |
|
30 CreateSharedRGBImage(ImageContainer *aImageContainer, |
|
31 nsIntSize aSize, |
|
32 gfxImageFormat aImageFormat) |
|
33 { |
|
34 NS_ASSERTION(aImageFormat == gfxImageFormat::ARGB32 || |
|
35 aImageFormat == gfxImageFormat::RGB24 || |
|
36 aImageFormat == gfxImageFormat::RGB16_565, |
|
37 "RGB formats supported only"); |
|
38 |
|
39 if (!aImageContainer) { |
|
40 NS_WARNING("No ImageContainer to allocate SharedRGBImage"); |
|
41 return nullptr; |
|
42 } |
|
43 |
|
44 nsRefPtr<Image> image = aImageContainer->CreateImage(ImageFormat::SHARED_RGB); |
|
45 |
|
46 if (!image) { |
|
47 NS_WARNING("Failed to create SharedRGBImage"); |
|
48 return nullptr; |
|
49 } |
|
50 |
|
51 nsRefPtr<SharedRGBImage> rgbImage = static_cast<SharedRGBImage*>(image.get()); |
|
52 if (!rgbImage->Allocate(gfx::ToIntSize(aSize), |
|
53 gfx::ImageFormatToSurfaceFormat(aImageFormat))) { |
|
54 NS_WARNING("Failed to allocate a shared image"); |
|
55 return nullptr; |
|
56 } |
|
57 return image.forget(); |
|
58 } |
|
59 |
|
60 SharedRGBImage::SharedRGBImage(ImageClient* aCompositable) |
|
61 : Image(nullptr, ImageFormat::SHARED_RGB) |
|
62 , mCompositable(aCompositable) |
|
63 { |
|
64 MOZ_COUNT_CTOR(SharedRGBImage); |
|
65 } |
|
66 |
|
67 SharedRGBImage::~SharedRGBImage() |
|
68 { |
|
69 MOZ_COUNT_DTOR(SharedRGBImage); |
|
70 |
|
71 if (mCompositable->GetAsyncID() != 0 && |
|
72 !InImageBridgeChildThread()) { |
|
73 ImageBridgeChild::DispatchReleaseTextureClient(mTextureClient.forget().drop()); |
|
74 ImageBridgeChild::DispatchReleaseImageClient(mCompositable.forget().drop()); |
|
75 } |
|
76 } |
|
77 |
|
78 bool |
|
79 SharedRGBImage::Allocate(gfx::IntSize aSize, gfx::SurfaceFormat aFormat) |
|
80 { |
|
81 mSize = aSize; |
|
82 mTextureClient = mCompositable->CreateBufferTextureClient(aFormat); |
|
83 return mTextureClient->AllocateForSurface(aSize); |
|
84 } |
|
85 |
|
86 uint8_t* |
|
87 SharedRGBImage::GetBuffer() |
|
88 { |
|
89 if (!mTextureClient) { |
|
90 return nullptr; |
|
91 } |
|
92 |
|
93 ImageDataSerializer serializer(mTextureClient->GetBuffer(), mTextureClient->GetBufferSize()); |
|
94 return serializer.GetData(); |
|
95 } |
|
96 |
|
97 gfx::IntSize |
|
98 SharedRGBImage::GetSize() |
|
99 { |
|
100 return mSize; |
|
101 } |
|
102 |
|
103 size_t |
|
104 SharedRGBImage::GetBufferSize() |
|
105 { |
|
106 return mTextureClient ? mTextureClient->GetBufferSize() |
|
107 : 0; |
|
108 } |
|
109 |
|
110 TextureClient* |
|
111 SharedRGBImage::GetTextureClient(CompositableClient* aClient) |
|
112 { |
|
113 return mTextureClient.get(); |
|
114 } |
|
115 |
|
116 TemporaryRef<gfx::SourceSurface> |
|
117 SharedRGBImage::GetAsSourceSurface() |
|
118 { |
|
119 return nullptr; |
|
120 } |
|
121 |
|
122 } // namespace layers |
|
123 } // namespace mozilla |