Files
palemoon27/gfx/gl/SharedSurface.cpp
T
roytam1 ee05861206 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1187056 - Dispatch the meta tag mutation events sooner so that any meta-viewport changes can be processed before scripts continue running. r=smaug (2e6f76079f)
- Bug 1238804. Make <base> actually work in a srcdoc document. r=smaug (a421e7c598)
- Bug 1239585 - Remove eSupportSVG from nsObjectLoadingContent capabilities; r=bz (68a79ef21b)
- Bug 1170572 - MQ CSS change not observed by picture source elements r=jdm (efc0be616e)
- Bug 1206720 - Remove invalid assertions during response image loading. r=jdm (0b6485de95)
- Bug 1229032 - don't copy arrays in PerformanceObserver::Observe; r=baku (f1b70e0bd2)
- Bug 1241840 - Set table cell colspan=0 to 1 instead per the HTML spec. r=bz (1900962d4c)
- Bug 812899 part 1. Split the "image is overflow" concept in ImageDocument into two separate booleans for vertical and horizontal overflow. r=khuey (8735b610f3)
- Bug 812899 part 2. Change the centering code in nsImageDocument to only try vertically centering via auto margins when we're not overflowing in the vertical direction, because if we _are_ overflowing that should cut off part of the image per spec. r=khuey (c3b84a8048)
- Bug 812899 part 3 - Make vertical 'auto' margins on absolutely positioned elements always center, even when the margins are negative. r=dbaron (e345fb9383)
- Bug 1238427 - Avoid a strong reference from the timeout timer to nsGeolocationRequest. r=jdm (2ad4dd3955)
- Bug 1240906 - Shut down geolocation service at xpcom-shutdown instead of quit-application. r=dougt (1e7fc9e624)
- Bug 1129633 - part1. Use win8 geolocation with a fallback to MLS - r=m_kato (3323b4a120)
- Bug 1145111: ensure the pos. cache isn't reset when nsGeolocationService shuts down provider. r=jdm (e69cab889a)
- Bug 1240664 - Only enable bug 1216148's behavior when there is a wakelock support. r=kanru (543a3a55b4)
- Bug 1240766 - Fix startup crash in Geolocation::Init() when principal URI is null r=jdm (1ece684418)
- Bug 1238873 - Handle the bug that if we take cached data, we might not get any update later. r=kchen. r=jdm. (ca186e89b3)
- Bug 1240666 - Follow-up to bug 1216148. r=kchen. (3f650b5f72)
- Bug 1227119 - Set a default value for variable ret. r=dougt (5b68dd1a22)
- Bug 1027734 - Convert mozPay to WebIDL. r=bzbarsky. (a4fd10d539)
- Bug 1214488 - Allow native callers in GetEntryGlobal. r=me (ca68cc8896)
- Bug 1248719. Fix things so that taking ownership of error reporting on an AutoJSAPI on a worker is OK even if that AutoJSAPI was initialized without an explicit global. r=bholley (b8e59eebc6)
- Bug 1156065 - Send cloneable messages from SiteSpecificUserAgent.js to fix UA overrides r=billm (e5cdbe96f6)
- Bug 1210099 - Fix structured clone of expanded principal (r=bholley) (01a5fd4e47)
- Bug 1230351: Replace ThirdPartyUtil::IsThirdPartyInternal's warning-spammy NS_ENSURE_ARG with an equivalent check that lacks the warning. r=mrbkap (037c39472c)
- Bug 1184293 - Don't call ThirdPartyUtil::GetBaseDomain if aUri is null. r=jduell (4853c0e9e0)
- Bug 862147 - instrument usage of window.sidebar.addSearchEngine, r=froydnj,smaug. (ef52ded1c0)
- Bug 1214764 - Convert NS_ENSURE to a plain return in nsDocument::IsScriptEnabled. r=bholley (0f3b6d63b4)
- Bug 1245950: Privately inherit from legacy interfaces nsIDOMWindow/nsIDOMWindowInternal. r=smaug (ff1d431d0d)
- Bug 1245950: Followup to null check before asserting. r=me CLOSED TREE (6e93af4a4a)
- Bug 1236607, pointerlock code should null check inner window before using it, r=xidorn (896d9723d5)
- Bug 1245245 - Fix typo in error string URL. r=botond (58c9548f15)
- Bug 1220604 - Remove dead code from nsGlobalWindow::SetNewDocument. r=baku (fc875db5e4)
- Bug 1247049 - Optimize GetScreenXY by using the new nsDeviceContext method to get desktop scale factor. r=emk (de66d59631)
- missing bit of  Bug 962249 part 3 (0b15f8b670)
- Bug 1240978 - Shorten timeout for black screen in fullscreen transition. r=smaug (2554a8e5a7)
- Bug 1246346 - patch 1 - Expose a DesktopToDevice scale factor on nsDeviceContext. r=emk (03856ec7b4)
- Bug 1246346 - patch 2 - Correct the origin of the DOM screen coordinates (screen.left, screen.top) for secondary display on a mixed-DPI configuration. r=emk (15ac2d3be6)
- Bug 1196159 - Set nsGlobalWindow::mIsClosed properly after window.close(). r=billm (27334e4b61)
- Bug 1240241 - Don't recurse infinitely in FilterCachedColorModels::ForColorModel if the original filter node was null. r=roc (1730db5ca7)
- Bug 1247706 - "Please don't disable WebGL if crashed on WebGL context". r=dvander (e25621753e)
- Bug 1244742. Use the default copy constructor and assignment operators. (0dac3bcbfb)
- Bug 1230929 - modified the logic in order to prvent null pointer dereference. r=jmuizelaar (1d5761abc9)
- Bug 1247979. Optimize ScaleToOutsidePixels. r=kats (2b9ef41111)
- Bug 1249368 - use UniquePtr instead of nsAutoArrayPtr in gfxAlphaBoxBlur; r=jrmuizel (e60419d49d)
- Bug 1082598 - Part 4: Workaround for naming conflict in unified sources for Skia and thebes DWrite fonts. r=jrmuizel (84a9e11ded)
- Bug 1172817 - Remove PL_DHashTableEnumerate() use from FontNameCache. r=jtd. (b0632cd825)
- Bug 1197717 - Load fonts from profile-agnostic writable location. r=jfkthame (c9227268ff)
- Bug 1246889 - Correctly check whether hb_blob_create failed in ShareTableAndGetBlob. r=jfkthame (f8c62776be)
- Bug 1246834 - Fix memory reporting of nsFontFamily{List,Name}. r=jfkthame. (0535b8c110)
- Bug 1238134 P1 Provide a Response.cloneUnfiltered() method for chrome code to access internal Response state. r=ehsan (3dc25b1501)
- Bug 1238134 P2 Test chrome-only Response.cloneUnfiltered(). r=ehsan (3c307c4ec0)
- Bug 1100949 - wrap font info reads with structured exception handler. r=bas (2c2cacb05e)
- Bug 1241931 - On shutdown stop any ongoing loading of fonts; GDI in particular. r=jdaggett (057aae8cab)
- Bug 1154182 - enable native keybindings for graphene. r=billm (33e706a020)
- Bug 1167081 - Call PuppetWidget::Destroy() in its destructor. r=roc (58115c3029)
- Bug 1222662 - Disable cursor caching in the content process on Windows. r=aklotz (e760794ee2)
- Bug 1240891 - Silence warning from calling GetNativeData(NS_NATIVE_WINDOW). r=roc (d766a47f66)
- Bug 1248981 - Fix spelling error in nsMenuUtilsX.mm, a=tomcat (b93c16b961)
- Bug 1197925 - Use channel->Open2() in intl/hyphenation/hnjstdio.cpp (r=sicking) (2bf5dd3cd3)
- Bug 1248339 - fix header sort order of hal. r=gsvelto (01b61a2f5d)
- Bug 1232687: Add system-service interface to HAL, r=gsvelto (ab692e1508)
- Bug 1236762 - Simplify locking APIs for SharedSurface. - r=jrmuizel (901ae94a85)
2024-01-09 11:57:12 +08:00

616 lines
18 KiB
C++

/* -*- 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"
#include "../2d/2D.h"
#include "GLBlitHelper.h"
#include "GLContext.h"
#include "GLReadTexImageHelper.h"
#include "GLScreenBuffer.h"
#include "nsThreadUtils.h"
#include "ScopedGLHelpers.h"
#include "SharedSurfaceGL.h"
#include "mozilla/layers/CompositorTypes.h"
#include "mozilla/layers/TextureClientSharedSurface.h"
#include "mozilla/unused.h"
namespace mozilla {
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.
UniquePtr<SharedSurface_Basic> tempSurf;
tempSurf = SharedSurface_Basic::Create(gl, factory->mFormats, src->mSize,
factory->mCaps.alpha);
ProdCopy(src, tempSurf.get(), factory);
ProdCopy(tempSurf.get(), dest, factory);
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();
gl->BlitHelper()->BlitFramebufferToTexture(0, destTex,
src->mSize,
dest->mSize,
destTarget,
true);
} else if (dest->mAttachType == AttachmentType::GLRenderbuffer) {
GLuint destRB = dest->ProdRenderbuffer();
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
gl->BlitHelper()->BlitFramebufferToFramebuffer(0,
destWrapper.FB(),
src->mSize,
dest->mSize,
true);
} else {
MOZ_CRASH("GFX: Unhandled dest->mAttachType 1.");
}
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();
gl->BlitHelper()->BlitTextureToFramebuffer(srcTex, 0,
src->mSize,
dest->mSize,
srcTarget,
true);
} else if (src->mAttachType == AttachmentType::GLRenderbuffer) {
GLuint srcRB = src->ProdRenderbuffer();
ScopedFramebufferForRenderbuffer srcWrapper(gl, srcRB);
gl->BlitHelper()->BlitFramebufferToFramebuffer(srcWrapper.FB(),
0,
src->mSize,
dest->mSize,
true);
} else {
MOZ_CRASH("GFX: Unhandled src->mAttachType 2.");
}
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("GFX: Unhandled dest->mAttachType 3.");
}
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("GFX: Unhandled dest->mAttachType 4.");
}
MOZ_CRASH("GFX: Unhandled src->mAttachType 5.");
}
////////////////////////////////////////////////////////////////////////
// SharedSurface
SharedSurface::SharedSurface(SharedSurfaceType type,
AttachmentType attachType,
GLContext* gl,
const gfx::IntSize& size,
bool hasAlpha,
bool canRecycle)
: mType(type)
, mAttachType(attachType)
, mGL(gl)
, mSize(size)
, mHasAlpha(hasAlpha)
, mCanRecycle(canRecycle)
, mIsLocked(false)
, mIsProducerAcquired(false)
, mIsConsumerAcquired(false)
#ifdef DEBUG
, mOwningThread(NS_GetCurrentThread())
#endif
{ }
layers::TextureFlags
SharedSurface::GetTextureFlags() const
{
return layers::TextureFlags::NO_FLAGS;
}
void
SharedSurface::LockProd()
{
MOZ_ASSERT(!mIsLocked);
LockProdImpl();
mGL->LockSurface(this);
mIsLocked = true;
}
void
SharedSurface::UnlockProd()
{
if (!mIsLocked)
return;
UnlockProdImpl();
mGL->UnlockSurface(this);
mIsLocked = false;
}
////////////////////////////////////////////////////////////////////////
// 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;
}
}
SurfaceFactory::SurfaceFactory(SharedSurfaceType type, GLContext* gl,
const SurfaceCaps& caps,
const RefPtr<layers::ISurfaceAllocator>& allocator,
const layers::TextureFlags& flags)
: mType(type)
, mGL(gl)
, mCaps(caps)
, mAllocator(allocator)
, mFlags(flags)
, mFormats(gl->ChooseGLFormats(caps))
, mMutex("SurfaceFactor::mMutex")
{
ChooseBufferBits(mCaps, &mDrawCaps, &mReadCaps);
}
SurfaceFactory::~SurfaceFactory()
{
while (!mRecycleTotalPool.empty()) {
StopRecycling(*mRecycleTotalPool.begin());
}
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();
}
already_AddRefed<layers::SharedSurfaceTextureClient>
SurfaceFactory::NewTexClient(const gfx::IntSize& size)
{
while (!mRecycleFreePool.empty()) {
RefPtr<layers::SharedSurfaceTextureClient> cur = mRecycleFreePool.front();
mRecycleFreePool.pop();
if (cur->Surf()->mSize == size) {
cur->Surf()->WaitForBufferOwnership();
return cur.forget();
}
StopRecycling(cur);
}
UniquePtr<SharedSurface> surf = Move(CreateShared(size));
if (!surf)
return nullptr;
RefPtr<layers::SharedSurfaceTextureClient> ret;
ret = layers::SharedSurfaceTextureClient::Create(Move(surf), this, mAllocator, mFlags);
StartRecycling(ret);
return ret.forget();
}
void
SurfaceFactory::StartRecycling(layers::SharedSurfaceTextureClient* tc)
{
tc->SetRecycleCallback(&SurfaceFactory::RecycleCallback, static_cast<void*>(this));
bool didInsert = mRecycleTotalPool.insert(tc);
MOZ_RELEASE_ASSERT(didInsert);
mozilla::Unused << didInsert;
}
void
SurfaceFactory::StopRecycling(layers::SharedSurfaceTextureClient* tc)
{
MutexAutoLock autoLock(mMutex);
// Must clear before releasing ref.
tc->ClearRecycleCallback();
bool didErase = mRecycleTotalPool.erase(tc);
MOZ_RELEASE_ASSERT(didErase);
mozilla::Unused << didErase;
}
/*static*/ void
SurfaceFactory::RecycleCallback(layers::TextureClient* rawTC, void* rawFactory)
{
RefPtr<layers::SharedSurfaceTextureClient> tc;
tc = static_cast<layers::SharedSurfaceTextureClient*>(rawTC);
SurfaceFactory* factory = static_cast<SurfaceFactory*>(rawFactory);
if (tc->Surf()->mCanRecycle) {
if (factory->Recycle(tc))
return;
}
// Did not recover the tex client. End the (re)cycle!
factory->StopRecycling(tc);
}
bool
SurfaceFactory::Recycle(layers::SharedSurfaceTextureClient* texClient)
{
MOZ_ASSERT(texClient);
MutexAutoLock autoLock(mMutex);
if (mRecycleFreePool.size() >= 2) {
return false;
}
RefPtr<layers::SharedSurfaceTextureClient> texClientRef = texClient;
mRecycleFreePool.push(texClientRef);
return true;
}
////////////////////////////////////////////////////////////////////////////////
// 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("GFX: 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();
}
}
////////////////////////////////////////////////////////////////////////////////
class AutoLockBits
{
gfx::DrawTarget* mDT;
uint8_t* mLockedBits;
public:
explicit AutoLockBits(gfx::DrawTarget* dt)
: mDT(dt)
, mLockedBits(nullptr)
{
MOZ_ASSERT(mDT);
}
bool Lock(uint8_t** data, gfx::IntSize* size, int32_t* stride,
gfx::SurfaceFormat* format)
{
if (!mDT->LockBits(data, size, stride, format))
return false;
mLockedBits = *data;
return true;
}
~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;
if (!lock.Lock(&dstBytes, &dstSize, &dstStride, &dstFormat))
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;
}
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;
}
} // namespace gl
} /* namespace mozilla */