2011-12-21 23:03:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2011-12-21 23:03:26 +04:00
|
|
|
|
2012-01-18 00:00:56 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2011-12-21 23:03:26 +04:00
|
|
|
#include "AndroidDirectTexture.h"
|
|
|
|
|
|
|
|
typedef gfxASurface::gfxImageFormat gfxImageFormat;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
AndroidDirectTexture::AndroidDirectTexture(uint32_t width, uint32_t height, uint32_t usage,
|
2011-12-21 23:03:26 +04:00
|
|
|
gfxImageFormat format) :
|
|
|
|
mLock("AndroidDirectTexture.mLock")
|
|
|
|
, mNeedFlip(false)
|
|
|
|
, mWidth(width)
|
|
|
|
, mHeight(height)
|
|
|
|
, mFormat(format)
|
|
|
|
, mPendingReallocBuffer(NULL)
|
|
|
|
{
|
|
|
|
mFrontBuffer = new AndroidGraphicBuffer(width, height, usage, format);
|
|
|
|
mBackBuffer = new AndroidGraphicBuffer(width, height, usage, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
AndroidDirectTexture::~AndroidDirectTexture()
|
|
|
|
{
|
|
|
|
if (mFrontBuffer) {
|
|
|
|
delete mFrontBuffer;
|
|
|
|
mFrontBuffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mBackBuffer) {
|
|
|
|
delete mBackBuffer;
|
|
|
|
mBackBuffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AndroidDirectTexture::ReallocPendingBuffer()
|
|
|
|
{
|
|
|
|
// We may have reallocated while the current back buffer was being
|
|
|
|
// used as the front buffer. If we have such a reallocation pending
|
|
|
|
// and the current back buffer is the target buffer, do it now.
|
|
|
|
//
|
|
|
|
// It is assumed that mLock is already acquired
|
|
|
|
if (mPendingReallocBuffer == mBackBuffer) {
|
|
|
|
mBackBuffer->Reallocate(mWidth, mHeight, mFormat);
|
|
|
|
mPendingReallocBuffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
AndroidDirectTexture::Lock(uint32_t aUsage, unsigned char **bits)
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
2012-01-18 00:00:56 +04:00
|
|
|
nsIntRect rect(0, 0, mWidth, mHeight);
|
|
|
|
return Lock(aUsage, rect, bits);
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
AndroidDirectTexture::Lock(uint32_t aUsage, const nsIntRect& aRect, unsigned char **bits)
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
|
|
|
mLock.Lock();
|
|
|
|
ReallocPendingBuffer();
|
2012-01-18 00:00:56 +04:00
|
|
|
|
|
|
|
int result;
|
|
|
|
while ((result = mBackBuffer->Lock(aUsage, aRect, bits)) == -EBUSY) {
|
|
|
|
usleep(1000); // 1ms
|
|
|
|
}
|
|
|
|
|
|
|
|
return result == 0;
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AndroidDirectTexture::Unlock(bool aFlip)
|
|
|
|
{
|
|
|
|
if (aFlip) {
|
|
|
|
mNeedFlip = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = mBackBuffer->Unlock();
|
|
|
|
mLock.Unlock();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
AndroidDirectTexture::Reallocate(uint32_t aWidth, uint32_t aHeight) {
|
2011-12-21 23:03:26 +04:00
|
|
|
return Reallocate(aWidth, aHeight, mFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
AndroidDirectTexture::Reallocate(uint32_t aWidth, uint32_t aHeight, gfxASurface::gfxImageFormat aFormat)
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// We only reallocate the current back buffer. The front buffer is likely
|
|
|
|
// in use, so we'll reallocate it on the first Lock() after it is rotated
|
|
|
|
// to the back.
|
|
|
|
bool result = mBackBuffer->Reallocate(aWidth, aHeight, aFormat);
|
|
|
|
if (result) {
|
|
|
|
mPendingReallocBuffer = mFrontBuffer;
|
2012-01-05 06:53:09 +04:00
|
|
|
|
|
|
|
mWidth = aWidth;
|
|
|
|
mHeight = aHeight;
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-03-16 10:21:54 +04:00
|
|
|
AndroidDirectTexture::Bind()
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (mNeedFlip) {
|
|
|
|
AndroidGraphicBuffer* tmp = mBackBuffer;
|
|
|
|
mBackBuffer = mFrontBuffer;
|
|
|
|
mFrontBuffer = tmp;
|
|
|
|
mNeedFlip = false;
|
|
|
|
}
|
|
|
|
|
2012-03-16 10:21:54 +04:00
|
|
|
return mFrontBuffer->Bind();
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* mozilla */
|