2013-02-14 03:26:24 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
|
|
|
|
/* 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 "SharedSurface.h"
|
|
|
|
|
2014-10-11 01:36:17 +04:00
|
|
|
#include "../2d/2D.h"
|
2014-07-12 02:10:49 +04:00
|
|
|
#include "GLBlitHelper.h"
|
2014-09-03 02:15:41 +04:00
|
|
|
#include "GLContext.h"
|
2014-10-11 01:36:17 +04:00
|
|
|
#include "GLReadTexImageHelper.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
#include "GLScreenBuffer.h"
|
2014-09-03 02:15:41 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2014-07-12 02:10:49 +04:00
|
|
|
#include "ScopedGLHelpers.h"
|
|
|
|
#include "SharedSurfaceGL.h"
|
2015-06-05 03:15:38 +03:00
|
|
|
#include "mozilla/layers/CompositorTypes.h"
|
|
|
|
#include "mozilla/layers/TextureClientSharedSurface.h"
|
|
|
|
#include "mozilla/unused.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-07-12 02:10:49 +04:00
|
|
|
namespace gl {
|
|
|
|
|
|
|
|
/*static*/ void
|
|
|
|
SharedSurface::ProdCopy(SharedSurface* src, SharedSurface* dest,
|
|
|
|
SurfaceFactory* factory)
|
|
|
|
{
|
|
|
|
GLContext* gl = src->mGL;
|
|
|
|
|
|
|
|
// If `src` begins locked, it must end locked, though we may
|
|
|
|
// temporarily unlock it if we need to.
|
|
|
|
MOZ_ASSERT((src == gl->GetLockedSurface()) == src->IsLocked());
|
|
|
|
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::Screen &&
|
|
|
|
dest->mAttachType == AttachmentType::Screen)
|
|
|
|
{
|
|
|
|
// Here, we actually need to blit through a temp surface, so let's make one.
|
2015-06-05 03:15:38 +03:00
|
|
|
UniquePtr<SharedSurface_Basic> tempSurf;
|
|
|
|
tempSurf = SharedSurface_Basic::Create(gl, factory->mFormats, src->mSize,
|
|
|
|
factory->mCaps.alpha);
|
2014-07-12 02:10:49 +04:00
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
ProdCopy(src, tempSurf.get(), factory);
|
|
|
|
ProdCopy(tempSurf.get(), dest, factory);
|
2014-07-12 02:10:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::Screen) {
|
|
|
|
SharedSurface* origLocked = gl->GetLockedSurface();
|
|
|
|
bool srcNeedsUnlock = false;
|
|
|
|
bool origNeedsRelock = false;
|
|
|
|
if (origLocked != src) {
|
|
|
|
if (origLocked) {
|
|
|
|
origLocked->UnlockProd();
|
|
|
|
origNeedsRelock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
src->LockProd();
|
|
|
|
srcNeedsUnlock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint destTex = dest->ProdTexture();
|
|
|
|
GLenum destTarget = dest->ProdTextureTarget();
|
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
gl->BlitHelper()->BlitFramebufferToTexture(0, destTex,
|
|
|
|
src->mSize,
|
|
|
|
dest->mSize,
|
|
|
|
destTarget,
|
|
|
|
true);
|
2014-07-12 02:10:49 +04:00
|
|
|
} else if (dest->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint destRB = dest->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
|
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
gl->BlitHelper()->BlitFramebufferToFramebuffer(0,
|
|
|
|
destWrapper.FB(),
|
|
|
|
src->mSize,
|
|
|
|
dest->mSize,
|
|
|
|
true);
|
2014-07-12 02:10:49 +04:00
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unhandled dest->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srcNeedsUnlock)
|
|
|
|
src->UnlockProd();
|
|
|
|
|
|
|
|
if (origNeedsRelock)
|
|
|
|
origLocked->LockProd();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::Screen) {
|
|
|
|
SharedSurface* origLocked = gl->GetLockedSurface();
|
|
|
|
bool destNeedsUnlock = false;
|
|
|
|
bool origNeedsRelock = false;
|
|
|
|
if (origLocked != dest) {
|
|
|
|
if (origLocked) {
|
|
|
|
origLocked->UnlockProd();
|
|
|
|
origNeedsRelock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest->LockProd();
|
|
|
|
destNeedsUnlock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint srcTex = src->ProdTexture();
|
|
|
|
GLenum srcTarget = src->ProdTextureTarget();
|
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
gl->BlitHelper()->BlitTextureToFramebuffer(srcTex, 0,
|
|
|
|
src->mSize,
|
|
|
|
dest->mSize,
|
|
|
|
srcTarget,
|
|
|
|
true);
|
2014-07-12 02:10:49 +04:00
|
|
|
} else if (src->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint srcRB = src->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer srcWrapper(gl, srcRB);
|
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
gl->BlitHelper()->BlitFramebufferToFramebuffer(srcWrapper.FB(),
|
|
|
|
0,
|
|
|
|
src->mSize,
|
|
|
|
dest->mSize,
|
|
|
|
true);
|
2014-07-12 02:10:49 +04:00
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unhandled src->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destNeedsUnlock)
|
|
|
|
dest->UnlockProd();
|
|
|
|
|
|
|
|
if (origNeedsRelock)
|
|
|
|
origLocked->LockProd();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alright, done with cases involving Screen types.
|
|
|
|
// Only {src,dest}x{texture,renderbuffer} left.
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint srcTex = src->ProdTexture();
|
|
|
|
GLenum srcTarget = src->ProdTextureTarget();
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint destTex = dest->ProdTexture();
|
|
|
|
GLenum destTarget = dest->ProdTextureTarget();
|
|
|
|
|
|
|
|
gl->BlitHelper()->BlitTextureToTexture(srcTex, destTex,
|
|
|
|
src->mSize, dest->mSize,
|
|
|
|
srcTarget, destTarget);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint destRB = dest->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
|
|
|
|
|
|
|
|
gl->BlitHelper()->BlitTextureToFramebuffer(srcTex, destWrapper.FB(),
|
|
|
|
src->mSize, dest->mSize, srcTarget);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Unhandled dest->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint srcRB = src->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer srcWrapper(gl, srcRB);
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint destTex = dest->ProdTexture();
|
|
|
|
GLenum destTarget = dest->ProdTextureTarget();
|
|
|
|
|
|
|
|
gl->BlitHelper()->BlitFramebufferToTexture(srcWrapper.FB(), destTex,
|
|
|
|
src->mSize, dest->mSize, destTarget);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint destRB = dest->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
|
|
|
|
|
|
|
|
gl->BlitHelper()->BlitFramebufferToFramebuffer(srcWrapper.FB(), destWrapper.FB(),
|
|
|
|
src->mSize, dest->mSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Unhandled dest->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Unhandled src->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// SharedSurface
|
|
|
|
|
2014-09-03 02:15:41 +04:00
|
|
|
|
|
|
|
SharedSurface::SharedSurface(SharedSurfaceType type,
|
|
|
|
AttachmentType attachType,
|
|
|
|
GLContext* gl,
|
|
|
|
const gfx::IntSize& size,
|
2015-06-05 03:15:38 +03:00
|
|
|
bool hasAlpha,
|
|
|
|
bool canRecycle)
|
2014-09-03 02:15:41 +04:00
|
|
|
: mType(type)
|
|
|
|
, mAttachType(attachType)
|
|
|
|
, mGL(gl)
|
|
|
|
, mSize(size)
|
|
|
|
, mHasAlpha(hasAlpha)
|
2015-06-05 03:15:38 +03:00
|
|
|
, mCanRecycle(canRecycle)
|
2014-09-03 02:15:41 +04:00
|
|
|
, mIsLocked(false)
|
2014-10-10 00:33:22 +04:00
|
|
|
, mIsProducerAcquired(false)
|
|
|
|
, mIsConsumerAcquired(false)
|
2014-09-03 02:15:41 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mOwningThread(NS_GetCurrentThread())
|
|
|
|
#endif
|
2015-06-05 03:15:38 +03:00
|
|
|
{ }
|
2014-09-03 02:15:41 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
void
|
|
|
|
SharedSurface::LockProd()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsLocked);
|
|
|
|
|
|
|
|
LockProdImpl();
|
|
|
|
|
|
|
|
mGL->LockSurface(this);
|
|
|
|
mIsLocked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface::UnlockProd()
|
|
|
|
{
|
|
|
|
if (!mIsLocked)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UnlockProdImpl();
|
|
|
|
|
|
|
|
mGL->UnlockSurface(this);
|
|
|
|
mIsLocked = false;
|
|
|
|
}
|
|
|
|
|
2014-09-03 02:15:41 +04:00
|
|
|
void
|
|
|
|
SharedSurface::Fence_ContentThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
|
|
|
Fence_ContentThread_Impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface::WaitSync_ContentThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
|
|
|
return WaitSync_ContentThread_Impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface::PollSync_ContentThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
|
|
|
return PollSync_ContentThread_Impl();
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// SurfaceFactory
|
|
|
|
|
|
|
|
static void
|
|
|
|
ChooseBufferBits(const SurfaceCaps& caps,
|
|
|
|
SurfaceCaps* const out_drawCaps,
|
|
|
|
SurfaceCaps* const out_readCaps)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(out_drawCaps);
|
|
|
|
MOZ_ASSERT(out_readCaps);
|
|
|
|
|
|
|
|
SurfaceCaps screenCaps;
|
|
|
|
|
|
|
|
screenCaps.color = caps.color;
|
|
|
|
screenCaps.alpha = caps.alpha;
|
|
|
|
screenCaps.bpp16 = caps.bpp16;
|
|
|
|
|
|
|
|
screenCaps.depth = caps.depth;
|
|
|
|
screenCaps.stencil = caps.stencil;
|
|
|
|
|
|
|
|
screenCaps.antialias = caps.antialias;
|
|
|
|
screenCaps.preserve = caps.preserve;
|
|
|
|
|
|
|
|
if (caps.antialias) {
|
|
|
|
*out_drawCaps = screenCaps;
|
|
|
|
out_readCaps->Clear();
|
|
|
|
|
|
|
|
// Color caps need to be duplicated in readCaps.
|
|
|
|
out_readCaps->color = caps.color;
|
|
|
|
out_readCaps->alpha = caps.alpha;
|
|
|
|
out_readCaps->bpp16 = caps.bpp16;
|
|
|
|
} else {
|
|
|
|
out_drawCaps->Clear();
|
|
|
|
*out_readCaps = screenCaps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
SurfaceFactory::SurfaceFactory(SharedSurfaceType type, GLContext* gl,
|
|
|
|
const SurfaceCaps& caps,
|
|
|
|
const RefPtr<layers::ISurfaceAllocator>& allocator,
|
|
|
|
const layers::TextureFlags& flags)
|
|
|
|
: mType(type)
|
|
|
|
, mGL(gl)
|
2014-07-12 02:10:49 +04:00
|
|
|
, mCaps(caps)
|
2015-06-05 03:15:38 +03:00
|
|
|
, mAllocator(allocator)
|
|
|
|
, mFlags(flags)
|
2014-07-12 02:10:49 +04:00
|
|
|
, mFormats(gl->ChooseGLFormats(caps))
|
|
|
|
{
|
|
|
|
ChooseBufferBits(mCaps, &mDrawCaps, &mReadCaps);
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceFactory::~SurfaceFactory()
|
|
|
|
{
|
2015-06-05 03:15:38 +03:00
|
|
|
while (!mRecycleTotalPool.empty()) {
|
|
|
|
StopRecycling(*mRecycleTotalPool.begin());
|
2014-07-12 02:10:49 +04:00
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(mRecycleTotalPool.empty());
|
|
|
|
|
|
|
|
// If we mRecycleFreePool.clear() before StopRecycling(), we may try to recycle it,
|
|
|
|
// fail, call StopRecycling(), then return here and call it again.
|
|
|
|
mRecycleFreePool.clear();
|
2014-07-12 02:10:49 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<layers::SharedSurfaceTextureClient>
|
2015-06-05 03:15:38 +03:00
|
|
|
SurfaceFactory::NewTexClient(const gfx::IntSize& size)
|
2014-07-12 02:10:49 +04:00
|
|
|
{
|
2015-06-05 03:15:38 +03:00
|
|
|
while (!mRecycleFreePool.empty()) {
|
|
|
|
RefPtr<layers::SharedSurfaceTextureClient> cur = mRecycleFreePool.front();
|
|
|
|
mRecycleFreePool.pop();
|
2014-08-16 04:38:08 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
if (cur->Surf()->mSize == size) {
|
2015-09-21 17:21:52 +03:00
|
|
|
cur->Surf()->WaitForBufferOwnership();
|
2015-06-05 03:15:38 +03:00
|
|
|
return cur.forget();
|
|
|
|
}
|
2014-07-12 02:10:49 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
StopRecycling(cur);
|
2014-07-12 02:10:49 +04:00
|
|
|
}
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
UniquePtr<SharedSurface> surf = Move(CreateShared(size));
|
2015-06-05 02:50:59 +03:00
|
|
|
if (!surf)
|
|
|
|
return nullptr;
|
2015-06-05 00:21:32 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
RefPtr<layers::SharedSurfaceTextureClient> ret;
|
|
|
|
ret = new layers::SharedSurfaceTextureClient(mAllocator, mFlags, Move(surf), this);
|
|
|
|
|
|
|
|
StartRecycling(ret);
|
|
|
|
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SurfaceFactory::StartRecycling(layers::SharedSurfaceTextureClient* tc)
|
|
|
|
{
|
|
|
|
tc->SetRecycleCallback(&SurfaceFactory::RecycleCallback, static_cast<void*>(this));
|
2015-06-05 00:21:32 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
bool didInsert = mRecycleTotalPool.insert(tc);
|
|
|
|
MOZ_RELEASE_ASSERT(didInsert);
|
|
|
|
mozilla::unused << didInsert;
|
2015-06-05 00:21:32 +03:00
|
|
|
}
|
|
|
|
|
2015-06-05 02:50:59 +03:00
|
|
|
void
|
2015-06-05 03:15:38 +03:00
|
|
|
SurfaceFactory::StopRecycling(layers::SharedSurfaceTextureClient* tc)
|
2015-06-05 00:21:32 +03:00
|
|
|
{
|
2015-06-05 03:15:38 +03:00
|
|
|
// Must clear before releasing ref.
|
|
|
|
tc->ClearRecycleCallback();
|
|
|
|
|
|
|
|
bool didErase = mRecycleTotalPool.erase(tc);
|
|
|
|
MOZ_RELEASE_ASSERT(didErase);
|
|
|
|
mozilla::unused << didErase;
|
|
|
|
}
|
2015-06-05 00:21:32 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
/*static*/ void
|
|
|
|
SurfaceFactory::RecycleCallback(layers::TextureClient* rawTC, void* rawFactory)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
RefPtr<layers::SharedSurfaceTextureClient> tc;
|
|
|
|
tc = static_cast<layers::SharedSurfaceTextureClient*>(rawTC);
|
|
|
|
|
|
|
|
SurfaceFactory* factory = static_cast<SurfaceFactory*>(rawFactory);
|
|
|
|
|
|
|
|
if (tc->mSurf->mCanRecycle) {
|
|
|
|
if (factory->Recycle(tc))
|
|
|
|
return;
|
2015-06-05 00:21:32 +03:00
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
|
|
|
|
// Did not recover the tex client. End the (re)cycle!
|
|
|
|
factory->StopRecycling(tc);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SurfaceFactory::Recycle(layers::SharedSurfaceTextureClient* texClient)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(texClient);
|
|
|
|
|
|
|
|
if (mRecycleFreePool.size() >= 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<layers::SharedSurfaceTextureClient> texClientRef = texClient;
|
|
|
|
mRecycleFreePool.push(texClientRef);
|
|
|
|
return true;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 08:01:51 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ScopedReadbackFB
|
|
|
|
|
|
|
|
ScopedReadbackFB::ScopedReadbackFB(SharedSurface* src)
|
|
|
|
: mGL(src->mGL)
|
|
|
|
, mAutoFB(mGL)
|
|
|
|
, mTempFB(0)
|
|
|
|
, mTempTex(0)
|
|
|
|
, mSurfToUnlock(nullptr)
|
|
|
|
, mSurfToLock(nullptr)
|
|
|
|
{
|
|
|
|
switch (src->mAttachType) {
|
|
|
|
case AttachmentType::GLRenderbuffer:
|
|
|
|
{
|
|
|
|
mGL->fGenFramebuffers(1, &mTempFB);
|
|
|
|
mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, mTempFB);
|
|
|
|
|
|
|
|
GLuint rb = src->ProdRenderbuffer();
|
|
|
|
mGL->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
|
|
|
|
LOCAL_GL_COLOR_ATTACHMENT0,
|
|
|
|
LOCAL_GL_RENDERBUFFER, rb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AttachmentType::GLTexture:
|
|
|
|
{
|
|
|
|
mGL->fGenFramebuffers(1, &mTempFB);
|
|
|
|
mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, mTempFB);
|
|
|
|
|
|
|
|
GLuint tex = src->ProdTexture();
|
|
|
|
GLenum texImageTarget = src->ProdTextureTarget();
|
|
|
|
mGL->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER,
|
|
|
|
LOCAL_GL_COLOR_ATTACHMENT0,
|
|
|
|
texImageTarget, tex, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AttachmentType::Screen:
|
|
|
|
{
|
|
|
|
SharedSurface* origLocked = mGL->GetLockedSurface();
|
|
|
|
if (origLocked != src) {
|
|
|
|
if (origLocked) {
|
|
|
|
mSurfToLock = origLocked;
|
|
|
|
mSurfToLock->UnlockProd();
|
|
|
|
}
|
|
|
|
|
|
|
|
mSurfToUnlock = src;
|
|
|
|
mSurfToUnlock->LockProd();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This should just be BindFB, but we don't have
|
|
|
|
// the patch for this yet. (bug 1045955)
|
|
|
|
MOZ_ASSERT(mGL->Screen());
|
|
|
|
mGL->Screen()->BindReadFB_Internal(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unhandled `mAttachType`.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->NeedsIndirectReads()) {
|
|
|
|
mGL->fGenTextures(1, &mTempTex);
|
|
|
|
|
|
|
|
{
|
|
|
|
ScopedBindTexture autoTex(mGL, mTempTex);
|
|
|
|
|
|
|
|
GLenum format = src->mHasAlpha ? LOCAL_GL_RGBA
|
|
|
|
: LOCAL_GL_RGB;
|
|
|
|
auto width = src->mSize.width;
|
|
|
|
auto height = src->mSize.height;
|
|
|
|
mGL->fCopyTexImage2D(LOCAL_GL_TEXTURE_2D, 0, format, 0, 0, width,
|
|
|
|
height, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
mGL->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER,
|
|
|
|
LOCAL_GL_COLOR_ATTACHMENT0,
|
|
|
|
LOCAL_GL_TEXTURE_2D, mTempTex, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedReadbackFB::~ScopedReadbackFB()
|
|
|
|
{
|
|
|
|
if (mTempFB) {
|
|
|
|
mGL->fDeleteFramebuffers(1, &mTempFB);
|
|
|
|
}
|
|
|
|
if (mTempTex) {
|
|
|
|
mGL->fDeleteTextures(1, &mTempTex);
|
|
|
|
}
|
|
|
|
if (mSurfToUnlock) {
|
|
|
|
mSurfToUnlock->UnlockProd();
|
|
|
|
}
|
|
|
|
if (mSurfToLock) {
|
|
|
|
mSurfToLock->LockProd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 01:36:17 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class AutoLockBits
|
|
|
|
{
|
|
|
|
gfx::DrawTarget* mDT;
|
|
|
|
uint8_t* mLockedBits;
|
|
|
|
|
|
|
|
public:
|
2014-12-11 01:48:11 +03:00
|
|
|
explicit AutoLockBits(gfx::DrawTarget* dt)
|
2014-10-11 01:36:17 +04:00
|
|
|
: mDT(dt)
|
|
|
|
, mLockedBits(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDT);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Lock(uint8_t** data, gfx::IntSize* size, int32_t* stride,
|
|
|
|
gfx::SurfaceFormat* format)
|
|
|
|
{
|
2014-10-18 08:31:27 +04:00
|
|
|
if (!mDT->LockBits(data, size, stride, format))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mLockedBits = *data;
|
|
|
|
return true;
|
2014-10-11 01:36:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoLockBits() {
|
|
|
|
if (mLockedBits)
|
|
|
|
mDT->ReleaseBits(mLockedBits);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
ReadbackSharedSurface(SharedSurface* src, gfx::DrawTarget* dst)
|
|
|
|
{
|
|
|
|
AutoLockBits lock(dst);
|
|
|
|
|
|
|
|
uint8_t* dstBytes;
|
|
|
|
gfx::IntSize dstSize;
|
|
|
|
int32_t dstStride;
|
|
|
|
gfx::SurfaceFormat dstFormat;
|
2014-10-18 11:14:45 +04:00
|
|
|
if (!lock.Lock(&dstBytes, &dstSize, &dstStride, &dstFormat))
|
2014-10-11 01:36:17 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const bool isDstRGBA = (dstFormat == gfx::SurfaceFormat::R8G8B8A8 ||
|
|
|
|
dstFormat == gfx::SurfaceFormat::R8G8B8X8);
|
|
|
|
MOZ_ASSERT_IF(!isDstRGBA, dstFormat == gfx::SurfaceFormat::B8G8R8A8 ||
|
|
|
|
dstFormat == gfx::SurfaceFormat::B8G8R8X8);
|
|
|
|
|
|
|
|
size_t width = src->mSize.width;
|
|
|
|
size_t height = src->mSize.height;
|
|
|
|
MOZ_ASSERT(width == (size_t)dstSize.width);
|
|
|
|
MOZ_ASSERT(height == (size_t)dstSize.height);
|
|
|
|
|
|
|
|
GLenum readGLFormat;
|
|
|
|
GLenum readType;
|
|
|
|
|
|
|
|
{
|
|
|
|
ScopedReadbackFB autoReadback(src);
|
|
|
|
|
|
|
|
|
|
|
|
// We have a source FB, now we need a format.
|
|
|
|
GLenum dstGLFormat = isDstRGBA ? LOCAL_GL_BGRA : LOCAL_GL_RGBA;
|
|
|
|
GLenum dstType = LOCAL_GL_UNSIGNED_BYTE;
|
|
|
|
|
|
|
|
// We actually don't care if they match, since we can handle
|
|
|
|
// any read{Format,Type} we get.
|
|
|
|
GLContext* gl = src->mGL;
|
|
|
|
GetActualReadFormats(gl, dstGLFormat, dstType, &readGLFormat,
|
|
|
|
&readType);
|
|
|
|
|
|
|
|
MOZ_ASSERT(readGLFormat == LOCAL_GL_RGBA ||
|
|
|
|
readGLFormat == LOCAL_GL_BGRA);
|
|
|
|
MOZ_ASSERT(readType == LOCAL_GL_UNSIGNED_BYTE);
|
|
|
|
|
|
|
|
// ReadPixels from the current FB into lockedBits.
|
|
|
|
{
|
|
|
|
size_t alignment = 8;
|
|
|
|
if (dstStride % 4 == 0)
|
|
|
|
alignment = 4;
|
|
|
|
ScopedPackAlignment autoAlign(gl, alignment);
|
|
|
|
|
|
|
|
gl->raw_fReadPixels(0, 0, width, height, readGLFormat, readType,
|
|
|
|
dstBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool isReadRGBA = readGLFormat == LOCAL_GL_RGBA;
|
|
|
|
|
|
|
|
if (isReadRGBA != isDstRGBA) {
|
|
|
|
for (size_t j = 0; j < height; ++j) {
|
|
|
|
uint8_t* rowItr = dstBytes + j*dstStride;
|
|
|
|
uint8_t* rowEnd = rowItr + 4*width;
|
|
|
|
while (rowItr != rowEnd) {
|
|
|
|
Swap(rowItr[0], rowItr[2]);
|
|
|
|
rowItr += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:04:50 +03:00
|
|
|
uint32_t
|
|
|
|
ReadPixel(SharedSurface* src)
|
|
|
|
{
|
|
|
|
GLContext* gl = src->mGL;
|
|
|
|
|
|
|
|
uint32_t pixel;
|
|
|
|
|
|
|
|
ScopedReadbackFB a(src);
|
|
|
|
{
|
|
|
|
ScopedPackAlignment autoAlign(gl, 4);
|
|
|
|
|
|
|
|
UniquePtr<uint8_t[]> bytes(new uint8_t[4]);
|
|
|
|
gl->raw_fReadPixels(0, 0, 1, 1, LOCAL_GL_RGBA, LOCAL_GL_UNSIGNED_BYTE,
|
|
|
|
bytes.get());
|
|
|
|
memcpy(&pixel, bytes.get(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixel;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gl
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
} /* namespace mozilla */
|