summaryrefslogtreecommitdiffstats
path: root/gfx/layers/NativeLayerWayland.h
blob: 0c45adc3119d485d51e649dd45548c93a31b6a64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_layers_NativeLayerWayland_h
#define mozilla_layers_NativeLayerWayland_h

#include <deque>
#include <unordered_map>

#include "mozilla/Mutex.h"
#include "mozilla/layers/NativeLayer.h"
#include "mozilla/layers/SurfacePoolWayland.h"
#include "mozilla/widget/MozContainerWayland.h"
#include "nsRegion.h"
#include "nsTArray.h"

namespace mozilla::layers {

typedef void (*CallbackFunc)(void* aData, uint32_t aTime);

class CallbackMultiplexHelper final {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CallbackMultiplexHelper);

  explicit CallbackMultiplexHelper(CallbackFunc aCallbackFunc,
                                   void* aCallbackData);

  void Callback(uint32_t aTime);
  bool IsActive() { return mActive; }

 private:
  ~CallbackMultiplexHelper() = default;

  void RunCallback(uint32_t aTime);

  bool mActive = true;
  CallbackFunc mCallbackFunc = nullptr;
  void* mCallbackData = nullptr;
};

class NativeLayerRootWayland final : public NativeLayerRoot {
 public:
  static already_AddRefed<NativeLayerRootWayland> CreateForMozContainer(
      MozContainer* aContainer);

  virtual NativeLayerRootWayland* AsNativeLayerRootWayland() override {
    return this;
  }

  // Overridden methods
  already_AddRefed<NativeLayer> CreateLayer(
      const gfx::IntSize& aSize, bool aIsOpaque,
      SurfacePoolHandle* aSurfacePoolHandle) override;
  already_AddRefed<NativeLayer> CreateLayerForExternalTexture(
      bool aIsOpaque) override;

  void AppendLayer(NativeLayer* aLayer) override;
  void RemoveLayer(NativeLayer* aLayer) override;
  void SetLayers(const nsTArray<RefPtr<NativeLayer>>& aLayers) override;

  void PrepareForCommit() override { mFrameInProcess = true; };
  bool CommitToScreen() override;

  void UpdateLayersOnMainThread();
  void AfterFrameClockAfterPaint();
  void RequestFrameCallback(CallbackFunc aCallbackFunc, void* aCallbackData);

 private:
  explicit NativeLayerRootWayland(MozContainer* aContainer);
  ~NativeLayerRootWayland();

  bool CommitToScreen(const MutexAutoLock& aProofOfLock);

  Mutex mMutex MOZ_UNANNOTATED;

  MozContainer* mContainer = nullptr;
  wl_surface* mWlSurface = nullptr;
  RefPtr<widget::WaylandBufferSHM> mShmBuffer;

  nsTArray<RefPtr<NativeLayerWayland>> mSublayers;
  nsTArray<RefPtr<NativeLayerWayland>> mOldSublayers;
  nsTArray<RefPtr<NativeLayerWayland>> mSublayersOnMainThread;
  bool mNewLayers = false;

  bool mFrameInProcess = false;
  bool mCallbackRequested = false;

  gulong mGdkAfterPaintId = 0;
  RefPtr<CallbackMultiplexHelper> mCallbackMultiplexHelper;
};

class NativeLayerWayland final : public NativeLayer {
 public:
  virtual NativeLayerWayland* AsNativeLayerWayland() override { return this; }

  // Overridden methods
  gfx::IntSize GetSize() override;
  void SetPosition(const gfx::IntPoint& aPosition) override;
  gfx::IntPoint GetPosition() override;
  void SetTransform(const gfx::Matrix4x4& aTransform) override;
  gfx::Matrix4x4 GetTransform() override;
  gfx::IntRect GetRect() override;
  void SetSamplingFilter(gfx::SamplingFilter aSamplingFilter) override;
  RefPtr<gfx::DrawTarget> NextSurfaceAsDrawTarget(
      const gfx::IntRect& aDisplayRect, const gfx::IntRegion& aUpdateRegion,
      gfx::BackendType aBackendType) override;
  Maybe<GLuint> NextSurfaceAsFramebuffer(const gfx::IntRect& aDisplayRect,
                                         const gfx::IntRegion& aUpdateRegion,
                                         bool aNeedsDepth) override;
  void NotifySurfaceReady() override;
  void DiscardBackbuffers() override;
  bool IsOpaque() override;
  void SetClipRect(const Maybe<gfx::IntRect>& aClipRect) override;
  Maybe<gfx::IntRect> ClipRect() override;
  gfx::IntRect CurrentSurfaceDisplayRect() override;
  void SetSurfaceIsFlipped(bool aIsFlipped) override;
  bool SurfaceIsFlipped() override;

  void AttachExternalImage(wr::RenderTextureHost* aExternalImage) override;

  void Commit();
  void Unmap();
  void EnsureParentSurface(wl_surface* aParentSurface);
  const RefPtr<SurfacePoolHandleWayland> GetSurfacePoolHandle() {
    return mSurfacePoolHandle;
  };
  void SetBufferTransformFlipped(bool aFlippedX, bool aFlippedY);
  void SetSubsurfacePosition(int aX, int aY);
  void SetViewportSourceRect(const gfx::Rect aSourceRect);
  void SetViewportDestinationSize(int aWidth, int aHeight);

  void RequestFrameCallback(
      const RefPtr<CallbackMultiplexHelper>& aMultiplexHelper);
  static void FrameCallbackHandler(void* aData, wl_callback* aCallback,
                                   uint32_t aTime);

 private:
  friend class NativeLayerRootWayland;

  NativeLayerWayland(const gfx::IntSize& aSize, bool aIsOpaque,
                     SurfacePoolHandleWayland* aSurfacePoolHandle);
  explicit NativeLayerWayland(bool aIsOpaque);
  ~NativeLayerWayland() override;

  void HandlePartialUpdate(const MutexAutoLock& aProofOfLock);
  void FrameCallbackHandler(wl_callback* aCallback, uint32_t aTime);

  Mutex mMutex MOZ_UNANNOTATED;

  const RefPtr<SurfacePoolHandleWayland> mSurfacePoolHandle;
  const gfx::IntSize mSize;
  const bool mIsOpaque = false;
  gfx::IntPoint mPosition;
  gfx::Matrix4x4 mTransform;
  gfx::IntRect mDisplayRect;
  gfx::IntRegion mDirtyRegion;
  Maybe<gfx::IntRect> mClipRect;
  gfx::SamplingFilter mSamplingFilter = gfx::SamplingFilter::POINT;
  bool mSurfaceIsFlipped = false;
  bool mHasBufferAttached = false;

  wl_surface* mWlSurface = nullptr;
  wl_surface* mParentWlSurface = nullptr;
  wl_subsurface* mWlSubsurface = nullptr;
  wl_callback* mCallback = nullptr;
  wp_viewport* mViewport = nullptr;
  bool mBufferTransformFlippedX = false;
  bool mBufferTransformFlippedY = false;
  gfx::IntPoint mSubsurfacePosition = gfx::IntPoint(0, 0);
  gfx::Rect mViewportSourceRect = gfx::Rect(-1, -1, -1, -1);
  gfx::IntSize mViewportDestinationSize = gfx::IntSize(-1, -1);
  nsTArray<RefPtr<CallbackMultiplexHelper>> mCallbackMultiplexHelpers;

  RefPtr<widget::WaylandBuffer> mInProgressBuffer;
  RefPtr<widget::WaylandBuffer> mFrontBuffer;
};

}  // namespace mozilla::layers

#endif  // mozilla_layers_NativeLayerWayland_h