blob: 84cb8f37946701abba1e2058c0d5853062a57d86 [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>
Jamie Gennisd8e812c2012-06-13 16:32:25 -070031#include <ui/Fence.h>
Mathias Agopian0926f502009-05-04 14:17:04 -070032#include <ui/FramebufferNativeWindow.h>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080033#include <ui/Rect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
35#include <EGL/egl.h>
36
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <hardware/hardware.h>
38#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40// ----------------------------------------------------------------------------
41namespace android {
42// ----------------------------------------------------------------------------
43
Mathias Agopian7189c002009-05-05 18:11:11 -070044class NativeBuffer
Mathias Agopian5f2165f2012-02-24 18:25:41 -080045 : public ANativeObjectBase<
Iliyan Malchev697526b2011-05-01 11:33:26 -070046 ANativeWindowBuffer,
Mathias Agopian7189c002009-05-05 18:11:11 -070047 NativeBuffer,
48 LightRefBase<NativeBuffer> >
49{
50public:
51 NativeBuffer(int w, int h, int f, int u) : BASE() {
Iliyan Malchev697526b2011-05-01 11:33:26 -070052 ANativeWindowBuffer::width = w;
53 ANativeWindowBuffer::height = h;
54 ANativeWindowBuffer::format = f;
55 ANativeWindowBuffer::usage = u;
Mathias Agopian7189c002009-05-05 18:11:11 -070056 }
57private:
58 friend class LightRefBase<NativeBuffer>;
59 ~NativeBuffer() { }; // this class cannot be overloaded
60};
61
62
Mathias Agopian076b1cc2009-04-10 14:24:30 -070063/*
64 * This implements the (main) framebuffer management. This class is used
65 * mostly by SurfaceFlinger, but also by command line GL application.
66 *
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -070067 * In fact this is an implementation of ANativeWindow on top of
Mathias Agopian076b1cc2009-04-10 14:24:30 -070068 * the framebuffer.
69 *
70 * Currently it is pretty simple, it manages only two buffers (the front and
71 * back buffer).
72 *
73 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075FramebufferNativeWindow::FramebufferNativeWindow()
Mathias Agopian1e16b132009-05-07 17:40:23 -070076 : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 hw_module_t const* module;
79 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
80 int stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070081 int err;
Rodrigo Obregon71484f22010-11-03 15:16:18 -050082 int i;
Mathias Agopian42db9dc2009-08-06 20:46:44 -070083 err = framebuffer_open(module, &fbDev);
Steve Blocke6f43dd2012-01-06 19:20:56 +000084 ALOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
Mathias Agopian42db9dc2009-08-06 20:46:44 -070085
86 err = gralloc_open(module, &grDev);
Steve Blocke6f43dd2012-01-06 19:20:56 +000087 ALOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
Mathias Agopian42db9dc2009-08-06 20:46:44 -070089 // bail out if we can't initialize the modules
90 if (!fbDev || !grDev)
91 return;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070092
Mathias Agopian1e16b132009-05-07 17:40:23 -070093 mUpdateOnDemand = (fbDev->setUpdateRect != 0);
94
Mathias Agopian076b1cc2009-04-10 14:24:30 -070095 // initialize the buffer FIFO
Rodrigo Obregon71484f22010-11-03 15:16:18 -050096 mNumBuffers = NUM_FRAME_BUFFERS;
97 mNumFreeBuffers = NUM_FRAME_BUFFERS;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 mBufferHead = mNumBuffers-1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099
Dima Zavinc6cd27c2012-02-22 14:37:57 -0800100 /*
101 * This does not actually change the framebuffer format. It merely
102 * fakes this format to surfaceflinger so that when it creates
103 * framebuffer surfaces it will use this format. It's really a giant
104 * HACK to allow interworking with buggy gralloc+GPU driver
105 * implementations. You should *NEVER* need to set this for shipping
106 * devices.
107 */
108#ifdef FRAMEBUFFER_FORCE_FORMAT
109 *((uint32_t *)&fbDev->format) = FRAMEBUFFER_FORCE_FORMAT;
110#endif
111
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500112 for (i = 0; i < mNumBuffers; i++)
113 {
114 buffers[i] = new NativeBuffer(
115 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
116 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500118 for (i = 0; i < mNumBuffers; i++)
119 {
120 err = grDev->alloc(grDev,
121 fbDev->width, fbDev->height, fbDev->format,
122 GRALLOC_USAGE_HW_FB, &buffers[i]->handle, &buffers[i]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
Steve Blocke6f43dd2012-01-06 19:20:56 +0000124 ALOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500125 i, fbDev->width, fbDev->height, strerror(-err));
126
127 if (err)
128 {
129 mNumBuffers = i;
130 mNumFreeBuffers = i;
131 mBufferHead = mNumBuffers-1;
132 break;
133 }
134 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700136 const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
137 const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
138 const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
139 const_cast<int&>(ANativeWindow::minSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700140 fbDev->minSwapInterval;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700141 const_cast<int&>(ANativeWindow::maxSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700142 fbDev->maxSwapInterval;
143 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000144 ALOGE("Couldn't get gralloc module");
Marco Nelissena4557932009-09-23 10:54:36 -0700145 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700146
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700147 ANativeWindow::setSwapInterval = setSwapInterval;
148 ANativeWindow::dequeueBuffer = dequeueBuffer;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700149 ANativeWindow::queueBuffer = queueBuffer;
150 ANativeWindow::query = query;
151 ANativeWindow::perform = perform;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700152
153 ANativeWindow::dequeueBuffer_DEPRECATED = dequeueBuffer_DEPRECATED;
154 ANativeWindow::lockBuffer_DEPRECATED = lockBuffer_DEPRECATED;
155 ANativeWindow::queueBuffer_DEPRECATED = queueBuffer_DEPRECATED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700156}
157
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700158FramebufferNativeWindow::~FramebufferNativeWindow()
159{
160 if (grDev) {
161 if (buffers[0] != NULL)
162 grDev->free(grDev, buffers[0]->handle);
163 if (buffers[1] != NULL)
164 grDev->free(grDev, buffers[1]->handle);
165 gralloc_close(grDev);
166 }
167
168 if (fbDev) {
169 framebuffer_close(fbDev);
170 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171}
172
Mathias Agopian1e16b132009-05-07 17:40:23 -0700173status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
174{
175 if (!mUpdateOnDemand) {
176 return INVALID_OPERATION;
177 }
178 return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
179}
180
Mathias Agopian74faca22009-09-17 16:18:16 -0700181status_t FramebufferNativeWindow::compositionComplete()
182{
183 if (fbDev->compositionComplete) {
184 return fbDev->compositionComplete(fbDev);
185 }
186 return INVALID_OPERATION;
187}
188
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700189int FramebufferNativeWindow::setSwapInterval(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700190 ANativeWindow* window, int interval)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700191{
192 framebuffer_device_t* fb = getSelf(window)->fbDev;
193 return fb->setSwapInterval(fb, interval);
194}
195
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800196void FramebufferNativeWindow::dump(String8& result) {
197 if (fbDev->common.version >= 1 && fbDev->dump) {
198 const size_t SIZE = 4096;
199 char buffer[SIZE];
200
201 fbDev->dump(fbDev, buffer, SIZE);
202 result.append(buffer);
203 }
204}
205
Mathias Agopian35b48d12010-09-13 22:57:58 -0700206// only for debugging / logging
207int FramebufferNativeWindow::getCurrentBufferIndex() const
208{
209 Mutex::Autolock _l(mutex);
210 const int index = mCurrentBufferIndex;
211 return index;
212}
213
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700214int FramebufferNativeWindow::dequeueBuffer_DEPRECATED(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700215 ANativeWindowBuffer** buffer)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700216{
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700217 int fenceFd = -1;
218 int result = dequeueBuffer(window, buffer, &fenceFd);
219 sp<Fence> fence(new Fence(fenceFd));
220 int waitResult = fence->wait(Fence::TIMEOUT_NEVER);
221 if (waitResult != OK) {
222 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an "
223 "error: %d", waitResult);
224 return waitResult;
225 }
226 return result;
227}
228
229int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window,
230 ANativeWindowBuffer** buffer, int* fenceFd)
231{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232 FramebufferNativeWindow* self = getSelf(window);
233 Mutex::Autolock _l(self->mutex);
234 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
Mathias Agopian35b48d12010-09-13 22:57:58 -0700236 int index = self->mBufferHead++;
237 if (self->mBufferHead >= self->mNumBuffers)
238 self->mBufferHead = 0;
239
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 // wait for a free buffer
241 while (!self->mNumFreeBuffers) {
242 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700244 // get this buffer
245 self->mNumFreeBuffers--;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700246 self->mCurrentBufferIndex = index;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700247
248 *buffer = self->buffers[index].get();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700249 *fenceFd = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250
251 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252}
253
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700254int FramebufferNativeWindow::lockBuffer_DEPRECATED(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700255 ANativeWindowBuffer* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256{
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700257 // XXX: Can this code all get ripped out? Should it move to dequeueBuffer?
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700258 FramebufferNativeWindow* self = getSelf(window);
259 Mutex::Autolock _l(self->mutex);
260
Mathias Agopian35b48d12010-09-13 22:57:58 -0700261 const int index = self->mCurrentBufferIndex;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700262
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700263 // wait that the buffer we're locking is not front anymore
264 while (self->front == buffer) {
265 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700267
Mathias Agopian0926f502009-05-04 14:17:04 -0700268 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700269}
270
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700271int FramebufferNativeWindow::queueBuffer_DEPRECATED(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700272 ANativeWindowBuffer* buffer)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700273{
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700274 return queueBuffer(window, buffer, -1);
275}
276
277int FramebufferNativeWindow::queueBuffer(ANativeWindow* window,
278 ANativeWindowBuffer* buffer, int fenceFd)
279{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280 FramebufferNativeWindow* self = getSelf(window);
281 Mutex::Autolock _l(self->mutex);
282 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700283 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700284
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700285 sp<Fence> fence(new Fence(fenceFd));
286 fence->wait(Fence::TIMEOUT_NEVER);
287
Mathias Agopian35b48d12010-09-13 22:57:58 -0700288 const int index = self->mCurrentBufferIndex;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289 int res = fb->post(fb, handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700290 self->front = static_cast<NativeBuffer*>(buffer);
291 self->mNumFreeBuffers++;
292 self->mCondition.broadcast();
293 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294}
295
Iliyan Malchev41abd672011-04-14 16:54:38 -0700296int FramebufferNativeWindow::query(const ANativeWindow* window,
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700297 int what, int* value)
298{
Iliyan Malchev41abd672011-04-14 16:54:38 -0700299 const FramebufferNativeWindow* self = getSelf(window);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700300 Mutex::Autolock _l(self->mutex);
301 framebuffer_device_t* fb = self->fbDev;
302 switch (what) {
303 case NATIVE_WINDOW_WIDTH:
304 *value = fb->width;
305 return NO_ERROR;
306 case NATIVE_WINDOW_HEIGHT:
307 *value = fb->height;
308 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700309 case NATIVE_WINDOW_FORMAT:
310 *value = fb->format;
311 return NO_ERROR;
Jamie Gennis391bbe22011-03-14 15:00:06 -0700312 case NATIVE_WINDOW_CONCRETE_TYPE:
313 *value = NATIVE_WINDOW_FRAMEBUFFER;
314 return NO_ERROR;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700315 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
316 *value = 0;
317 return NO_ERROR;
318 case NATIVE_WINDOW_DEFAULT_WIDTH:
319 *value = fb->width;
320 return NO_ERROR;
321 case NATIVE_WINDOW_DEFAULT_HEIGHT:
322 *value = fb->height;
323 return NO_ERROR;
324 case NATIVE_WINDOW_TRANSFORM_HINT:
325 *value = 0;
326 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700327 }
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700328 *value = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700329 return BAD_VALUE;
330}
331
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700332int FramebufferNativeWindow::perform(ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700333 int operation, ...)
334{
335 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800336 case NATIVE_WINDOW_CONNECT:
337 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700338 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700339 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700340 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700341 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700342 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
Mathias Agopian81a63352011-07-29 17:55:48 -0700343 case NATIVE_WINDOW_API_CONNECT:
344 case NATIVE_WINDOW_API_DISCONNECT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700345 // TODO: we should implement these
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700346 return NO_ERROR;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700347
348 case NATIVE_WINDOW_LOCK:
349 case NATIVE_WINDOW_UNLOCK_AND_POST:
350 case NATIVE_WINDOW_SET_CROP:
351 case NATIVE_WINDOW_SET_BUFFER_COUNT:
352 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700353 case NATIVE_WINDOW_SET_SCALING_MODE:
354 return INVALID_OPERATION;
Mathias Agopian52212712009-08-11 22:34:02 -0700355 }
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700356 return NAME_NOT_FOUND;
Mathias Agopian52212712009-08-11 22:34:02 -0700357}
358
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359// ----------------------------------------------------------------------------
360}; // namespace android
361// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700362
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700363using namespace android;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700364
365EGLNativeWindowType android_createDisplaySurface(void)
366{
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700367 FramebufferNativeWindow* w;
368 w = new FramebufferNativeWindow();
369 if (w->getDevice() == NULL) {
370 // get a ref so it can be destroyed when we exit this block
371 sp<FramebufferNativeWindow> ref(w);
372 return NULL;
373 }
374 return (EGLNativeWindowType)w;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700375}