blob: dc223f96453af329990cd4e2e5cc96090ac302e0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2007 The Android Open Source Project
4**
5** Licensed under the Apache License Version 2.0(the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing software
12** distributed under the License is distributed on an "AS IS" BASIS
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
Mathias Agopiandff8e582009-05-04 14:17:04 -070018#define LOG_TAG "FramebufferNativeWindow"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070023#include <errno.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <cutils/log.h>
26#include <cutils/atomic.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070027#include <utils/threads.h>
Mathias Agopian6693f232009-08-06 20:46:44 -070028#include <utils/RefBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include <ui/Rect.h>
Mathias Agopiandff8e582009-05-04 14:17:04 -070031#include <ui/FramebufferNativeWindow.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070032#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34#include <EGL/egl.h>
35
36#include <pixelflinger/format.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070037#include <pixelflinger/pixelflinger.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Mathias Agopian1473f462009-04-10 14:24:30 -070039#include <hardware/hardware.h>
40#include <hardware/gralloc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Mathias Agopianb51e18d2009-05-05 18:21:32 -070042#include <private/ui/android_natives_priv.h>
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044// ----------------------------------------------------------------------------
45namespace android {
46// ----------------------------------------------------------------------------
47
Mathias Agopianac2523b2009-05-05 18:11:11 -070048class NativeBuffer
49 : public EGLNativeBase<
50 android_native_buffer_t,
51 NativeBuffer,
52 LightRefBase<NativeBuffer> >
53{
54public:
55 NativeBuffer(int w, int h, int f, int u) : BASE() {
56 android_native_buffer_t::width = w;
57 android_native_buffer_t::height = h;
58 android_native_buffer_t::format = f;
59 android_native_buffer_t::usage = u;
60 }
61private:
62 friend class LightRefBase<NativeBuffer>;
63 ~NativeBuffer() { }; // this class cannot be overloaded
64};
65
66
Mathias Agopian1473f462009-04-10 14:24:30 -070067/*
68 * This implements the (main) framebuffer management. This class is used
69 * mostly by SurfaceFlinger, but also by command line GL application.
70 *
Dianne Hackborn8b49bd12010-06-30 13:56:17 -070071 * In fact this is an implementation of ANativeWindow on top of
Mathias Agopian1473f462009-04-10 14:24:30 -070072 * the framebuffer.
73 *
74 * Currently it is pretty simple, it manages only two buffers (the front and
75 * back buffer).
76 *
77 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
Mathias Agopian1473f462009-04-10 14:24:30 -070079FramebufferNativeWindow::FramebufferNativeWindow()
Mathias Agopian97b80562009-05-07 17:40:23 -070080 : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081{
Mathias Agopian1473f462009-04-10 14:24:30 -070082 hw_module_t const* module;
83 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
84 int stride;
Mathias Agopian1473f462009-04-10 14:24:30 -070085 int err;
Rodrigo Obregon84043432010-11-03 15:16:18 -050086 int i;
Mathias Agopian6693f232009-08-06 20:46:44 -070087 err = framebuffer_open(module, &fbDev);
88 LOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
89
90 err = gralloc_open(module, &grDev);
91 LOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Mathias Agopian6693f232009-08-06 20:46:44 -070093 // bail out if we can't initialize the modules
94 if (!fbDev || !grDev)
95 return;
Mathias Agopian1473f462009-04-10 14:24:30 -070096
Mathias Agopian97b80562009-05-07 17:40:23 -070097 mUpdateOnDemand = (fbDev->setUpdateRect != 0);
98
Mathias Agopian1473f462009-04-10 14:24:30 -070099 // initialize the buffer FIFO
Rodrigo Obregon84043432010-11-03 15:16:18 -0500100 mNumBuffers = NUM_FRAME_BUFFERS;
101 mNumFreeBuffers = NUM_FRAME_BUFFERS;
Mathias Agopian1473f462009-04-10 14:24:30 -0700102 mBufferHead = mNumBuffers-1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Rodrigo Obregon84043432010-11-03 15:16:18 -0500104 for (i = 0; i < mNumBuffers; i++)
105 {
106 buffers[i] = new NativeBuffer(
107 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
Rodrigo Obregon84043432010-11-03 15:16:18 -0500110 for (i = 0; i < mNumBuffers; i++)
111 {
112 err = grDev->alloc(grDev,
113 fbDev->width, fbDev->height, fbDev->format,
114 GRALLOC_USAGE_HW_FB, &buffers[i]->handle, &buffers[i]->stride);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Rodrigo Obregon84043432010-11-03 15:16:18 -0500116 LOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
117 i, fbDev->width, fbDev->height, strerror(-err));
118
119 if (err)
120 {
121 mNumBuffers = i;
122 mNumFreeBuffers = i;
123 mBufferHead = mNumBuffers-1;
124 break;
125 }
126 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700127
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700128 const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
129 const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
130 const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
131 const_cast<int&>(ANativeWindow::minSwapInterval) =
Marco Nelissene6134682009-09-23 10:54:36 -0700132 fbDev->minSwapInterval;
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700133 const_cast<int&>(ANativeWindow::maxSwapInterval) =
Marco Nelissene6134682009-09-23 10:54:36 -0700134 fbDev->maxSwapInterval;
135 } else {
136 LOGE("Couldn't get gralloc module");
137 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700138
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700139 ANativeWindow::setSwapInterval = setSwapInterval;
140 ANativeWindow::dequeueBuffer = dequeueBuffer;
141 ANativeWindow::lockBuffer = lockBuffer;
142 ANativeWindow::queueBuffer = queueBuffer;
143 ANativeWindow::query = query;
144 ANativeWindow::perform = perform;
Mathias Agopian1473f462009-04-10 14:24:30 -0700145}
146
Mathias Agopian6693f232009-08-06 20:46:44 -0700147FramebufferNativeWindow::~FramebufferNativeWindow()
148{
149 if (grDev) {
150 if (buffers[0] != NULL)
151 grDev->free(grDev, buffers[0]->handle);
152 if (buffers[1] != NULL)
153 grDev->free(grDev, buffers[1]->handle);
154 gralloc_close(grDev);
155 }
156
157 if (fbDev) {
158 framebuffer_close(fbDev);
159 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700160}
161
Mathias Agopian97b80562009-05-07 17:40:23 -0700162status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
163{
164 if (!mUpdateOnDemand) {
165 return INVALID_OPERATION;
166 }
167 return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
168}
169
Mathias Agopianb1a18742009-09-17 16:18:16 -0700170status_t FramebufferNativeWindow::compositionComplete()
171{
172 if (fbDev->compositionComplete) {
173 return fbDev->compositionComplete(fbDev);
174 }
175 return INVALID_OPERATION;
176}
177
Mathias Agopian1473f462009-04-10 14:24:30 -0700178int FramebufferNativeWindow::setSwapInterval(
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700179 ANativeWindow* window, int interval)
Mathias Agopian1473f462009-04-10 14:24:30 -0700180{
181 framebuffer_device_t* fb = getSelf(window)->fbDev;
182 return fb->setSwapInterval(fb, interval);
183}
184
Erik Gilling94720d72010-12-01 16:38:01 -0800185void FramebufferNativeWindow::dump(String8& result) {
186 if (fbDev->common.version >= 1 && fbDev->dump) {
187 const size_t SIZE = 4096;
188 char buffer[SIZE];
189
190 fbDev->dump(fbDev, buffer, SIZE);
191 result.append(buffer);
192 }
193}
194
Mathias Agopian04262e92010-09-13 22:57:58 -0700195// only for debugging / logging
196int FramebufferNativeWindow::getCurrentBufferIndex() const
197{
198 Mutex::Autolock _l(mutex);
199 const int index = mCurrentBufferIndex;
200 return index;
201}
202
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700203int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window,
Mathias Agopian1473f462009-04-10 14:24:30 -0700204 android_native_buffer_t** buffer)
205{
206 FramebufferNativeWindow* self = getSelf(window);
207 Mutex::Autolock _l(self->mutex);
208 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
Mathias Agopian04262e92010-09-13 22:57:58 -0700210 int index = self->mBufferHead++;
211 if (self->mBufferHead >= self->mNumBuffers)
212 self->mBufferHead = 0;
213
214 GraphicLog& logger(GraphicLog::getInstance());
215 logger.log(GraphicLog::SF_FB_DEQUEUE_BEFORE, index);
216
Mathias Agopian1473f462009-04-10 14:24:30 -0700217 // wait for a free buffer
218 while (!self->mNumFreeBuffers) {
219 self->mCondition.wait(self->mutex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700221 // get this buffer
222 self->mNumFreeBuffers--;
Mathias Agopian04262e92010-09-13 22:57:58 -0700223 self->mCurrentBufferIndex = index;
Mathias Agopian1473f462009-04-10 14:24:30 -0700224
225 *buffer = self->buffers[index].get();
226
Mathias Agopian04262e92010-09-13 22:57:58 -0700227 logger.log(GraphicLog::SF_FB_DEQUEUE_AFTER, index);
Mathias Agopian1473f462009-04-10 14:24:30 -0700228 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229}
230
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700231int FramebufferNativeWindow::lockBuffer(ANativeWindow* window,
Mathias Agopian1473f462009-04-10 14:24:30 -0700232 android_native_buffer_t* buffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233{
Mathias Agopian1473f462009-04-10 14:24:30 -0700234 FramebufferNativeWindow* self = getSelf(window);
235 Mutex::Autolock _l(self->mutex);
236
Mathias Agopian04262e92010-09-13 22:57:58 -0700237 const int index = self->mCurrentBufferIndex;
238 GraphicLog& logger(GraphicLog::getInstance());
239 logger.log(GraphicLog::SF_FB_LOCK_BEFORE, index);
240
Mathias Agopian1473f462009-04-10 14:24:30 -0700241 // wait that the buffer we're locking is not front anymore
242 while (self->front == buffer) {
243 self->mCondition.wait(self->mutex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700245
Mathias Agopian04262e92010-09-13 22:57:58 -0700246 logger.log(GraphicLog::SF_FB_LOCK_AFTER, index);
247
Mathias Agopiandff8e582009-05-04 14:17:04 -0700248 return NO_ERROR;
Mathias Agopian1473f462009-04-10 14:24:30 -0700249}
250
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700251int FramebufferNativeWindow::queueBuffer(ANativeWindow* window,
Mathias Agopian1473f462009-04-10 14:24:30 -0700252 android_native_buffer_t* buffer)
253{
254 FramebufferNativeWindow* self = getSelf(window);
255 Mutex::Autolock _l(self->mutex);
256 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian1473f462009-04-10 14:24:30 -0700257 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian04262e92010-09-13 22:57:58 -0700258
259 const int index = self->mCurrentBufferIndex;
260 GraphicLog& logger(GraphicLog::getInstance());
261 logger.log(GraphicLog::SF_FB_POST_BEFORE, index);
262
Mathias Agopian1473f462009-04-10 14:24:30 -0700263 int res = fb->post(fb, handle);
Mathias Agopian04262e92010-09-13 22:57:58 -0700264
265 logger.log(GraphicLog::SF_FB_POST_AFTER, index);
266
Mathias Agopian1473f462009-04-10 14:24:30 -0700267 self->front = static_cast<NativeBuffer*>(buffer);
268 self->mNumFreeBuffers++;
269 self->mCondition.broadcast();
270 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271}
272
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700273int FramebufferNativeWindow::query(ANativeWindow* window,
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700274 int what, int* value)
275{
276 FramebufferNativeWindow* self = getSelf(window);
277 Mutex::Autolock _l(self->mutex);
278 framebuffer_device_t* fb = self->fbDev;
279 switch (what) {
280 case NATIVE_WINDOW_WIDTH:
281 *value = fb->width;
282 return NO_ERROR;
283 case NATIVE_WINDOW_HEIGHT:
284 *value = fb->height;
285 return NO_ERROR;
Mathias Agopian25ec00f2009-08-06 16:04:29 -0700286 case NATIVE_WINDOW_FORMAT:
287 *value = fb->format;
288 return NO_ERROR;
Jamie Gennisc4ca7c52011-03-14 15:00:06 -0700289 case NATIVE_WINDOW_CONCRETE_TYPE:
290 *value = NATIVE_WINDOW_FRAMEBUFFER;
291 return NO_ERROR;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700292 }
Mathias Agopian6693f232009-08-06 20:46:44 -0700293 *value = 0;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700294 return BAD_VALUE;
295}
296
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700297int FramebufferNativeWindow::perform(ANativeWindow* window,
Mathias Agopian5cec4742009-08-11 22:34:02 -0700298 int operation, ...)
299{
300 switch (operation) {
301 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800302 case NATIVE_WINDOW_CONNECT:
303 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian5cec4742009-08-11 22:34:02 -0700304 break;
305 default:
306 return NAME_NOT_FOUND;
307 }
308 return NO_ERROR;
309}
310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311// ----------------------------------------------------------------------------
312}; // namespace android
313// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -0700314
Mathias Agopian6693f232009-08-06 20:46:44 -0700315using namespace android;
Mathias Agopian1473f462009-04-10 14:24:30 -0700316
317EGLNativeWindowType android_createDisplaySurface(void)
318{
Mathias Agopian6693f232009-08-06 20:46:44 -0700319 FramebufferNativeWindow* w;
320 w = new FramebufferNativeWindow();
321 if (w->getDevice() == NULL) {
322 // get a ref so it can be destroyed when we exit this block
323 sp<FramebufferNativeWindow> ref(w);
324 return NULL;
325 }
326 return (EGLNativeWindowType)w;
Mathias Agopian1473f462009-04-10 14:24:30 -0700327}