Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/layers/PersistentBufferProvider.h
Line
Count
Source (jump to first uncovered line)
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
}
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
{
39
public:
40
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProvider)
41
42
0
  virtual ~PersistentBufferProvider() { }
43
44
0
  virtual LayersBackend GetType() { return LayersBackend::LAYERS_NONE; }
45
46
  /**
47
   * Get a DrawTarget from the PersistentBufferProvider.
48
   *
49
   * \param aPersistedRect This indicates the area of the DrawTarget that needs
50
   *                       to have remained the same since the call to
51
   *                       ReturnDrawTarget.
52
   */
53
  virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(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(already_AddRefed<gfx::SourceSurface> aSnapshot) = 0;
65
66
0
  virtual TextureClient* GetTextureClient() { return nullptr; }
67
68
0
  virtual void OnShutdown() {}
69
70
0
  virtual bool SetKnowsCompositor(KnowsCompositor* aKnowsCompositor) { return true; }
71
72
0
  virtual void ClearCachedResources() {}
73
74
  /**
75
   * Return true if this provider preserves the drawing state (clips, transforms,
76
   * etc.) across frames. In practice this means users of the provider can skip
77
   * popping all of the clips at the end of the frames and pushing them back at
78
   * the beginning of the following frames, which can be costly (cf. bug 1294351).
79
   */
80
  virtual bool PreservesDrawingState() const = 0;
81
};
82
83
84
class PersistentBufferProviderBasic : public PersistentBufferProvider
85
{
86
public:
87
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderBasic, override)
88
89
  static already_AddRefed<PersistentBufferProviderBasic>
90
  Create(gfx::IntSize aSize, gfx::SurfaceFormat aFormat, gfx::BackendType aBackend);
91
92
  explicit PersistentBufferProviderBasic(gfx::DrawTarget* aTarget);
93
94
0
  virtual LayersBackend GetType() override { return LayersBackend::LAYERS_BASIC; }
95
96
  virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(const gfx::IntRect& aPersistedRect) override;
97
98
  virtual bool ReturnDrawTarget(already_AddRefed<gfx::DrawTarget> aDT) override;
99
100
  virtual already_AddRefed<gfx::SourceSurface> BorrowSnapshot() override;
101
102
  virtual void ReturnSnapshot(already_AddRefed<gfx::SourceSurface> aSnapshot) override;
103
104
0
  virtual bool PreservesDrawingState() const override { return true; }
105
106
0
  virtual void OnShutdown() override { Destroy(); }
107
108
protected:
109
  void Destroy();
110
111
private:
112
  ~PersistentBufferProviderBasic();
113
114
  RefPtr<gfx::DrawTarget> mDrawTarget;
115
  RefPtr<gfx::SourceSurface> mSnapshot;
116
};
117
118
119
/**
120
 * Provides access to a buffer which can be sent to the compositor without
121
 * requiring a copy.
122
 */
123
class PersistentBufferProviderShared : public PersistentBufferProvider
124
                                     , public ActiveResource
125
{
126
public:
127
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PersistentBufferProviderShared, override)
128
129
  static already_AddRefed<PersistentBufferProviderShared>
130
  Create(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
131
         KnowsCompositor* aKnowsCompositor);
132
133
  virtual LayersBackend GetType() override;
134
135
  virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget(const gfx::IntRect& aPersistedRect) override;
136
137
  virtual bool ReturnDrawTarget(already_AddRefed<gfx::DrawTarget> aDT) override;
138
139
  virtual already_AddRefed<gfx::SourceSurface> BorrowSnapshot() override;
140
141
  virtual void ReturnSnapshot(already_AddRefed<gfx::SourceSurface> aSnapshot) override;
142
143
  virtual TextureClient* GetTextureClient() override;
144
145
  virtual void NotifyInactive() override;
146
147
0
  virtual void OnShutdown() override { Destroy(); }
148
149
  virtual bool SetKnowsCompositor(KnowsCompositor* aKnowsCompositor) override;
150
151
  virtual void ClearCachedResources() override;
152
153
0
  virtual bool PreservesDrawingState() const override { return false; }
154
protected:
155
  PersistentBufferProviderShared(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
156
                                 KnowsCompositor* aKnowsCompositor,
157
                                 RefPtr<TextureClient>& aTexture);
158
159
  ~PersistentBufferProviderShared();
160
161
  TextureClient* GetTexture(const Maybe<uint32_t>& aIndex);
162
0
  bool CheckIndex(uint32_t aIndex) { return aIndex < mTextures.length(); }
163
164
  void Destroy();
165
166
  gfx::IntSize mSize;
167
  gfx::SurfaceFormat mFormat;
168
  RefPtr<KnowsCompositor> mKnowsCompositor;
169
  Vector<RefPtr<TextureClient>, 4> mTextures;
170
  // Offset of the texture in mTextures that the canvas uses.
171
  Maybe<uint32_t> mBack;
172
  // Offset of the texture in mTextures that is presented to the compositor.
173
  Maybe<uint32_t> mFront;
174
175
  RefPtr<gfx::DrawTarget> mDrawTarget;
176
  RefPtr<gfx::SourceSurface > mSnapshot;
177
};
178
179
struct AutoReturnSnapshot
180
{
181
  PersistentBufferProvider* mBufferProvider;
182
  RefPtr<gfx::SourceSurface>* mSnapshot;
183
184
  explicit AutoReturnSnapshot(PersistentBufferProvider* aProvider = nullptr)
185
  : mBufferProvider(aProvider)
186
  , mSnapshot(nullptr)
187
  {}
188
189
  ~AutoReturnSnapshot()
190
  {
191
    if (mBufferProvider) {
192
      mBufferProvider->ReturnSnapshot(mSnapshot ? mSnapshot->forget() : nullptr);
193
    }
194
  }
195
};
196
197
} // namespace layers
198
} // namespace mozilla
199
200
#endif