blob: f235cb44a5466532afc96f18bdfe0117ac33489a [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 Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057FramebufferNativeWindow::FramebufferNativeWindow()
58 : BASE(), fbDev(0), grDev(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059{
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080080
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080083
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080087
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080090 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091
92 uint32_t flags = fbDev->flags & SURFACE_FLAG_MAPPED;
93
94 /*
95 * FIXME: SURFACE_FLAG_PRESERVE_CONTENT
96 * how to implement this, there is no concept of preserve content in
97 * the framebuffer, which just "posts" buffer.
98 *
99 * It looks like what we need is a way to know if the posted buffer can
100 * be reused. But if so, why allocating 2 buffers?...
101 *
102 * should the lock/unlock calls take care of the copy-back?
103 *
104 *
105 * In the end, the client wants to know if the backbuffer is preserved
106 * though... it's complicated.
107 *
108 */
109
110 //flags |= SURFACE_FLAG_PRESERVE_CONTENT;
111
112
113 const_cast<uint32_t&>(android_native_window_t::flags) = flags;
114 const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi;
115 const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi;
116 const_cast<int&>(android_native_window_t::minSwapInterval) =
117 fbDev->minSwapInterval;
118 const_cast<int&>(android_native_window_t::maxSwapInterval) =
119 fbDev->maxSwapInterval;
120
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122 android_native_window_t::dequeueBuffer = dequeueBuffer;
123 android_native_window_t::lockBuffer = lockBuffer;
124 android_native_window_t::queueBuffer = queueBuffer;
125}
126
127FramebufferNativeWindow::~FramebufferNativeWindow() {
128 grDev->free(grDev, buffers[0]->handle);
129 grDev->free(grDev, buffers[1]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130 gralloc_close(grDev);
131 framebuffer_close(fbDev);
132}
133
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134int FramebufferNativeWindow::setSwapInterval(
135 android_native_window_t* window, int interval)
136{
137 framebuffer_device_t* fb = getSelf(window)->fbDev;
138 return fb->setSwapInterval(fb, interval);
139}
140
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window,
142 android_native_buffer_t** buffer)
143{
144 FramebufferNativeWindow* self = getSelf(window);
145 Mutex::Autolock _l(self->mutex);
146 framebuffer_device_t* fb = self->fbDev;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700148 // wait for a free buffer
149 while (!self->mNumFreeBuffers) {
150 self->mCondition.wait(self->mutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700152 // get this buffer
153 self->mNumFreeBuffers--;
154 int index = self->mBufferHead++;
155 if (self->mBufferHead >= self->mNumBuffers)
156 self->mBufferHead = 0;
157
158 *buffer = self->buffers[index].get();
159
160 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161}
162
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700163int FramebufferNativeWindow::lockBuffer(android_native_window_t* window,
164 android_native_buffer_t* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700166 FramebufferNativeWindow* self = getSelf(window);
167 Mutex::Autolock _l(self->mutex);
168
169 // wait that the buffer we're locking is not front anymore
170 while (self->front == buffer) {
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
Mathias Agopian0926f502009-05-04 14:17:04 -0700174 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700175}
176
177int FramebufferNativeWindow::queueBuffer(android_native_window_t* window,
178 android_native_buffer_t* buffer)
179{
180 FramebufferNativeWindow* self = getSelf(window);
181 Mutex::Autolock _l(self->mutex);
182 framebuffer_device_t* fb = self->fbDev;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700183 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184 int res = fb->post(fb, handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700185 self->front = static_cast<NativeBuffer*>(buffer);
186 self->mNumFreeBuffers++;
187 self->mCondition.broadcast();
188 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189}
190
191// ----------------------------------------------------------------------------
192}; // namespace android
193// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194
195
196EGLNativeWindowType android_createDisplaySurface(void)
197{
198 return new android::FramebufferNativeWindow();
199}
200