blob: 5e69cff985c718ddd81e2a168d757107af47edd8 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <ui/SurfaceComposerClient.h>
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
41// ----------------------------------------------------------------------------
42namespace android {
43// ----------------------------------------------------------------------------
44
Mathias Agopian7189c002009-05-05 18:11:11 -070045class NativeBuffer
46 : public EGLNativeBase<
47 android_native_buffer_t,
48 NativeBuffer,
49 LightRefBase<NativeBuffer> >
50{
51public:
52 NativeBuffer(int w, int h, int f, int u) : BASE() {
53 android_native_buffer_t::width = w;
54 android_native_buffer_t::height = h;
55 android_native_buffer_t::format = f;
56 android_native_buffer_t::usage = u;
57 }
58private:
59 friend class LightRefBase<NativeBuffer>;
60 ~NativeBuffer() { }; // this class cannot be overloaded
61};
62
63
Mathias Agopian076b1cc2009-04-10 14:24:30 -070064/*
65 * This implements the (main) framebuffer management. This class is used
66 * mostly by SurfaceFlinger, but also by command line GL application.
67 *
68 * In fact this is an implementation of android_native_window_t on top of
69 * the framebuffer.
70 *
71 * Currently it is pretty simple, it manages only two buffers (the front and
72 * back buffer).
73 *
74 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076FramebufferNativeWindow::FramebufferNativeWindow()
77 : BASE(), fbDev(0), grDev(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079 hw_module_t const* module;
80 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
81 int stride;
82 framebuffer_open(module, &fbDev);
83 gralloc_open(module, &grDev);
84 int err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086
87 // initialize the buffer FIFO
88 mNumBuffers = 2;
89 mNumFreeBuffers = 2;
90 mBufferHead = mNumBuffers-1;
91 buffers[0] = new NativeBuffer(
92 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
93 buffers[1] = new NativeBuffer(
94 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
95
96 err = grDev->alloc(grDev,
97 fbDev->width, fbDev->height, fbDev->format,
98 GRALLOC_USAGE_HW_FB, &buffers[0]->handle, &buffers[0]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100 LOGE_IF(err, "fb buffer 0 allocation failed w=%d, h=%d, err=%s",
101 fbDev->width, fbDev->height, strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103 err = grDev->alloc(grDev,
104 fbDev->width, fbDev->height, fbDev->format,
105 GRALLOC_USAGE_HW_FB, &buffers[1]->handle, &buffers[1]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700107 LOGE_IF(err, "fb buffer 1 allocation failed w=%d, h=%d, err=%s",
108 fbDev->width, fbDev->height, strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110
111 uint32_t flags = fbDev->flags & SURFACE_FLAG_MAPPED;
112
113 /*
114 * FIXME: SURFACE_FLAG_PRESERVE_CONTENT
115 * how to implement this, there is no concept of preserve content in
116 * the framebuffer, which just "posts" buffer.
117 *
118 * It looks like what we need is a way to know if the posted buffer can
119 * be reused. But if so, why allocating 2 buffers?...
120 *
121 * should the lock/unlock calls take care of the copy-back?
122 *
123 *
124 * In the end, the client wants to know if the backbuffer is preserved
125 * though... it's complicated.
126 *
127 */
128
129 //flags |= SURFACE_FLAG_PRESERVE_CONTENT;
130
131
132 const_cast<uint32_t&>(android_native_window_t::flags) = flags;
133 const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi;
134 const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi;
135 const_cast<int&>(android_native_window_t::minSwapInterval) =
136 fbDev->minSwapInterval;
137 const_cast<int&>(android_native_window_t::maxSwapInterval) =
138 fbDev->maxSwapInterval;
139
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141 android_native_window_t::dequeueBuffer = dequeueBuffer;
142 android_native_window_t::lockBuffer = lockBuffer;
143 android_native_window_t::queueBuffer = queueBuffer;
144}
145
146FramebufferNativeWindow::~FramebufferNativeWindow() {
147 grDev->free(grDev, buffers[0]->handle);
148 grDev->free(grDev, buffers[1]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700149 gralloc_close(grDev);
150 framebuffer_close(fbDev);
151}
152
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700153int FramebufferNativeWindow::setSwapInterval(
154 android_native_window_t* window, int interval)
155{
156 framebuffer_device_t* fb = getSelf(window)->fbDev;
157 return fb->setSwapInterval(fb, interval);
158}
159
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700160int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window,
161 android_native_buffer_t** buffer)
162{
163 FramebufferNativeWindow* self = getSelf(window);
164 Mutex::Autolock _l(self->mutex);
165 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700167 // wait for a free buffer
168 while (!self->mNumFreeBuffers) {
169 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171 // get this buffer
172 self->mNumFreeBuffers--;
173 int index = self->mBufferHead++;
174 if (self->mBufferHead >= self->mNumBuffers)
175 self->mBufferHead = 0;
176
177 *buffer = self->buffers[index].get();
178
179 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180}
181
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700182int FramebufferNativeWindow::lockBuffer(android_native_window_t* window,
183 android_native_buffer_t* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700185 FramebufferNativeWindow* self = getSelf(window);
186 Mutex::Autolock _l(self->mutex);
187
188 // wait that the buffer we're locking is not front anymore
189 while (self->front == buffer) {
190 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192
Mathias Agopian0926f502009-05-04 14:17:04 -0700193 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194}
195
196int FramebufferNativeWindow::queueBuffer(android_native_window_t* window,
197 android_native_buffer_t* buffer)
198{
199 FramebufferNativeWindow* self = getSelf(window);
200 Mutex::Autolock _l(self->mutex);
201 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700202 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700203 int res = fb->post(fb, handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204 self->front = static_cast<NativeBuffer*>(buffer);
205 self->mNumFreeBuffers++;
206 self->mCondition.broadcast();
207 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208}
209
210// ----------------------------------------------------------------------------
211}; // namespace android
212// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213
214
215EGLNativeWindowType android_createDisplaySurface(void)
216{
217 return new android::FramebufferNativeWindow();
218}
219