blob: f6c666df4263fabb159215b0ad50ee91364633ab [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
30#include <ui/SurfaceComposerClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <ui/Rect.h>
Mathias Agopian0926f502009-05-04 14:17:04 -070032#include <ui/FramebufferNativeWindow.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 *
71 * In fact this is an implementation of android_native_window_t on top of
72 * 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));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -0700122 const_cast<uint32_t&>(android_native_window_t::flags) = fbDev->flags;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700123 const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi;
124 const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi;
125 const_cast<int&>(android_native_window_t::minSwapInterval) =
126 fbDev->minSwapInterval;
127 const_cast<int&>(android_native_window_t::maxSwapInterval) =
128 fbDev->maxSwapInterval;
129
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131 android_native_window_t::dequeueBuffer = dequeueBuffer;
132 android_native_window_t::lockBuffer = lockBuffer;
133 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700134 android_native_window_t::query = query;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135}
136
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700137FramebufferNativeWindow::~FramebufferNativeWindow()
138{
139 if (grDev) {
140 if (buffers[0] != NULL)
141 grDev->free(grDev, buffers[0]->handle);
142 if (buffers[1] != NULL)
143 grDev->free(grDev, buffers[1]->handle);
144 gralloc_close(grDev);
145 }
146
147 if (fbDev) {
148 framebuffer_close(fbDev);
149 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700150}
151
Mathias Agopian1e16b132009-05-07 17:40:23 -0700152status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
153{
154 if (!mUpdateOnDemand) {
155 return INVALID_OPERATION;
156 }
157 return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
158}
159
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700160int FramebufferNativeWindow::setSwapInterval(
161 android_native_window_t* window, int interval)
162{
163 framebuffer_device_t* fb = getSelf(window)->fbDev;
164 return fb->setSwapInterval(fb, interval);
165}
166
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700167int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window,
168 android_native_buffer_t** buffer)
169{
170 FramebufferNativeWindow* self = getSelf(window);
171 Mutex::Autolock _l(self->mutex);
172 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700174 // wait for a free buffer
175 while (!self->mNumFreeBuffers) {
176 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178 // get this buffer
179 self->mNumFreeBuffers--;
180 int index = self->mBufferHead++;
181 if (self->mBufferHead >= self->mNumBuffers)
182 self->mBufferHead = 0;
183
184 *buffer = self->buffers[index].get();
185
186 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187}
188
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700189int FramebufferNativeWindow::lockBuffer(android_native_window_t* window,
190 android_native_buffer_t* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 FramebufferNativeWindow* self = getSelf(window);
193 Mutex::Autolock _l(self->mutex);
194
195 // wait that the buffer we're locking is not front anymore
196 while (self->front == buffer) {
197 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700199
Mathias Agopian0926f502009-05-04 14:17:04 -0700200 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201}
202
203int FramebufferNativeWindow::queueBuffer(android_native_window_t* window,
204 android_native_buffer_t* buffer)
205{
206 FramebufferNativeWindow* self = getSelf(window);
207 Mutex::Autolock _l(self->mutex);
208 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700210 int res = fb->post(fb, handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211 self->front = static_cast<NativeBuffer*>(buffer);
212 self->mNumFreeBuffers++;
213 self->mCondition.broadcast();
214 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215}
216
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700217int FramebufferNativeWindow::query(android_native_window_t* window,
218 int what, int* value)
219{
220 FramebufferNativeWindow* self = getSelf(window);
221 Mutex::Autolock _l(self->mutex);
222 framebuffer_device_t* fb = self->fbDev;
223 switch (what) {
224 case NATIVE_WINDOW_WIDTH:
225 *value = fb->width;
226 return NO_ERROR;
227 case NATIVE_WINDOW_HEIGHT:
228 *value = fb->height;
229 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700230 case NATIVE_WINDOW_FORMAT:
231 *value = fb->format;
232 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700233 }
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700234 *value = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700235 return BAD_VALUE;
236}
237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238// ----------------------------------------------------------------------------
239}; // namespace android
240// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700241
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700242using namespace android;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243
244EGLNativeWindowType android_createDisplaySurface(void)
245{
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700246 FramebufferNativeWindow* w;
247 w = new FramebufferNativeWindow();
248 if (w->getDevice() == NULL) {
249 // get a ref so it can be destroyed when we exit this block
250 sp<FramebufferNativeWindow> ref(w);
251 return NULL;
252 }
253 return (EGLNativeWindowType)w;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254}