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 MOZILLA_GFX_PersistentBUFFERPROVIDER_H
7 : #define MOZILLA_GFX_PersistentBUFFERPROVIDER_H
8 :
9 : #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
10 : #include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed, etc
11 : #include "mozilla/layers/LayersTypes.h"
12 : #include "mozilla/layers/ShadowLayers.h"
13 : #include "mozilla/gfx/Types.h"
14 : #include "mozilla/Vector.h"
15 :
16 : namespace mozilla {
17 :
18 : namespace gfx {
19 : class SourceSurface;
20 : class DrawTarget;
21 : }
22 :
23 : namespace layers {
24 :
25 : class CopyableCanvasLayer;
26 :
27 : /**
28 : * A PersistentBufferProvider is for users which require the temporary use of
29 : * a DrawTarget to draw into. When they're done drawing they return the
30 : * DrawTarget, when they later need to continue drawing they get a DrawTarget
31 : * from the provider again, the provider will guarantee the contents of the
32 : * previously returned DrawTarget is persisted into the one newly returned.
33 : */
34 0 : class PersistentBufferProvider : public RefCounted<PersistentBufferProvider>
35 : {
36 : public:
37 0 : MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProvider)
38 :
39 0 : virtual ~PersistentBufferProvider() { }
40 :
41 0 : virtual LayersBackend GetType() { return LayersBackend::LAYERS_NONE; }
42 :
43 : /**
44 : * Get a DrawTarget from the PersistentBufferProvider.
45 : *
46 : * \param aPersistedRect This indicates the area of the DrawTarget that needs
47 : * to have remained the same since the call to
48 : * ReturnDrawTarget.
49 : */
50 : virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(const gfx::IntRect& aPersistedRect) = 0;
51 :
52 : /**
53 : * Return a DrawTarget to the PersistentBufferProvider and indicate the
54 : * contents of this DrawTarget is to be considered current by the
55 : * BufferProvider. The caller should forget any references to the DrawTarget.
56 : */
57 : virtual bool ReturnDrawTarget(already_AddRefed<gfx::DrawTarget> aDT) = 0;
58 :
59 : virtual already_AddRefed<gfx::SourceSurface> BorrowSnapshot() = 0;
60 :
61 : virtual void ReturnSnapshot(already_AddRefed<gfx::SourceSurface> aSnapshot) = 0;
62 :
63 0 : virtual TextureClient* GetTextureClient() { return nullptr; }
64 :
65 0 : virtual void OnShutdown() {}
66 :
67 0 : virtual bool SetForwarder(ShadowLayerForwarder* aFwd) { return true; }
68 :
69 0 : virtual void ClearCachedResources() {}
70 :
71 : /**
72 : * Return true if this provider preserves the drawing state (clips, transforms,
73 : * etc.) across frames. In practice this means users of the provider can skip
74 : * popping all of the clips at the end of the frames and pushing them back at
75 : * the beginning of the following frames, which can be costly (cf. bug 1294351).
76 : */
77 : virtual bool PreservesDrawingState() const = 0;
78 : };
79 :
80 :
81 : class PersistentBufferProviderBasic : public PersistentBufferProvider
82 : {
83 : public:
84 0 : MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderBasic, override)
85 :
86 : static already_AddRefed<PersistentBufferProviderBasic>
87 : Create(gfx::IntSize aSize, gfx::SurfaceFormat aFormat, gfx::BackendType aBackend);
88 :
89 : explicit PersistentBufferProviderBasic(gfx::DrawTarget* aTarget);
90 :
91 0 : virtual LayersBackend GetType() override { return LayersBackend::LAYERS_BASIC; }
92 :
93 : virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(const gfx::IntRect& aPersistedRect) override;
94 :
95 : virtual bool ReturnDrawTarget(already_AddRefed<gfx::DrawTarget> aDT) override;
96 :
97 : virtual already_AddRefed<gfx::SourceSurface> BorrowSnapshot() override;
98 :
99 : virtual void ReturnSnapshot(already_AddRefed<gfx::SourceSurface> aSnapshot) override;
100 :
101 0 : virtual bool PreservesDrawingState() const override { return true; }
102 : private:
103 : ~PersistentBufferProviderBasic();
104 :
105 : RefPtr<gfx::DrawTarget> mDrawTarget;
106 : RefPtr<gfx::SourceSurface> mSnapshot;
107 : };
108 :
109 :
110 : /**
111 : * Provides access to a buffer which can be sent to the compositor without
112 : * requiring a copy.
113 : */
114 : class PersistentBufferProviderShared : public PersistentBufferProvider
115 : , public ActiveResource
116 : {
117 : public:
118 0 : MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderShared, override)
119 :
120 : static already_AddRefed<PersistentBufferProviderShared>
121 : Create(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
122 : ShadowLayerForwarder* aFwd);
123 :
124 0 : virtual LayersBackend GetType() override { return LayersBackend::LAYERS_CLIENT; }
125 :
126 : virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(const gfx::IntRect& aPersistedRect) override;
127 :
128 : virtual bool ReturnDrawTarget(already_AddRefed<gfx::DrawTarget> aDT) override;
129 :
130 : virtual already_AddRefed<gfx::SourceSurface> BorrowSnapshot() override;
131 :
132 : virtual void ReturnSnapshot(already_AddRefed<gfx::SourceSurface> aSnapshot) override;
133 :
134 : virtual TextureClient* GetTextureClient() override;
135 :
136 : virtual void NotifyInactive() override;
137 :
138 0 : virtual void OnShutdown() override { Destroy(); }
139 :
140 : virtual bool SetForwarder(ShadowLayerForwarder* aFwd) override;
141 :
142 : virtual void ClearCachedResources() override;
143 :
144 0 : virtual bool PreservesDrawingState() const override { return false; }
145 : protected:
146 : PersistentBufferProviderShared(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
147 : ShadowLayerForwarder* aFwd,
148 : RefPtr<TextureClient>& aTexture);
149 :
150 : ~PersistentBufferProviderShared();
151 :
152 : TextureClient* GetTexture(const Maybe<uint32_t>& aIndex);
153 0 : bool CheckIndex(uint32_t aIndex) { return aIndex < mTextures.length(); }
154 :
155 : void Destroy();
156 :
157 : gfx::IntSize mSize;
158 : gfx::SurfaceFormat mFormat;
159 : RefPtr<ShadowLayerForwarder> mFwd;
160 : Vector<RefPtr<TextureClient>, 4> mTextures;
161 : // Offset of the texture in mTextures that the canvas uses.
162 : Maybe<uint32_t> mBack;
163 : // Offset of the texture in mTextures that is presented to the compositor.
164 : Maybe<uint32_t> mFront;
165 :
166 : RefPtr<gfx::DrawTarget> mDrawTarget;
167 : RefPtr<gfx::SourceSurface > mSnapshot;
168 : };
169 :
170 : struct AutoReturnSnapshot
171 : {
172 : PersistentBufferProvider* mBufferProvider;
173 : RefPtr<gfx::SourceSurface>* mSnapshot;
174 :
175 0 : explicit AutoReturnSnapshot(PersistentBufferProvider* aProvider = nullptr)
176 0 : : mBufferProvider(aProvider)
177 0 : , mSnapshot(nullptr)
178 0 : {}
179 :
180 0 : ~AutoReturnSnapshot()
181 0 : {
182 0 : if (mBufferProvider) {
183 0 : mBufferProvider->ReturnSnapshot(mSnapshot ? mSnapshot->forget() : nullptr);
184 : }
185 0 : }
186 : };
187 :
188 : } // namespace layers
189 : } // namespace mozilla
190 :
191 : #endif
|