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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/* -*- 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
* 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 "TextureRecorded.h"
#include "mozilla/gfx/DrawTargetRecording.h"
#include "mozilla/layers/CompositableForwarder.h"
#include "RecordedCanvasEventImpl.h"
namespace mozilla {
namespace layers {
RecordedTextureData::RecordedTextureData(
already_AddRefed<CanvasChild> aCanvasChild, gfx::IntSize aSize,
gfx::SurfaceFormat aFormat, TextureType aTextureType,
TextureType aWebglTextureType)
: mRemoteTextureOwnerId(RemoteTextureOwnerId::GetNext()),
mCanvasChild(aCanvasChild),
mSize(aSize),
mFormat(aFormat) {}
RecordedTextureData::~RecordedTextureData() {
// We need the translator to drop its reference for the DrawTarget first,
// because the TextureData might need to destroy its DrawTarget within a lock.
mSnapshot = nullptr;
DetachSnapshotWrapper();
if (mDT) {
mDT->DetachTextureData(this);
mDT = nullptr;
}
mCanvasChild->RecordEvent(RecordedTextureDestruction(
mRemoteTextureOwnerId, ToRemoteTextureTxnType(mFwdTransactionTracker),
ToRemoteTextureTxnId(mFwdTransactionTracker)));
}
void RecordedTextureData::FillInfo(TextureData::Info& aInfo) const {
aInfo.size = mSize;
aInfo.format = mFormat;
aInfo.supportsMoz2D = true;
aInfo.hasSynchronization = true;
}
void RecordedTextureData::InvalidateContents() { mInvalidContents = true; }
bool RecordedTextureData::Lock(OpenMode aMode) {
if (!mCanvasChild->EnsureBeginTransaction()) {
return false;
}
if (!mDT && mInited) {
return false;
}
// If modifying the texture, then allocate a new remote texture id.
if (aMode & OpenMode::OPEN_WRITE) {
mUsedRemoteTexture = false;
}
bool wasInvalidContents = mInvalidContents;
mInvalidContents = false;
if (!mDT && !mInited) {
mInited = true;
mDT = mCanvasChild->CreateDrawTarget(mRemoteTextureOwnerId, mSize, mFormat);
if (!mDT) {
return false;
}
mDT->AttachTextureData(this);
// We lock the TextureData when we create it to get the remote DrawTarget.
mLockedMode = aMode;
return true;
}
mCanvasChild->RecordEvent(
RecordedTextureLock(mRemoteTextureOwnerId, aMode, wasInvalidContents));
mLockedMode = aMode;
return true;
}
void RecordedTextureData::DetachSnapshotWrapper(bool aInvalidate,
bool aRelease) {
if (mSnapshotWrapper) {
// If the snapshot only has one ref, then we don't need to worry about
// copying before invalidation since it is about to be deleted. Otherwise,
// we need to ensure any internal data is appropriately copied before
// shmems are potentially overwritten if there are still existing users.
mCanvasChild->DetachSurface(mSnapshotWrapper,
aInvalidate && !mSnapshotWrapper->hasOneRef());
if (aRelease) {
mSnapshotWrapper = nullptr;
}
}
}
void RecordedTextureData::Unlock() {
if ((mLockedMode == OpenMode::OPEN_READ_WRITE) &&
mCanvasChild->ShouldCacheDataSurface()) {
DetachSnapshotWrapper();
mSnapshot = mDT->Snapshot();
mDT->DetachAllSnapshots();
mCanvasChild->RecordEvent(RecordedCacheDataSurface(mSnapshot.get()));
}
mCanvasChild->RecordEvent(RecordedTextureUnlock(mRemoteTextureOwnerId));
mLockedMode = OpenMode::OPEN_NONE;
}
already_AddRefed<gfx::DrawTarget> RecordedTextureData::BorrowDrawTarget() {
if (mLockedMode & OpenMode::OPEN_WRITE) {
// The snapshot will be invalidated.
mSnapshot = nullptr;
DetachSnapshotWrapper(true);
}
return do_AddRef(mDT);
}
void RecordedTextureData::EndDraw() {
MOZ_ASSERT(mDT->hasOneRef());
MOZ_ASSERT(mLockedMode == OpenMode::OPEN_READ_WRITE);
if (mCanvasChild->ShouldCacheDataSurface()) {
DetachSnapshotWrapper();
mSnapshot = mDT->Snapshot();
mCanvasChild->RecordEvent(RecordedCacheDataSurface(mSnapshot.get()));
}
}
void RecordedTextureData::DrawTargetWillChange() {
// The DrawTargetRecording will be modified, so ensure that possibly the last
// reference to a snapshot is discarded so that it does not inadvertently
// force a copy.
mSnapshot = nullptr;
DetachSnapshotWrapper(true);
}
already_AddRefed<gfx::SourceSurface> RecordedTextureData::BorrowSnapshot() {
if (mSnapshotWrapper) {
// The DT is unmodified since the last time snapshot was borrowed, so it
// is safe to reattach the snapshot for shmem readbacks.
mCanvasChild->AttachSurface(mSnapshotWrapper);
return do_AddRef(mSnapshotWrapper);
}
// There are some failure scenarios where we have no DrawTarget and
// BorrowSnapshot is called in an attempt to copy to a new texture.
if (!mDT) {
return nullptr;
}
RefPtr<gfx::SourceSurface> wrapper = mCanvasChild->WrapSurface(
mSnapshot ? mSnapshot : mDT->Snapshot(), mRemoteTextureOwnerId);
mSnapshotWrapper = wrapper;
return wrapper.forget();
}
void RecordedTextureData::ReturnDrawTarget(
already_AddRefed<gfx::DrawTarget> aDT) {
RefPtr<gfx::DrawTarget> dt(aDT);
}
void RecordedTextureData::ReturnSnapshot(
already_AddRefed<gfx::SourceSurface> aSnapshot) {
RefPtr<gfx::SourceSurface> snapshot = aSnapshot;
// The snapshot needs to be marked detached but we keep the wrapper around
// so that it can be reused without repeatedly creating it and accidentally
// reading back data for each new instantiation.
DetachSnapshotWrapper(false, false);
}
void RecordedTextureData::Deallocate(LayersIPCChannel* aAllocator) {}
bool RecordedTextureData::Serialize(SurfaceDescriptor& aDescriptor) {
// If something is querying the id, assume it is going to be composited.
if (!mUsedRemoteTexture) {
mLastRemoteTextureId = RemoteTextureId::GetNext();
mCanvasChild->RecordEvent(
RecordedPresentTexture(mRemoteTextureOwnerId, mLastRemoteTextureId));
mUsedRemoteTexture = true;
}
aDescriptor = SurfaceDescriptorRemoteTexture(mLastRemoteTextureId,
mRemoteTextureOwnerId);
return true;
}
already_AddRefed<FwdTransactionTracker>
RecordedTextureData::UseCompositableForwarder(
CompositableForwarder* aForwarder) {
return FwdTransactionTracker::GetOrCreate(mFwdTransactionTracker);
}
void RecordedTextureData::OnForwardedToHost() {
// Compositing with RecordedTextureData requires RemoteTextureMap.
MOZ_CRASH("OnForwardedToHost not supported!");
}
TextureFlags RecordedTextureData::GetTextureFlags() const {
// With WebRender, resource open happens asynchronously on RenderThread.
// Use WAIT_HOST_USAGE_END to keep TextureClient alive during host side usage.
return TextureFlags::WAIT_HOST_USAGE_END;
}
bool RecordedTextureData::RequiresRefresh() const {
return mCanvasChild->RequiresRefresh(mRemoteTextureOwnerId);
}
} // namespace layers
} // namespace mozilla
|