The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 18 | #define LOG_TAG "FramebufferNativeWindow" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 23 | #include <errno.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | #include <cutils/log.h> |
| 26 | #include <cutils/atomic.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 27 | #include <utils/threads.h> |
Mathias Agopian | 6693f23 | 2009-08-06 20:46:44 -0700 | [diff] [blame] | 28 | #include <utils/RefBase.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | #include <ui/SurfaceComposerClient.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <ui/Rect.h> |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 32 | #include <ui/FramebufferNativeWindow.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | #include <EGL/egl.h> |
| 35 | |
| 36 | #include <pixelflinger/format.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 | #include <pixelflinger/pixelflinger.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 39 | #include <hardware/hardware.h> |
| 40 | #include <hardware/gralloc.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Mathias Agopian | b51e18d | 2009-05-05 18:21:32 -0700 | [diff] [blame] | 42 | #include <private/ui/android_natives_priv.h> |
| 43 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | // ---------------------------------------------------------------------------- |
| 45 | namespace android { |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
Mathias Agopian | ac2523b | 2009-05-05 18:11:11 -0700 | [diff] [blame] | 48 | class NativeBuffer |
| 49 | : public EGLNativeBase< |
| 50 | android_native_buffer_t, |
| 51 | NativeBuffer, |
| 52 | LightRefBase<NativeBuffer> > |
| 53 | { |
| 54 | public: |
| 55 | NativeBuffer(int w, int h, int f, int u) : BASE() { |
| 56 | android_native_buffer_t::width = w; |
| 57 | android_native_buffer_t::height = h; |
| 58 | android_native_buffer_t::format = f; |
| 59 | android_native_buffer_t::usage = u; |
| 60 | } |
| 61 | private: |
| 62 | friend class LightRefBase<NativeBuffer>; |
| 63 | ~NativeBuffer() { }; // this class cannot be overloaded |
| 64 | }; |
| 65 | |
| 66 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 67 | /* |
| 68 | * This implements the (main) framebuffer management. This class is used |
| 69 | * mostly by SurfaceFlinger, but also by command line GL application. |
| 70 | * |
| 71 | * In fact this is an implementation of android_native_window_t on top of |
| 72 | * the framebuffer. |
| 73 | * |
| 74 | * Currently it is pretty simple, it manages only two buffers (the front and |
| 75 | * back buffer). |
| 76 | * |
| 77 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 79 | FramebufferNativeWindow::FramebufferNativeWindow() |
Mathias Agopian | 97b8056 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 80 | : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 82 | hw_module_t const* module; |
| 83 | if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) { |
| 84 | int stride; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 85 | int err; |
Mathias Agopian | 6693f23 | 2009-08-06 20:46:44 -0700 | [diff] [blame] | 86 | err = framebuffer_open(module, &fbDev); |
| 87 | LOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err)); |
| 88 | |
| 89 | err = gralloc_open(module, &grDev); |
| 90 | LOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
Mathias Agopian | 6693f23 | 2009-08-06 20:46:44 -0700 | [diff] [blame] | 92 | // bail out if we can't initialize the modules |
| 93 | if (!fbDev || !grDev) |
| 94 | return; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 95 | |
Mathias Agopian | 97b8056 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 96 | mUpdateOnDemand = (fbDev->setUpdateRect != 0); |
| 97 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 98 | // initialize the buffer FIFO |
| 99 | mNumBuffers = 2; |
| 100 | mNumFreeBuffers = 2; |
| 101 | mBufferHead = mNumBuffers-1; |
| 102 | buffers[0] = new NativeBuffer( |
| 103 | fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB); |
| 104 | buffers[1] = new NativeBuffer( |
| 105 | fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB); |
| 106 | |
| 107 | err = grDev->alloc(grDev, |
| 108 | fbDev->width, fbDev->height, fbDev->format, |
| 109 | GRALLOC_USAGE_HW_FB, &buffers[0]->handle, &buffers[0]->stride); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 111 | LOGE_IF(err, "fb buffer 0 allocation failed w=%d, h=%d, err=%s", |
| 112 | fbDev->width, fbDev->height, strerror(-err)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 114 | err = grDev->alloc(grDev, |
| 115 | fbDev->width, fbDev->height, fbDev->format, |
| 116 | GRALLOC_USAGE_HW_FB, &buffers[1]->handle, &buffers[1]->stride); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 118 | LOGE_IF(err, "fb buffer 1 allocation failed w=%d, h=%d, err=%s", |
| 119 | fbDev->width, fbDev->height, strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 120 | |
Marco Nelissen | e613468 | 2009-09-23 10:54:36 -0700 | [diff] [blame] | 121 | const_cast<uint32_t&>(android_native_window_t::flags) = fbDev->flags; |
| 122 | const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi; |
| 123 | const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi; |
| 124 | const_cast<int&>(android_native_window_t::minSwapInterval) = |
| 125 | fbDev->minSwapInterval; |
| 126 | const_cast<int&>(android_native_window_t::maxSwapInterval) = |
| 127 | fbDev->maxSwapInterval; |
| 128 | } else { |
| 129 | LOGE("Couldn't get gralloc module"); |
| 130 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 131 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 132 | android_native_window_t::setSwapInterval = setSwapInterval; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 133 | android_native_window_t::dequeueBuffer = dequeueBuffer; |
| 134 | android_native_window_t::lockBuffer = lockBuffer; |
| 135 | android_native_window_t::queueBuffer = queueBuffer; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 136 | android_native_window_t::query = query; |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 137 | android_native_window_t::perform = perform; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Mathias Agopian | 6693f23 | 2009-08-06 20:46:44 -0700 | [diff] [blame] | 140 | FramebufferNativeWindow::~FramebufferNativeWindow() |
| 141 | { |
| 142 | if (grDev) { |
| 143 | if (buffers[0] != NULL) |
| 144 | grDev->free(grDev, buffers[0]->handle); |
| 145 | if (buffers[1] != NULL) |
| 146 | grDev->free(grDev, buffers[1]->handle); |
| 147 | gralloc_close(grDev); |
| 148 | } |
| 149 | |
| 150 | if (fbDev) { |
| 151 | framebuffer_close(fbDev); |
| 152 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Mathias Agopian | 97b8056 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 155 | status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r) |
| 156 | { |
| 157 | if (!mUpdateOnDemand) { |
| 158 | return INVALID_OPERATION; |
| 159 | } |
| 160 | return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height()); |
| 161 | } |
| 162 | |
Mathias Agopian | b1a1874 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 163 | status_t FramebufferNativeWindow::compositionComplete() |
| 164 | { |
| 165 | if (fbDev->compositionComplete) { |
| 166 | return fbDev->compositionComplete(fbDev); |
| 167 | } |
| 168 | return INVALID_OPERATION; |
| 169 | } |
| 170 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 171 | int FramebufferNativeWindow::setSwapInterval( |
| 172 | android_native_window_t* window, int interval) |
| 173 | { |
| 174 | framebuffer_device_t* fb = getSelf(window)->fbDev; |
| 175 | return fb->setSwapInterval(fb, interval); |
| 176 | } |
| 177 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 178 | int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window, |
| 179 | android_native_buffer_t** buffer) |
| 180 | { |
| 181 | FramebufferNativeWindow* self = getSelf(window); |
| 182 | Mutex::Autolock _l(self->mutex); |
| 183 | framebuffer_device_t* fb = self->fbDev; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 185 | // wait for a free buffer |
| 186 | while (!self->mNumFreeBuffers) { |
| 187 | self->mCondition.wait(self->mutex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 189 | // get this buffer |
| 190 | self->mNumFreeBuffers--; |
| 191 | int index = self->mBufferHead++; |
| 192 | if (self->mBufferHead >= self->mNumBuffers) |
| 193 | self->mBufferHead = 0; |
| 194 | |
| 195 | *buffer = self->buffers[index].get(); |
| 196 | |
| 197 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 200 | int FramebufferNativeWindow::lockBuffer(android_native_window_t* window, |
| 201 | android_native_buffer_t* buffer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 203 | FramebufferNativeWindow* self = getSelf(window); |
| 204 | Mutex::Autolock _l(self->mutex); |
| 205 | |
| 206 | // wait that the buffer we're locking is not front anymore |
| 207 | while (self->front == buffer) { |
| 208 | self->mCondition.wait(self->mutex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 210 | |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 211 | return NO_ERROR; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | int FramebufferNativeWindow::queueBuffer(android_native_window_t* window, |
| 215 | android_native_buffer_t* buffer) |
| 216 | { |
| 217 | FramebufferNativeWindow* self = getSelf(window); |
| 218 | Mutex::Autolock _l(self->mutex); |
| 219 | framebuffer_device_t* fb = self->fbDev; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 220 | buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 221 | int res = fb->post(fb, handle); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 222 | self->front = static_cast<NativeBuffer*>(buffer); |
| 223 | self->mNumFreeBuffers++; |
| 224 | self->mCondition.broadcast(); |
| 225 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 228 | int FramebufferNativeWindow::query(android_native_window_t* window, |
| 229 | int what, int* value) |
| 230 | { |
| 231 | FramebufferNativeWindow* self = getSelf(window); |
| 232 | Mutex::Autolock _l(self->mutex); |
| 233 | framebuffer_device_t* fb = self->fbDev; |
| 234 | switch (what) { |
| 235 | case NATIVE_WINDOW_WIDTH: |
| 236 | *value = fb->width; |
| 237 | return NO_ERROR; |
| 238 | case NATIVE_WINDOW_HEIGHT: |
| 239 | *value = fb->height; |
| 240 | return NO_ERROR; |
Mathias Agopian | 25ec00f | 2009-08-06 16:04:29 -0700 | [diff] [blame] | 241 | case NATIVE_WINDOW_FORMAT: |
| 242 | *value = fb->format; |
| 243 | return NO_ERROR; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 244 | } |
Mathias Agopian | 6693f23 | 2009-08-06 20:46:44 -0700 | [diff] [blame] | 245 | *value = 0; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 246 | return BAD_VALUE; |
| 247 | } |
| 248 | |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 249 | int FramebufferNativeWindow::perform(android_native_window_t* window, |
| 250 | int operation, ...) |
| 251 | { |
| 252 | switch (operation) { |
| 253 | case NATIVE_WINDOW_SET_USAGE: |
| 254 | break; |
| 255 | default: |
| 256 | return NAME_NOT_FOUND; |
| 257 | } |
| 258 | return NO_ERROR; |
| 259 | } |
| 260 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | // ---------------------------------------------------------------------------- |
| 262 | }; // namespace android |
| 263 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 264 | |
Mathias Agopian | 6693f23 | 2009-08-06 20:46:44 -0700 | [diff] [blame] | 265 | using namespace android; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 266 | |
| 267 | EGLNativeWindowType android_createDisplaySurface(void) |
| 268 | { |
Mathias Agopian | 6693f23 | 2009-08-06 20:46:44 -0700 | [diff] [blame] | 269 | FramebufferNativeWindow* w; |
| 270 | w = new FramebufferNativeWindow(); |
| 271 | if (w->getDevice() == NULL) { |
| 272 | // get a ref so it can be destroyed when we exit this block |
| 273 | sp<FramebufferNativeWindow> ref(w); |
| 274 | return NULL; |
| 275 | } |
| 276 | return (EGLNativeWindowType)w; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 277 | } |