2018-02-02 20:07:36 +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
|
2017-10-31 22:02:31 +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 "SourceSurfaceCapture.h"
|
|
|
|
#include "DrawCommand.h"
|
|
|
|
#include "DrawTargetCapture.h"
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/gfx/Logging.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
SourceSurfaceCapture::SourceSurfaceCapture(DrawTargetCaptureImpl* aOwner)
|
|
|
|
: mOwner(aOwner),
|
|
|
|
mHasCommandList(false),
|
2017-11-21 18:41:08 +03:00
|
|
|
mShouldResolveToLuminance{false},
|
|
|
|
mLuminanceType{LuminanceType::LUMINANCE},
|
|
|
|
mOpacity{1.0f},
|
2017-10-31 22:02:31 +03:00
|
|
|
mLock("SourceSurfaceCapture.mLock")
|
|
|
|
{
|
|
|
|
mSize = mOwner->GetSize();
|
|
|
|
mFormat = mOwner->GetFormat();
|
|
|
|
mRefDT = mOwner->mRefDT;
|
|
|
|
mStride = mOwner->mStride;
|
|
|
|
mSurfaceAllocationSize = mOwner->mSurfaceAllocationSize;
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:41:08 +03:00
|
|
|
SourceSurfaceCapture::SourceSurfaceCapture(DrawTargetCaptureImpl* aOwner,
|
|
|
|
LuminanceType aLuminanceType /* = LuminanceType::LINEARRGB */,
|
|
|
|
Float aOpacity /* = 1.0f */)
|
|
|
|
: mOwner{aOwner}
|
|
|
|
, mHasCommandList{false}
|
|
|
|
, mShouldResolveToLuminance{true}
|
|
|
|
, mLuminanceType{aLuminanceType}
|
|
|
|
, mOpacity{aOpacity}
|
|
|
|
, mLock{"SourceSurfaceCapture.mLock"}
|
|
|
|
{
|
|
|
|
mSize = mOwner->GetSize();
|
|
|
|
mFormat = mOwner->GetFormat();
|
|
|
|
mRefDT = mOwner->mRefDT;
|
|
|
|
mStride = mOwner->mStride;
|
|
|
|
mSurfaceAllocationSize = mOwner->mSurfaceAllocationSize;
|
|
|
|
|
|
|
|
// In this case our DrawTarget will not track us, so copy its drawing commands.
|
|
|
|
DrawTargetWillChange();
|
|
|
|
}
|
|
|
|
|
2017-10-31 22:02:31 +03:00
|
|
|
SourceSurfaceCapture::~SourceSurfaceCapture()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SourceSurfaceCapture::IsValid() const
|
|
|
|
{
|
|
|
|
// We must either be able to source a command list, or we must have a cached
|
|
|
|
// and rasterized surface.
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
return (mOwner || mHasCommandList) || mResolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceSurface>
|
|
|
|
SourceSurfaceCapture::Resolve(BackendType aBackendType)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (!mOwner && !mHasCommandList) {
|
|
|
|
// There is no way we can rasterize anything, we don't have a source
|
|
|
|
// DrawTarget and we don't have a command list. Return whatever our
|
|
|
|
// cached surface is.
|
|
|
|
return mResolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
BackendType backendType = aBackendType;
|
|
|
|
if (backendType == BackendType::NONE) {
|
|
|
|
backendType = mRefDT->GetBackendType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If on the paint thread, we require that the owning DrawTarget be detached
|
|
|
|
// from this snapshot. This roughly approximates an assert that nothing can
|
|
|
|
// mutate the snapshot.
|
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread() || !mOwner);
|
|
|
|
|
|
|
|
// Note: SurfaceType is not 1:1 with BackendType, so we can't easily decide
|
|
|
|
// that they match. Instead we just cache the first thing to be requested.
|
|
|
|
if (!mResolved) {
|
|
|
|
mResolved = ResolveImpl(backendType);
|
|
|
|
}
|
|
|
|
return mResolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceSurface>
|
|
|
|
SourceSurfaceCapture::ResolveImpl(BackendType aBackendType)
|
|
|
|
{
|
|
|
|
RefPtr<DrawTarget> dt;
|
2017-12-06 06:59:18 +03:00
|
|
|
uint8_t* data = nullptr;
|
2017-10-31 22:02:31 +03:00
|
|
|
if (!mSurfaceAllocationSize) {
|
|
|
|
if (aBackendType == mRefDT->GetBackendType()) {
|
|
|
|
dt = mRefDT->CreateSimilarDrawTarget(mSize, mFormat);
|
|
|
|
} else {
|
|
|
|
dt = Factory::CreateDrawTarget(aBackendType, mSize, mFormat);
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-06 06:59:18 +03:00
|
|
|
data = static_cast<uint8_t*>(calloc(1, mSurfaceAllocationSize));
|
2017-10-31 22:02:31 +03:00
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
BackendType type = Factory::DoesBackendSupportDataDrawtarget(aBackendType)
|
|
|
|
? aBackendType
|
|
|
|
: BackendType::SKIA;
|
|
|
|
dt = Factory::CreateDrawTargetForData(type, data, mSize, mStride, mFormat);
|
|
|
|
if (!dt) {
|
|
|
|
free(data);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 06:59:18 +03:00
|
|
|
|
2017-10-31 22:02:31 +03:00
|
|
|
if (!dt) {
|
2017-12-06 06:59:18 +03:00
|
|
|
// Make sure we haven't allocated and aren't leaking something, the code right
|
|
|
|
// anove here should have guaranteed that.
|
|
|
|
MOZ_ASSERT(!data);
|
2017-10-31 22:02:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're still attached to a DrawTarget, use its command list rather than
|
|
|
|
// our own (which will be empty).
|
|
|
|
CaptureCommandList& commands = mHasCommandList
|
|
|
|
? mCommands
|
|
|
|
: mOwner->mCommands;
|
|
|
|
for (CaptureCommandList::iterator iter(commands); !iter.Done(); iter.Next()) {
|
|
|
|
DrawingCommand* cmd = iter.Get();
|
|
|
|
cmd->ExecuteOnDT(dt, nullptr);
|
|
|
|
}
|
2017-12-06 06:59:18 +03:00
|
|
|
|
|
|
|
RefPtr<SourceSurface> surf;
|
2017-11-21 18:41:08 +03:00
|
|
|
if (!mShouldResolveToLuminance) {
|
2017-12-06 06:59:18 +03:00
|
|
|
surf = dt->Snapshot();
|
2017-11-21 18:41:08 +03:00
|
|
|
} else {
|
2017-12-06 06:59:18 +03:00
|
|
|
surf = dt->IntoLuminanceSource(mLuminanceType, mOpacity);
|
2017-11-21 18:41:08 +03:00
|
|
|
}
|
2017-12-06 06:59:18 +03:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
surf->AddUserData(reinterpret_cast<UserDataKey*>(dt.get()), data, free);
|
|
|
|
}
|
|
|
|
|
|
|
|
return surf.forget();
|
2017-10-31 22:02:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DataSourceSurface>
|
|
|
|
SourceSurfaceCapture::GetDataSurface()
|
|
|
|
{
|
|
|
|
RefPtr<SourceSurface> surface = Resolve();
|
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return surface->GetDataSurface();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceSurfaceCapture::DrawTargetWillDestroy()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// The source DrawTarget is going away, so we can just steal its commands.
|
2018-05-30 22:15:35 +03:00
|
|
|
mCommands = std::move(mOwner->mCommands);
|
2017-10-31 22:02:31 +03:00
|
|
|
mHasCommandList = true;
|
|
|
|
mOwner = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceSurfaceCapture::DrawTargetWillChange()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
for (CaptureCommandList::iterator iter(mOwner->mCommands); !iter.Done(); iter.Next()) {
|
|
|
|
DrawingCommand* cmd = iter.Get();
|
|
|
|
cmd->CloneInto(&mCommands);
|
|
|
|
}
|
|
|
|
|
|
|
|
mHasCommandList = true;
|
|
|
|
mOwner = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|