tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

GLScreenBuffer.h (2560B)


      1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /* GLScreenBuffer is the abstraction for the "default framebuffer" used
      7 * by an offscreen GLContext. Since it's only for offscreen GLContext's,
      8 * it's only useful for things like WebGL, and is NOT used by the
      9 * compositor's GLContext. Remember that GLContext provides an abstraction
     10 * so that even if you want to draw to the 'screen', even if that's not
     11 * actually the screen, just draw to 0. This GLScreenBuffer class takes the
     12 * logic handling out of GLContext.
     13 */
     14 
     15 #ifndef SCREEN_BUFFER_H_
     16 #define SCREEN_BUFFER_H_
     17 
     18 #include "GLTypes.h"
     19 #include "mozilla/gfx/Point.h"
     20 #include "mozilla/UniquePtr.h"
     21 
     22 #include <functional>
     23 #include <queue>
     24 #include <memory>
     25 
     26 namespace mozilla {
     27 namespace gl {
     28 
     29 class SharedSurface;
     30 class SurfaceFactory;
     31 class SwapChain;
     32 
     33 class SwapChainPresenter final {
     34  friend class SwapChain;
     35 
     36  SwapChain* mSwapChain;
     37  std::shared_ptr<SharedSurface> mBackBuffer;
     38 
     39 public:
     40  explicit SwapChainPresenter(SwapChain& swapChain);
     41  ~SwapChainPresenter();
     42 
     43  const auto& BackBuffer() const { return mBackBuffer; }
     44 
     45  std::shared_ptr<SharedSurface> SwapBackBuffer(std::shared_ptr<SharedSurface>);
     46  GLuint Fb() const;
     47 };
     48 
     49 // -
     50 
     51 class SwapChain final {
     52  friend class SwapChainPresenter;
     53 
     54 public:
     55  UniquePtr<SurfaceFactory> mFactory;
     56 
     57 private:
     58  size_t mPoolLimit;
     59  std::queue<std::shared_ptr<SharedSurface>> mPool;
     60  std::shared_ptr<SharedSurface> mFrontBuffer;
     61  std::function<void()> mDestroyedCallback;
     62 
     63 public:
     64  std::shared_ptr<SharedSurface>
     65      mPrevFrontBuffer;  // Hold this ref while it's in-flight.
     66 private:
     67  SwapChainPresenter* mPresenter = nullptr;
     68 
     69 public:
     70  SwapChain();
     71  virtual ~SwapChain();
     72 
     73  void DisablePool() {
     74    if (mPoolLimit) {
     75      MOZ_ASSERT(mPool.empty());
     76      mPool = {};
     77      mPoolLimit = 0;
     78    }
     79  }
     80 
     81  void ClearPool();
     82  bool StoreRecycledSurface(const std::shared_ptr<SharedSurface>& surf);
     83  const auto& FrontBuffer() const { return mFrontBuffer; }
     84  UniquePtr<SwapChainPresenter> Acquire(const gfx::IntSize&, gfx::ColorSpace2);
     85 
     86  void SetDestroyedCallback(std::function<void()>&& aDestroyedCallback) {
     87    MOZ_ASSERT(!mDestroyedCallback);
     88    mDestroyedCallback = std::move(aDestroyedCallback);
     89    mPool = {};
     90  }
     91 };
     92 
     93 }  // namespace gl
     94 }  // namespace mozilla
     95 
     96 #endif  // SCREEN_BUFFER_H_