Bug 1521243 - Show a warning for invalid declarations and filter icon for overridden...
[gecko.git] / gfx / layers / PersistentBufferProvider.h
blob4bfcde6e571c9fd3b2569ce8f4120ba2493c9e8e
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"
18 namespace mozilla {
20 namespace gfx {
21 class SourceSurface;
22 class DrawTarget;
23 } // namespace gfx
25 namespace layers {
27 class CopyableCanvasLayer;
28 class TextureClient;
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.
37 class PersistentBufferProvider : public RefCounted<PersistentBufferProvider> {
38 public:
39 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProvider)
41 virtual ~PersistentBufferProvider() {}
43 virtual LayersBackend GetType() { return LayersBackend::LAYERS_NONE; }
45 /**
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
50 * ReturnDrawTarget.
52 virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(
53 const gfx::IntRect& aPersistedRect) = 0;
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.
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) {
72 return true;
75 virtual void ClearCachedResources() {}
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).
84 virtual bool PreservesDrawingState() const = 0;
87 class PersistentBufferProviderBasic : public PersistentBufferProvider {
88 public:
89 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderBasic,
90 override)
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(); }
116 protected:
117 void Destroy();
119 private:
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
128 * requiring a copy.
130 class PersistentBufferProviderShared : public PersistentBufferProvider,
131 public ActiveResource {
132 public:
133 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderShared,
134 override)
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; }
164 protected:
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(); }
174 void Destroy();
176 gfx::IntSize mSize;
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()
199 : nullptr);
204 } // namespace layers
205 } // namespace mozilla
207 #endif