2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
**
|
|
|
|
** Copyright 2007 The Android Open Source Project
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
*/
|
|
|
|
|
2009-05-04 21:17:04 +00:00
|
|
|
#define LOG_TAG "FramebufferNativeWindow"
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2009-04-10 21:24:30 +00:00
|
|
|
#include <errno.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <cutils/log.h>
|
|
|
|
#include <cutils/atomic.h>
|
2009-04-10 21:24:30 +00:00
|
|
|
#include <utils/threads.h>
|
2009-08-07 03:46:44 +00:00
|
|
|
#include <utils/RefBase.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <ui/Rect.h>
|
2009-05-04 21:17:04 +00:00
|
|
|
#include <ui/FramebufferNativeWindow.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
|
|
|
|
#include <pixelflinger/format.h>
|
2009-04-10 21:24:30 +00:00
|
|
|
#include <pixelflinger/pixelflinger.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
#include <hardware/hardware.h>
|
|
|
|
#include <hardware/gralloc.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-05-06 01:21:32 +00:00
|
|
|
#include <private/ui/android_natives_priv.h>
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-05-06 01:11:11 +00:00
|
|
|
class NativeBuffer
|
|
|
|
: public EGLNativeBase<
|
|
|
|
android_native_buffer_t,
|
|
|
|
NativeBuffer,
|
|
|
|
LightRefBase<NativeBuffer> >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NativeBuffer(int w, int h, int f, int u) : BASE() {
|
|
|
|
android_native_buffer_t::width = w;
|
|
|
|
android_native_buffer_t::height = h;
|
|
|
|
android_native_buffer_t::format = f;
|
|
|
|
android_native_buffer_t::usage = u;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
friend class LightRefBase<NativeBuffer>;
|
|
|
|
~NativeBuffer() { }; // this class cannot be overloaded
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
/*
|
|
|
|
* This implements the (main) framebuffer management. This class is used
|
|
|
|
* mostly by SurfaceFlinger, but also by command line GL application.
|
|
|
|
*
|
|
|
|
* In fact this is an implementation of android_native_window_t on top of
|
|
|
|
* the framebuffer.
|
|
|
|
*
|
|
|
|
* Currently it is pretty simple, it manages only two buffers (the front and
|
|
|
|
* back buffer).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
FramebufferNativeWindow::FramebufferNativeWindow()
|
2009-05-08 00:40:23 +00:00
|
|
|
: BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-04-10 21:24:30 +00:00
|
|
|
hw_module_t const* module;
|
|
|
|
if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
|
|
|
|
int stride;
|
|
|
|
int err;
|
2010-11-03 20:16:18 +00:00
|
|
|
int i;
|
2009-08-07 03:46:44 +00:00
|
|
|
err = framebuffer_open(module, &fbDev);
|
|
|
|
LOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
|
|
|
|
|
|
|
|
err = gralloc_open(module, &grDev);
|
|
|
|
LOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2009-08-07 03:46:44 +00:00
|
|
|
// bail out if we can't initialize the modules
|
|
|
|
if (!fbDev || !grDev)
|
|
|
|
return;
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2009-05-08 00:40:23 +00:00
|
|
|
mUpdateOnDemand = (fbDev->setUpdateRect != 0);
|
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
// initialize the buffer FIFO
|
2010-11-03 20:16:18 +00:00
|
|
|
mNumBuffers = NUM_FRAME_BUFFERS;
|
|
|
|
mNumFreeBuffers = NUM_FRAME_BUFFERS;
|
2009-04-10 21:24:30 +00:00
|
|
|
mBufferHead = mNumBuffers-1;
|
|
|
|
|
2010-11-03 20:16:18 +00:00
|
|
|
for (i = 0; i < mNumBuffers; i++)
|
|
|
|
{
|
|
|
|
buffers[i] = new NativeBuffer(
|
|
|
|
fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < mNumBuffers; i++)
|
|
|
|
{
|
|
|
|
err = grDev->alloc(grDev,
|
|
|
|
fbDev->width, fbDev->height, fbDev->format,
|
|
|
|
GRALLOC_USAGE_HW_FB, &buffers[i]->handle, &buffers[i]->stride);
|
|
|
|
|
|
|
|
LOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
|
|
|
|
i, fbDev->width, fbDev->height, strerror(-err));
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
mNumBuffers = i;
|
|
|
|
mNumFreeBuffers = i;
|
|
|
|
mBufferHead = mNumBuffers-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2009-09-23 17:54:36 +00:00
|
|
|
const_cast<uint32_t&>(android_native_window_t::flags) = fbDev->flags;
|
|
|
|
const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi;
|
|
|
|
const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi;
|
|
|
|
const_cast<int&>(android_native_window_t::minSwapInterval) =
|
|
|
|
fbDev->minSwapInterval;
|
|
|
|
const_cast<int&>(android_native_window_t::maxSwapInterval) =
|
|
|
|
fbDev->maxSwapInterval;
|
|
|
|
} else {
|
|
|
|
LOGE("Couldn't get gralloc module");
|
|
|
|
}
|
2009-04-10 21:24:30 +00:00
|
|
|
|
|
|
|
android_native_window_t::setSwapInterval = setSwapInterval;
|
|
|
|
android_native_window_t::dequeueBuffer = dequeueBuffer;
|
|
|
|
android_native_window_t::lockBuffer = lockBuffer;
|
|
|
|
android_native_window_t::queueBuffer = queueBuffer;
|
2009-07-31 01:14:56 +00:00
|
|
|
android_native_window_t::query = query;
|
2009-08-12 05:34:02 +00:00
|
|
|
android_native_window_t::perform = perform;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 03:46:44 +00:00
|
|
|
FramebufferNativeWindow::~FramebufferNativeWindow()
|
|
|
|
{
|
|
|
|
if (grDev) {
|
|
|
|
if (buffers[0] != NULL)
|
|
|
|
grDev->free(grDev, buffers[0]->handle);
|
|
|
|
if (buffers[1] != NULL)
|
|
|
|
grDev->free(grDev, buffers[1]->handle);
|
|
|
|
gralloc_close(grDev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fbDev) {
|
|
|
|
framebuffer_close(fbDev);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-05-08 00:40:23 +00:00
|
|
|
status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
|
|
|
|
{
|
|
|
|
if (!mUpdateOnDemand) {
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
|
|
|
|
}
|
|
|
|
|
2009-09-17 23:18:16 +00:00
|
|
|
status_t FramebufferNativeWindow::compositionComplete()
|
|
|
|
{
|
|
|
|
if (fbDev->compositionComplete) {
|
|
|
|
return fbDev->compositionComplete(fbDev);
|
|
|
|
}
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
int FramebufferNativeWindow::setSwapInterval(
|
|
|
|
android_native_window_t* window, int interval)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-04-10 21:24:30 +00:00
|
|
|
framebuffer_device_t* fb = getSelf(window)->fbDev;
|
|
|
|
return fb->setSwapInterval(fb, interval);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window,
|
|
|
|
android_native_buffer_t** buffer)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-04-10 21:24:30 +00:00
|
|
|
FramebufferNativeWindow* self = getSelf(window);
|
|
|
|
Mutex::Autolock _l(self->mutex);
|
|
|
|
framebuffer_device_t* fb = self->fbDev;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
// wait for a free buffer
|
|
|
|
while (!self->mNumFreeBuffers) {
|
|
|
|
self->mCondition.wait(self->mutex);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-04-10 21:24:30 +00:00
|
|
|
// get this buffer
|
|
|
|
self->mNumFreeBuffers--;
|
|
|
|
int index = self->mBufferHead++;
|
|
|
|
if (self->mBufferHead >= self->mNumBuffers)
|
|
|
|
self->mBufferHead = 0;
|
|
|
|
|
|
|
|
*buffer = self->buffers[index].get();
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
int FramebufferNativeWindow::lockBuffer(android_native_window_t* window,
|
|
|
|
android_native_buffer_t* buffer)
|
|
|
|
{
|
|
|
|
FramebufferNativeWindow* self = getSelf(window);
|
|
|
|
Mutex::Autolock _l(self->mutex);
|
|
|
|
|
|
|
|
// wait that the buffer we're locking is not front anymore
|
|
|
|
while (self->front == buffer) {
|
|
|
|
self->mCondition.wait(self->mutex);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2009-05-04 21:17:04 +00:00
|
|
|
return NO_ERROR;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
int FramebufferNativeWindow::queueBuffer(android_native_window_t* window,
|
|
|
|
android_native_buffer_t* buffer)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-04-10 21:24:30 +00:00
|
|
|
FramebufferNativeWindow* self = getSelf(window);
|
|
|
|
Mutex::Autolock _l(self->mutex);
|
|
|
|
framebuffer_device_t* fb = self->fbDev;
|
|
|
|
buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
|
|
|
|
int res = fb->post(fb, handle);
|
|
|
|
self->front = static_cast<NativeBuffer*>(buffer);
|
|
|
|
self->mNumFreeBuffers++;
|
|
|
|
self->mCondition.broadcast();
|
|
|
|
return res;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 01:14:56 +00:00
|
|
|
int FramebufferNativeWindow::query(android_native_window_t* window,
|
|
|
|
int what, int* value)
|
|
|
|
{
|
|
|
|
FramebufferNativeWindow* self = getSelf(window);
|
|
|
|
Mutex::Autolock _l(self->mutex);
|
|
|
|
framebuffer_device_t* fb = self->fbDev;
|
|
|
|
switch (what) {
|
|
|
|
case NATIVE_WINDOW_WIDTH:
|
|
|
|
*value = fb->width;
|
|
|
|
return NO_ERROR;
|
|
|
|
case NATIVE_WINDOW_HEIGHT:
|
|
|
|
*value = fb->height;
|
|
|
|
return NO_ERROR;
|
2009-08-06 23:04:29 +00:00
|
|
|
case NATIVE_WINDOW_FORMAT:
|
|
|
|
*value = fb->format;
|
|
|
|
return NO_ERROR;
|
2009-07-31 01:14:56 +00:00
|
|
|
}
|
2009-08-07 03:46:44 +00:00
|
|
|
*value = 0;
|
2009-07-31 01:14:56 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2009-08-12 05:34:02 +00:00
|
|
|
int FramebufferNativeWindow::perform(android_native_window_t* window,
|
|
|
|
int operation, ...)
|
|
|
|
{
|
|
|
|
switch (operation) {
|
|
|
|
case NATIVE_WINDOW_SET_USAGE:
|
2010-03-11 23:06:54 +00:00
|
|
|
case NATIVE_WINDOW_CONNECT:
|
|
|
|
case NATIVE_WINDOW_DISCONNECT:
|
2009-08-12 05:34:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
// ----------------------------------------------------------------------------
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2009-08-07 03:46:44 +00:00
|
|
|
using namespace android;
|
2009-04-10 21:24:30 +00:00
|
|
|
|
|
|
|
EGLNativeWindowType android_createDisplaySurface(void)
|
|
|
|
{
|
2009-08-07 03:46:44 +00:00
|
|
|
FramebufferNativeWindow* w;
|
|
|
|
w = new FramebufferNativeWindow();
|
|
|
|
if (w->getDevice() == NULL) {
|
|
|
|
// get a ref so it can be destroyed when we exit this block
|
|
|
|
sp<FramebufferNativeWindow> ref(w);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (EGLNativeWindowType)w;
|
2009-04-10 21:24:30 +00:00
|
|
|
}
|