blob: a36d555f070b339e6139fc347e38f16b8d4c4fe6 [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>
Mathias Agopian35b48d12010-09-13 22:57:58 -070032#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include <EGL/egl.h>
35
36#include <pixelflinger/format.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <pixelflinger/pixelflinger.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039#include <hardware/hardware.h>
40#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Mathias Agopian58a79f42009-05-05 18:21:32 -070042#include <private/ui/android_natives_priv.h>
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044// ----------------------------------------------------------------------------
45namespace android {
46// ----------------------------------------------------------------------------
47
Mathias Agopian7189c002009-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 Agopian076b1cc2009-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 Hackborn4b5e91e2010-06-30 13:56:17 -070071 * In fact this is an implementation of ANativeWindow on top of
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080078
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079FramebufferNativeWindow::FramebufferNativeWindow()
Mathias Agopian1e16b132009-05-07 17:40:23 -070080 : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081{
Mathias Agopian076b1cc2009-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 Agopian076b1cc2009-04-10 14:24:30 -070085 int err;
Mathias Agopian42db9dc2009-08-06 20:46:44 -070086 err = framebuffer_open(module, &fbDev);
87 LOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
88
89 err = gralloc_open(module, &grDev);
90 LOGE_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
99 mNumBuffers = 2;
100 mNumFreeBuffers = 2;
101 mBufferHead = mNumBuffers-1;
102 buffers[0] = new NativeBuffer(
103 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
104 buffers[1] = new NativeBuffer(
105 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
106
107 err = grDev->alloc(grDev,
108 fbDev->width, fbDev->height, fbDev->format,
109 GRALLOC_USAGE_HW_FB, &buffers[0]->handle, &buffers[0]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700111 LOGE_IF(err, "fb buffer 0 allocation failed w=%d, h=%d, err=%s",
112 fbDev->width, fbDev->height, strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700114 err = grDev->alloc(grDev,
115 fbDev->width, fbDev->height, fbDev->format,
116 GRALLOC_USAGE_HW_FB, &buffers[1]->handle, &buffers[1]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118 LOGE_IF(err, "fb buffer 1 allocation failed w=%d, h=%d, err=%s",
119 fbDev->width, fbDev->height, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700121 const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
122 const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
123 const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
124 const_cast<int&>(ANativeWindow::minSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700125 fbDev->minSwapInterval;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700126 const_cast<int&>(ANativeWindow::maxSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700127 fbDev->maxSwapInterval;
128 } else {
129 LOGE("Couldn't get gralloc module");
130 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700132 ANativeWindow::setSwapInterval = setSwapInterval;
133 ANativeWindow::dequeueBuffer = dequeueBuffer;
134 ANativeWindow::lockBuffer = lockBuffer;
135 ANativeWindow::queueBuffer = queueBuffer;
136 ANativeWindow::query = query;
137 ANativeWindow::perform = perform;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138}
139
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700140FramebufferNativeWindow::~FramebufferNativeWindow()
141{
142 if (grDev) {
143 if (buffers[0] != NULL)
144 grDev->free(grDev, buffers[0]->handle);
145 if (buffers[1] != NULL)
146 grDev->free(grDev, buffers[1]->handle);
147 gralloc_close(grDev);
148 }
149
150 if (fbDev) {
151 framebuffer_close(fbDev);
152 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700153}
154
Mathias Agopian1e16b132009-05-07 17:40:23 -0700155status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
156{
157 if (!mUpdateOnDemand) {
158 return INVALID_OPERATION;
159 }
160 return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
161}
162
Mathias Agopian74faca22009-09-17 16:18:16 -0700163status_t FramebufferNativeWindow::compositionComplete()
164{
165 if (fbDev->compositionComplete) {
166 return fbDev->compositionComplete(fbDev);
167 }
168 return INVALID_OPERATION;
169}
170
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171int FramebufferNativeWindow::setSwapInterval(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700172 ANativeWindow* window, int interval)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700173{
174 framebuffer_device_t* fb = getSelf(window)->fbDev;
175 return fb->setSwapInterval(fb, interval);
176}
177
Mathias Agopian35b48d12010-09-13 22:57:58 -0700178// only for debugging / logging
179int FramebufferNativeWindow::getCurrentBufferIndex() const
180{
181 Mutex::Autolock _l(mutex);
182 const int index = mCurrentBufferIndex;
183 return index;
184}
185
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700186int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187 android_native_buffer_t** buffer)
188{
189 FramebufferNativeWindow* self = getSelf(window);
190 Mutex::Autolock _l(self->mutex);
191 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192
Mathias Agopian35b48d12010-09-13 22:57:58 -0700193 int index = self->mBufferHead++;
194 if (self->mBufferHead >= self->mNumBuffers)
195 self->mBufferHead = 0;
196
197 GraphicLog& logger(GraphicLog::getInstance());
198 logger.log(GraphicLog::SF_FB_DEQUEUE_BEFORE, index);
199
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 // wait for a free buffer
201 while (!self->mNumFreeBuffers) {
202 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204 // get this buffer
205 self->mNumFreeBuffers--;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700206 self->mCurrentBufferIndex = index;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207
208 *buffer = self->buffers[index].get();
209
Mathias Agopian35b48d12010-09-13 22:57:58 -0700210 logger.log(GraphicLog::SF_FB_DEQUEUE_AFTER, index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212}
213
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700214int FramebufferNativeWindow::lockBuffer(ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215 android_native_buffer_t* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 FramebufferNativeWindow* self = getSelf(window);
218 Mutex::Autolock _l(self->mutex);
219
Mathias Agopian35b48d12010-09-13 22:57:58 -0700220 const int index = self->mCurrentBufferIndex;
221 GraphicLog& logger(GraphicLog::getInstance());
222 logger.log(GraphicLog::SF_FB_LOCK_BEFORE, index);
223
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700224 // wait that the buffer we're locking is not front anymore
225 while (self->front == buffer) {
226 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700228
Mathias Agopian35b48d12010-09-13 22:57:58 -0700229 logger.log(GraphicLog::SF_FB_LOCK_AFTER, index);
230
Mathias Agopian0926f502009-05-04 14:17:04 -0700231 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232}
233
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700234int FramebufferNativeWindow::queueBuffer(ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700235 android_native_buffer_t* buffer)
236{
237 FramebufferNativeWindow* self = getSelf(window);
238 Mutex::Autolock _l(self->mutex);
239 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700241
242 const int index = self->mCurrentBufferIndex;
243 GraphicLog& logger(GraphicLog::getInstance());
244 logger.log(GraphicLog::SF_FB_POST_BEFORE, index);
245
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700246 int res = fb->post(fb, handle);
Mathias Agopian35b48d12010-09-13 22:57:58 -0700247
248 logger.log(GraphicLog::SF_FB_POST_AFTER, index);
249
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250 self->front = static_cast<NativeBuffer*>(buffer);
251 self->mNumFreeBuffers++;
252 self->mCondition.broadcast();
253 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254}
255
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700256int FramebufferNativeWindow::query(ANativeWindow* window,
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700257 int what, int* value)
258{
259 FramebufferNativeWindow* self = getSelf(window);
260 Mutex::Autolock _l(self->mutex);
261 framebuffer_device_t* fb = self->fbDev;
262 switch (what) {
263 case NATIVE_WINDOW_WIDTH:
264 *value = fb->width;
265 return NO_ERROR;
266 case NATIVE_WINDOW_HEIGHT:
267 *value = fb->height;
268 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700269 case NATIVE_WINDOW_FORMAT:
270 *value = fb->format;
271 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700272 }
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700273 *value = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700274 return BAD_VALUE;
275}
276
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700277int FramebufferNativeWindow::perform(ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700278 int operation, ...)
279{
280 switch (operation) {
281 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian55fa2512010-03-11 15:06:54 -0800282 case NATIVE_WINDOW_CONNECT:
283 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian52212712009-08-11 22:34:02 -0700284 break;
285 default:
286 return NAME_NOT_FOUND;
287 }
288 return NO_ERROR;
289}
290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291// ----------------------------------------------------------------------------
292}; // namespace android
293// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700294
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700295using namespace android;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700296
297EGLNativeWindowType android_createDisplaySurface(void)
298{
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700299 FramebufferNativeWindow* w;
300 w = new FramebufferNativeWindow();
301 if (w->getDevice() == NULL) {
302 // get a ref so it can be destroyed when we exit this block
303 sp<FramebufferNativeWindow> ref(w);
304 return NULL;
305 }
306 return (EGLNativeWindowType)w;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700307}