ScreenshotGrabber.h (4338B)
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_ScreenshotGrabber_h 8 #define mozilla_layers_ScreenshotGrabber_h 9 10 #include "nsISupportsImpl.h" 11 #include "mozilla/UniquePtr.h" 12 #include "mozilla/gfx/2D.h" 13 #include "mozilla/gfx/Point.h" 14 #include "mozilla/gfx/Rect.h" 15 16 namespace mozilla { 17 namespace layers { 18 19 namespace profiler_screenshots { 20 class Window; 21 class RenderSource; 22 class DownscaleTarget; 23 class AsyncReadbackBuffer; 24 25 class ScreenshotGrabberImpl; 26 } // namespace profiler_screenshots 27 28 /** 29 * Used by various renderers / layer managers to grab snapshots from the window 30 * and submit them to the Gecko profiler. 31 * Doesn't do any work if the profiler is not running or the "screenshots" 32 * feature is not enabled. 33 * Screenshots are scaled down to fit within a fixed size, and read back to 34 * main memory using async readback. Scaling is done in multiple scale-by-0.5x 35 * steps using DownscaleTarget::CopyFrom, and readback is done using 36 * AsyncReadbackBuffers. 37 */ 38 class ScreenshotGrabber final { 39 public: 40 ScreenshotGrabber(); 41 ~ScreenshotGrabber(); 42 43 // Scale the contents of aWindow's current render target into an 44 // appropriately sized DownscaleTarget and read its contents into an 45 // AsyncReadbackBuffer. The AsyncReadbackBuffer is not mapped into main 46 // memory until the second call to MaybeProcessQueue() after this call to 47 // MaybeGrabScreenshot(). 48 void MaybeGrabScreenshot(profiler_screenshots::Window& aWindow, 49 const gfx::IntSize& aWindowSize); 50 51 // Map the contents of any outstanding AsyncReadbackBuffers from previous 52 // composites into main memory and submit each screenshot to the profiler. 53 void MaybeProcessQueue(); 54 55 // Insert a special profiler marker for a composite that didn't do any actual 56 // compositing, so that the profiler knows why no screenshot was taken for 57 // this frame. 58 void NotifyEmptyFrame(); 59 60 // Destroy all Window-related resources that this class is holding on to. 61 void Destroy(); 62 63 private: 64 // non-null while ProfilerScreenshots::IsEnabled() returns true 65 UniquePtr<profiler_screenshots::ScreenshotGrabberImpl> mImpl; 66 }; 67 68 // Interface definitions. 69 70 namespace profiler_screenshots { 71 72 class Window { 73 public: 74 virtual already_AddRefed<RenderSource> GetWindowContents( 75 const gfx::IntSize& aWindowSize) = 0; 76 virtual already_AddRefed<DownscaleTarget> CreateDownscaleTarget( 77 const gfx::IntSize& aSize) = 0; 78 virtual already_AddRefed<AsyncReadbackBuffer> CreateAsyncReadbackBuffer( 79 const gfx::IntSize& aSize) = 0; 80 81 protected: 82 virtual ~Window() {} 83 }; 84 85 class RenderSource { 86 public: 87 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RenderSource) 88 89 const auto& Size() const { return mSize; } 90 91 protected: 92 explicit RenderSource(const gfx::IntSize& aSize) : mSize(aSize) {} 93 virtual ~RenderSource() {} 94 95 const gfx::IntSize mSize; 96 }; 97 98 class DownscaleTarget { 99 public: 100 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DownscaleTarget) 101 102 virtual already_AddRefed<RenderSource> AsRenderSource() = 0; 103 104 const auto& Size() const { return mSize; } 105 virtual bool DownscaleFrom(RenderSource* aSource, 106 const gfx::IntRect& aSourceRect, 107 const gfx::IntRect& aDestRect) = 0; 108 109 protected: 110 explicit DownscaleTarget(const gfx::IntSize& aSize) : mSize(aSize) {} 111 virtual ~DownscaleTarget() {} 112 113 const gfx::IntSize mSize; 114 }; 115 116 class AsyncReadbackBuffer { 117 public: 118 NS_INLINE_DECL_THREADSAFE_REFCOUNTING( 119 mozilla::layers::profiler_screenshots::AsyncReadbackBuffer) 120 121 const auto& Size() const { return mSize; } 122 virtual void CopyFrom(RenderSource* aSource) = 0; 123 virtual bool MapAndCopyInto(gfx::DataSourceSurface* aSurface, 124 const gfx::IntSize& aReadSize) = 0; 125 126 protected: 127 explicit AsyncReadbackBuffer(const gfx::IntSize& aSize) : mSize(aSize) {} 128 virtual ~AsyncReadbackBuffer() {} 129 130 const gfx::IntSize mSize; 131 }; 132 133 } // namespace profiler_screenshots 134 135 } // namespace layers 136 } // namespace mozilla 137 138 #endif // mozilla_layers_ScreenshotGrabber_h