blob: d1dca0c906dbb9ace093473e8a7d18a100ec8284 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <ui/Rect.h>
Mathias Agopian0926f502009-05-04 14:17:04 -070031#include <ui/FramebufferNativeWindow.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include <EGL/egl.h>
34
35#include <pixelflinger/format.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <pixelflinger/pixelflinger.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038#include <hardware/hardware.h>
39#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040
Mathias Agopian58a79f42009-05-05 18:21:32 -070041#include <private/ui/android_natives_priv.h>
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043// ----------------------------------------------------------------------------
44namespace android {
45// ----------------------------------------------------------------------------
46
Mathias Agopian7189c002009-05-05 18:11:11 -070047class NativeBuffer
48 : public EGLNativeBase<
Iliyan Malchev697526b2011-05-01 11:33:26 -070049 ANativeWindowBuffer,
Mathias Agopian7189c002009-05-05 18:11:11 -070050 NativeBuffer,
51 LightRefBase<NativeBuffer> >
52{
53public:
54 NativeBuffer(int w, int h, int f, int u) : BASE() {
Iliyan Malchev697526b2011-05-01 11:33:26 -070055 ANativeWindowBuffer::width = w;
56 ANativeWindowBuffer::height = h;
57 ANativeWindowBuffer::format = f;
58 ANativeWindowBuffer::usage = u;
Mathias Agopian7189c002009-05-05 18:11:11 -070059 }
60private:
61 friend class LightRefBase<NativeBuffer>;
62 ~NativeBuffer() { }; // this class cannot be overloaded
63};
64
65
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066/*
67 * This implements the (main) framebuffer management. This class is used
68 * mostly by SurfaceFlinger, but also by command line GL application.
69 *
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -070070 * In fact this is an implementation of ANativeWindow on top of
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071 * the framebuffer.
72 *
73 * Currently it is pretty simple, it manages only two buffers (the front and
74 * back buffer).
75 *
76 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078FramebufferNativeWindow::FramebufferNativeWindow()
Mathias Agopian1e16b132009-05-07 17:40:23 -070079 : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070081 hw_module_t const* module;
82 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
83 int stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 int err;
Rodrigo Obregon71484f22010-11-03 15:16:18 -050085 int i;
Mathias Agopian42db9dc2009-08-06 20:46:44 -070086 err = framebuffer_open(module, &fbDev);
Steve Blocke6f43dd2012-01-06 19:20:56 +000087 ALOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
Mathias Agopian42db9dc2009-08-06 20:46:44 -070088
89 err = gralloc_open(module, &grDev);
Steve Blocke6f43dd2012-01-06 19:20:56 +000090 ALOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091
Mathias Agopian42db9dc2009-08-06 20:46:44 -070092 // bail out if we can't initialize the modules
93 if (!fbDev || !grDev)
94 return;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070095
Mathias Agopian1e16b132009-05-07 17:40:23 -070096 mUpdateOnDemand = (fbDev->setUpdateRect != 0);
97
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 // initialize the buffer FIFO
Rodrigo Obregon71484f22010-11-03 15:16:18 -050099 mNumBuffers = NUM_FRAME_BUFFERS;
100 mNumFreeBuffers = NUM_FRAME_BUFFERS;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 mBufferHead = mNumBuffers-1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500103 for (i = 0; i < mNumBuffers; i++)
104 {
105 buffers[i] = new NativeBuffer(
106 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
107 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500109 for (i = 0; i < mNumBuffers; i++)
110 {
111 err = grDev->alloc(grDev,
112 fbDev->width, fbDev->height, fbDev->format,
113 GRALLOC_USAGE_HW_FB, &buffers[i]->handle, &buffers[i]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114
Steve Blocke6f43dd2012-01-06 19:20:56 +0000115 ALOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500116 i, fbDev->width, fbDev->height, strerror(-err));
117
118 if (err)
119 {
120 mNumBuffers = i;
121 mNumFreeBuffers = i;
122 mBufferHead = mNumBuffers-1;
123 break;
124 }
125 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700126
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700127 const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
128 const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
129 const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
130 const_cast<int&>(ANativeWindow::minSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700131 fbDev->minSwapInterval;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700132 const_cast<int&>(ANativeWindow::maxSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700133 fbDev->maxSwapInterval;
134 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000135 ALOGE("Couldn't get gralloc module");
Marco Nelissena4557932009-09-23 10:54:36 -0700136 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700138 ANativeWindow::setSwapInterval = setSwapInterval;
139 ANativeWindow::dequeueBuffer = dequeueBuffer;
140 ANativeWindow::lockBuffer = lockBuffer;
141 ANativeWindow::queueBuffer = queueBuffer;
142 ANativeWindow::query = query;
143 ANativeWindow::perform = perform;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700144}
145
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700146FramebufferNativeWindow::~FramebufferNativeWindow()
147{
148 if (grDev) {
149 if (buffers[0] != NULL)
150 grDev->free(grDev, buffers[0]->handle);
151 if (buffers[1] != NULL)
152 grDev->free(grDev, buffers[1]->handle);
153 gralloc_close(grDev);
154 }
155
156 if (fbDev) {
157 framebuffer_close(fbDev);
158 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159}
160
Mathias Agopian1e16b132009-05-07 17:40:23 -0700161status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
162{
163 if (!mUpdateOnDemand) {
164 return INVALID_OPERATION;
165 }
166 return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
167}
168
Mathias Agopian74faca22009-09-17 16:18:16 -0700169status_t FramebufferNativeWindow::compositionComplete()
170{
171 if (fbDev->compositionComplete) {
172 return fbDev->compositionComplete(fbDev);
173 }
174 return INVALID_OPERATION;
175}
176
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177int FramebufferNativeWindow::setSwapInterval(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700178 ANativeWindow* window, int interval)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700179{
180 framebuffer_device_t* fb = getSelf(window)->fbDev;
181 return fb->setSwapInterval(fb, interval);
182}
183
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800184void FramebufferNativeWindow::dump(String8& result) {
185 if (fbDev->common.version >= 1 && fbDev->dump) {
186 const size_t SIZE = 4096;
187 char buffer[SIZE];
188
189 fbDev->dump(fbDev, buffer, SIZE);
190 result.append(buffer);
191 }
192}
193
Mathias Agopian35b48d12010-09-13 22:57:58 -0700194// only for debugging / logging
195int FramebufferNativeWindow::getCurrentBufferIndex() const
196{
197 Mutex::Autolock _l(mutex);
198 const int index = mCurrentBufferIndex;
199 return index;
200}
201
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700202int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700203 ANativeWindowBuffer** buffer)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204{
205 FramebufferNativeWindow* self = getSelf(window);
206 Mutex::Autolock _l(self->mutex);
207 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208
Mathias Agopian35b48d12010-09-13 22:57:58 -0700209 int index = self->mBufferHead++;
210 if (self->mBufferHead >= self->mNumBuffers)
211 self->mBufferHead = 0;
212
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213 // wait for a free buffer
214 while (!self->mNumFreeBuffers) {
215 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 // get this buffer
218 self->mNumFreeBuffers--;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700219 self->mCurrentBufferIndex = index;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220
221 *buffer = self->buffers[index].get();
222
223 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224}
225
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700226int FramebufferNativeWindow::lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700227 ANativeWindowBuffer* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700229 FramebufferNativeWindow* self = getSelf(window);
230 Mutex::Autolock _l(self->mutex);
231
Mathias Agopian35b48d12010-09-13 22:57:58 -0700232 const int index = self->mCurrentBufferIndex;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700233
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234 // wait that the buffer we're locking is not front anymore
235 while (self->front == buffer) {
236 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700238
Mathias Agopian0926f502009-05-04 14:17:04 -0700239 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240}
241
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700242int FramebufferNativeWindow::queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700243 ANativeWindowBuffer* buffer)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700244{
245 FramebufferNativeWindow* self = getSelf(window);
246 Mutex::Autolock _l(self->mutex);
247 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700248 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700249
250 const int index = self->mCurrentBufferIndex;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700251 int res = fb->post(fb, handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252 self->front = static_cast<NativeBuffer*>(buffer);
253 self->mNumFreeBuffers++;
254 self->mCondition.broadcast();
255 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256}
257
Iliyan Malchev41abd672011-04-14 16:54:38 -0700258int FramebufferNativeWindow::query(const ANativeWindow* window,
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700259 int what, int* value)
260{
Iliyan Malchev41abd672011-04-14 16:54:38 -0700261 const FramebufferNativeWindow* self = getSelf(window);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700262 Mutex::Autolock _l(self->mutex);
263 framebuffer_device_t* fb = self->fbDev;
264 switch (what) {
265 case NATIVE_WINDOW_WIDTH:
266 *value = fb->width;
267 return NO_ERROR;
268 case NATIVE_WINDOW_HEIGHT:
269 *value = fb->height;
270 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700271 case NATIVE_WINDOW_FORMAT:
272 *value = fb->format;
273 return NO_ERROR;
Jamie Gennis391bbe22011-03-14 15:00:06 -0700274 case NATIVE_WINDOW_CONCRETE_TYPE:
275 *value = NATIVE_WINDOW_FRAMEBUFFER;
276 return NO_ERROR;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700277 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
278 *value = 0;
279 return NO_ERROR;
280 case NATIVE_WINDOW_DEFAULT_WIDTH:
281 *value = fb->width;
282 return NO_ERROR;
283 case NATIVE_WINDOW_DEFAULT_HEIGHT:
284 *value = fb->height;
285 return NO_ERROR;
286 case NATIVE_WINDOW_TRANSFORM_HINT:
287 *value = 0;
288 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700289 }
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700290 *value = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700291 return BAD_VALUE;
292}
293
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700294int FramebufferNativeWindow::perform(ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700295 int operation, ...)
296{
297 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800298 case NATIVE_WINDOW_CONNECT:
299 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700300 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700301 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700302 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700303 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700304 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
Mathias Agopian81a63352011-07-29 17:55:48 -0700305 case NATIVE_WINDOW_API_CONNECT:
306 case NATIVE_WINDOW_API_DISCONNECT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700307 // TODO: we should implement these
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700308 return NO_ERROR;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700309
310 case NATIVE_WINDOW_LOCK:
311 case NATIVE_WINDOW_UNLOCK_AND_POST:
312 case NATIVE_WINDOW_SET_CROP:
313 case NATIVE_WINDOW_SET_BUFFER_COUNT:
314 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700315 case NATIVE_WINDOW_SET_SCALING_MODE:
316 return INVALID_OPERATION;
Mathias Agopian52212712009-08-11 22:34:02 -0700317 }
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700318 return NAME_NOT_FOUND;
Mathias Agopian52212712009-08-11 22:34:02 -0700319}
320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321// ----------------------------------------------------------------------------
322}; // namespace android
323// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700325using namespace android;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326
327EGLNativeWindowType android_createDisplaySurface(void)
328{
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700329 FramebufferNativeWindow* w;
330 w = new FramebufferNativeWindow();
331 if (w->getDevice() == NULL) {
332 // get a ref so it can be destroyed when we exit this block
333 sp<FramebufferNativeWindow> ref(w);
334 return NULL;
335 }
336 return (EGLNativeWindowType)w;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700337}