1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "GLScreenBuffer.h"
#include "CompositorTypes.h"
#include "GLContext.h"
#include "gfx2DGlue.h"
#include "MozFramebuffer.h"
#include "SharedSurface.h"
#include "mozilla/gfx/BuildConstants.h"
namespace mozilla::gl {
// -
// SwapChainPresenter
UniquePtr<SwapChainPresenter> SwapChain::Acquire(
const gfx::IntSize& size, const gfx::ColorSpace2 colorSpace) {
MOZ_ASSERT(mFactory);
std::shared_ptr<SharedSurface> surf;
if (!mPool.empty()) {
// Try reuse
const auto& existingDesc = mPool.front()->mDesc;
auto newDesc = existingDesc;
newDesc.size = size;
newDesc.colorSpace = colorSpace;
if (newDesc != existingDesc || !mPool.front()->IsValid()) {
mPool = {};
}
}
// When pooling is disabled, recycling of SharedSurfaces is managed by the
// owner of the SwapChain by calling StoreRecycledSurface().
if (!mPool.empty() && (!mPoolLimit || mPool.size() >= mPoolLimit)) {
surf = mPool.front();
mPool.pop();
}
if (!surf) {
auto uniquePtrSurf = mFactory->CreateShared(size, colorSpace);
if (!uniquePtrSurf) return nullptr;
surf.reset(uniquePtrSurf.release());
}
if (mPoolLimit > 0) {
mPool.push(surf);
while (mPool.size() > mPoolLimit) {
mPool.pop();
}
}
auto ret = MakeUnique<SwapChainPresenter>(*this);
const auto old = ret->SwapBackBuffer(surf);
MOZ_ALWAYS_TRUE(!old);
return ret;
}
void SwapChain::ClearPool() {
mPool = {};
mPrevFrontBuffer = nullptr;
}
bool SwapChain::StoreRecycledSurface(
const std::shared_ptr<SharedSurface>& surf) {
MOZ_ASSERT(mFactory);
// Don't allow external recycled surfaces if SwapChain is managing own pool.
MOZ_ASSERT(!mPoolLimit);
if (mPoolLimit > 0 || !mFactory ||
NS_WARN_IF(surf->mDesc.gl != mFactory->mDesc.gl)) {
// Ensure we don't accidentally store an expired shared surface or from a
// different context.
return false;
}
mPool.push(surf);
return true;
}
// -
SwapChainPresenter::SwapChainPresenter(SwapChain& swapChain)
: mSwapChain(&swapChain) {
MOZ_RELEASE_ASSERT(mSwapChain->mPresenter == nullptr);
mSwapChain->mPresenter = this;
}
SwapChainPresenter::~SwapChainPresenter() {
if (!mSwapChain) return;
MOZ_RELEASE_ASSERT(mSwapChain->mPresenter == this);
mSwapChain->mPresenter = nullptr;
auto newFront = SwapBackBuffer(nullptr);
if (newFront) {
mSwapChain->mPrevFrontBuffer = mSwapChain->mFrontBuffer;
mSwapChain->mFrontBuffer = newFront;
}
}
std::shared_ptr<SharedSurface> SwapChainPresenter::SwapBackBuffer(
std::shared_ptr<SharedSurface> back) {
if (mBackBuffer) {
mBackBuffer->EndWrite();
}
auto old = mBackBuffer;
mBackBuffer = back;
if (mBackBuffer) {
mBackBuffer->BeginWrite();
}
return old;
}
GLuint SwapChainPresenter::Fb() const {
if (!mBackBuffer) return 0;
const auto& fb = mBackBuffer->mFb;
if (!fb) return 0;
return fb->mFB;
}
// -
// SwapChain
SwapChain::SwapChain()
: // We need to apply pooling on Android because of the AndroidSurface slow
// destructor bugs. They cause a noticeable performance hit. See bug
// #1646073.
mPoolLimit(kIsAndroid ? 4 : 0) {}
SwapChain::~SwapChain() {
if (mPresenter) {
// Out of order destruction, but ok.
(void)mPresenter->SwapBackBuffer(nullptr);
mPresenter->mSwapChain = nullptr;
mPresenter = nullptr;
}
if (mDestroyedCallback) {
mDestroyedCallback();
}
}
} // namespace mozilla::gl
|