blob: 295300e3f8eb01135820943a134c344154d4f755 [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>
Dan Stozaf10c46e2014-11-11 10:32:31 -080032#define INCLUDED_FROM_FRAMEBUFFER_NATIVE_WINDOW_CPP
Mathias Agopian0926f502009-05-04 14:17:04 -070033#include <ui/FramebufferNativeWindow.h>
Dan Stozaf10c46e2014-11-11 10:32:31 -080034#undef INCLUDED_FROM_FRAMEBUFFER_NATIVE_WINDOW_CPP
Mathias Agopian5f2165f2012-02-24 18:25:41 -080035#include <ui/Rect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
37#include <EGL/egl.h>
38
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
42// ----------------------------------------------------------------------------
43namespace android {
44// ----------------------------------------------------------------------------
45
Mathias Agopian7189c002009-05-05 18:11:11 -070046class NativeBuffer
Mathias Agopian5f2165f2012-02-24 18:25:41 -080047 : public ANativeObjectBase<
Iliyan Malchev697526b2011-05-01 11:33:26 -070048 ANativeWindowBuffer,
Mathias Agopian7189c002009-05-05 18:11:11 -070049 NativeBuffer,
50 LightRefBase<NativeBuffer> >
51{
52public:
53 NativeBuffer(int w, int h, int f, int u) : BASE() {
Iliyan Malchev697526b2011-05-01 11:33:26 -070054 ANativeWindowBuffer::width = w;
55 ANativeWindowBuffer::height = h;
56 ANativeWindowBuffer::format = f;
57 ANativeWindowBuffer::usage = u;
Mathias Agopian7189c002009-05-05 18:11:11 -070058 }
59private:
60 friend class LightRefBase<NativeBuffer>;
61 ~NativeBuffer() { }; // this class cannot be overloaded
62};
63
64
Mathias Agopian076b1cc2009-04-10 14:24:30 -070065/*
66 * This implements the (main) framebuffer management. This class is used
67 * mostly by SurfaceFlinger, but also by command line GL application.
68 *
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -070069 * In fact this is an implementation of ANativeWindow on top of
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 * the framebuffer.
71 *
72 * Currently it is pretty simple, it manages only two buffers (the front and
73 * back buffer).
74 *
75 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077FramebufferNativeWindow::FramebufferNativeWindow()
Mathias Agopian1e16b132009-05-07 17:40:23 -070078 : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070080 hw_module_t const* module;
81 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
82 int stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083 int err;
Rodrigo Obregon71484f22010-11-03 15:16:18 -050084 int i;
Mathias Agopian42db9dc2009-08-06 20:46:44 -070085 err = framebuffer_open(module, &fbDev);
Steve Blocke6f43dd2012-01-06 19:20:56 +000086 ALOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
Mathias Agopian42db9dc2009-08-06 20:46:44 -070087
88 err = gralloc_open(module, &grDev);
Steve Blocke6f43dd2012-01-06 19:20:56 +000089 ALOGE_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
Naseer Ahmed0bc64be2012-06-29 12:02:32 -070098 if(fbDev->numFramebuffers >= MIN_NUM_FRAME_BUFFERS &&
99 fbDev->numFramebuffers <= MAX_NUM_FRAME_BUFFERS){
100 mNumBuffers = fbDev->numFramebuffers;
101 } else {
102 mNumBuffers = MIN_NUM_FRAME_BUFFERS;
103 }
104 mNumFreeBuffers = mNumBuffers;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105 mBufferHead = mNumBuffers-1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106
Dima Zavinc6cd27c2012-02-22 14:37:57 -0800107 /*
108 * This does not actually change the framebuffer format. It merely
109 * fakes this format to surfaceflinger so that when it creates
110 * framebuffer surfaces it will use this format. It's really a giant
111 * HACK to allow interworking with buggy gralloc+GPU driver
112 * implementations. You should *NEVER* need to set this for shipping
113 * devices.
114 */
115#ifdef FRAMEBUFFER_FORCE_FORMAT
116 *((uint32_t *)&fbDev->format) = FRAMEBUFFER_FORCE_FORMAT;
117#endif
118
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500119 for (i = 0; i < mNumBuffers; i++)
120 {
121 buffers[i] = new NativeBuffer(
122 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
123 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500125 for (i = 0; i < mNumBuffers; i++)
126 {
127 err = grDev->alloc(grDev,
128 fbDev->width, fbDev->height, fbDev->format,
129 GRALLOC_USAGE_HW_FB, &buffers[i]->handle, &buffers[i]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130
Steve Blocke6f43dd2012-01-06 19:20:56 +0000131 ALOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
Rodrigo Obregon71484f22010-11-03 15:16:18 -0500132 i, fbDev->width, fbDev->height, strerror(-err));
133
134 if (err)
135 {
136 mNumBuffers = i;
137 mNumFreeBuffers = i;
138 mBufferHead = mNumBuffers-1;
139 break;
140 }
141 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700143 const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
144 const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
145 const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
146 const_cast<int&>(ANativeWindow::minSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700147 fbDev->minSwapInterval;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700148 const_cast<int&>(ANativeWindow::maxSwapInterval) =
Marco Nelissena4557932009-09-23 10:54:36 -0700149 fbDev->maxSwapInterval;
150 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000151 ALOGE("Couldn't get gralloc module");
Marco Nelissena4557932009-09-23 10:54:36 -0700152 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700153
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700154 ANativeWindow::setSwapInterval = setSwapInterval;
155 ANativeWindow::dequeueBuffer = dequeueBuffer;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700156 ANativeWindow::queueBuffer = queueBuffer;
157 ANativeWindow::query = query;
158 ANativeWindow::perform = perform;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700159
160 ANativeWindow::dequeueBuffer_DEPRECATED = dequeueBuffer_DEPRECATED;
161 ANativeWindow::lockBuffer_DEPRECATED = lockBuffer_DEPRECATED;
162 ANativeWindow::queueBuffer_DEPRECATED = queueBuffer_DEPRECATED;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700163}
164
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700165FramebufferNativeWindow::~FramebufferNativeWindow()
166{
167 if (grDev) {
Naseer Ahmed0bc64be2012-06-29 12:02:32 -0700168 for(int i = 0; i < mNumBuffers; i++) {
169 if (buffers[i] != NULL) {
170 grDev->free(grDev, buffers[i]->handle);
171 }
172 }
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700173 gralloc_close(grDev);
174 }
175
176 if (fbDev) {
177 framebuffer_close(fbDev);
178 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700179}
180
Mathias Agopian1e16b132009-05-07 17:40:23 -0700181status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
182{
183 if (!mUpdateOnDemand) {
184 return INVALID_OPERATION;
185 }
186 return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
187}
188
Mathias Agopian74faca22009-09-17 16:18:16 -0700189status_t FramebufferNativeWindow::compositionComplete()
190{
191 if (fbDev->compositionComplete) {
192 return fbDev->compositionComplete(fbDev);
193 }
194 return INVALID_OPERATION;
195}
196
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197int FramebufferNativeWindow::setSwapInterval(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700198 ANativeWindow* window, int interval)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700199{
200 framebuffer_device_t* fb = getSelf(window)->fbDev;
201 return fb->setSwapInterval(fb, interval);
202}
203
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800204void FramebufferNativeWindow::dump(String8& result) {
205 if (fbDev->common.version >= 1 && fbDev->dump) {
206 const size_t SIZE = 4096;
207 char buffer[SIZE];
208
209 fbDev->dump(fbDev, buffer, SIZE);
210 result.append(buffer);
211 }
212}
213
Mathias Agopian35b48d12010-09-13 22:57:58 -0700214// only for debugging / logging
215int FramebufferNativeWindow::getCurrentBufferIndex() const
216{
217 Mutex::Autolock _l(mutex);
218 const int index = mCurrentBufferIndex;
219 return index;
220}
221
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700222int FramebufferNativeWindow::dequeueBuffer_DEPRECATED(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700223 ANativeWindowBuffer** buffer)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700224{
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700225 int fenceFd = -1;
226 int result = dequeueBuffer(window, buffer, &fenceFd);
227 sp<Fence> fence(new Fence(fenceFd));
228 int waitResult = fence->wait(Fence::TIMEOUT_NEVER);
229 if (waitResult != OK) {
230 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an "
231 "error: %d", waitResult);
232 return waitResult;
233 }
234 return result;
235}
236
237int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window,
238 ANativeWindowBuffer** buffer, int* fenceFd)
239{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 FramebufferNativeWindow* self = getSelf(window);
241 Mutex::Autolock _l(self->mutex);
242 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243
Mathias Agopian35b48d12010-09-13 22:57:58 -0700244 int index = self->mBufferHead++;
245 if (self->mBufferHead >= self->mNumBuffers)
246 self->mBufferHead = 0;
247
Jesse Halla74cbc02012-06-21 11:35:23 -0700248 // wait for a free non-front buffer
249 while (self->mNumFreeBuffers < 2) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 }
Jesse Halla74cbc02012-06-21 11:35:23 -0700252 ALOG_ASSERT(self->buffers[index] != self->front);
253
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254 // get this buffer
255 self->mNumFreeBuffers--;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700256 self->mCurrentBufferIndex = index;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257
258 *buffer = self->buffers[index].get();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700259 *fenceFd = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700260
261 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262}
263
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700264int FramebufferNativeWindow::lockBuffer_DEPRECATED(ANativeWindow* /*window*/,
265 ANativeWindowBuffer* /*buffer*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266{
Mathias Agopian0926f502009-05-04 14:17:04 -0700267 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700268}
269
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700270int FramebufferNativeWindow::queueBuffer_DEPRECATED(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700271 ANativeWindowBuffer* buffer)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272{
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700273 return queueBuffer(window, buffer, -1);
274}
275
276int FramebufferNativeWindow::queueBuffer(ANativeWindow* window,
277 ANativeWindowBuffer* buffer, int fenceFd)
278{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 FramebufferNativeWindow* self = getSelf(window);
280 Mutex::Autolock _l(self->mutex);
281 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700282 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian35b48d12010-09-13 22:57:58 -0700283
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700284 sp<Fence> fence(new Fence(fenceFd));
285 fence->wait(Fence::TIMEOUT_NEVER);
286
Mathias Agopian35b48d12010-09-13 22:57:58 -0700287 const int index = self->mCurrentBufferIndex;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700288 int res = fb->post(fb, handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289 self->front = static_cast<NativeBuffer*>(buffer);
290 self->mNumFreeBuffers++;
291 self->mCondition.broadcast();
292 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293}
294
Iliyan Malchev41abd672011-04-14 16:54:38 -0700295int FramebufferNativeWindow::query(const ANativeWindow* window,
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700296 int what, int* value)
297{
Iliyan Malchev41abd672011-04-14 16:54:38 -0700298 const FramebufferNativeWindow* self = getSelf(window);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700299 Mutex::Autolock _l(self->mutex);
300 framebuffer_device_t* fb = self->fbDev;
301 switch (what) {
302 case NATIVE_WINDOW_WIDTH:
303 *value = fb->width;
304 return NO_ERROR;
305 case NATIVE_WINDOW_HEIGHT:
306 *value = fb->height;
307 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700308 case NATIVE_WINDOW_FORMAT:
309 *value = fb->format;
310 return NO_ERROR;
Jamie Gennis391bbe22011-03-14 15:00:06 -0700311 case NATIVE_WINDOW_CONCRETE_TYPE:
312 *value = NATIVE_WINDOW_FRAMEBUFFER;
313 return NO_ERROR;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700314 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
315 *value = 0;
316 return NO_ERROR;
317 case NATIVE_WINDOW_DEFAULT_WIDTH:
318 *value = fb->width;
319 return NO_ERROR;
320 case NATIVE_WINDOW_DEFAULT_HEIGHT:
321 *value = fb->height;
322 return NO_ERROR;
323 case NATIVE_WINDOW_TRANSFORM_HINT:
324 *value = 0;
325 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700326 }
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700327 *value = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700328 return BAD_VALUE;
329}
330
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700331int FramebufferNativeWindow::perform(ANativeWindow* /*window*/,
Mathias Agopian52212712009-08-11 22:34:02 -0700332 int operation, ...)
333{
334 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800335 case NATIVE_WINDOW_CONNECT:
336 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700337 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700338 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700339 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700340 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700341 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
Mathias Agopian81a63352011-07-29 17:55:48 -0700342 case NATIVE_WINDOW_API_CONNECT:
343 case NATIVE_WINDOW_API_DISCONNECT:
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700344 // TODO: we should implement these
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700345 return NO_ERROR;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -0700346
347 case NATIVE_WINDOW_LOCK:
348 case NATIVE_WINDOW_UNLOCK_AND_POST:
349 case NATIVE_WINDOW_SET_CROP:
350 case NATIVE_WINDOW_SET_BUFFER_COUNT:
351 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700352 case NATIVE_WINDOW_SET_SCALING_MODE:
353 return INVALID_OPERATION;
Mathias Agopian52212712009-08-11 22:34:02 -0700354 }
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700355 return NAME_NOT_FOUND;
Mathias Agopian52212712009-08-11 22:34:02 -0700356}
357
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358// ----------------------------------------------------------------------------
359}; // namespace android
360// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700361
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700362using namespace android;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700363
364EGLNativeWindowType android_createDisplaySurface(void)
365{
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700366 FramebufferNativeWindow* w;
367 w = new FramebufferNativeWindow();
368 if (w->getDevice() == NULL) {
369 // get a ref so it can be destroyed when we exit this block
370 sp<FramebufferNativeWindow> ref(w);
371 return NULL;
372 }
373 return (EGLNativeWindowType)w;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700374}