Source code

Revision control

Other Tools

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/. */
6
7
#ifndef MOZILLA_GFX_PersistentBUFFERPROVIDER_H
8
#define MOZILLA_GFX_PersistentBUFFERPROVIDER_H
9
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"
17
18
namespace mozilla {
19
20
namespace gfx {
21
class SourceSurface;
22
class DrawTarget;
23
} // namespace gfx
24
25
namespace layers {
26
27
class CopyableCanvasLayer;
28
class TextureClient;
29
30
/**
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.
36
*/
37
class PersistentBufferProvider : public RefCounted<PersistentBufferProvider> {
38
public:
39
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProvider)
40
41
virtual ~PersistentBufferProvider() = default;
42
43
virtual LayersBackend GetType() { return LayersBackend::LAYERS_NONE; }
44
45
/**
46
* Get a DrawTarget from the PersistentBufferProvider.
47
*
48
* \param aPersistedRect This indicates the area of the DrawTarget that needs
49
* to have remained the same since the call to
50
* ReturnDrawTarget.
51
*/
52
virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(
53
const gfx::IntRect& aPersistedRect) = 0;
54
55
/**
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.
59
*/
60
virtual bool ReturnDrawTarget(already_AddRefed<gfx::DrawTarget> aDT) = 0;
61
62
virtual already_AddRefed<gfx::SourceSurface> BorrowSnapshot() = 0;
63
64
virtual void ReturnSnapshot(
65
already_AddRefed<gfx::SourceSurface> aSnapshot) = 0;
66
67
virtual TextureClient* GetTextureClient() { return nullptr; }
68
69
virtual void OnShutdown() {}
70
71
virtual bool SetKnowsCompositor(KnowsCompositor* aKnowsCompositor) {
72
return true;
73
}
74
75
virtual void ClearCachedResources() {}
76
77
/**
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).
83
*/
84
virtual bool PreservesDrawingState() const = 0;
85
};
86
87
class PersistentBufferProviderBasic : public PersistentBufferProvider {
88
public:
89
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderBasic,
90
override)
91
92
static already_AddRefed<PersistentBufferProviderBasic> Create(
93
gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
94
gfx::BackendType aBackend);
95
96
explicit PersistentBufferProviderBasic(gfx::DrawTarget* aTarget);
97
98
LayersBackend GetType() override { return LayersBackend::LAYERS_BASIC; }
99
100
already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(
101
const gfx::IntRect& aPersistedRect) override;
102
103
bool ReturnDrawTarget(already_AddRefed<gfx::DrawTarget> aDT) override;
104
105
already_AddRefed<gfx::SourceSurface> BorrowSnapshot() override;
106
107
void ReturnSnapshot(already_AddRefed<gfx::SourceSurface> aSnapshot) override;
108
109
bool PreservesDrawingState() const override { return true; }
110
111
void OnShutdown() override { Destroy(); }
112
113
protected:
114
void Destroy();
115
116
private:
117
virtual ~PersistentBufferProviderBasic();
118
119
RefPtr<gfx::DrawTarget> mDrawTarget;
120
RefPtr<gfx::SourceSurface> mSnapshot;
121
};
122
123
/**
124
* Provides access to a buffer which can be sent to the compositor without
125
* requiring a copy.
126
*/
127
class PersistentBufferProviderShared : public PersistentBufferProvider,
128
public ActiveResource {
129
public:
130
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderShared,
131
override)
132
133
static already_AddRefed<PersistentBufferProviderShared> Create(
134
gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
135
KnowsCompositor* aKnowsCompositor);
136
137
LayersBackend GetType() override;
138
139
already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(
140
const gfx::IntRect& aPersistedRect) override;
141
142
bool ReturnDrawTarget(already_AddRefed<gfx::DrawTarget> aDT) override;
143
144
already_AddRefed<gfx::SourceSurface> BorrowSnapshot() override;
145
146
void ReturnSnapshot(already_AddRefed<gfx::SourceSurface> aSnapshot) override;
147
148
TextureClient* GetTextureClient() override;
149
150
void NotifyInactive() override;
151
152
void OnShutdown() override { Destroy(); }
153
154
bool SetKnowsCompositor(KnowsCompositor* aKnowsCompositor) override;
155
156
void ClearCachedResources() override;
157
158
bool PreservesDrawingState() const override { return false; }
159
160
protected:
161
PersistentBufferProviderShared(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
162
KnowsCompositor* aKnowsCompositor,
163
RefPtr<TextureClient>& aTexture);
164
165
~PersistentBufferProviderShared();
166
167
TextureClient* GetTexture(const Maybe<uint32_t>& aIndex);
168
bool CheckIndex(uint32_t aIndex) { return aIndex < mTextures.length(); }
169
170
void Destroy();
171
172
gfx::IntSize mSize;
173
gfx::SurfaceFormat mFormat;
174
RefPtr<KnowsCompositor> mKnowsCompositor;
175
// We may need two extra textures if webrender is enabled.
176
static const size_t kMaxTexturesAllowed = 4;
177
Vector<RefPtr<TextureClient>, kMaxTexturesAllowed + 2> mTextures;
178
// Offset of the texture in mTextures that the canvas uses.
179
Maybe<uint32_t> mBack;
180
// Offset of the texture in mTextures that is presented to the compositor.
181
Maybe<uint32_t> mFront;
182
// Offset of the texture in mTextures which texture's readlock is unreliable.
183
// Therefore it should not be used as next back buffer.
184
Maybe<uint32_t> mTextureLockIsUnreliable;
185
186
RefPtr<gfx::DrawTarget> mDrawTarget;
187
RefPtr<gfx::SourceSurface> mSnapshot;
188
RefPtr<gfx::SourceSurface> mPreviousSnapshot;
189
size_t mMaxAllowedTextures = kMaxTexturesAllowed;
190
};
191
192
struct AutoReturnSnapshot final {
193
PersistentBufferProvider* mBufferProvider;
194
RefPtr<gfx::SourceSurface>* mSnapshot;
195
196
explicit AutoReturnSnapshot(PersistentBufferProvider* aProvider = nullptr)
197
: mBufferProvider(aProvider), mSnapshot(nullptr) {}
198
199
~AutoReturnSnapshot() {
200
if (mBufferProvider) {
201
mBufferProvider->ReturnSnapshot(mSnapshot ? mSnapshot->forget()
202
: nullptr);
203
}
204
}
205
};
206
207
} // namespace layers
208
} // namespace mozilla
209
210
#endif