Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/layers/CompositableClient.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_BUFFERCLIENT_H
8
#define MOZILLA_GFX_BUFFERCLIENT_H
9
10
#include <stdint.h>                     // for uint64_t
11
#include <vector>                       // for vector
12
#include <map>                          // for map
13
#include "mozilla/Assertions.h"         // for MOZ_CRASH
14
#include "mozilla/RefPtr.h"             // for already_AddRefed, RefCounted
15
#include "mozilla/gfx/Types.h"          // for SurfaceFormat
16
#include "mozilla/layers/CompositorTypes.h"
17
#include "mozilla/layers/LayersTypes.h"  // for LayersBackend, TextureDumpMode
18
#include "mozilla/layers/TextureClient.h"  // for TextureClient
19
#include "nsISupportsImpl.h"            // for MOZ_COUNT_CTOR, etc
20
21
namespace mozilla {
22
namespace layers {
23
24
class CompositableClient;
25
class ImageBridgeChild;
26
class ImageContainer;
27
class CompositableForwarder;
28
class CompositableChild;
29
class TextureClientRecycleAllocator;
30
class ContentClientRemoteBuffer;
31
32
/**
33
 * CompositableClient manages the texture-specific logic for composite layers,
34
 * independently of the layer. It is the content side of a CompositableClient/
35
 * CompositableHost pair.
36
 *
37
 * CompositableClient's purpose is to send texture data to the compositor side
38
 * along with any extra information about how the texture is to be composited.
39
 * Things like opacity or transformation belong to layer and not compositable.
40
 *
41
 * Since Compositables are independent of layers it is possible to create one,
42
 * connect it to the compositor side, and start sending images to it. This alone
43
 * is arguably not very useful, but it means that as long as a shadow layer can
44
 * do the proper magic to find a reference to the right CompositableHost on the
45
 * Compositor side, a Compositable client can be used outside of the main
46
 * shadow layer forwarder machinery that is used on the main thread.
47
 *
48
 * The first step is to create a Compositable client and call Connect().
49
 * Connect() creates the underlying IPDL actor (see CompositableChild) and the
50
 * corresponding CompositableHost on the other side.
51
 *
52
 * To do in-transaction texture transfer (the default), call
53
 * ShadowLayerForwarder::Attach(CompositableClient*, ShadowableLayer*). This
54
 * will let the LayerComposite on the compositor side know which CompositableHost
55
 * to use for compositing.
56
 *
57
 * To do async texture transfer (like async-video), the CompositableClient
58
 * should be created with a different CompositableForwarder (like
59
 * ImageBridgeChild) and attachment is done with
60
 * CompositableForwarder::AttachAsyncCompositable that takes an identifier
61
 * instead of a CompositableChild, since the CompositableClient is not managed
62
 * by this layer forwarder (the matching uses a global map on the compositor side,
63
 * see CompositableMap in ImageBridgeParent.cpp)
64
 *
65
 * Subclasses: Painted layers use ContentClients, ImageLayers use ImageClients,
66
 * Canvas layers use CanvasClients (but ImageHosts). We have a different subclass
67
 * where we have a different way of interfacing with the textures - in terms of
68
 * drawing into the compositable and/or passing its contents to the compostior.
69
 */
70
class CompositableClient
71
{
72
protected:
73
  virtual ~CompositableClient();
74
75
public:
76
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositableClient)
77
78
  explicit CompositableClient(CompositableForwarder* aForwarder, TextureFlags aFlags = TextureFlags::NO_FLAGS);
79
80
  virtual void Dump(std::stringstream& aStream,
81
                    const char* aPrefix="",
82
                    bool aDumpHtml=false,
83
0
                    TextureDumpMode aCompress=TextureDumpMode::Compress) {};
84
85
  virtual TextureInfo GetTextureInfo() const = 0;
86
87
  LayersBackend GetCompositorBackendType() const;
88
89
  already_AddRefed<TextureClient>
90
  CreateBufferTextureClient(gfx::SurfaceFormat aFormat,
91
                            gfx::IntSize aSize,
92
                            gfx::BackendType aMoz2dBackend = gfx::BackendType::NONE,
93
                            TextureFlags aFlags = TextureFlags::DEFAULT);
94
95
  already_AddRefed<TextureClient>
96
  CreateTextureClientForDrawing(gfx::SurfaceFormat aFormat,
97
                                gfx::IntSize aSize,
98
                                BackendSelector aSelector,
99
                                TextureFlags aTextureFlags,
100
                                TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT);
101
102
  already_AddRefed<TextureClient>
103
  CreateTextureClientFromSurface(gfx::SourceSurface* aSurface,
104
                                 BackendSelector aSelector,
105
                                 TextureFlags aTextureFlags,
106
                                 TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT);
107
108
  /**
109
   * Establishes the connection with compositor side through IPDL
110
   */
111
  virtual bool Connect(ImageContainer* aImageContainer = nullptr);
112
113
  void Destroy();
114
115
  bool IsConnected() const;
116
117
  CompositableForwarder* GetForwarder() const
118
0
  {
119
0
    return mForwarder;
120
0
  }
121
122
  /**
123
   * This identifier is what lets us attach async compositables with a shadow
124
   * layer. It is not used if the compositable is used with the regular shadow
125
   * layer forwarder.
126
   *
127
   * If this returns empty, it means the compositable is not async (it is used
128
   * on the main thread).
129
   */
130
  CompositableHandle GetAsyncHandle() const;
131
132
  /**
133
   * Handle for IPDL communication.
134
   */
135
0
  CompositableHandle GetIPCHandle() const {
136
0
    return mHandle;
137
0
  }
138
139
  /**
140
   * Tells the Compositor to create a TextureHost for this TextureClient.
141
   */
142
  virtual bool AddTextureClient(TextureClient* aClient);
143
144
  /**
145
   * A hook for the when the Compositable is detached from it's layer.
146
   */
147
0
  virtual void OnDetach() {}
148
149
  /**
150
   * Clear any resources that are not immediately necessary. This may be called
151
   * in low-memory conditions.
152
   */
153
  virtual void ClearCachedResources();
154
155
  /**
156
   * Shrink memory usage.
157
   * Called when "memory-pressure" is observed.
158
   */
159
  virtual void HandleMemoryPressure();
160
161
  /**
162
   * Should be called when deataching a TextureClient from a Compositable, because
163
   * some platforms need to do some extra book keeping when this happens.
164
   *
165
   * See AutoRemoveTexture to automatically invoke this at the end of a scope.
166
   */
167
  virtual void RemoveTexture(TextureClient* aTexture);
168
169
  void InitIPDL(const CompositableHandle& aHandle);
170
171
0
  TextureFlags GetTextureFlags() const { return mTextureFlags; }
172
173
  TextureClientRecycleAllocator* GetTextureClientRecycler();
174
175
0
  bool HasTextureClientRecycler() { return !!mTextureClientRecycler; }
176
177
  static void DumpTextureClient(std::stringstream& aStream,
178
                                TextureClient* aTexture,
179
                                TextureDumpMode aCompress);
180
protected:
181
  RefPtr<CompositableForwarder> mForwarder;
182
  // Some layers may want to enforce some flags to all their textures
183
  // (like disallowing tiling)
184
  TextureFlags mTextureFlags;
185
  RefPtr<TextureClientRecycleAllocator> mTextureClientRecycler;
186
187
  CompositableHandle mHandle;
188
  bool mIsAsync;
189
190
  friend class CompositableChild;
191
};
192
193
/**
194
 * Helper to call RemoveTexture at the end of a scope.
195
 */
196
struct AutoRemoveTexture
197
{
198
  explicit AutoRemoveTexture(CompositableClient* aCompositable,
199
                             TextureClient* aTexture = nullptr)
200
    : mTexture(aTexture)
201
    , mCompositable(aCompositable)
202
0
  {}
203
204
  ~AutoRemoveTexture();
205
206
  RefPtr<TextureClient> mTexture;
207
private:
208
  CompositableClient* mCompositable;
209
};
210
211
} // namespace layers
212
} // namespace mozilla
213
214
#endif