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 MOZILLA_GFX_PersistentBUFFERPROVIDER_H
8 #define MOZILLA_GFX_PersistentBUFFERPROVIDER_H
10 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
11 #include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed, etc
12 #include "mozilla/layers/KnowsCompositor.h"
13 #include "mozilla/layers/LayersTypes.h"
14 #include "mozilla/RefCounted.h"
15 #include "mozilla/gfx/Types.h"
16 #include "mozilla/Vector.h"
27 class CopyableCanvasLayer
;
31 * A PersistentBufferProvider is for users which require the temporary use of
32 * a DrawTarget to draw into. When they're done drawing they return the
33 * DrawTarget, when they later need to continue drawing they get a DrawTarget
34 * from the provider again, the provider will guarantee the contents of the
35 * previously returned DrawTarget is persisted into the one newly returned.
37 class PersistentBufferProvider
: public RefCounted
<PersistentBufferProvider
> {
39 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProvider
)
41 virtual ~PersistentBufferProvider() {}
43 virtual LayersBackend
GetType() { return LayersBackend::LAYERS_NONE
; }
46 * Get a DrawTarget from the PersistentBufferProvider.
48 * \param aPersistedRect This indicates the area of the DrawTarget that needs
49 * to have remained the same since the call to
52 virtual already_AddRefed
<gfx::DrawTarget
> BorrowDrawTarget(
53 const gfx::IntRect
& aPersistedRect
) = 0;
56 * Return a DrawTarget to the PersistentBufferProvider and indicate the
57 * contents of this DrawTarget is to be considered current by the
58 * BufferProvider. The caller should forget any references to the DrawTarget.
60 virtual bool ReturnDrawTarget(already_AddRefed
<gfx::DrawTarget
> aDT
) = 0;
62 virtual already_AddRefed
<gfx::SourceSurface
> BorrowSnapshot() = 0;
64 virtual void ReturnSnapshot(
65 already_AddRefed
<gfx::SourceSurface
> aSnapshot
) = 0;
67 virtual TextureClient
* GetTextureClient() { return nullptr; }
69 virtual void OnShutdown() {}
71 virtual bool SetKnowsCompositor(KnowsCompositor
* aKnowsCompositor
) {
75 virtual void ClearCachedResources() {}
78 * Return true if this provider preserves the drawing state (clips,
79 * transforms, etc.) across frames. In practice this means users of the
80 * provider can skip popping all of the clips at the end of the frames and
81 * pushing them back at the beginning of the following frames, which can be
82 * costly (cf. bug 1294351).
84 virtual bool PreservesDrawingState() const = 0;
87 class PersistentBufferProviderBasic
: public PersistentBufferProvider
{
89 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderBasic
,
92 static already_AddRefed
<PersistentBufferProviderBasic
> Create(
93 gfx::IntSize aSize
, gfx::SurfaceFormat aFormat
,
94 gfx::BackendType aBackend
);
96 explicit PersistentBufferProviderBasic(gfx::DrawTarget
* aTarget
);
98 virtual LayersBackend
GetType() override
{
99 return LayersBackend::LAYERS_BASIC
;
102 virtual already_AddRefed
<gfx::DrawTarget
> BorrowDrawTarget(
103 const gfx::IntRect
& aPersistedRect
) override
;
105 virtual bool ReturnDrawTarget(already_AddRefed
<gfx::DrawTarget
> aDT
) override
;
107 virtual already_AddRefed
<gfx::SourceSurface
> BorrowSnapshot() override
;
109 virtual void ReturnSnapshot(
110 already_AddRefed
<gfx::SourceSurface
> aSnapshot
) override
;
112 virtual bool PreservesDrawingState() const override
{ return true; }
114 virtual void OnShutdown() override
{ Destroy(); }
120 ~PersistentBufferProviderBasic();
122 RefPtr
<gfx::DrawTarget
> mDrawTarget
;
123 RefPtr
<gfx::SourceSurface
> mSnapshot
;
127 * Provides access to a buffer which can be sent to the compositor without
130 class PersistentBufferProviderShared
: public PersistentBufferProvider
,
131 public ActiveResource
{
133 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderShared
,
136 static already_AddRefed
<PersistentBufferProviderShared
> Create(
137 gfx::IntSize aSize
, gfx::SurfaceFormat aFormat
,
138 KnowsCompositor
* aKnowsCompositor
);
140 virtual LayersBackend
GetType() override
;
142 virtual already_AddRefed
<gfx::DrawTarget
> BorrowDrawTarget(
143 const gfx::IntRect
& aPersistedRect
) override
;
145 virtual bool ReturnDrawTarget(already_AddRefed
<gfx::DrawTarget
> aDT
) override
;
147 virtual already_AddRefed
<gfx::SourceSurface
> BorrowSnapshot() override
;
149 virtual void ReturnSnapshot(
150 already_AddRefed
<gfx::SourceSurface
> aSnapshot
) override
;
152 virtual TextureClient
* GetTextureClient() override
;
154 virtual void NotifyInactive() override
;
156 virtual void OnShutdown() override
{ Destroy(); }
158 virtual bool SetKnowsCompositor(KnowsCompositor
* aKnowsCompositor
) override
;
160 virtual void ClearCachedResources() override
;
162 virtual bool PreservesDrawingState() const override
{ return false; }
165 PersistentBufferProviderShared(gfx::IntSize aSize
, gfx::SurfaceFormat aFormat
,
166 KnowsCompositor
* aKnowsCompositor
,
167 RefPtr
<TextureClient
>& aTexture
);
169 ~PersistentBufferProviderShared();
171 TextureClient
* GetTexture(const Maybe
<uint32_t>& aIndex
);
172 bool CheckIndex(uint32_t aIndex
) { return aIndex
< mTextures
.length(); }
177 gfx::SurfaceFormat mFormat
;
178 RefPtr
<KnowsCompositor
> mKnowsCompositor
;
179 Vector
<RefPtr
<TextureClient
>, 4> mTextures
;
180 // Offset of the texture in mTextures that the canvas uses.
181 Maybe
<uint32_t> mBack
;
182 // Offset of the texture in mTextures that is presented to the compositor.
183 Maybe
<uint32_t> mFront
;
185 RefPtr
<gfx::DrawTarget
> mDrawTarget
;
186 RefPtr
<gfx::SourceSurface
> mSnapshot
;
189 struct AutoReturnSnapshot
{
190 PersistentBufferProvider
* mBufferProvider
;
191 RefPtr
<gfx::SourceSurface
>* mSnapshot
;
193 explicit AutoReturnSnapshot(PersistentBufferProvider
* aProvider
= nullptr)
194 : mBufferProvider(aProvider
), mSnapshot(nullptr) {}
196 ~AutoReturnSnapshot() {
197 if (mBufferProvider
) {
198 mBufferProvider
->ReturnSnapshot(mSnapshot
? mSnapshot
->forget()
204 } // namespace layers
205 } // namespace mozilla