blob: a1f204c065e50fa6bcadf92b0cc612a9b0762a0e [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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 Agopian0926f502009-05-04 14:17:04 -070018#define LOG_TAG "FramebufferNativeWindow"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070023#include <errno.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
25#include <cutils/log.h>
26#include <cutils/atomic.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#include <utils/threads.h>
Mathias Agopian42db9dc2009-08-06 20:46:44 -070028#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Mathias Agopian5f2165f2012-02-24 18:25:41 -080030#include <ui/ANativeObjectBase.h>
Mathias Agopian0926f502009-05-04 14:17:04 -070031#include <ui/FramebufferNativeWindow.h>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080032#include <ui/Rect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include <EGL/egl.h>
35
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <hardware/hardware.h>
37#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
39// ----------------------------------------------------------------------------
40namespace android {
41// ----------------------------------------------------------------------------
42
Mathias Agopian7189c002009-05-05 18:11:11 -070043class NativeBuffer
Mathias Agopian5f2165f2012-02-24 18:25:41 -080044 : public ANativeObjectBase<
Iliyan Malchev697526b2011-05-01 11:33:26 -070045 ANativeWindowBuffer,
Mathias Agopian7189c002009-05-05 18:11:11 -070046 NativeBuffer,
47 LightRefBase<NativeBuffer> >
48{
49public:
50 NativeBuffer(int w, int h, int f, int u) : BASE() {
Iliyan Malchev697526b2011-05-01 11:33:26 -070051 ANativeWindowBuffer::width = w;
52 ANativeWindowBuffer::height = h;
53 ANativeWindowBuffer::format = f;
54 ANativeWindowBuffer::usage = u;
Mathias Agopian7189c002009-05-05 18:11:11 -070055 }
56private:
57 friend class LightRefBase<NativeBuffer>;
58 ~NativeBuffer() { }; // this class cannot be overloaded
59};
60
61
Mathias Agopian076b1cc2009-04-10 14:24:30 -070062/*
63 * This implements the (main) framebuffer management. This class is used
64 * mostly by SurfaceFlinger, but also by command line GL application.
65 *
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -070066 * In fact this is an implementation of ANativeWindow on top of
Mathias Agopian076b1cc2009-04-10 14:24:30 -070067 * the framebuffer.
68 *
69 * Currently it is pretty simple, it manages only two buffers (the front and
70 * back buffer).
71 *
72 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074FramebufferNativeWindow::FramebufferNativeWindow()
Mathias Agopian1e16b132009-05-07 17:40:23 -070075 : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077 hw_module_t const* module;
78 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
79 int stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070080 int err;
Rodrigo Obregon71484f22010-11-03 15:16:18 -050081 int i;
Mathias Agopian42db9dc2009-08-06 20:46:44 -070082 err = framebuffer_open(module, &fbDev);
Steve Blocke6f43dd2012-01-06 19:20:56 +000083 ALOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
Mathias Agopian42db9dc2009-08-06 20:46:44 -070084
85 err = gralloc_open(module, &grDev);
Steve Blocke6f43dd2012-01-06 19:20:56 +000086 ALOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087
Mathias Agopian42db9dc2009-08-06 20:46:44 -070088 // bail out if we can't initialize the modules
89 if (!fbDev || !grDev)
90 return;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091
Mathias Agopian1e16b132009-05-07 17:40:23 -070092 mUpdateOnDemand = (fbDev->setUpdateRect != 0);
93
Mathias Agopian076b1cc2009-04-10 14:24:30 -070094 // initialize the buffer FIFO
Naseer Ahmed0bc64be2012-06-29 12:02:32 -070095 if(fbDev->numFramebuffers >= MIN_NUM_FRAME_BUFFERS &&
96 fbDev->numFramebuffers <= MAX_NUM_FRAME_BUFFERS){
97 mNumBuffers = fbDev->numFramebuffers;
98 } else {
99 mNumBuffers = MIN_NUM_FRAME_BUFFERS;
100 }
101 mNumFreeBuffers = mNumBuffers;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102 mBufferHead = mNumBuffers-1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103
Dima Zavinc6cd27c2012-02-22 14:37:57 -0800104 /*
105 * This does not actually change the framebuffer format. It merely
106 * fakes this format to surfaceflinger so that when it creates
107 * framebuffer surfaces it will use this format. It's really a giant
108 * HACK to allow interworking with buggy gralloc+GPU driver
109 * implementations. You should *NEVER* need to set this for shipping
110 * devices.
111 */
112#ifdef FRAMEBUFFER_FORCE_FORMAT
113 *((uint32_t *)&fbDev->format) = FRAMEBUFFER_FORCE_FORMAT;
114#endif
115
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500116 for (i = 0; i < mNumBuffers; i++)
117 {
118 buffers[i] = new NativeBuffer(
119 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
120 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500122 for (i = 0; i < mNumBuffers; i++)
123 {
124 err = grDev->alloc(grDev,
125 fbDev->width, fbDev->height, fbDev->format,
126 GRALLOC_USAGE_HW_FB, &buffers[i]->handle, &buffers[i]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127
Steve Blocke6f43dd2012-01-06 19:20:56 +0000128 ALOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500129 i, fbDev->width, fbDev->height, strerror(-err));
130
131 if (err)
132 {
133 mNumBuffers = i;
134 mNumFreeBuffers = i;
135 mBufferHead = mNumBuffers-1;
136 break;
137 }
138 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700140 const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
141 const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
142 const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
143 const_cast<int&>(ANativeWindow::minSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700144 fbDev->minSwapInterval;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700145 const_cast<int&>(ANativeWindow::maxSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700146 fbDev->maxSwapInterval;
147 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000148 ALOGE("Couldn't get gralloc module");
Marco Nelissena4557932009-09-23 10:54:36 -0700149 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700150
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700151 ANativeWindow::setSwapInterval = setSwapInterval;
152 ANativeWindow::dequeueBuffer = dequeueBuffer;
153 ANativeWindow::lockBuffer = lockBuffer;
154 ANativeWindow::queueBuffer = queueBuffer;
155 ANativeWindow::query = query;
156 ANativeWindow::perform = perform;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157}
158
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700159FramebufferNativeWindow::~FramebufferNativeWindow()
160{
161 if (grDev) {
Naseer Ahmed0bc64be2012-06-29 12:02:32 -0700162 for(int i = 0; i < mNumBuffers; i++) {
163 if (buffers[i] != NULL) {
164 grDev->free(grDev, buffers[i]->handle);
165 }
166 }
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700167 gralloc_close(grDev);
168 }
169
170 if (fbDev) {
171 framebuffer_close(fbDev);
172 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700173}
174
Mathias Agopian1e16b132009-05-07 17:40:23 -0700175status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
176{
177 if (!mUpdateOnDemand) {
178 return INVALID_OPERATION;
179 }
180 return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
181}
182
Mathias Agopian74faca22009-09-17 16:18:16 -0700183status_t FramebufferNativeWindow::compositionComplete()
184{
185 if (fbDev->compositionComplete) {
186 return fbDev->compositionComplete(fbDev);
187 }
188 return INVALID_OPERATION;
189}
190
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700191int FramebufferNativeWindow::setSwapInterval(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700192 ANativeWindow* window, int interval)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700193{
194 framebuffer_device_t* fb = getSelf(window)->fbDev;
195 return fb->setSwapInterval(fb, interval);
196}
197
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800198void FramebufferNativeWindow::dump(String8& result) {
199 if (fbDev->common.version >= 1 && fbDev->dump) {
200 const size_t SIZE = 4096;
201 char buffer[SIZE];
202
203 fbDev->dump(fbDev, buffer, SIZE);
204 result.append(buffer);
205 }
206}
207
Mathias Agopian35b48d12010-09-13 22:57:58 -0700208// only for debugging / logging
209int FramebufferNativeWindow::getCurrentBufferIndex() const
210{
211 Mutex::Autolock _l(mutex);
212 const int index = mCurrentBufferIndex;
213 return index;
214}
215
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700216int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700217 ANativeWindowBuffer** buffer)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700218{
219 FramebufferNativeWindow* self = getSelf(window);
220 Mutex::Autolock _l(self->mutex);
221 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222
Mathias Agopian35b48d12010-09-13 22:57:58 -0700223 int index = self->mBufferHead++;
224 if (self->mBufferHead >= self->mNumBuffers)
225 self->mBufferHead = 0;
226
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700227 // wait for a free buffer
228 while (!self->mNumFreeBuffers) {
229 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700231 // get this buffer
232 self->mNumFreeBuffers--;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700233 self->mCurrentBufferIndex = index;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234
235 *buffer = self->buffers[index].get();
236
237 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238}
239
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700240int FramebufferNativeWindow::lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700241 ANativeWindowBuffer* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243 FramebufferNativeWindow* self = getSelf(window);
244 Mutex::Autolock _l(self->mutex);
245
Mathias Agopian35b48d12010-09-13 22:57:58 -0700246 const int index = self->mCurrentBufferIndex;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700247
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700248 // wait that the buffer we're locking is not front anymore
249 while (self->front == buffer) {
250 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252
Mathias Agopian0926f502009-05-04 14:17:04 -0700253 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254}
255
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700256int FramebufferNativeWindow::queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700257 ANativeWindowBuffer* buffer)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700258{
259 FramebufferNativeWindow* self = getSelf(window);
260 Mutex::Autolock _l(self->mutex);
261 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700262 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700263
264 const int index = self->mCurrentBufferIndex;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700265 int res = fb->post(fb, handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700266 self->front = static_cast<NativeBuffer*>(buffer);
267 self->mNumFreeBuffers++;
268 self->mCondition.broadcast();
269 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270}
271
Iliyan Malchev41abd672011-04-14 16:54:38 -0700272int FramebufferNativeWindow::query(const ANativeWindow* window,
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700273 int what, int* value)
274{
Iliyan Malchev41abd672011-04-14 16:54:38 -0700275 const FramebufferNativeWindow* self = getSelf(window);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700276 Mutex::Autolock _l(self->mutex);
277 framebuffer_device_t* fb = self->fbDev;
278 switch (what) {
279 case NATIVE_WINDOW_WIDTH:
280 *value = fb->width;
281 return NO_ERROR;
282 case NATIVE_WINDOW_HEIGHT:
283 *value = fb->height;
284 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700285 case NATIVE_WINDOW_FORMAT:
286 *value = fb->format;
287 return NO_ERROR;
Jamie Gennis391bbe22011-03-14 15:00:06 -0700288 case NATIVE_WINDOW_CONCRETE_TYPE:
289 *value = NATIVE_WINDOW_FRAMEBUFFER;
290 return NO_ERROR;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700291 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
292 *value = 0;
293 return NO_ERROR;
294 case NATIVE_WINDOW_DEFAULT_WIDTH:
295 *value = fb->width;
296 return NO_ERROR;
297 case NATIVE_WINDOW_DEFAULT_HEIGHT:
298 *value = fb->height;
299 return NO_ERROR;
300 case NATIVE_WINDOW_TRANSFORM_HINT:
301 *value = 0;
302 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700303 }
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700304 *value = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700305 return BAD_VALUE;
306}
307
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700308int FramebufferNativeWindow::perform(ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700309 int operation, ...)
310{
311 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800312 case NATIVE_WINDOW_CONNECT:
313 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700314 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700315 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700316 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700317 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700318 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
Mathias Agopian81a63352011-07-29 17:55:48 -0700319 case NATIVE_WINDOW_API_CONNECT:
320 case NATIVE_WINDOW_API_DISCONNECT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700321 // TODO: we should implement these
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700322 return NO_ERROR;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700323
324 case NATIVE_WINDOW_LOCK:
325 case NATIVE_WINDOW_UNLOCK_AND_POST:
326 case NATIVE_WINDOW_SET_CROP:
327 case NATIVE_WINDOW_SET_BUFFER_COUNT:
328 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700329 case NATIVE_WINDOW_SET_SCALING_MODE:
330 return INVALID_OPERATION;
Mathias Agopian52212712009-08-11 22:34:02 -0700331 }
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700332 return NAME_NOT_FOUND;
Mathias Agopian52212712009-08-11 22:34:02 -0700333}
334
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335// ----------------------------------------------------------------------------
336}; // namespace android
337// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700338
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700339using namespace android;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700340
341EGLNativeWindowType android_createDisplaySurface(void)
342{
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700343 FramebufferNativeWindow* w;
344 w = new FramebufferNativeWindow();
345 if (w->getDevice() == NULL) {
346 // get a ref so it can be destroyed when we exit this block
347 sp<FramebufferNativeWindow> ref(w);
348 return NULL;
349 }
350 return (EGLNativeWindowType)w;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700351}