2014-07-11 01:29:40 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 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 "TextureDIB.h"
|
|
|
|
#include "gfx2DGlue.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
|
|
|
|
namespace layers {
|
|
|
|
|
2014-12-22 11:49:00 +03:00
|
|
|
DIBTextureClient::DIBTextureClient(ISurfaceAllocator* aAllocator,
|
|
|
|
gfx::SurfaceFormat aFormat,
|
|
|
|
TextureFlags aFlags)
|
|
|
|
: TextureClient(aAllocator, aFlags)
|
2014-07-11 01:29:40 +04:00
|
|
|
, mFormat(aFormat)
|
|
|
|
, mIsLocked(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(DIBTextureClient);
|
|
|
|
}
|
|
|
|
|
|
|
|
DIBTextureClient::~DIBTextureClient()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(DIBTextureClient);
|
|
|
|
}
|
|
|
|
|
2014-07-25 13:24:46 +04:00
|
|
|
TemporaryRef<TextureClient>
|
|
|
|
DIBTextureClient::CreateSimilar(TextureFlags aFlags,
|
2014-08-06 16:40:07 +04:00
|
|
|
TextureAllocationFlags aAllocFlags) const
|
2014-07-25 13:24:46 +04:00
|
|
|
{
|
2014-12-22 11:49:00 +03:00
|
|
|
RefPtr<TextureClient> tex = new DIBTextureClient(mAllocator, mFormat,
|
|
|
|
mFlags | aFlags);
|
2014-07-25 13:24:46 +04:00
|
|
|
|
2014-08-06 16:40:07 +04:00
|
|
|
if (!tex->AllocateForSurface(mSize, aAllocFlags)) {
|
2014-07-25 13:24:46 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:29:40 +04:00
|
|
|
bool
|
|
|
|
DIBTextureClient::Lock(OpenMode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsLocked);
|
|
|
|
if (!IsValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mIsLocked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DIBTextureClient::Unlock()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIsLocked, "Unlocked called while the texture is not locked!");
|
|
|
|
if (mDrawTarget) {
|
2014-07-30 17:38:46 +04:00
|
|
|
if (mReadbackSink) {
|
|
|
|
RefPtr<SourceSurface> snapshot = mDrawTarget->Snapshot();
|
|
|
|
RefPtr<DataSourceSurface> dataSurf = snapshot->GetDataSurface();
|
|
|
|
mReadbackSink->ProcessReadback(dataSurf);
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:29:40 +04:00
|
|
|
mDrawTarget->Flush();
|
|
|
|
mDrawTarget = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsLocked = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DIBTextureClient::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
|
|
|
if (!IsAllocated()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mSurface);
|
|
|
|
// The host will release this ref when it receives the surface descriptor.
|
|
|
|
// We AddRef in case we die before the host receives the pointer.
|
|
|
|
aOutDescriptor = SurfaceDescriptorDIB(reinterpret_cast<uintptr_t>(mSurface.get()));
|
|
|
|
mSurface->AddRef();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::DrawTarget*
|
|
|
|
DIBTextureClient::BorrowDrawTarget()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIsLocked && IsAllocated());
|
|
|
|
|
|
|
|
if (!mDrawTarget) {
|
|
|
|
mDrawTarget =
|
|
|
|
gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(mSurface, mSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mDrawTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DIBTextureClient::AllocateForSurface(gfx::IntSize aSize, TextureAllocationFlags aFlags)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!IsAllocated());
|
|
|
|
mSize = aSize;
|
|
|
|
|
|
|
|
mSurface = new gfxWindowsSurface(gfxIntSize(aSize.width, aSize.height),
|
|
|
|
SurfaceFormatToImageFormat(mFormat));
|
|
|
|
if (!mSurface || mSurface->CairoStatus())
|
|
|
|
{
|
|
|
|
NS_WARNING("Could not create surface");
|
|
|
|
mSurface = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIBTextureHost::DIBTextureHost(TextureFlags aFlags,
|
|
|
|
const SurfaceDescriptorDIB& aDescriptor)
|
|
|
|
: TextureHost(aFlags)
|
|
|
|
, mIsLocked(false)
|
|
|
|
{
|
|
|
|
// We added an extra ref for transport, so we shouldn't AddRef now.
|
|
|
|
mSurface =
|
|
|
|
dont_AddRef(reinterpret_cast<gfxWindowsSurface*>(aDescriptor.surface()));
|
|
|
|
MOZ_ASSERT(mSurface);
|
|
|
|
|
2015-04-07 17:08:57 +03:00
|
|
|
mSize = mSurface->GetSize();
|
2014-07-11 01:29:40 +04:00
|
|
|
mFormat = ImageFormatToSurfaceFormat(
|
|
|
|
gfxPlatform::GetPlatform()->OptimalFormatForContent(mSurface->GetContentType()));
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:16:34 +03:00
|
|
|
bool
|
|
|
|
DIBTextureHost::BindTextureSource(CompositableTextureSourceRef& aTexture)
|
2014-07-11 01:29:40 +04:00
|
|
|
{
|
|
|
|
if (!mTextureSource) {
|
|
|
|
Updated();
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:16:34 +03:00
|
|
|
aTexture = mTextureSource;
|
|
|
|
return !!aTexture;
|
2014-07-11 01:29:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DIBTextureHost::Updated(const nsIntRegion* aRegion)
|
|
|
|
{
|
2015-03-27 13:16:27 +03:00
|
|
|
if (!mCompositor) {
|
|
|
|
// This can happen if we send textures to a compositable that isn't yet
|
|
|
|
// attached to a layer.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:29:40 +04:00
|
|
|
if (!mTextureSource) {
|
|
|
|
mTextureSource = mCompositor->CreateDataTextureSource(mFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> imgSurf = mSurface->GetAsImageSurface();
|
|
|
|
|
|
|
|
RefPtr<DataSourceSurface> surf = Factory::CreateWrappingDataSourceSurface(imgSurf->Data(), imgSurf->Stride(), mSize, mFormat);
|
|
|
|
|
|
|
|
if (!mTextureSource->Update(surf, const_cast<nsIntRegion*>(aRegion))) {
|
|
|
|
mTextureSource = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DIBTextureHost::Lock()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsLocked);
|
|
|
|
mIsLocked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DIBTextureHost::Unlock()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIsLocked);
|
|
|
|
mIsLocked = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DIBTextureHost::SetCompositor(Compositor* aCompositor)
|
|
|
|
{
|
|
|
|
mCompositor = aCompositor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DIBTextureHost::DeallocateDeviceData()
|
|
|
|
{
|
|
|
|
if (mTextureSource) {
|
|
|
|
mTextureSource->DeallocateDeviceData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|