Source code

Revision control

Other Tools

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