summaryrefslogtreecommitdiffstats
path: root/gfx/layers/mlgpu/MLGPUScreenshotGrabber.cpp
blob: 01ca9dbf9c8363f9635467f0c947bc318f6e25bc (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "MLGPUScreenshotGrabber.h"

#include "mozilla/RefPtr.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/UniquePtr.h"

#include "mozilla/layers/ProfilerScreenshots.h"
#include "mozilla/gfx/Point.h"
#include "mozilla/gfx/Swizzle.h"
#include "mozilla/ProfilerMarkers.h"
#include "SharedBufferMLGPU.h"
#include "ShaderDefinitionsMLGPU.h"
#include "nsTArray.h"

namespace mozilla {

using namespace gfx;

namespace layers {

using namespace mlg;

/**
 * The actual implementation of screenshot grabbing.
 * The MLGPUScreenshotGrabberImpl object is destroyed if the profiler is
 * disabled and MaybeGrabScreenshot notices it.
 */
class MLGPUScreenshotGrabberImpl final {
 public:
  explicit MLGPUScreenshotGrabberImpl(const IntSize& aReadbackTextureSize);
  ~MLGPUScreenshotGrabberImpl();

  void GrabScreenshot(MLGDevice* aDevice, MLGTexture* aTexture);
  void ProcessQueue();

 private:
  struct QueueItem final {
    mozilla::TimeStamp mTimeStamp;
    RefPtr<MLGTexture> mScreenshotReadbackTexture;
    gfx::IntSize mScreenshotSize;
    gfx::IntSize mWindowSize;
    RefPtr<MLGDevice> mDevice;
    uintptr_t mWindowIdentifier;
  };

  RefPtr<MLGTexture> ScaleDownWindowTargetToSize(MLGDevice* aCompositor,
                                                 const gfx::IntSize& aDestSize,
                                                 MLGTexture* aWindowTarget,
                                                 size_t aLevel);

  struct CachedLevel {
    RefPtr<MLGRenderTarget> mRenderTarget;
    RefPtr<MLGBuffer> mVertexBuffer;
    RefPtr<MLGBuffer> mWorldConstants;
  };
  bool BlitTexture(MLGDevice* aDevice, CachedLevel& aDest, MLGTexture* aSource,
                   const IntSize& aSourceSize, const IntSize& aDestSize);

  already_AddRefed<MLGTexture> TakeNextReadbackTexture(MLGDevice* aCompositor);
  void ReturnReadbackTexture(MLGTexture* aReadbackTexture);

  nsTArray<CachedLevel> mCachedLevels;
  nsTArray<RefPtr<MLGTexture>> mAvailableReadbackTextures;
  Maybe<QueueItem> mCurrentFrameQueueItem;
  nsTArray<QueueItem> mQueue;
  RefPtr<ProfilerScreenshots> mProfilerScreenshots;
  const IntSize mReadbackTextureSize;
};

MLGPUScreenshotGrabber::MLGPUScreenshotGrabber() = default;

MLGPUScreenshotGrabber::~MLGPUScreenshotGrabber() = default;

void MLGPUScreenshotGrabber::MaybeGrabScreenshot(MLGDevice* aDevice,
                                                 MLGTexture* aTexture) {
  if (ProfilerScreenshots::IsEnabled()) {
    if (!mImpl) {
      mImpl = MakeUnique<MLGPUScreenshotGrabberImpl>(
          ProfilerScreenshots::ScreenshotSize());
    }
    mImpl->GrabScreenshot(aDevice, aTexture);
  } else if (mImpl) {
    Destroy();
  }
}

void MLGPUScreenshotGrabber::MaybeProcessQueue() {
  if (ProfilerScreenshots::IsEnabled()) {
    if (!mImpl) {
      mImpl = MakeUnique<MLGPUScreenshotGrabberImpl>(
          ProfilerScreenshots::ScreenshotSize());
    }
    mImpl->ProcessQueue();
  } else if (mImpl) {
    Destroy();
  }
}

void MLGPUScreenshotGrabber::NotifyEmptyFrame() {
#ifdef MOZ_GECKO_PROFILER
  PROFILER_MARKER_UNTYPED("NoCompositorScreenshot because nothing changed",
                          GRAPHICS);
#endif
}

void MLGPUScreenshotGrabber::Destroy() { mImpl = nullptr; }

MLGPUScreenshotGrabberImpl::MLGPUScreenshotGrabberImpl(
    const IntSize& aReadbackTextureSize)
    : mReadbackTextureSize(aReadbackTextureSize) {}

MLGPUScreenshotGrabberImpl::~MLGPUScreenshotGrabberImpl() {
  // Any queue items in mQueue or mCurrentFrameQueueItem will be lost.
  // That's ok: Either the profiler has stopped and we don't care about these
  // screenshots, or the window is closing and we don't really need the last
  // few frames from the window.
}

// Scale down aWindowTexture into a MLGTexture of size
// mReadbackTextureSize * (1 << aLevel) and return that MLGTexture.
// Don't scale down by more than a factor of 2 with a single scaling operation,
// because it'll look bad. If higher scales are needed, use another
// intermediate target by calling this function recursively with aLevel + 1.
RefPtr<MLGTexture> MLGPUScreenshotGrabberImpl::ScaleDownWindowTargetToSize(
    MLGDevice* aDevice, const IntSize& aDestSize, MLGTexture* aWindowTexture,
    size_t aLevel) {
  aDevice->SetScissorRect(Nothing());
  aDevice->SetDepthTestMode(MLGDepthTestMode::Disabled);
  aDevice->SetTopology(MLGPrimitiveTopology::UnitQuad);
  // DiagnosticText happens to be the simplest shader we have to draw a quad.
  aDevice->SetVertexShader(VertexShaderID::DiagnosticText);
  aDevice->SetPixelShader(PixelShaderID::DiagnosticText);
  aDevice->SetBlendState(MLGBlendState::Copy);
  aDevice->SetSamplerMode(0, SamplerMode::LinearClamp);

  if (aLevel == mCachedLevels.Length()) {
    RefPtr<MLGRenderTarget> rt =
        aDevice->CreateRenderTarget(mReadbackTextureSize * (1 << aLevel));
    mCachedLevels.AppendElement(CachedLevel{rt, nullptr, nullptr});
  }
  MOZ_RELEASE_ASSERT(aLevel < mCachedLevels.Length());

  RefPtr<MLGTexture> sourceTarget = aWindowTexture;
  IntSize sourceSize = aWindowTexture->GetSize();
  if (aWindowTexture->GetSize().width > aDestSize.width * 2) {
    sourceSize = aDestSize * 2;
    sourceTarget = ScaleDownWindowTargetToSize(aDevice, sourceSize,
                                               aWindowTexture, aLevel + 1);
  }

  if (sourceTarget) {
    if (BlitTexture(aDevice, mCachedLevels[aLevel], sourceTarget, sourceSize,
                    aDestSize)) {
      return mCachedLevels[aLevel].mRenderTarget->GetTexture();
    }
  }
  return nullptr;
}

bool MLGPUScreenshotGrabberImpl::BlitTexture(MLGDevice* aDevice,
                                             CachedLevel& aLevel,
                                             MLGTexture* aSource,
                                             const IntSize& aSourceSize,
                                             const IntSize& aDestSize) {
  MOZ_ASSERT(aLevel.mRenderTarget);
  MLGRenderTarget* rt = aLevel.mRenderTarget;
  MOZ_ASSERT(aDestSize <= rt->GetSize());

  struct TextureRect {
    Rect bounds;
    Rect texCoords;
  };

  if (!aLevel.mVertexBuffer) {
    TextureRect rect;
    rect.bounds = Rect(Point(), Size(aDestSize));
    rect.texCoords =
        Rect(0.0, 0.0, Float(aSourceSize.width) / aSource->GetSize().width,
             Float(aSourceSize.height) / aSource->GetSize().height);

    VertexStagingBuffer instances;
    if (!instances.AppendItem(rect)) {
      return false;
    }

    RefPtr<MLGBuffer> vertices = aDevice->CreateBuffer(
        MLGBufferType::Vertex, instances.NumItems() * instances.SizeOfItem(),
        MLGUsage::Immutable, instances.GetBufferStart());
    if (!vertices) {
      return false;
    }

    aLevel.mVertexBuffer = vertices;
  }

  if (!aLevel.mWorldConstants) {
    WorldConstants vsConstants;
    Matrix4x4 projection = Matrix4x4::Translation(-1.0, 1.0, 0.0);
    projection.PreScale(2.0 / float(rt->GetSize().width),
                        2.0 / float(rt->GetSize().height), 1.0f);
    projection.PreScale(1.0f, -1.0f, 1.0f);

    memcpy(vsConstants.projection, &projection._11, 64);
    vsConstants.targetOffset = Point();
    vsConstants.sortIndexOffset = 0;
    vsConstants.debugFrameNumber = 0;

    aLevel.mWorldConstants =
        aDevice->CreateBuffer(MLGBufferType::Constant, sizeof(vsConstants),
                              MLGUsage::Immutable, &vsConstants);

    if (!aLevel.mWorldConstants) {
      return false;
    }
  }

  aDevice->SetRenderTarget(rt);
  aDevice->SetPSTexture(0, aSource);
  aDevice->SetViewport(IntRect(IntPoint(0, 0), rt->GetSize()));
  aDevice->SetVertexBuffer(1, aLevel.mVertexBuffer, sizeof(TextureRect));
  aDevice->SetVSConstantBuffer(kWorldConstantBufferSlot,
                               aLevel.mWorldConstants);
  aDevice->DrawInstanced(4, 1, 0, 0);
  return true;
}

void MLGPUScreenshotGrabberImpl::GrabScreenshot(MLGDevice* aDevice,
                                                MLGTexture* aTexture) {
  Size windowSize(aTexture->GetSize());
  float scale = std::min(mReadbackTextureSize.width / windowSize.width,
                         mReadbackTextureSize.height / windowSize.height);
  IntSize scaledSize = IntSize::Round(windowSize * scale);

  // The initial target is non-GPU readable. This copy could probably be
  // avoided if we had created the swap chain differently. However we
  // don't know if that may inadvertently affect performance in the
  // non-profiling case.
  RefPtr<MLGTexture> windowTexture = aDevice->CreateTexture(
      aTexture->GetSize(), SurfaceFormat::B8G8R8A8, MLGUsage::Default,
      MLGTextureFlags::ShaderResource);
  aDevice->CopyTexture(windowTexture, IntPoint(), aTexture,
                       IntRect(IntPoint(), aTexture->GetSize()));

  RefPtr<MLGTexture> scaledTarget =
      ScaleDownWindowTargetToSize(aDevice, scaledSize, windowTexture, 0);

  if (!scaledTarget) {
    PROFILER_MARKER_UNTYPED(
        "NoCompositorScreenshot because ScaleDownWindowTargetToSize failed",
        GRAPHICS);
    return;
  }

  RefPtr<MLGTexture> readbackTexture = TakeNextReadbackTexture(aDevice);
  if (!readbackTexture) {
    PROFILER_MARKER_UNTYPED(
        "NoCompositorScreenshot because AsyncReadbackReadbackTexture creation "
        "failed",
        GRAPHICS);
    return;
  }

  aDevice->CopyTexture(readbackTexture, IntPoint(), scaledTarget,
                       IntRect(IntPoint(), mReadbackTextureSize));

  // This QueueItem will be added to the queue at the end of the next call to
  // ProcessQueue(). This ensures that the ReadbackTexture isn't mapped into
  // main memory until the next frame. If we did it in this frame, we'd block on
  // the GPU.
  mCurrentFrameQueueItem =
      Some(QueueItem{TimeStamp::Now(), std::move(readbackTexture), scaledSize,
                     aTexture->GetSize(), aDevice,
                     reinterpret_cast<uintptr_t>(static_cast<void*>(this))});
}

already_AddRefed<MLGTexture>
MLGPUScreenshotGrabberImpl::TakeNextReadbackTexture(MLGDevice* aDevice) {
  if (!mAvailableReadbackTextures.IsEmpty()) {
    RefPtr<MLGTexture> readbackTexture = mAvailableReadbackTextures[0];
    mAvailableReadbackTextures.RemoveElementAt(0);
    return readbackTexture.forget();
  }
  return aDevice
      ->CreateTexture(mReadbackTextureSize, SurfaceFormat::B8G8R8A8,
                      MLGUsage::Staging, MLGTextureFlags::None)
      .forget();
}

void MLGPUScreenshotGrabberImpl::ReturnReadbackTexture(
    MLGTexture* aReadbackTexture) {
  mAvailableReadbackTextures.AppendElement(aReadbackTexture);
}

void MLGPUScreenshotGrabberImpl::ProcessQueue() {
  if (!mQueue.IsEmpty()) {
    if (!mProfilerScreenshots) {
      mProfilerScreenshots = new ProfilerScreenshots();
    }
    for (const auto& item : mQueue) {
      mProfilerScreenshots->SubmitScreenshot(
          item.mWindowIdentifier, item.mWindowSize, item.mScreenshotSize,
          item.mTimeStamp, [&item](DataSourceSurface* aTargetSurface) {
            MLGMappedResource map;
            if (!item.mDevice->Map(item.mScreenshotReadbackTexture,
                                   MLGMapType::READ, &map)) {
              return false;
            }
            DataSourceSurface::ScopedMap destMap(aTargetSurface,
                                                 DataSourceSurface::WRITE);
            bool result =
                SwizzleData(map.mData, map.mStride, SurfaceFormat::B8G8R8A8,
                            destMap.GetData(), destMap.GetStride(),
                            aTargetSurface->GetFormat(), item.mScreenshotSize);

            item.mDevice->Unmap(item.mScreenshotReadbackTexture);
            return result;
          });
      ReturnReadbackTexture(item.mScreenshotReadbackTexture);
    }
  }
  mQueue.Clear();

  if (mCurrentFrameQueueItem) {
    mQueue.AppendElement(std::move(*mCurrentFrameQueueItem));
    mCurrentFrameQueueItem = Nothing();
  }
}

}  // namespace layers
}  // namespace mozilla