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> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include <ui/SurfaceComposerClient.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | #include <ui/Rect.h> |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 31 | #include <ui/FramebufferNativeWindow.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | #include <EGL/egl.h> |
| 34 | |
| 35 | #include <pixelflinger/format.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 36 | #include <pixelflinger/pixelflinger.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 38 | #include <hardware/hardware.h> |
| 39 | #include <hardware/gralloc.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Mathias Agopian | b51e18d | 2009-05-05 18:21:32 -0700 | [diff] [blame] | 41 | #include <private/ui/android_natives_priv.h> |
| 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | // ---------------------------------------------------------------------------- |
| 44 | namespace android { |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | |
Mathias Agopian | ac2523b | 2009-05-05 18:11:11 -0700 | [diff] [blame] | 47 | class NativeBuffer |
| 48 | : public EGLNativeBase< |
| 49 | android_native_buffer_t, |
| 50 | NativeBuffer, |
| 51 | LightRefBase<NativeBuffer> > |
| 52 | { |
| 53 | public: |
| 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 | } |
| 60 | private: |
| 61 | friend class LightRefBase<NativeBuffer>; |
| 62 | ~NativeBuffer() { }; // this class cannot be overloaded |
| 63 | }; |
| 64 | |
| 65 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 66 | /* |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 78 | FramebufferNativeWindow::FramebufferNativeWindow() |
Mathias Agopian | 97b8056 | 2009-05-07 17:40:23 -0700 | [diff] [blame^] | 79 | : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 81 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 88 | |
Mathias Agopian | 97b8056 | 2009-05-07 17:40:23 -0700 | [diff] [blame^] | 89 | mUpdateOnDemand = (fbDev->setUpdateRect != 0); |
| 90 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 91 | // initialize the buffer FIFO |
| 92 | mNumBuffers = 2; |
| 93 | mNumFreeBuffers = 2; |
| 94 | mBufferHead = mNumBuffers-1; |
| 95 | buffers[0] = new NativeBuffer( |
| 96 | fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB); |
| 97 | buffers[1] = new NativeBuffer( |
| 98 | fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB); |
| 99 | |
| 100 | err = grDev->alloc(grDev, |
| 101 | fbDev->width, fbDev->height, fbDev->format, |
| 102 | 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] | 103 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 104 | LOGE_IF(err, "fb buffer 0 allocation failed w=%d, h=%d, err=%s", |
| 105 | fbDev->width, fbDev->height, strerror(-err)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 107 | err = grDev->alloc(grDev, |
| 108 | fbDev->width, fbDev->height, fbDev->format, |
| 109 | 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] | 110 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 111 | LOGE_IF(err, "fb buffer 1 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 | |
Mathias Agopian | 9bd5da4 | 2009-05-05 18:29:35 -0700 | [diff] [blame] | 115 | const_cast<uint32_t&>(android_native_window_t::flags) = fbDev->flags; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 116 | const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi; |
| 117 | const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi; |
| 118 | const_cast<int&>(android_native_window_t::minSwapInterval) = |
| 119 | fbDev->minSwapInterval; |
| 120 | const_cast<int&>(android_native_window_t::maxSwapInterval) = |
| 121 | fbDev->maxSwapInterval; |
| 122 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 123 | android_native_window_t::setSwapInterval = setSwapInterval; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 124 | android_native_window_t::dequeueBuffer = dequeueBuffer; |
| 125 | android_native_window_t::lockBuffer = lockBuffer; |
| 126 | android_native_window_t::queueBuffer = queueBuffer; |
| 127 | } |
| 128 | |
| 129 | FramebufferNativeWindow::~FramebufferNativeWindow() { |
| 130 | grDev->free(grDev, buffers[0]->handle); |
| 131 | grDev->free(grDev, buffers[1]->handle); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 132 | gralloc_close(grDev); |
| 133 | framebuffer_close(fbDev); |
| 134 | } |
| 135 | |
Mathias Agopian | 97b8056 | 2009-05-07 17:40:23 -0700 | [diff] [blame^] | 136 | status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r) |
| 137 | { |
| 138 | if (!mUpdateOnDemand) { |
| 139 | return INVALID_OPERATION; |
| 140 | } |
| 141 | return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height()); |
| 142 | } |
| 143 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 144 | int FramebufferNativeWindow::setSwapInterval( |
| 145 | android_native_window_t* window, int interval) |
| 146 | { |
| 147 | framebuffer_device_t* fb = getSelf(window)->fbDev; |
| 148 | return fb->setSwapInterval(fb, interval); |
| 149 | } |
| 150 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 151 | int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window, |
| 152 | android_native_buffer_t** buffer) |
| 153 | { |
| 154 | FramebufferNativeWindow* self = getSelf(window); |
| 155 | Mutex::Autolock _l(self->mutex); |
| 156 | framebuffer_device_t* fb = self->fbDev; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 158 | // wait for a free buffer |
| 159 | while (!self->mNumFreeBuffers) { |
| 160 | self->mCondition.wait(self->mutex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 162 | // get this buffer |
| 163 | self->mNumFreeBuffers--; |
| 164 | int index = self->mBufferHead++; |
| 165 | if (self->mBufferHead >= self->mNumBuffers) |
| 166 | self->mBufferHead = 0; |
| 167 | |
| 168 | *buffer = self->buffers[index].get(); |
| 169 | |
| 170 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 173 | int FramebufferNativeWindow::lockBuffer(android_native_window_t* window, |
| 174 | android_native_buffer_t* buffer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 176 | FramebufferNativeWindow* self = getSelf(window); |
| 177 | Mutex::Autolock _l(self->mutex); |
| 178 | |
| 179 | // wait that the buffer we're locking is not front anymore |
| 180 | while (self->front == buffer) { |
| 181 | self->mCondition.wait(self->mutex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 183 | |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 184 | return NO_ERROR; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | int FramebufferNativeWindow::queueBuffer(android_native_window_t* window, |
| 188 | android_native_buffer_t* buffer) |
| 189 | { |
| 190 | FramebufferNativeWindow* self = getSelf(window); |
| 191 | Mutex::Autolock _l(self->mutex); |
| 192 | framebuffer_device_t* fb = self->fbDev; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 193 | buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 194 | int res = fb->post(fb, handle); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 195 | self->front = static_cast<NativeBuffer*>(buffer); |
| 196 | self->mNumFreeBuffers++; |
| 197 | self->mCondition.broadcast(); |
| 198 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // ---------------------------------------------------------------------------- |
| 202 | }; // namespace android |
| 203 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 204 | |
| 205 | |
| 206 | EGLNativeWindowType android_createDisplaySurface(void) |
| 207 | { |
| 208 | return new android::FramebufferNativeWindow(); |
| 209 | } |
| 210 | |