2014-12-18 00:30:00 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
2014-12-22 00:01:00 +03:00
|
|
|
* Copyright (C) 2014 Mozilla Foundation
|
2014-12-18 00:30:00 +03:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define LOG_NDEBUG 0
|
2014-12-22 00:01:00 +03:00
|
|
|
#define LOG_TAG "GonkNativeWindow"
|
2014-12-18 00:30:00 +03:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
#include "GonkNativeWindowLL.h"
|
2014-12-18 00:30:00 +03:00
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::layers;
|
2014-12-18 00:30:00 +03:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
GonkNativeWindow::GonkNativeWindow(
|
|
|
|
const sp<IGonkGraphicBufferConsumer>& consumer, int bufferCount) :
|
|
|
|
GonkConsumerBase(consumer, false),
|
|
|
|
mNewFrameCallback(nullptr)
|
|
|
|
{
|
|
|
|
if (bufferCount != DEFAULT_MAX_BUFFERS) {
|
|
|
|
status_t err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
|
|
|
|
LOG_ALWAYS_FATAL_IF(err != OK,
|
|
|
|
"Failed to set max acquired buffer count to %d", bufferCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GonkNativeWindow::GonkNativeWindow(
|
|
|
|
const sp<IGonkGraphicBufferConsumer>& consumer, uint32_t consumerUsage,
|
2014-12-18 00:30:00 +03:00
|
|
|
int bufferCount, bool controlledByApp) :
|
2014-12-22 00:01:00 +03:00
|
|
|
GonkConsumerBase(consumer, controlledByApp)
|
2014-12-18 00:30:00 +03:00
|
|
|
{
|
|
|
|
status_t err = mConsumer->setConsumerUsageBits(consumerUsage);
|
|
|
|
LOG_ALWAYS_FATAL_IF(err != OK,
|
|
|
|
"Failed to set consumer usage bits to %#x", consumerUsage);
|
|
|
|
if (bufferCount != DEFAULT_MAX_BUFFERS) {
|
|
|
|
err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
|
|
|
|
LOG_ALWAYS_FATAL_IF(err != OK,
|
|
|
|
"Failed to set max acquired buffer count to %d", bufferCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
GonkNativeWindow::~GonkNativeWindow() {
|
2014-12-18 00:30:00 +03:00
|
|
|
}
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
void GonkNativeWindow::setName(const String8& name) {
|
2014-12-18 00:30:00 +03:00
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
mName = name;
|
|
|
|
mConsumer->setConsumerName(name);
|
|
|
|
}
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
status_t GonkNativeWindow::acquireBuffer(BufferItem *item,
|
2014-12-18 00:30:00 +03:00
|
|
|
nsecs_t presentWhen, bool waitForFence) {
|
|
|
|
status_t err;
|
|
|
|
|
|
|
|
if (!item) return BAD_VALUE;
|
|
|
|
|
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
|
|
|
|
err = acquireBufferLocked(item, presentWhen);
|
|
|
|
if (err != OK) {
|
|
|
|
if (err != NO_BUFFER_AVAILABLE) {
|
2014-12-22 00:01:00 +03:00
|
|
|
ALOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
|
2014-12-18 00:30:00 +03:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (waitForFence) {
|
2014-12-22 00:01:00 +03:00
|
|
|
err = item->mFence->waitForever("GonkNativeWindow::acquireBuffer");
|
2014-12-18 00:30:00 +03:00
|
|
|
if (err != OK) {
|
2014-12-22 00:01:00 +03:00
|
|
|
ALOGE("Failed to wait for fence of acquired buffer: %s (%d)",
|
2014-12-18 00:30:00 +03:00
|
|
|
strerror(-err), err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item->mGraphicBuffer = mSlots[item->mBuf].mGraphicBuffer;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
status_t GonkNativeWindow::releaseBuffer(const BufferItem &item,
|
2014-12-18 00:30:00 +03:00
|
|
|
const sp<Fence>& releaseFence) {
|
|
|
|
status_t err;
|
|
|
|
|
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
|
|
|
|
err = addReleaseFenceLocked(item.mBuf, item.mGraphicBuffer, releaseFence);
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
err = releaseBufferLocked(item.mBuf, item.mGraphicBuffer);
|
2014-12-18 00:30:00 +03:00
|
|
|
if (err != OK) {
|
2014-12-22 00:01:00 +03:00
|
|
|
ALOGE("Failed to release buffer: %s (%d)",
|
2014-12-18 00:30:00 +03:00
|
|
|
strerror(-err), err);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
status_t GonkNativeWindow::setDefaultBufferSize(uint32_t w, uint32_t h) {
|
2014-12-18 00:30:00 +03:00
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
return mConsumer->setDefaultBufferSize(w, h);
|
|
|
|
}
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
status_t GonkNativeWindow::setDefaultBufferFormat(uint32_t defaultFormat) {
|
2014-12-18 00:30:00 +03:00
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
return mConsumer->setDefaultBufferFormat(defaultFormat);
|
|
|
|
}
|
|
|
|
|
2014-12-22 00:01:00 +03:00
|
|
|
TemporaryRef<TextureClient>
|
|
|
|
GonkNativeWindow::getCurrentBuffer() {
|
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
BufferItem item;
|
|
|
|
|
|
|
|
// In asynchronous mode the list is guaranteed to be one buffer
|
|
|
|
// deep, while in synchronous mode we use the oldest buffer.
|
|
|
|
status_t err = acquireBufferLocked(&item, 0); //???
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<TextureClient> textureClient =
|
|
|
|
mConsumer->getTextureClientFromBuffer(item.mGraphicBuffer.get());
|
|
|
|
if (!textureClient) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
textureClient->SetRecycleCallback(GonkNativeWindow::RecycleCallback, this);
|
2015-05-01 16:14:16 +03:00
|
|
|
return textureClient.forget();
|
2014-12-22 00:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
GonkNativeWindow::RecycleCallback(TextureClient* client, void* closure) {
|
|
|
|
GonkNativeWindow* nativeWindow =
|
|
|
|
static_cast<GonkNativeWindow*>(closure);
|
|
|
|
|
2015-05-29 23:41:28 +03:00
|
|
|
MOZ_ASSERT(client && !client->IsDead());
|
2014-12-22 00:01:00 +03:00
|
|
|
client->ClearRecycleCallback();
|
|
|
|
nativeWindow->returnBuffer(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GonkNativeWindow::returnBuffer(TextureClient* client) {
|
|
|
|
ALOGD("GonkNativeWindow::returnBuffer");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
int index = mConsumer->getSlotFromTextureClientLocked(client);
|
|
|
|
if (index < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-13 02:42:00 +03:00
|
|
|
FenceHandle handle = client->GetAndResetReleaseFenceHandle();
|
|
|
|
nsRefPtr<FenceHandle::FdObj> fdObj = handle.GetAndResetFdObj();
|
|
|
|
sp<Fence> fence = new Fence(fdObj->GetAndResetFd());
|
2014-12-22 00:01:00 +03:00
|
|
|
|
|
|
|
status_t err;
|
|
|
|
err = addReleaseFenceLocked(index,
|
|
|
|
mSlots[index].mGraphicBuffer,
|
|
|
|
fence);
|
|
|
|
|
|
|
|
err = releaseBufferLocked(index, mSlots[index].mGraphicBuffer);
|
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
ALOGE("Failed to return buffer: %s (%d)", strerror(-err), err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<TextureClient>
|
|
|
|
GonkNativeWindow::getTextureClientFromBuffer(ANativeWindowBuffer* buffer) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mConsumer->getTextureClientFromBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GonkNativeWindow::setNewFrameCallback(
|
|
|
|
GonkNativeWindowNewFrameCallback* callback) {
|
|
|
|
ALOGD("setNewFrameCallback");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mNewFrameCallback = callback;
|
|
|
|
}
|
|
|
|
|
2015-03-19 01:03:15 +03:00
|
|
|
#if ANDROID_VERSION == 21
|
2014-12-22 00:01:00 +03:00
|
|
|
void GonkNativeWindow::onFrameAvailable() {
|
|
|
|
GonkConsumerBase::onFrameAvailable();
|
2015-03-19 01:03:15 +03:00
|
|
|
#else
|
|
|
|
void GonkNativeWindow::onFrameAvailable(const ::android::BufferItem &item) {
|
|
|
|
GonkConsumerBase::onFrameAvailable(item);
|
|
|
|
#endif
|
2014-12-22 00:01:00 +03:00
|
|
|
|
|
|
|
if (mNewFrameCallback) {
|
|
|
|
mNewFrameCallback->OnNewFrame();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 00:30:00 +03:00
|
|
|
} // namespace android
|