Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/layers/ProfilerScreenshots.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_layers_ProfilerScreenshots_h
8
#define mozilla_layers_ProfilerScreenshots_h
9
10
#include <functional>
11
12
#include "mozilla/Mutex.h"
13
#include "mozilla/RefPtr.h"
14
15
#include "mozilla/gfx/Point.h"
16
17
class nsIThread;
18
19
namespace mozilla {
20
21
namespace gfx {
22
class DataSourceSurface;
23
}
24
25
namespace layers {
26
27
/**
28
 * Can be used to submit screenshots from the compositor to the profiler.
29
 * Screenshots have a fixed bounding size. The user of this class will usually
30
 * scale down the window contents first, ideally on the GPU, then read back the
31
 * small scaled down image into main memory, and then call SubmitScreenshot to
32
 * pass the data to the profiler.
33
 * This class encodes each screenshot to a JPEG data URL, on a separate thread.
34
 * This class manages that thread and recycles memory buffers.
35
 */
36
class ProfilerScreenshots final
37
{
38
public:
39
  ProfilerScreenshots();
40
  ~ProfilerScreenshots();
41
42
  /**
43
   * Returns whether the profiler is currently active and is running with the
44
   * "screenshots" feature enabled.
45
   */
46
  static bool IsEnabled();
47
48
  /**
49
   * Returns a fixed size that all screenshots should be resized to fit into.
50
   */
51
0
  static gfx::IntSize ScreenshotSize() { return gfx::IntSize(350, 350); }
52
53
  /**
54
   * The main functionality provided by this class.
55
   * This method will synchronously invoke the supplied aPopulateSurface
56
   * callback function with a DataSourceSurface in which the callback should
57
   * store the pixel data. This surface may be larger than aScaledSize, but
58
   * only the data in the rectangle (0, 0, aScaledSize.width, aScaledSize.height)
59
   * will be read.
60
   * @param aWindowIdentifier A pointer-sized integer that can be used to match
61
   *   up multiple screenshots from the same window.
62
   * @param aOriginalSize The unscaled size of the snapshotted window.
63
   * @param aScaledSize The scaled size, aScaled <= ScreenshotSize(), which the
64
   *   snapshot has been resized to.
65
   * @param aTimeStamp The time at which the snapshot was taken. In
66
   *   asynchronous readback implementations, this is the time at which the
67
   *   readback / copy command was put into the command stream to the GPU, not
68
   *   the time at which the readback data was mapped into main memory.
69
   * @param aPopulateSurface A callback that the caller needs to implement,
70
   *   which needs to copy the screenshot pixel data into the surface that's
71
   *   supplied to the callback. Called zero or one times, synchronously.
72
   */
73
  void SubmitScreenshot(uintptr_t aWindowIdentifier, const gfx::IntSize& aOriginalSize,
74
                        const gfx::IntSize& aScaledSize, const TimeStamp& aTimeStamp,
75
                        const std::function<bool(gfx::DataSourceSurface*)>& aPopulateSurface);
76
77
private:
78
  /**
79
   * Recycle a surface from mAvailableSurfaces or create a new one if all
80
   * surfaces are currently in use, up to some maximum limit.
81
   * Returns null if the limit is reached.
82
   * Can be called on any thread.
83
   */
84
  already_AddRefed<DataSourceSurface> TakeNextSurface();
85
86
  /**
87
   * Return aSurface back into the mAvailableSurfaces pool. Can be called on
88
   * any thread.
89
   */
90
  void ReturnSurface(DataSourceSurface* aSurface);
91
92
  // The thread on which encoding happens.
93
  nsCOMPtr<nsIThread> mThread;
94
  // An array of surfaces ready to be recycled. Can be accessed from multiple
95
  // threads, protected by mMutex.
96
  nsTArray<RefPtr<DataSourceSurface>> mAvailableSurfaces;
97
  // Protects mAvailableSurfaces.
98
  Mutex mMutex;
99
  // The total number of surfaces created. If encoding is fast enough to happen
100
  // entirely in the time between two calls to SubmitScreenshot, this should
101
  // never exceed 1.
102
  uint32_t mLiveSurfaceCount;
103
};
104
105
} // namespace layers
106
} // namespace mozilla
107
108
#endif // mozilla_layers_ProfilerScreenshots_h