1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef mozilla_layers_RendererScreenshotGrabber_h
6 #define mozilla_layers_RendererScreenshotGrabber_h
7 
8 #include "mozilla/TimeStamp.h"
9 #include "mozilla/UniquePtr.h"
10 #include "mozilla/gfx/Point.h"
11 #include "mozilla/layers/ProfilerScreenshots.h"
12 #include "mozilla/webrender/webrender_ffi.h"
13 #include "nsTArray.h"
14 
15 namespace mozilla {
16 namespace wr {
17 
18 struct Renderer;
19 class RendererOGL;
20 
21 /**
22  * Used by |RendererOGL| to grab screenshots from WebRender and submit them to
23  * the Gecko profiler.
24  *
25  * If the profiler is not running or the screenshots feature is disabled, no
26  * work will be done.
27  */
28 class RendererScreenshotGrabber final {
29  public:
30   RendererScreenshotGrabber();
31 
32   /**
33    * Grab a screenshot from WebRender if we are profiling and screenshots are
34    * enabled.
35    *
36    * The captured screenshot will not be mapped until the second call to
37    * |MaybeProcessQueue| after this call to |MaybeGrabScreenshot|.
38    */
39   void MaybeGrabScreenshot(RendererOGL* aRendererOGL,
40                            const gfx::IntSize& aWindowSize);
41 
42   /**
43    * Process the screenshots pending in the queue if we are profiling and
44    * screenshots are enabled.
45    */
46   void MaybeProcessQueue(RendererOGL* aRenderer);
47 
48  private:
49   /**
50    * Drop all our allocated memory when we are no longer profiling.
51    *
52    * This will also instruct WebRender to drop all its Gecko profiler
53    * associated memory.
54    */
55   void Destroy(Renderer* aRenderer);
56 
57   /**
58    * Actually grab a screenshot from WebRender.
59    */
60   void GrabScreenshot(Renderer* aRenderer, const gfx::IntSize& aWindowSize);
61 
62   /**
63    * Process the screenshots pending in the queue.
64    */
65   void ProcessQueue(Renderer* aRenderer);
66 
67   struct QueueItem {
68     mozilla::TimeStamp mTimeStamp;
69     AsyncScreenshotHandle mHandle;
70     gfx::IntSize mScreenshotSize;
71     gfx::IntSize mWindowSize;
72     uintptr_t mWindowIdentifier;
73   };
74 
75   /**
76    * The maximum size for screenshots, as dictated by
77    * |ProfilerScrenshots::ScreenshotSize|.
78    */
79   gfx::IntSize mMaxScreenshotSize;
80 
81   /**
82    * The queue of screenshots waiting to be processed and submitted.
83    */
84   nsTArray<QueueItem> mQueue;
85 
86   /**
87    * The queue item for the current frame. This will be inserted into the queue
88    * after a call to |MaybeProcessQueue| so it will be not be processed until
89    * the next frame.
90    */
91   Maybe<QueueItem> mCurrentFrameQueueItem;
92 
93   /**
94    * Our handle to the profiler screenshots object.
95    */
96   RefPtr<mozilla::layers::ProfilerScreenshots> mProfilerScreenshots;
97 };
98 
99 }  // namespace wr
100 }  // namespace mozilla
101 
102 #endif  // mozilla_layers_RendererScreenshotGrabber_h
103