Line data Source code
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 : #ifndef GFX_CONTENTHOST_H
7 : #define GFX_CONTENTHOST_H
8 :
9 : #include <stdint.h> // for uint32_t
10 : #include <stdio.h> // for FILE
11 : #include "mozilla-config.h" // for MOZ_DUMP_PAINTING
12 : #include "CompositableHost.h" // for CompositableHost, etc
13 : #include "RotatedBuffer.h" // for RotatedContentBuffer, etc
14 : #include "mozilla/Attributes.h" // for override
15 : #include "mozilla/RefPtr.h" // for RefPtr
16 : #include "mozilla/gfx/BasePoint.h" // for BasePoint
17 : #include "mozilla/gfx/MatrixFwd.h" // for Matrix4x4
18 : #include "mozilla/gfx/Point.h" // for Point
19 : #include "mozilla/gfx/Polygon.h" // for Polygon
20 : #include "mozilla/gfx/Rect.h" // for Rect
21 : #include "mozilla/gfx/Types.h" // for SamplingFilter
22 : #include "mozilla/layers/CompositorTypes.h" // for TextureInfo, etc
23 : #include "mozilla/layers/ISurfaceAllocator.h" // for ISurfaceAllocator
24 : #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor
25 : #include "mozilla/layers/LayersTypes.h" // for etc
26 : #include "mozilla/layers/TextureHost.h" // for TextureHost
27 : #include "mozilla/mozalloc.h" // for operator delete
28 : #include "mozilla/UniquePtr.h" // for UniquePtr
29 : #include "nsCOMPtr.h" // for already_AddRefed
30 : #include "nsDebug.h" // for NS_RUNTIMEABORT
31 : #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
32 : #include "nsPoint.h" // for nsIntPoint
33 : #include "nsRect.h" // for mozilla::gfx::IntRect
34 : #include "nsRegion.h" // for nsIntRegion
35 : #include "nsTArray.h" // for nsTArray
36 : #include "nscore.h" // for nsACString
37 :
38 : namespace mozilla {
39 : namespace layers {
40 : class Compositor;
41 : class ThebesBufferData;
42 : struct EffectChain;
43 :
44 : struct TexturedEffect;
45 :
46 : /**
47 : * ContentHosts are used for compositing Painted layers, always matched by a
48 : * ContentClient of the same type.
49 : *
50 : * ContentHosts support only UpdateThebes(), not Update().
51 : */
52 19 : class ContentHost : public CompositableHost
53 : {
54 : public:
55 : virtual bool UpdateThebes(const ThebesBufferData& aData,
56 : const nsIntRegion& aUpdated,
57 : const nsIntRegion& aOldValidRegionBack) = 0;
58 :
59 56 : virtual void SetPaintWillResample(bool aResample) { mPaintWillResample = aResample; }
60 56 : bool PaintWillResample() { return mPaintWillResample; }
61 :
62 : // We use this to allow TiledContentHost to invalidate regions where
63 : // tiles are fading in.
64 67 : virtual void AddAnimationInvalidation(nsIntRegion& aRegion) { }
65 :
66 0 : virtual gfx::IntRect GetBufferRect() {
67 0 : MOZ_ASSERT_UNREACHABLE("Must be implemented in derived class");
68 : return gfx::IntRect();
69 : }
70 :
71 : protected:
72 22 : explicit ContentHost(const TextureInfo& aTextureInfo)
73 22 : : CompositableHost(aTextureInfo)
74 22 : , mPaintWillResample(false)
75 22 : {}
76 :
77 : bool mPaintWillResample;
78 : };
79 :
80 : /**
81 : * Base class for non-tiled ContentHosts.
82 : *
83 : * Ownership of the SurfaceDescriptor and the resources it represents is passed
84 : * from the ContentClient to the ContentHost when the TextureClient/Hosts are
85 : * created, that is recevied here by SetTextureHosts which assigns one or two
86 : * texture hosts (for single and double buffering) to the ContentHost.
87 : *
88 : * It is the responsibility of the ContentHost to destroy its resources when
89 : * they are recreated or the ContentHost dies.
90 : */
91 : class ContentHostBase : public ContentHost
92 : {
93 : public:
94 : typedef RotatedContentBuffer::ContentType ContentType;
95 : typedef RotatedContentBuffer::PaintState PaintState;
96 :
97 : explicit ContentHostBase(const TextureInfo& aTextureInfo);
98 : virtual ~ContentHostBase();
99 :
100 0 : virtual gfx::IntRect GetBufferRect() override { return mBufferRect; }
101 :
102 56 : virtual nsIntPoint GetOriginOffset()
103 : {
104 56 : return mBufferRect.TopLeft() - mBufferRotation;
105 : }
106 :
107 : gfx::IntPoint GetBufferRotation()
108 : {
109 : return mBufferRotation.ToUnknownPoint();
110 : }
111 :
112 : protected:
113 : gfx::IntRect mBufferRect;
114 : nsIntPoint mBufferRotation;
115 : bool mInitialised;
116 : };
117 :
118 : /**
119 : * Shared ContentHostBase implementation for content hosts that
120 : * use up to two TextureHosts.
121 : */
122 19 : class ContentHostTexture : public ContentHostBase
123 : {
124 : public:
125 22 : explicit ContentHostTexture(const TextureInfo& aTextureInfo)
126 22 : : ContentHostBase(aTextureInfo)
127 : , mLocked(false)
128 22 : , mReceivedNewHost(false)
129 22 : { }
130 :
131 : virtual void Composite(Compositor* aCompositor,
132 : LayerComposite* aLayer,
133 : EffectChain& aEffectChain,
134 : float aOpacity,
135 : const gfx::Matrix4x4& aTransform,
136 : const gfx::SamplingFilter aSamplingFilter,
137 : const gfx::IntRect& aClipRect,
138 : const nsIntRegion* aVisibleRegion = nullptr,
139 : const Maybe<gfx::Polygon>& aGeometry = Nothing()) override;
140 :
141 : virtual void SetTextureSourceProvider(TextureSourceProvider* aProvider) override;
142 :
143 : virtual already_AddRefed<gfx::DataSourceSurface> GetAsSurface() override;
144 :
145 : virtual void Dump(std::stringstream& aStream,
146 : const char* aPrefix="",
147 : bool aDumpHtml=false) override;
148 :
149 : virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix) override;
150 :
151 : virtual void UseTextureHost(const nsTArray<TimedTexture>& aTextures) override;
152 : virtual void UseComponentAlphaTextures(TextureHost* aTextureOnBlack,
153 : TextureHost* aTextureOnWhite) override;
154 :
155 56 : virtual bool Lock() override {
156 56 : MOZ_ASSERT(!mLocked);
157 56 : if (!mTextureHost) {
158 0 : return false;
159 : }
160 56 : if (!mTextureHost->Lock()) {
161 0 : return false;
162 : }
163 :
164 56 : if (mTextureHostOnWhite && !mTextureHostOnWhite->Lock()) {
165 0 : return false;
166 : }
167 :
168 56 : mLocked = true;
169 56 : return true;
170 : }
171 56 : virtual void Unlock() override {
172 56 : MOZ_ASSERT(mLocked);
173 56 : mTextureHost->Unlock();
174 56 : if (mTextureHostOnWhite) {
175 0 : mTextureHostOnWhite->Unlock();
176 : }
177 56 : mLocked = false;
178 56 : }
179 :
180 : bool HasComponentAlpha() const {
181 : return !!mTextureHostOnWhite;
182 : }
183 :
184 : RefPtr<TextureSource> AcquireTextureSource();
185 : RefPtr<TextureSource> AcquireTextureSourceOnWhite();
186 :
187 0 : ContentHostTexture* AsContentHostTexture() override { return this; }
188 :
189 : virtual already_AddRefed<TexturedEffect> GenEffect(const gfx::SamplingFilter aSamplingFilter) override;
190 :
191 : protected:
192 : CompositableTextureHostRef mTextureHost;
193 : CompositableTextureHostRef mTextureHostOnWhite;
194 : CompositableTextureSourceRef mTextureSource;
195 : CompositableTextureSourceRef mTextureSourceOnWhite;
196 : bool mLocked;
197 : bool mReceivedNewHost;
198 : };
199 :
200 : /**
201 : * Double buffering is implemented by swapping the front and back TextureHosts.
202 : * We assume that whenever we use double buffering, then we have
203 : * render-to-texture and thus no texture upload to do.
204 : */
205 : class ContentHostDoubleBuffered : public ContentHostTexture
206 : {
207 : public:
208 22 : explicit ContentHostDoubleBuffered(const TextureInfo& aTextureInfo)
209 22 : : ContentHostTexture(aTextureInfo)
210 22 : {}
211 :
212 57 : virtual ~ContentHostDoubleBuffered() {}
213 :
214 22 : virtual CompositableType GetType() { return CompositableType::CONTENT_DOUBLE; }
215 :
216 : virtual bool UpdateThebes(const ThebesBufferData& aData,
217 : const nsIntRegion& aUpdated,
218 : const nsIntRegion& aOldValidRegionBack);
219 :
220 : protected:
221 : nsIntRegion mValidRegionForNextBackBuffer;
222 : };
223 :
224 : /**
225 : * Single buffered, therefore we must synchronously upload the image from the
226 : * TextureHost in the layers transaction (i.e., in UpdateThebes).
227 : */
228 : class ContentHostSingleBuffered : public ContentHostTexture
229 : {
230 : public:
231 0 : explicit ContentHostSingleBuffered(const TextureInfo& aTextureInfo)
232 0 : : ContentHostTexture(aTextureInfo)
233 0 : {}
234 0 : virtual ~ContentHostSingleBuffered() {}
235 :
236 0 : virtual CompositableType GetType() { return CompositableType::CONTENT_SINGLE; }
237 :
238 : virtual bool UpdateThebes(const ThebesBufferData& aData,
239 : const nsIntRegion& aUpdated,
240 : const nsIntRegion& aOldValidRegionBack);
241 : };
242 :
243 : } // namespace layers
244 : } // namespace mozilla
245 :
246 : #endif
|