1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef GFX_CONTENTHOST_H
8 #define GFX_CONTENTHOST_H
10 #include <stdint.h> // for uint32_t
11 #include <stdio.h> // for FILE
12 #include "mozilla-config.h" // for MOZ_DUMP_PAINTING
13 #include "CompositableHost.h" // for CompositableHost, etc
14 #include "RotatedBuffer.h" // for RotatedBuffer, etc
15 #include "mozilla/Attributes.h" // for override
16 #include "mozilla/RefPtr.h" // for RefPtr
17 #include "mozilla/gfx/BasePoint.h" // for BasePoint
18 #include "mozilla/gfx/MatrixFwd.h" // for Matrix4x4
19 #include "mozilla/gfx/Point.h" // for Point
20 #include "mozilla/gfx/Polygon.h" // for Polygon
21 #include "mozilla/gfx/Rect.h" // for Rect
22 #include "mozilla/gfx/Types.h" // for SamplingFilter
23 #include "mozilla/layers/CompositorTypes.h" // for TextureInfo, etc
24 #include "mozilla/layers/ContentClient.h" // for ContentClient
25 #include "mozilla/layers/ISurfaceAllocator.h" // for ISurfaceAllocator
26 #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor
27 #include "mozilla/layers/LayersTypes.h" // for etc
28 #include "mozilla/layers/TextureHost.h" // for TextureHost
29 #include "mozilla/mozalloc.h" // for operator delete
30 #include "mozilla/UniquePtr.h" // for UniquePtr
31 #include "nsCOMPtr.h" // for already_AddRefed
32 #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
33 #include "nsPoint.h" // for nsIntPoint
34 #include "nsRect.h" // for mozilla::gfx::IntRect
35 #include "nsRegion.h" // for nsIntRegion
36 #include "nsTArray.h" // for nsTArray
37 #include "nscore.h" // for nsACString
42 class ThebesBufferData
;
45 struct TexturedEffect
;
48 * ContentHosts are used for compositing Painted layers, always matched by a
49 * ContentClient of the same type.
51 * ContentHosts support only UpdateThebes(), not Update().
53 class ContentHost
: public CompositableHost
{
55 virtual bool UpdateThebes(
56 const ThebesBufferData
& aData
, const nsIntRegion
& aUpdated
,
57 const nsIntRegion
& aOldValidRegionBack
) override
= 0;
59 virtual void SetPaintWillResample(bool aResample
) {
60 mPaintWillResample
= aResample
;
62 bool PaintWillResample() { return mPaintWillResample
; }
64 // We use this to allow TiledContentHost to invalidate regions where
65 // tiles are fading in.
66 virtual void AddAnimationInvalidation(nsIntRegion
& aRegion
) {}
68 virtual gfx::IntRect
GetBufferRect() {
69 MOZ_ASSERT_UNREACHABLE("Must be implemented in derived class");
70 return gfx::IntRect();
73 ContentHost
* AsContentHost() override
{ return this; }
76 explicit ContentHost(const TextureInfo
& aTextureInfo
)
77 : CompositableHost(aTextureInfo
), mPaintWillResample(false) {}
79 bool mPaintWillResample
;
83 * Base class for non-tiled ContentHosts.
85 * Ownership of the SurfaceDescriptor and the resources it represents is passed
86 * from the ContentClient to the ContentHost when the TextureClient/Hosts are
87 * created, that is recevied here by SetTextureHosts which assigns one or two
88 * texture hosts (for single and double buffering) to the ContentHost.
90 * It is the responsibility of the ContentHost to destroy its resources when
91 * they are recreated or the ContentHost dies.
93 class ContentHostBase
: public ContentHost
{
95 typedef ContentClient::ContentType ContentType
;
96 typedef ContentClient::PaintState PaintState
;
98 explicit ContentHostBase(const TextureInfo
& aTextureInfo
);
99 virtual ~ContentHostBase();
101 gfx::IntRect
GetBufferRect() override
{ return mBufferRect
; }
103 virtual nsIntPoint
GetOriginOffset() {
104 return mBufferRect
.TopLeft() - mBufferRotation
;
107 gfx::IntPoint
GetBufferRotation() { return mBufferRotation
.ToUnknownPoint(); }
110 gfx::IntRect mBufferRect
;
111 nsIntPoint mBufferRotation
;
116 * Shared ContentHostBase implementation for content hosts that
117 * use up to two TextureHosts.
119 class ContentHostTexture
: public ContentHostBase
{
121 explicit ContentHostTexture(const TextureInfo
& aTextureInfo
)
122 : ContentHostBase(aTextureInfo
),
124 mReceivedNewHost(false) {}
126 void Composite(Compositor
* aCompositor
, LayerComposite
* aLayer
,
127 EffectChain
& aEffectChain
, float aOpacity
,
128 const gfx::Matrix4x4
& aTransform
,
129 const gfx::SamplingFilter aSamplingFilter
,
130 const gfx::IntRect
& aClipRect
,
131 const nsIntRegion
* aVisibleRegion
= nullptr,
132 const Maybe
<gfx::Polygon
>& aGeometry
= Nothing()) override
;
134 void SetTextureSourceProvider(TextureSourceProvider
* aProvider
) override
;
136 already_AddRefed
<gfx::DataSourceSurface
> GetAsSurface() override
;
138 void Dump(std::stringstream
& aStream
, const char* aPrefix
= "",
139 bool aDumpHtml
= false) override
;
141 void PrintInfo(std::stringstream
& aStream
, const char* aPrefix
) override
;
143 void UseTextureHost(const nsTArray
<TimedTexture
>& aTextures
) override
;
144 void UseComponentAlphaTextures(TextureHost
* aTextureOnBlack
,
145 TextureHost
* aTextureOnWhite
) override
;
147 bool Lock() override
{
148 MOZ_ASSERT(!mLocked
);
152 if (!mTextureHost
->Lock()) {
156 if (mTextureHostOnWhite
&& !mTextureHostOnWhite
->Lock()) {
163 void Unlock() override
{
165 mTextureHost
->Unlock();
166 if (mTextureHostOnWhite
) {
167 mTextureHostOnWhite
->Unlock();
172 bool HasComponentAlpha() const { return !!mTextureHostOnWhite
; }
174 RefPtr
<TextureSource
> AcquireTextureSource();
175 RefPtr
<TextureSource
> AcquireTextureSourceOnWhite();
177 ContentHostTexture
* AsContentHostTexture() override
{ return this; }
179 already_AddRefed
<TexturedEffect
> GenEffect(
180 const gfx::SamplingFilter aSamplingFilter
) override
;
183 CompositableTextureHostRef mTextureHost
;
184 CompositableTextureHostRef mTextureHostOnWhite
;
185 CompositableTextureSourceRef mTextureSource
;
186 CompositableTextureSourceRef mTextureSourceOnWhite
;
188 bool mReceivedNewHost
;
192 * Double buffering is implemented by swapping the front and back TextureHosts.
193 * We assume that whenever we use double buffering, then we have
194 * render-to-texture and thus no texture upload to do.
196 class ContentHostDoubleBuffered
: public ContentHostTexture
{
198 explicit ContentHostDoubleBuffered(const TextureInfo
& aTextureInfo
)
199 : ContentHostTexture(aTextureInfo
) {}
201 virtual ~ContentHostDoubleBuffered() = default;
203 CompositableType
GetType() override
{
204 return CompositableType::CONTENT_DOUBLE
;
207 virtual bool UpdateThebes(const ThebesBufferData
& aData
,
208 const nsIntRegion
& aUpdated
,
209 const nsIntRegion
& aOldValidRegionBack
) override
;
212 nsIntRegion mValidRegionForNextBackBuffer
;
216 * Single buffered, therefore we must synchronously upload the image from the
217 * TextureHost in the layers transaction (i.e., in UpdateThebes).
219 class ContentHostSingleBuffered
: public ContentHostTexture
{
221 explicit ContentHostSingleBuffered(const TextureInfo
& aTextureInfo
)
222 : ContentHostTexture(aTextureInfo
) {}
223 virtual ~ContentHostSingleBuffered() = default;
225 CompositableType
GetType() override
{
226 return CompositableType::CONTENT_SINGLE
;
229 bool UpdateThebes(const ThebesBufferData
& aData
, const nsIntRegion
& aUpdated
,
230 const nsIntRegion
& aOldValidRegionBack
) override
;
233 } // namespace layers
234 } // namespace mozilla