blob: 5d9d6a4328aa0da23722e0b1deebc19d169a4780 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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
18#define LOG_TAG "EGLNativeWindowSurface"
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070023#include <errno.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <cutils/log.h>
26#include <cutils/atomic.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070027#include <utils/threads.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29#include <ui/SurfaceComposerClient.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include <ui/Rect.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070031#include <ui/EGLNativeWindowSurface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33#include <EGL/egl.h>
34
35#include <pixelflinger/format.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070036#include <pixelflinger/pixelflinger.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Mathias Agopian1473f462009-04-10 14:24:30 -070038#include <hardware/hardware.h>
39#include <hardware/gralloc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41// ----------------------------------------------------------------------------
42namespace android {
43// ----------------------------------------------------------------------------
44
Mathias Agopian1473f462009-04-10 14:24:30 -070045/*
46 * This implements the (main) framebuffer management. This class is used
47 * mostly by SurfaceFlinger, but also by command line GL application.
48 *
49 * In fact this is an implementation of android_native_window_t on top of
50 * the framebuffer.
51 *
52 * Currently it is pretty simple, it manages only two buffers (the front and
53 * back buffer).
54 *
55 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
Mathias Agopian1473f462009-04-10 14:24:30 -070057FramebufferNativeWindow::FramebufferNativeWindow()
58 : BASE(), fbDev(0), grDev(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059{
Mathias Agopian1473f462009-04-10 14:24:30 -070060 hw_module_t const* module;
61 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
62 int stride;
63 framebuffer_open(module, &fbDev);
64 gralloc_open(module, &grDev);
65 int err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
Mathias Agopian1473f462009-04-10 14:24:30 -070067
68 // initialize the buffer FIFO
69 mNumBuffers = 2;
70 mNumFreeBuffers = 2;
71 mBufferHead = mNumBuffers-1;
72 buffers[0] = new NativeBuffer(
73 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
74 buffers[1] = new NativeBuffer(
75 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
76
77 err = grDev->alloc(grDev,
78 fbDev->width, fbDev->height, fbDev->format,
79 GRALLOC_USAGE_HW_FB, &buffers[0]->handle, &buffers[0]->stride);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Mathias Agopian1473f462009-04-10 14:24:30 -070081 LOGE_IF(err, "fb buffer 0 allocation failed w=%d, h=%d, err=%s",
82 fbDev->width, fbDev->height, strerror(-err));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
Mathias Agopian1473f462009-04-10 14:24:30 -070084 err = grDev->alloc(grDev,
85 fbDev->width, fbDev->height, fbDev->format,
86 GRALLOC_USAGE_HW_FB, &buffers[1]->handle, &buffers[1]->stride);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
Mathias Agopian1473f462009-04-10 14:24:30 -070088 LOGE_IF(err, "fb buffer 1 allocation failed w=%d, h=%d, err=%s",
89 fbDev->width, fbDev->height, strerror(-err));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
Mathias Agopian1473f462009-04-10 14:24:30 -070091 gralloc_module_t* m =
92 reinterpret_cast<gralloc_module_t*>(grDev->common.module);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
Mathias Agopian1473f462009-04-10 14:24:30 -070094 // FIXME: do we actually need to map the framebuffer?
95 m->map(m, buffers[0]->handle, &buffers[0]->bits);
96 m->map(m, buffers[1]->handle, &buffers[1]->bits);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
Mathias Agopian1473f462009-04-10 14:24:30 -070098
99 uint32_t flags = fbDev->flags & SURFACE_FLAG_MAPPED;
100
101 /*
102 * FIXME: SURFACE_FLAG_PRESERVE_CONTENT
103 * how to implement this, there is no concept of preserve content in
104 * the framebuffer, which just "posts" buffer.
105 *
106 * It looks like what we need is a way to know if the posted buffer can
107 * be reused. But if so, why allocating 2 buffers?...
108 *
109 * should the lock/unlock calls take care of the copy-back?
110 *
111 *
112 * In the end, the client wants to know if the backbuffer is preserved
113 * though... it's complicated.
114 *
115 */
116
117 //flags |= SURFACE_FLAG_PRESERVE_CONTENT;
118
119
120 const_cast<uint32_t&>(android_native_window_t::flags) = 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
128 android_native_window_t::connect = connect;
129 android_native_window_t::disconnect = disconnect;
130 android_native_window_t::setSwapInterval = setSwapInterval;
131 android_native_window_t::setSwapRectangle = setSwapRectangle;
132 android_native_window_t::dequeueBuffer = dequeueBuffer;
133 android_native_window_t::lockBuffer = lockBuffer;
134 android_native_window_t::queueBuffer = queueBuffer;
135}
136
137FramebufferNativeWindow::~FramebufferNativeWindow() {
138 grDev->free(grDev, buffers[0]->handle);
139 grDev->free(grDev, buffers[1]->handle);
140 gralloc_module_t* m =
141 reinterpret_cast<gralloc_module_t*>(grDev->common.module);
142 m->unmap(m, buffers[0]->handle);
143 m->unmap(m, buffers[1]->handle);
144 gralloc_close(grDev);
145 framebuffer_close(fbDev);
146}
147
148void FramebufferNativeWindow::connect(android_native_window_t* window)
149{
150}
151
152void FramebufferNativeWindow::disconnect(android_native_window_t* window)
153{
154}
155
156int FramebufferNativeWindow::setSwapInterval(
157 android_native_window_t* window, int interval)
158{
159 framebuffer_device_t* fb = getSelf(window)->fbDev;
160 return fb->setSwapInterval(fb, interval);
161}
162
163int FramebufferNativeWindow::setSwapRectangle(android_native_window_t* window,
164 int l, int t, int w, int h)
165{
166 FramebufferNativeWindow* self = getSelf(window);
167 Mutex::Autolock _l(self->mutex);
168 self->mDirty = Rect(l, t, l+w, t+h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 return 0;
170}
171
Mathias Agopian1473f462009-04-10 14:24:30 -0700172int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window,
173 android_native_buffer_t** buffer)
174{
175 FramebufferNativeWindow* self = getSelf(window);
176 Mutex::Autolock _l(self->mutex);
177 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
Mathias Agopian1473f462009-04-10 14:24:30 -0700179 // wait for a free buffer
180 while (!self->mNumFreeBuffers) {
181 self->mCondition.wait(self->mutex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700183 // get this buffer
184 self->mNumFreeBuffers--;
185 int index = self->mBufferHead++;
186 if (self->mBufferHead >= self->mNumBuffers)
187 self->mBufferHead = 0;
188
189 *buffer = self->buffers[index].get();
190
191 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192}
193
Mathias Agopian1473f462009-04-10 14:24:30 -0700194int FramebufferNativeWindow::lockBuffer(android_native_window_t* window,
195 android_native_buffer_t* buffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196{
Mathias Agopian1473f462009-04-10 14:24:30 -0700197 FramebufferNativeWindow* self = getSelf(window);
198 Mutex::Autolock _l(self->mutex);
199
200 // wait that the buffer we're locking is not front anymore
201 while (self->front == buffer) {
202 self->mCondition.wait(self->mutex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700204
205 gralloc_module_t* m =
206 reinterpret_cast<gralloc_module_t*>(self->grDev->common.module);
207 const Rect& dirty(self->mDirty);
208
209 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
210 int res = m->lock(m, handle, GRALLOC_USAGE_HW_FB,
211 dirty.left, dirty.right, dirty.width(), dirty.height());
212
213 return res;
214}
215
216int FramebufferNativeWindow::queueBuffer(android_native_window_t* window,
217 android_native_buffer_t* buffer)
218{
219 FramebufferNativeWindow* self = getSelf(window);
220 Mutex::Autolock _l(self->mutex);
221 framebuffer_device_t* fb = self->fbDev;
222 gralloc_module_t* m =
223 reinterpret_cast<gralloc_module_t*>(self->grDev->common.module);
224
225 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
226 m->unlock(m, handle);
227 int res = fb->post(fb, handle);
228
229 self->front = static_cast<NativeBuffer*>(buffer);
230 self->mNumFreeBuffers++;
231 self->mCondition.broadcast();
232 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233}
234
235// ----------------------------------------------------------------------------
236}; // namespace android
237// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -0700238
239
240EGLNativeWindowType android_createDisplaySurface(void)
241{
242 return new android::FramebufferNativeWindow();
243}
244