|
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include <stdint.h> // for uint8_t, uint32_t |
|
7 #include "BasicLayers.h" // for BasicLayerManager |
|
8 #include "ImageContainer.h" // for PlanarYCbCrImage, etc |
|
9 #include "ImageTypes.h" // for ImageFormat, etc |
|
10 #include "cairo.h" // for cairo_user_data_key_t |
|
11 #include "gfxASurface.h" // for gfxASurface, etc |
|
12 #include "gfxPlatform.h" // for gfxPlatform, gfxImageFormat |
|
13 #include "gfxUtils.h" // for gfxUtils |
|
14 #include "mozilla/mozalloc.h" // for operator delete[], etc |
|
15 #include "nsAutoPtr.h" // for nsRefPtr, nsAutoArrayPtr |
|
16 #include "nsAutoRef.h" // for nsCountedRef |
|
17 #include "nsCOMPtr.h" // for already_AddRefed |
|
18 #include "nsDebug.h" // for NS_ERROR, NS_ASSERTION |
|
19 #include "nsISupportsImpl.h" // for Image::Release, etc |
|
20 #include "nsThreadUtils.h" // for NS_IsMainThread |
|
21 #include "mozilla/gfx/Point.h" // for IntSize |
|
22 #include "gfx2DGlue.h" |
|
23 #include "YCbCrUtils.h" // for YCbCr conversions |
|
24 #ifdef XP_MACOSX |
|
25 #include "gfxQuartzImageSurface.h" |
|
26 #endif |
|
27 |
|
28 namespace mozilla { |
|
29 namespace layers { |
|
30 |
|
31 class BasicPlanarYCbCrImage : public PlanarYCbCrImage |
|
32 { |
|
33 public: |
|
34 BasicPlanarYCbCrImage(const gfx::IntSize& aScaleHint, gfxImageFormat aOffscreenFormat, BufferRecycleBin *aRecycleBin) |
|
35 : PlanarYCbCrImage(aRecycleBin) |
|
36 , mScaleHint(aScaleHint) |
|
37 , mDelayedConversion(false) |
|
38 { |
|
39 SetOffscreenFormat(aOffscreenFormat); |
|
40 } |
|
41 |
|
42 ~BasicPlanarYCbCrImage() |
|
43 { |
|
44 if (mDecodedBuffer) { |
|
45 // Right now this only happens if the Image was never drawn, otherwise |
|
46 // this will have been tossed away at surface destruction. |
|
47 mRecycleBin->RecycleBuffer(mDecodedBuffer.forget(), mSize.height * mStride); |
|
48 } |
|
49 } |
|
50 |
|
51 virtual void SetData(const Data& aData); |
|
52 virtual void SetDelayedConversion(bool aDelayed) { mDelayedConversion = aDelayed; } |
|
53 |
|
54 TemporaryRef<gfx::SourceSurface> GetAsSourceSurface(); |
|
55 |
|
56 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE |
|
57 { |
|
58 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); |
|
59 } |
|
60 |
|
61 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE |
|
62 { |
|
63 size_t size = PlanarYCbCrImage::SizeOfExcludingThis(aMallocSizeOf); |
|
64 size += mDecodedBuffer.SizeOfExcludingThis(aMallocSizeOf); |
|
65 return size; |
|
66 } |
|
67 |
|
68 private: |
|
69 nsAutoArrayPtr<uint8_t> mDecodedBuffer; |
|
70 gfx::IntSize mScaleHint; |
|
71 int mStride; |
|
72 bool mDelayedConversion; |
|
73 }; |
|
74 |
|
75 class BasicImageFactory : public ImageFactory |
|
76 { |
|
77 public: |
|
78 BasicImageFactory() {} |
|
79 |
|
80 virtual already_AddRefed<Image> CreateImage(ImageFormat aFormat, |
|
81 const gfx::IntSize &aScaleHint, |
|
82 BufferRecycleBin *aRecycleBin) |
|
83 { |
|
84 nsRefPtr<Image> image; |
|
85 if (aFormat == ImageFormat::PLANAR_YCBCR) { |
|
86 image = new BasicPlanarYCbCrImage(aScaleHint, gfxPlatform::GetPlatform()->GetOffscreenFormat(), aRecycleBin); |
|
87 return image.forget(); |
|
88 } |
|
89 |
|
90 return ImageFactory::CreateImage(aFormat, aScaleHint, aRecycleBin); |
|
91 } |
|
92 }; |
|
93 |
|
94 void |
|
95 BasicPlanarYCbCrImage::SetData(const Data& aData) |
|
96 { |
|
97 PlanarYCbCrImage::SetData(aData); |
|
98 |
|
99 if (mDelayedConversion) { |
|
100 return; |
|
101 } |
|
102 |
|
103 // Do some sanity checks to prevent integer overflow |
|
104 if (aData.mYSize.width > PlanarYCbCrImage::MAX_DIMENSION || |
|
105 aData.mYSize.height > PlanarYCbCrImage::MAX_DIMENSION) { |
|
106 NS_ERROR("Illegal image source width or height"); |
|
107 return; |
|
108 } |
|
109 |
|
110 gfx::SurfaceFormat format = gfx::ImageFormatToSurfaceFormat(GetOffscreenFormat()); |
|
111 |
|
112 gfx::IntSize size(mScaleHint); |
|
113 gfx::GetYCbCrToRGBDestFormatAndSize(aData, format, size); |
|
114 if (size.width > PlanarYCbCrImage::MAX_DIMENSION || |
|
115 size.height > PlanarYCbCrImage::MAX_DIMENSION) { |
|
116 NS_ERROR("Illegal image dest width or height"); |
|
117 return; |
|
118 } |
|
119 |
|
120 gfxImageFormat iFormat = gfx::SurfaceFormatToImageFormat(format); |
|
121 mStride = gfxASurface::FormatStrideForWidth(iFormat, size.width); |
|
122 mDecodedBuffer = AllocateBuffer(size.height * mStride); |
|
123 if (!mDecodedBuffer) { |
|
124 // out of memory |
|
125 return; |
|
126 } |
|
127 |
|
128 gfx::ConvertYCbCrToRGB(aData, format, size, mDecodedBuffer, mStride); |
|
129 SetOffscreenFormat(iFormat); |
|
130 mSize = size; |
|
131 } |
|
132 |
|
133 TemporaryRef<gfx::SourceSurface> |
|
134 BasicPlanarYCbCrImage::GetAsSourceSurface() |
|
135 { |
|
136 NS_ASSERTION(NS_IsMainThread(), "Must be main thread"); |
|
137 |
|
138 if (mSourceSurface) { |
|
139 return mSourceSurface.get(); |
|
140 } |
|
141 |
|
142 if (!mDecodedBuffer) { |
|
143 return PlanarYCbCrImage::GetAsSourceSurface(); |
|
144 } |
|
145 |
|
146 gfxImageFormat format = GetOffscreenFormat(); |
|
147 |
|
148 RefPtr<gfx::SourceSurface> surface; |
|
149 { |
|
150 // Create a DrawTarget so that we can own the data inside mDecodeBuffer. |
|
151 // We create the target out of mDecodedBuffer, and get a snapshot from it. |
|
152 // The draw target is destroyed on scope exit and the surface owns the data. |
|
153 RefPtr<gfx::DrawTarget> drawTarget |
|
154 = gfxPlatform::GetPlatform()->CreateDrawTargetForData(mDecodedBuffer, |
|
155 mSize, |
|
156 mStride, |
|
157 gfx::ImageFormatToSurfaceFormat(format)); |
|
158 if (!drawTarget) { |
|
159 return nullptr; |
|
160 } |
|
161 |
|
162 surface = drawTarget->Snapshot(); |
|
163 } |
|
164 |
|
165 mRecycleBin->RecycleBuffer(mDecodedBuffer.forget(), mSize.height * mStride); |
|
166 |
|
167 mSourceSurface = surface; |
|
168 return mSourceSurface.get(); |
|
169 } |
|
170 |
|
171 |
|
172 ImageFactory* |
|
173 BasicLayerManager::GetImageFactory() |
|
174 { |
|
175 if (!mFactory) { |
|
176 mFactory = new BasicImageFactory(); |
|
177 } |
|
178 |
|
179 return mFactory.get(); |
|
180 } |
|
181 |
|
182 } |
|
183 } |