2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2015-06-19 02:07:21 +03:00
|
|
|
* 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 "PersistentBufferProvider.h"
|
|
|
|
|
|
|
|
#include "Layers.h"
|
2016-10-11 14:23:11 +03:00
|
|
|
#include "mozilla/layers/ShadowLayers.h"
|
2017-11-16 08:47:26 +03:00
|
|
|
#include "mozilla/layers/TextureClient.h"
|
2019-10-30 13:05:59 +03:00
|
|
|
#include "mozilla/gfx/gfxVars.h"
|
2015-06-19 02:07:21 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_layers.h"
|
2015-06-19 02:07:21 +03:00
|
|
|
#include "pratom.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
|
|
|
|
namespace layers {
|
|
|
|
|
2016-09-16 19:55:44 +03:00
|
|
|
PersistentBufferProviderBasic::PersistentBufferProviderBasic(DrawTarget* aDt)
|
2016-07-01 11:58:06 +03:00
|
|
|
: mDrawTarget(aDt) {
|
|
|
|
MOZ_COUNT_CTOR(PersistentBufferProviderBasic);
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentBufferProviderBasic::~PersistentBufferProviderBasic() {
|
|
|
|
MOZ_COUNT_DTOR(PersistentBufferProviderBasic);
|
2017-11-23 11:31:55 +03:00
|
|
|
Destroy();
|
2016-07-01 11:58:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfx::DrawTarget>
|
|
|
|
PersistentBufferProviderBasic::BorrowDrawTarget(
|
|
|
|
const gfx::IntRect& aPersistedRect) {
|
|
|
|
MOZ_ASSERT(!mSnapshot);
|
|
|
|
RefPtr<gfx::DrawTarget> dt(mDrawTarget);
|
|
|
|
return dt.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PersistentBufferProviderBasic::ReturnDrawTarget(
|
|
|
|
already_AddRefed<gfx::DrawTarget> aDT) {
|
|
|
|
RefPtr<gfx::DrawTarget> dt(aDT);
|
|
|
|
MOZ_ASSERT(mDrawTarget == dt);
|
2016-07-11 18:44:27 +03:00
|
|
|
if (dt) {
|
|
|
|
// Since SkiaGL default to storing drawing command until flush
|
|
|
|
// we have to flush it before present.
|
|
|
|
dt->Flush();
|
|
|
|
}
|
2016-07-01 11:58:06 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfx::SourceSurface>
|
|
|
|
PersistentBufferProviderBasic::BorrowSnapshot() {
|
|
|
|
mSnapshot = mDrawTarget->Snapshot();
|
|
|
|
RefPtr<SourceSurface> snapshot = mSnapshot;
|
|
|
|
return snapshot.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PersistentBufferProviderBasic::ReturnSnapshot(
|
|
|
|
already_AddRefed<gfx::SourceSurface> aSnapshot) {
|
|
|
|
RefPtr<SourceSurface> snapshot = aSnapshot;
|
|
|
|
MOZ_ASSERT(!snapshot || snapshot == mSnapshot);
|
|
|
|
mSnapshot = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:31:55 +03:00
|
|
|
void PersistentBufferProviderBasic::Destroy() {
|
|
|
|
mSnapshot = nullptr;
|
|
|
|
mDrawTarget = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:06 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<PersistentBufferProviderBasic>
|
|
|
|
PersistentBufferProviderBasic::Create(gfx::IntSize aSize,
|
|
|
|
gfx::SurfaceFormat aFormat,
|
2016-09-16 19:55:44 +03:00
|
|
|
gfx::BackendType aBackend) {
|
2016-07-01 11:58:06 +03:00
|
|
|
RefPtr<DrawTarget> dt =
|
|
|
|
gfxPlatform::GetPlatform()->CreateDrawTargetForBackend(aBackend, aSize,
|
|
|
|
aFormat);
|
|
|
|
|
2019-02-01 15:18:55 +03:00
|
|
|
if (dt) {
|
|
|
|
// This is simply to ensure the DrawTarget gets initialized, and will detect
|
|
|
|
// a device reset, even if we're on the main thread.
|
|
|
|
dt->ClearRect(Rect(0, 0, 0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dt || !dt->IsValid()) {
|
2016-07-01 11:58:06 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<PersistentBufferProviderBasic> provider =
|
2016-09-16 19:55:44 +03:00
|
|
|
new PersistentBufferProviderBasic(dt);
|
2016-07-01 11:58:06 +03:00
|
|
|
|
|
|
|
return provider.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<PersistentBufferProviderShared>
|
|
|
|
PersistentBufferProviderShared::Create(gfx::IntSize aSize,
|
|
|
|
gfx::SurfaceFormat aFormat,
|
2017-11-16 08:47:26 +03:00
|
|
|
KnowsCompositor* aKnowsCompositor) {
|
2019-11-26 17:35:02 +03:00
|
|
|
if (!aKnowsCompositor || !aKnowsCompositor->GetTextureForwarder() ||
|
2019-06-10 12:23:17 +03:00
|
|
|
!aKnowsCompositor->GetTextureForwarder()->IPCOpen() ||
|
|
|
|
// Bug 1556433 - shared buffer provider and direct texture mapping do not
|
|
|
|
// synchronize properly
|
|
|
|
aKnowsCompositor->SupportsTextureDirectMapping()) {
|
2016-07-01 11:58:06 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:48:58 +03:00
|
|
|
if (!StaticPrefs::layers_shared_buffer_provider_enabled()) {
|
2018-11-28 23:44:27 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// Bug 1285271 - Disable shared buffer provider on Windows with D2D due to
|
|
|
|
// instability, unless we are remoting the canvas drawing to the GPU process.
|
|
|
|
if (gfxPlatform::GetPlatform()->GetPreferredCanvasBackend() ==
|
|
|
|
BackendType::DIRECT2D1_1 &&
|
|
|
|
!TextureData::IsRemote(aKnowsCompositor->GetCompositorBackendType(),
|
|
|
|
BackendSelector::Canvas)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-07-01 11:58:06 +03:00
|
|
|
RefPtr<TextureClient> texture = TextureClient::CreateForDrawing(
|
2017-11-16 08:47:26 +03:00
|
|
|
aKnowsCompositor, aFormat, aSize, BackendSelector::Canvas,
|
2018-03-12 16:10:13 +03:00
|
|
|
TextureFlags::DEFAULT | TextureFlags::NON_BLOCKING_READ_LOCK,
|
2016-07-01 11:58:06 +03:00
|
|
|
TextureAllocationFlags::ALLOC_DEFAULT);
|
|
|
|
|
|
|
|
if (!texture) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<PersistentBufferProviderShared> provider =
|
2017-11-16 08:47:26 +03:00
|
|
|
new PersistentBufferProviderShared(aSize, aFormat, aKnowsCompositor,
|
|
|
|
texture);
|
2016-07-01 11:58:06 +03:00
|
|
|
return provider.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentBufferProviderShared::PersistentBufferProviderShared(
|
|
|
|
gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
|
2017-11-16 08:47:26 +03:00
|
|
|
KnowsCompositor* aKnowsCompositor, RefPtr<TextureClient>& aTexture)
|
2016-07-01 11:58:06 +03:00
|
|
|
|
|
|
|
: mSize(aSize),
|
|
|
|
mFormat(aFormat),
|
2017-11-16 08:47:26 +03:00
|
|
|
mKnowsCompositor(aKnowsCompositor),
|
2016-07-27 17:50:20 +03:00
|
|
|
mFront(Nothing()) {
|
2017-11-16 08:47:26 +03:00
|
|
|
MOZ_ASSERT(aKnowsCompositor);
|
2016-07-27 17:50:20 +03:00
|
|
|
if (mTextures.append(aTexture)) {
|
|
|
|
mBack = Some<uint32_t>(0);
|
|
|
|
}
|
2019-10-30 13:05:59 +03:00
|
|
|
|
|
|
|
// If we are using webrender and our textures don't have an intermediate
|
|
|
|
// buffer, then we have to hold onto the textures for longer to make sure that
|
|
|
|
// the GPU has finished using them. So, we need to allow more TextureClients
|
|
|
|
// to be created.
|
|
|
|
if (!aTexture->HasIntermediateBuffer() && gfxVars::UseWebRender()) {
|
|
|
|
++mMaxAllowedTextures;
|
|
|
|
if (gfxVars::UseWebRenderTripleBufferingWin()) {
|
|
|
|
++mMaxAllowedTextures;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:06 +03:00
|
|
|
MOZ_COUNT_CTOR(PersistentBufferProviderShared);
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentBufferProviderShared::~PersistentBufferProviderShared() {
|
|
|
|
MOZ_COUNT_DTOR(PersistentBufferProviderShared);
|
|
|
|
|
2016-07-01 11:58:16 +03:00
|
|
|
if (IsActivityTracked()) {
|
2017-11-16 08:47:26 +03:00
|
|
|
mKnowsCompositor->GetActiveResourceTracker()->RemoveObject(this);
|
2016-07-01 11:58:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:18 +03:00
|
|
|
Destroy();
|
2016-07-01 11:58:06 +03:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:47:26 +03:00
|
|
|
LayersBackend PersistentBufferProviderShared::GetType() {
|
|
|
|
if (mKnowsCompositor->GetCompositorBackendType() ==
|
|
|
|
LayersBackend::LAYERS_WR) {
|
|
|
|
return LayersBackend::LAYERS_WR;
|
|
|
|
} else {
|
|
|
|
return LayersBackend::LAYERS_CLIENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PersistentBufferProviderShared::SetKnowsCompositor(
|
|
|
|
KnowsCompositor* aKnowsCompositor) {
|
|
|
|
MOZ_ASSERT(aKnowsCompositor);
|
|
|
|
if (!aKnowsCompositor) {
|
2016-08-09 15:42:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-16 08:47:26 +03:00
|
|
|
if (mKnowsCompositor == aKnowsCompositor) {
|
2016-08-09 15:42:15 +03:00
|
|
|
// The forwarder should not change most of the time.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsActivityTracked()) {
|
2017-11-16 08:47:26 +03:00
|
|
|
mKnowsCompositor->GetActiveResourceTracker()->RemoveObject(this);
|
2016-08-09 15:42:15 +03:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:47:26 +03:00
|
|
|
if (mKnowsCompositor->GetTextureForwarder() !=
|
|
|
|
aKnowsCompositor->GetTextureForwarder() ||
|
|
|
|
mKnowsCompositor->GetCompositorBackendType() !=
|
|
|
|
aKnowsCompositor->GetCompositorBackendType()) {
|
2016-08-09 15:42:15 +03:00
|
|
|
// We are going to be used with an different and/or incompatible forwarder.
|
|
|
|
// This should be extremely rare. We have to copy the front buffer into a
|
|
|
|
// texture that is compatible with the new forwarder.
|
|
|
|
|
|
|
|
// Grab the current front buffer.
|
|
|
|
RefPtr<TextureClient> prevTexture = GetTexture(mFront);
|
|
|
|
|
|
|
|
// Get rid of everything else
|
|
|
|
Destroy();
|
|
|
|
|
|
|
|
if (prevTexture) {
|
|
|
|
RefPtr<TextureClient> newTexture = TextureClient::CreateForDrawing(
|
2017-11-16 08:47:26 +03:00
|
|
|
aKnowsCompositor, mFormat, mSize, BackendSelector::Canvas,
|
2018-03-12 16:10:13 +03:00
|
|
|
TextureFlags::DEFAULT | TextureFlags::NON_BLOCKING_READ_LOCK,
|
2016-08-09 15:42:15 +03:00
|
|
|
TextureAllocationFlags::ALLOC_DEFAULT);
|
|
|
|
|
|
|
|
MOZ_ASSERT(newTexture);
|
|
|
|
if (!newTexture) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we early-return in one of the following branches, we will
|
|
|
|
// leave the buffer provider in an empty state, since we called
|
|
|
|
// Destroy. Not ideal but at least we won't try to use it with a
|
|
|
|
// an incompatible ipc channel.
|
|
|
|
|
|
|
|
if (!newTexture->Lock(OpenMode::OPEN_WRITE)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prevTexture->Lock(OpenMode::OPEN_READ)) {
|
|
|
|
newTexture->Unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success =
|
|
|
|
prevTexture->CopyToTextureClient(newTexture, nullptr, nullptr);
|
|
|
|
|
|
|
|
prevTexture->Unlock();
|
|
|
|
newTexture->Unlock();
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mTextures.append(newTexture)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mFront = Some<uint32_t>(mTextures.length() - 1);
|
|
|
|
mBack = mFront;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 08:47:26 +03:00
|
|
|
mKnowsCompositor = aKnowsCompositor;
|
2016-08-09 15:42:15 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-15 01:16:18 +03:00
|
|
|
TextureClient* PersistentBufferProviderShared::GetTexture(
|
|
|
|
const Maybe<uint32_t>& aIndex) {
|
2016-07-27 17:50:20 +03:00
|
|
|
if (aIndex.isNothing() || !CheckIndex(aIndex.value())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mTextures[aIndex.value()];
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:06 +03:00
|
|
|
already_AddRefed<gfx::DrawTarget>
|
|
|
|
PersistentBufferProviderShared::BorrowDrawTarget(
|
|
|
|
const gfx::IntRect& aPersistedRect) {
|
2019-11-05 04:54:54 +03:00
|
|
|
if (!mKnowsCompositor->GetTextureForwarder() ||
|
|
|
|
!mKnowsCompositor->GetTextureForwarder()->IPCOpen()) {
|
2016-07-01 11:58:06 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:16 +03:00
|
|
|
MOZ_ASSERT(!mSnapshot);
|
|
|
|
|
|
|
|
if (IsActivityTracked()) {
|
2017-11-16 08:47:26 +03:00
|
|
|
mKnowsCompositor->GetActiveResourceTracker()->MarkUsed(this);
|
2016-07-01 11:58:16 +03:00
|
|
|
} else {
|
2017-11-16 08:47:26 +03:00
|
|
|
mKnowsCompositor->GetActiveResourceTracker()->AddObject(this);
|
2016-07-01 11:58:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
if (mDrawTarget) {
|
|
|
|
RefPtr<gfx::DrawTarget> dt(mDrawTarget);
|
|
|
|
return dt.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto previousBackBuffer = mBack;
|
|
|
|
|
|
|
|
TextureClient* tex = GetTexture(mBack);
|
|
|
|
|
|
|
|
// First try to reuse the current back buffer. If we can do that it means
|
|
|
|
// we can skip copying its content to the new back buffer.
|
2019-08-06 03:43:09 +03:00
|
|
|
if ((mTextureLockIsUnreliable.isSome() &&
|
|
|
|
mTextureLockIsUnreliable == mBack) ||
|
|
|
|
(tex && tex->IsReadLocked())) {
|
2016-07-27 17:50:20 +03:00
|
|
|
// The back buffer is currently used by the compositor, we can't draw
|
|
|
|
// into it.
|
|
|
|
tex = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tex) {
|
|
|
|
// Try to grab an already allocated texture if any is available.
|
|
|
|
for (uint32_t i = 0; i < mTextures.length(); ++i) {
|
2019-08-06 03:43:09 +03:00
|
|
|
if (!mTextures[i]->IsReadLocked() &&
|
|
|
|
!(mTextureLockIsUnreliable.isSome() &&
|
|
|
|
mTextureLockIsUnreliable.ref() == i)) {
|
2016-07-27 17:50:20 +03:00
|
|
|
mBack = Some(i);
|
|
|
|
tex = mTextures[i];
|
|
|
|
break;
|
2016-07-01 11:58:16 +03:00
|
|
|
}
|
2016-07-01 11:58:06 +03:00
|
|
|
}
|
2016-07-27 17:50:20 +03:00
|
|
|
}
|
2016-07-01 11:58:06 +03:00
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
if (!tex) {
|
|
|
|
// We have to allocate a new texture.
|
2019-10-30 13:05:59 +03:00
|
|
|
if (mTextures.length() >= mMaxAllowedTextures) {
|
2016-07-27 17:50:20 +03:00
|
|
|
// We should never need to buffer that many textures, something's wrong.
|
2016-08-09 19:19:01 +03:00
|
|
|
// In theory we throttle the main thread when the compositor can't keep
|
|
|
|
// up, so we shoud never get in a situation where we sent 4 textures to
|
2016-10-11 14:23:11 +03:00
|
|
|
// the compositor and the latter has not released any of them. In
|
|
|
|
// practice, though, the throttling mechanism appears to have some issues,
|
|
|
|
// especially when switching between layer managers (during tab-switch).
|
|
|
|
// To make sure we don't get too far ahead of the compositor, we send a
|
|
|
|
// sync ping to the compositor thread...
|
2017-11-16 08:47:26 +03:00
|
|
|
mKnowsCompositor->SyncWithCompositor();
|
2016-10-11 14:23:11 +03:00
|
|
|
// ...and try again.
|
|
|
|
for (uint32_t i = 0; i < mTextures.length(); ++i) {
|
|
|
|
if (!mTextures[i]->IsReadLocked()) {
|
|
|
|
gfxCriticalNote << "Managed to allocate after flush.";
|
|
|
|
mBack = Some(i);
|
|
|
|
tex = mTextures[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tex) {
|
2019-03-07 18:51:17 +03:00
|
|
|
gfxCriticalNote << "Unexpected BufferProvider over-production.";
|
2016-10-11 14:23:11 +03:00
|
|
|
// It would be pretty bad to keep piling textures up at this point so we
|
|
|
|
// call NotifyInactive to remove some of our textures.
|
|
|
|
NotifyInactive();
|
|
|
|
// Give up now. The caller can fall-back to a non-shared buffer
|
|
|
|
// provider.
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-07-01 11:58:06 +03:00
|
|
|
}
|
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
RefPtr<TextureClient> newTexture = TextureClient::CreateForDrawing(
|
2017-11-16 08:47:26 +03:00
|
|
|
mKnowsCompositor, mFormat, mSize, BackendSelector::Canvas,
|
2018-03-12 16:10:13 +03:00
|
|
|
TextureFlags::DEFAULT | TextureFlags::NON_BLOCKING_READ_LOCK,
|
2016-07-27 17:50:20 +03:00
|
|
|
TextureAllocationFlags::ALLOC_DEFAULT);
|
|
|
|
|
|
|
|
MOZ_ASSERT(newTexture);
|
|
|
|
if (newTexture) {
|
|
|
|
if (mTextures.append(newTexture)) {
|
|
|
|
tex = newTexture;
|
|
|
|
mBack = Some<uint32_t>(mTextures.length() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-01 11:58:06 +03:00
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
if (!tex || !tex->Lock(OpenMode::OPEN_READ_WRITE)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-07-01 11:58:06 +03:00
|
|
|
|
2019-08-06 03:43:09 +03:00
|
|
|
// Clear dirty texture, since new back texture is selected.
|
|
|
|
mTextureLockIsUnreliable = Nothing();
|
|
|
|
|
2018-12-02 17:14:35 +03:00
|
|
|
mDrawTarget = tex->BorrowDrawTarget();
|
2016-07-27 17:50:20 +03:00
|
|
|
if (mBack != previousBackBuffer && !aPersistedRect.IsEmpty()) {
|
2018-12-02 17:14:35 +03:00
|
|
|
if (mPreviousSnapshot) {
|
|
|
|
mDrawTarget->CopySurface(mPreviousSnapshot, aPersistedRect,
|
|
|
|
gfx::IntPoint(0, 0));
|
|
|
|
} else {
|
|
|
|
TextureClient* previous = GetTexture(previousBackBuffer);
|
|
|
|
if (previous && previous->Lock(OpenMode::OPEN_READ)) {
|
|
|
|
DebugOnly<bool> success =
|
|
|
|
previous->CopyToTextureClient(tex, &aPersistedRect, nullptr);
|
|
|
|
MOZ_ASSERT(success);
|
|
|
|
|
|
|
|
previous->Unlock();
|
|
|
|
}
|
2016-07-01 11:58:06 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-02 17:14:35 +03:00
|
|
|
mPreviousSnapshot = nullptr;
|
2016-07-27 17:50:20 +03:00
|
|
|
|
2019-02-01 15:18:55 +03:00
|
|
|
if (mDrawTarget) {
|
|
|
|
// This is simply to ensure the DrawTarget gets initialized, and will detect
|
|
|
|
// a device reset, even if we're on the main thread.
|
|
|
|
mDrawTarget->ClearRect(Rect(0, 0, 0, 0));
|
|
|
|
|
|
|
|
if (!mDrawTarget->IsValid()) {
|
|
|
|
mDrawTarget = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:06 +03:00
|
|
|
RefPtr<gfx::DrawTarget> dt(mDrawTarget);
|
|
|
|
return dt.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PersistentBufferProviderShared::ReturnDrawTarget(
|
|
|
|
already_AddRefed<gfx::DrawTarget> aDT) {
|
|
|
|
RefPtr<gfx::DrawTarget> dt(aDT);
|
|
|
|
MOZ_ASSERT(mDrawTarget == dt);
|
2016-07-27 17:50:20 +03:00
|
|
|
// Can't change the current front buffer while its snapshot is borrowed!
|
2016-07-01 11:58:16 +03:00
|
|
|
MOZ_ASSERT(!mSnapshot);
|
2016-07-01 11:58:06 +03:00
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
TextureClient* back = GetTexture(mBack);
|
|
|
|
MOZ_ASSERT(back);
|
2016-07-01 11:58:06 +03:00
|
|
|
|
2018-12-02 17:14:35 +03:00
|
|
|
// If our TextureClients have internal synchronization then, if locks are
|
|
|
|
// needed for reading and writing, this can cause locking issues with the
|
|
|
|
// compositor. To prevent this we take a snapshot when the DrawTarget is
|
|
|
|
// returned, so this can be used when our own BorrowSnapshot is called and
|
|
|
|
// also for copying to the next TextureClient. Using this snapshot outside of
|
|
|
|
// the locks is safe, because the TextureClient calls DetachAllSnapshots on
|
|
|
|
// its DrawTarget when we Unlock below.
|
|
|
|
if (back->HasSynchronization()) {
|
|
|
|
mPreviousSnapshot = back->BorrowSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
mDrawTarget = nullptr;
|
|
|
|
dt = nullptr;
|
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
if (back) {
|
|
|
|
back->Unlock();
|
|
|
|
mFront = mBack;
|
2016-07-01 11:58:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
return !!back;
|
|
|
|
}
|
2016-07-01 11:58:06 +03:00
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
TextureClient* PersistentBufferProviderShared::GetTextureClient() {
|
|
|
|
// Can't access the front buffer while drawing.
|
|
|
|
MOZ_ASSERT(!mDrawTarget);
|
|
|
|
TextureClient* texture = GetTexture(mFront);
|
2018-03-12 16:10:13 +03:00
|
|
|
if (!texture) {
|
2016-07-27 17:50:20 +03:00
|
|
|
gfxCriticalNote
|
|
|
|
<< "PersistentBufferProviderShared: front buffer unavailable";
|
|
|
|
}
|
|
|
|
return texture;
|
2016-07-01 11:58:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfx::SourceSurface>
|
|
|
|
PersistentBufferProviderShared::BorrowSnapshot() {
|
2018-12-02 17:14:35 +03:00
|
|
|
if (mPreviousSnapshot) {
|
|
|
|
mSnapshot = mPreviousSnapshot;
|
|
|
|
return do_AddRef(mSnapshot);
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
if (mDrawTarget) {
|
|
|
|
auto back = GetTexture(mBack);
|
|
|
|
MOZ_ASSERT(back && back->IsLocked());
|
|
|
|
mSnapshot = back->BorrowSnapshot();
|
|
|
|
return do_AddRef(mSnapshot);
|
|
|
|
}
|
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
auto front = GetTexture(mFront);
|
|
|
|
if (!front || front->IsLocked()) {
|
2016-07-01 11:58:06 +03:00
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
if (!front->Lock(OpenMode::OPEN_READ)) {
|
2016-07-01 11:58:06 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:14:35 +03:00
|
|
|
mSnapshot = front->BorrowSnapshot();
|
2016-07-01 11:58:06 +03:00
|
|
|
|
2018-12-02 17:14:35 +03:00
|
|
|
return do_AddRef(mSnapshot);
|
2016-07-01 11:58:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PersistentBufferProviderShared::ReturnSnapshot(
|
|
|
|
already_AddRefed<gfx::SourceSurface> aSnapshot) {
|
|
|
|
RefPtr<SourceSurface> snapshot = aSnapshot;
|
|
|
|
MOZ_ASSERT(!snapshot || snapshot == mSnapshot);
|
|
|
|
|
|
|
|
mSnapshot = nullptr;
|
|
|
|
snapshot = nullptr;
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
if (mPreviousSnapshot || mDrawTarget) {
|
2018-12-02 17:14:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
auto front = GetTexture(mFront);
|
|
|
|
if (front) {
|
|
|
|
front->Unlock();
|
|
|
|
}
|
2015-06-19 02:07:21 +03:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:16 +03:00
|
|
|
void PersistentBufferProviderShared::NotifyInactive() {
|
2016-10-11 14:23:11 +03:00
|
|
|
ClearCachedResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PersistentBufferProviderShared::ClearCachedResources() {
|
2016-07-27 17:50:20 +03:00
|
|
|
RefPtr<TextureClient> front = GetTexture(mFront);
|
|
|
|
RefPtr<TextureClient> back = GetTexture(mBack);
|
|
|
|
|
|
|
|
// Clear all textures (except the front and back ones that we just kept).
|
|
|
|
mTextures.clear();
|
|
|
|
|
|
|
|
if (back) {
|
|
|
|
if (mTextures.append(back)) {
|
|
|
|
mBack = Some<uint32_t>(0);
|
|
|
|
}
|
|
|
|
if (front == back) {
|
|
|
|
mFront = mBack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (front && front != back) {
|
|
|
|
if (mTextures.append(front)) {
|
|
|
|
mFront = Some<uint32_t>(mTextures.length() - 1);
|
|
|
|
}
|
2016-07-01 11:58:16 +03:00
|
|
|
}
|
2019-08-06 03:43:09 +03:00
|
|
|
// Set front texture as dirty texture.
|
|
|
|
// The texture's read lock is unreliable after this function call.
|
|
|
|
mTextureLockIsUnreliable = mFront;
|
2016-07-01 11:58:16 +03:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:18 +03:00
|
|
|
void PersistentBufferProviderShared::Destroy() {
|
|
|
|
mSnapshot = nullptr;
|
2018-12-02 17:14:35 +03:00
|
|
|
mPreviousSnapshot = nullptr;
|
2016-07-01 11:58:18 +03:00
|
|
|
mDrawTarget = nullptr;
|
|
|
|
|
2016-11-15 11:55:49 +03:00
|
|
|
for (auto& mTexture : mTextures) {
|
|
|
|
TextureClient* texture = mTexture;
|
2016-07-27 17:50:20 +03:00
|
|
|
if (texture && texture->IsLocked()) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
texture->Unlock();
|
|
|
|
}
|
2016-07-01 11:58:18 +03:00
|
|
|
}
|
|
|
|
|
2016-07-27 17:50:20 +03:00
|
|
|
mTextures.clear();
|
2016-07-01 11:58:18 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
2016-09-27 06:22:20 +03:00
|
|
|
} // namespace mozilla
|