blob: 52380a077c7bac9feba86e9fc54653bab6929df0 [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<
49 android_native_buffer_t,
50 NativeBuffer,
51 LightRefBase<NativeBuffer> >
52{
53public:
54 NativeBuffer(int w, int h, int f, int u) : BASE() {
55 android_native_buffer_t::width = w;
56 android_native_buffer_t::height = h;
57 android_native_buffer_t::format = f;
58 android_native_buffer_t::usage = u;
59 }
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 *
70 * In fact this is an implementation of android_native_window_t on top of
71 * 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;
Mathias Agopian42db9dc2009-08-06 20:46:44 -070085 err = framebuffer_open(module, &fbDev);
86 LOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
87
88 err = gralloc_open(module, &grDev);
89 LOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090
Mathias Agopian42db9dc2009-08-06 20:46:44 -070091 // bail out if we can't initialize the modules
92 if (!fbDev || !grDev)
93 return;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070094
Mathias Agopian1e16b132009-05-07 17:40:23 -070095 mUpdateOnDemand = (fbDev->setUpdateRect != 0);
96
Mathias Agopian076b1cc2009-04-10 14:24:30 -070097 // initialize the buffer FIFO
98 mNumBuffers = 2;
99 mNumFreeBuffers = 2;
100 mBufferHead = mNumBuffers-1;
101 buffers[0] = new NativeBuffer(
102 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
103 buffers[1] = new NativeBuffer(
104 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
105
106 err = grDev->alloc(grDev,
107 fbDev->width, fbDev->height, fbDev->format,
108 GRALLOC_USAGE_HW_FB, &buffers[0]->handle, &buffers[0]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110 LOGE_IF(err, "fb buffer 0 allocation failed w=%d, h=%d, err=%s",
111 fbDev->width, fbDev->height, strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700113 err = grDev->alloc(grDev,
114 fbDev->width, fbDev->height, fbDev->format,
115 GRALLOC_USAGE_HW_FB, &buffers[1]->handle, &buffers[1]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700117 LOGE_IF(err, "fb buffer 1 allocation failed w=%d, h=%d, err=%s",
118 fbDev->width, fbDev->height, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700119
Marco Nelissena4557932009-09-23 10:54:36 -0700120 const_cast<uint32_t&>(android_native_window_t::flags) = fbDev->flags;
121 const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi;
122 const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi;
123 const_cast<int&>(android_native_window_t::minSwapInterval) =
124 fbDev->minSwapInterval;
125 const_cast<int&>(android_native_window_t::maxSwapInterval) =
126 fbDev->maxSwapInterval;
127 } else {
128 LOGE("Couldn't get gralloc module");
129 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 android_native_window_t::dequeueBuffer = dequeueBuffer;
133 android_native_window_t::lockBuffer = lockBuffer;
134 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700135 android_native_window_t::query = query;
Mathias Agopian52212712009-08-11 22:34:02 -0700136 android_native_window_t::perform = perform;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137}
138
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700139FramebufferNativeWindow::~FramebufferNativeWindow()
140{
141 if (grDev) {
142 if (buffers[0] != NULL)
143 grDev->free(grDev, buffers[0]->handle);
144 if (buffers[1] != NULL)
145 grDev->free(grDev, buffers[1]->handle);
146 gralloc_close(grDev);
147 }
148
149 if (fbDev) {
150 framebuffer_close(fbDev);
151 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700152}
153
Mathias Agopian1e16b132009-05-07 17:40:23 -0700154status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
155{
156 if (!mUpdateOnDemand) {
157 return INVALID_OPERATION;
158 }
159 return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
160}
161
Mathias Agopian74faca22009-09-17 16:18:16 -0700162status_t FramebufferNativeWindow::compositionComplete()
163{
164 if (fbDev->compositionComplete) {
165 return fbDev->compositionComplete(fbDev);
166 }
167 return INVALID_OPERATION;
168}
169
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700170int FramebufferNativeWindow::setSwapInterval(
171 android_native_window_t* window, int interval)
172{
173 framebuffer_device_t* fb = getSelf(window)->fbDev;
174 return fb->setSwapInterval(fb, interval);
175}
176
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window,
178 android_native_buffer_t** buffer)
179{
180 FramebufferNativeWindow* self = getSelf(window);
181 Mutex::Autolock _l(self->mutex);
182 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184 // wait for a free buffer
185 while (!self->mNumFreeBuffers) {
186 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700188 // get this buffer
189 self->mNumFreeBuffers--;
190 int index = self->mBufferHead++;
191 if (self->mBufferHead >= self->mNumBuffers)
192 self->mBufferHead = 0;
193
194 *buffer = self->buffers[index].get();
195
196 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}
198
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700199int FramebufferNativeWindow::lockBuffer(android_native_window_t* window,
200 android_native_buffer_t* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700202 FramebufferNativeWindow* self = getSelf(window);
203 Mutex::Autolock _l(self->mutex);
204
205 // wait that the buffer we're locking is not front anymore
206 while (self->front == buffer) {
207 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209
Mathias Agopian0926f502009-05-04 14:17:04 -0700210 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211}
212
213int FramebufferNativeWindow::queueBuffer(android_native_window_t* window,
214 android_native_buffer_t* buffer)
215{
216 FramebufferNativeWindow* self = getSelf(window);
217 Mutex::Autolock _l(self->mutex);
218 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700219 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 int res = fb->post(fb, handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700221 self->front = static_cast<NativeBuffer*>(buffer);
222 self->mNumFreeBuffers++;
223 self->mCondition.broadcast();
224 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225}
226
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700227int FramebufferNativeWindow::query(android_native_window_t* window,
228 int what, int* value)
229{
230 FramebufferNativeWindow* self = getSelf(window);
231 Mutex::Autolock _l(self->mutex);
232 framebuffer_device_t* fb = self->fbDev;
233 switch (what) {
234 case NATIVE_WINDOW_WIDTH:
235 *value = fb->width;
236 return NO_ERROR;
237 case NATIVE_WINDOW_HEIGHT:
238 *value = fb->height;
239 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700240 case NATIVE_WINDOW_FORMAT:
241 *value = fb->format;
242 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700243 }
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700244 *value = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700245 return BAD_VALUE;
246}
247
Mathias Agopian52212712009-08-11 22:34:02 -0700248int FramebufferNativeWindow::perform(android_native_window_t* window,
249 int operation, ...)
250{
251 switch (operation) {
252 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian55fa2512010-03-11 15:06:54 -0800253 case NATIVE_WINDOW_CONNECT:
254 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian52212712009-08-11 22:34:02 -0700255 break;
256 default:
257 return NAME_NOT_FOUND;
258 }
259 return NO_ERROR;
260}
261
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262// ----------------------------------------------------------------------------
263}; // namespace android
264// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700265
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700266using namespace android;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700267
268EGLNativeWindowType android_createDisplaySurface(void)
269{
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700270 FramebufferNativeWindow* w;
271 w = new FramebufferNativeWindow();
272 if (w->getDevice() == NULL) {
273 // get a ref so it can be destroyed when we exit this block
274 sp<FramebufferNativeWindow> ref(w);
275 return NULL;
276 }
277 return (EGLNativeWindowType)w;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700278}