blob: 4e692e2c27a19bf82bf3bf5b674854cc891f78b9 [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
Mathias Agopian58a79f42009-05-05 18:21:32 -070041#include <private/ui/android_natives_priv.h>
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043// ----------------------------------------------------------------------------
44namespace android {
45// ----------------------------------------------------------------------------
46
Mathias Agopian7189c002009-05-05 18:11:11 -070047class NativeBuffer
48 : public EGLNativeBase<
49 android_native_buffer_t,
50 NativeBuffer,
51 LightRefBase<NativeBuffer> >
52{
53public:
54 NativeBuffer(int w, int h, int f, int u) : BASE() {
55 android_native_buffer_t::width = w;
56 android_native_buffer_t::height = h;
57 android_native_buffer_t::format = f;
58 android_native_buffer_t::usage = u;
59 }
60private:
61 friend class LightRefBase<NativeBuffer>;
62 ~NativeBuffer() { }; // this class cannot be overloaded
63};
64
65
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066/*
67 * This implements the (main) framebuffer management. This class is used
68 * mostly by SurfaceFlinger, but also by command line GL application.
69 *
70 * In fact this is an implementation of android_native_window_t on top of
71 * the framebuffer.
72 *
73 * Currently it is pretty simple, it manages only two buffers (the front and
74 * back buffer).
75 *
76 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078FramebufferNativeWindow::FramebufferNativeWindow()
79 : BASE(), fbDev(0), grDev(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070081 hw_module_t const* module;
82 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
83 int stride;
84 framebuffer_open(module, &fbDev);
85 gralloc_open(module, &grDev);
86 int err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088
89 // initialize the buffer FIFO
90 mNumBuffers = 2;
91 mNumFreeBuffers = 2;
92 mBufferHead = mNumBuffers-1;
93 buffers[0] = new NativeBuffer(
94 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
95 buffers[1] = new NativeBuffer(
96 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
97
98 err = grDev->alloc(grDev,
99 fbDev->width, fbDev->height, fbDev->format,
100 GRALLOC_USAGE_HW_FB, &buffers[0]->handle, &buffers[0]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102 LOGE_IF(err, "fb buffer 0 allocation failed w=%d, h=%d, err=%s",
103 fbDev->width, fbDev->height, strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105 err = grDev->alloc(grDev,
106 fbDev->width, fbDev->height, fbDev->format,
107 GRALLOC_USAGE_HW_FB, &buffers[1]->handle, &buffers[1]->stride);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700109 LOGE_IF(err, "fb buffer 1 allocation failed w=%d, h=%d, err=%s",
110 fbDev->width, fbDev->height, strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112
113 uint32_t flags = fbDev->flags & SURFACE_FLAG_MAPPED;
114
115 /*
116 * FIXME: SURFACE_FLAG_PRESERVE_CONTENT
117 * how to implement this, there is no concept of preserve content in
118 * the framebuffer, which just "posts" buffer.
119 *
120 * It looks like what we need is a way to know if the posted buffer can
121 * be reused. But if so, why allocating 2 buffers?...
122 *
123 * should the lock/unlock calls take care of the copy-back?
124 *
125 *
126 * In the end, the client wants to know if the backbuffer is preserved
127 * though... it's complicated.
128 *
129 */
130
131 //flags |= SURFACE_FLAG_PRESERVE_CONTENT;
132
133
134 const_cast<uint32_t&>(android_native_window_t::flags) = flags;
135 const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi;
136 const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi;
137 const_cast<int&>(android_native_window_t::minSwapInterval) =
138 fbDev->minSwapInterval;
139 const_cast<int&>(android_native_window_t::maxSwapInterval) =
140 fbDev->maxSwapInterval;
141
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143 android_native_window_t::dequeueBuffer = dequeueBuffer;
144 android_native_window_t::lockBuffer = lockBuffer;
145 android_native_window_t::queueBuffer = queueBuffer;
146}
147
148FramebufferNativeWindow::~FramebufferNativeWindow() {
149 grDev->free(grDev, buffers[0]->handle);
150 grDev->free(grDev, buffers[1]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700151 gralloc_close(grDev);
152 framebuffer_close(fbDev);
153}
154
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700155int FramebufferNativeWindow::setSwapInterval(
156 android_native_window_t* window, int interval)
157{
158 framebuffer_device_t* fb = getSelf(window)->fbDev;
159 return fb->setSwapInterval(fb, interval);
160}
161
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700162int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window,
163 android_native_buffer_t** buffer)
164{
165 FramebufferNativeWindow* self = getSelf(window);
166 Mutex::Autolock _l(self->mutex);
167 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700169 // wait for a free buffer
170 while (!self->mNumFreeBuffers) {
171 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700173 // get this buffer
174 self->mNumFreeBuffers--;
175 int index = self->mBufferHead++;
176 if (self->mBufferHead >= self->mNumBuffers)
177 self->mBufferHead = 0;
178
179 *buffer = self->buffers[index].get();
180
181 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182}
183
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184int FramebufferNativeWindow::lockBuffer(android_native_window_t* window,
185 android_native_buffer_t* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187 FramebufferNativeWindow* self = getSelf(window);
188 Mutex::Autolock _l(self->mutex);
189
190 // wait that the buffer we're locking is not front anymore
191 while (self->front == buffer) {
192 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194
Mathias Agopian0926f502009-05-04 14:17:04 -0700195 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196}
197
198int FramebufferNativeWindow::queueBuffer(android_native_window_t* window,
199 android_native_buffer_t* buffer)
200{
201 FramebufferNativeWindow* self = getSelf(window);
202 Mutex::Autolock _l(self->mutex);
203 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700205 int res = fb->post(fb, handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 self->front = static_cast<NativeBuffer*>(buffer);
207 self->mNumFreeBuffers++;
208 self->mCondition.broadcast();
209 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210}
211
212// ----------------------------------------------------------------------------
213}; // namespace android
214// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215
216
217EGLNativeWindowType android_createDisplaySurface(void)
218{
219 return new android::FramebufferNativeWindow();
220}
221