Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 17 | #define LOG_TAG "GraphicBuffer" |
| 18 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 23 | #include <utils/Errors.h> |
| 24 | #include <utils/Log.h> |
| 25 | |
| 26 | #include <ui/GraphicBuffer.h> |
| 27 | #include <ui/GraphicBufferAllocator.h> |
| 28 | #include <ui/GraphicBufferMapper.h> |
| 29 | #include <ui/PixelFormat.h> |
| 30 | |
| 31 | #include <pixelflinger/pixelflinger.h> |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | // =========================================================================== |
| 36 | // Buffer and implementation of android_native_buffer_t |
| 37 | // =========================================================================== |
| 38 | |
| 39 | GraphicBuffer::GraphicBuffer() |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 40 | : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()), |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 41 | mInitCheck(NO_ERROR), mIndex(-1) |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 42 | { |
| 43 | width = |
| 44 | height = |
| 45 | stride = |
| 46 | format = |
| 47 | usage = 0; |
| 48 | handle = NULL; |
| 49 | } |
| 50 | |
| 51 | GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h, |
| 52 | PixelFormat reqFormat, uint32_t reqUsage) |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 53 | : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()), |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 54 | mInitCheck(NO_ERROR), mIndex(-1) |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 55 | { |
| 56 | width = |
| 57 | height = |
| 58 | stride = |
| 59 | format = |
| 60 | usage = 0; |
| 61 | handle = NULL; |
| 62 | mInitCheck = initSize(w, h, reqFormat, reqUsage); |
| 63 | } |
| 64 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 65 | GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h, |
| 66 | PixelFormat inFormat, uint32_t inUsage, |
| 67 | uint32_t inStride, native_handle_t* inHandle, bool keepOwnership) |
| 68 | : BASE(), mOwner(keepOwnership ? ownHandle : ownNone), |
| 69 | mBufferMapper(GraphicBufferMapper::get()), |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 70 | mInitCheck(NO_ERROR), mIndex(-1) |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 71 | { |
| 72 | width = w; |
| 73 | height = h; |
| 74 | stride = inStride; |
| 75 | format = inFormat; |
| 76 | usage = inUsage; |
| 77 | handle = inHandle; |
| 78 | } |
| 79 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 80 | GraphicBuffer::~GraphicBuffer() |
| 81 | { |
| 82 | if (handle) { |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 83 | free_handle(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void GraphicBuffer::free_handle() |
| 88 | { |
| 89 | if (mOwner == ownHandle) { |
| 90 | native_handle_close(handle); |
| 91 | native_handle_delete(const_cast<native_handle*>(handle)); |
| 92 | } else if (mOwner == ownData) { |
| 93 | GraphicBufferAllocator& allocator(GraphicBufferAllocator::get()); |
| 94 | allocator.free(handle); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | status_t GraphicBuffer::initCheck() const { |
| 99 | return mInitCheck; |
| 100 | } |
| 101 | |
| 102 | android_native_buffer_t* GraphicBuffer::getNativeBuffer() const |
| 103 | { |
| 104 | return static_cast<android_native_buffer_t*>( |
| 105 | const_cast<GraphicBuffer*>(this)); |
| 106 | } |
| 107 | |
| 108 | status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f, |
| 109 | uint32_t reqUsage) |
| 110 | { |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 111 | if (mOwner != ownData) |
| 112 | return INVALID_OPERATION; |
| 113 | |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 114 | if (handle && w==width && h==height && f==format && reqUsage==usage) |
| 115 | return NO_ERROR; |
| 116 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 117 | if (handle) { |
| 118 | GraphicBufferAllocator& allocator(GraphicBufferAllocator::get()); |
| 119 | allocator.free(handle); |
| 120 | handle = 0; |
| 121 | } |
| 122 | return initSize(w, h, f, reqUsage); |
| 123 | } |
| 124 | |
| 125 | status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format, |
| 126 | uint32_t reqUsage) |
| 127 | { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 128 | GraphicBufferAllocator& allocator = GraphicBufferAllocator::get(); |
| 129 | status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride); |
| 130 | if (err == NO_ERROR) { |
| 131 | this->width = w; |
| 132 | this->height = h; |
| 133 | this->format = format; |
| 134 | this->usage = reqUsage; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 135 | } |
| 136 | return err; |
| 137 | } |
| 138 | |
| 139 | status_t GraphicBuffer::lock(uint32_t usage, void** vaddr) |
| 140 | { |
| 141 | const Rect lockBounds(width, height); |
| 142 | status_t res = lock(usage, lockBounds, vaddr); |
| 143 | return res; |
| 144 | } |
| 145 | |
| 146 | status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr) |
| 147 | { |
| 148 | if (rect.left < 0 || rect.right > this->width || |
| 149 | rect.top < 0 || rect.bottom > this->height) { |
| 150 | LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
| 151 | rect.left, rect.top, rect.right, rect.bottom, |
| 152 | this->width, this->height); |
| 153 | return BAD_VALUE; |
| 154 | } |
| 155 | status_t res = getBufferMapper().lock(handle, usage, rect, vaddr); |
| 156 | return res; |
| 157 | } |
| 158 | |
| 159 | status_t GraphicBuffer::unlock() |
| 160 | { |
| 161 | status_t res = getBufferMapper().unlock(handle); |
| 162 | return res; |
| 163 | } |
| 164 | |
| 165 | status_t GraphicBuffer::lock(GGLSurface* sur, uint32_t usage) |
| 166 | { |
| 167 | void* vaddr; |
| 168 | status_t res = GraphicBuffer::lock(usage, &vaddr); |
| 169 | if (res == NO_ERROR && sur) { |
| 170 | sur->version = sizeof(GGLSurface); |
| 171 | sur->width = width; |
| 172 | sur->height = height; |
| 173 | sur->stride = stride; |
| 174 | sur->format = format; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 175 | sur->data = static_cast<GGLubyte*>(vaddr); |
| 176 | } |
| 177 | return res; |
| 178 | } |
| 179 | |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 180 | size_t GraphicBuffer::getFlattenedSize() const { |
| 181 | return (8 + (handle ? handle->numInts : 0))*sizeof(int); |
| 182 | } |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 183 | |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 184 | size_t GraphicBuffer::getFdCount() const { |
| 185 | return handle ? handle->numFds : 0; |
| 186 | } |
| 187 | |
| 188 | status_t GraphicBuffer::flatten(void* buffer, size_t size, |
| 189 | int fds[], size_t count) const |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 190 | { |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 191 | size_t sizeNeeded = GraphicBuffer::getFlattenedSize(); |
| 192 | if (size < sizeNeeded) return NO_MEMORY; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 193 | |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 194 | size_t fdCountNeeded = GraphicBuffer::getFdCount(); |
| 195 | if (count < fdCountNeeded) return NO_MEMORY; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 196 | |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 197 | int* buf = static_cast<int*>(buffer); |
| 198 | buf[0] = 'GBFR'; |
| 199 | buf[1] = width; |
| 200 | buf[2] = height; |
| 201 | buf[3] = stride; |
| 202 | buf[4] = format; |
| 203 | buf[5] = usage; |
| 204 | buf[6] = 0; |
| 205 | buf[7] = 0; |
| 206 | |
| 207 | if (handle) { |
| 208 | buf[6] = handle->numFds; |
| 209 | buf[7] = handle->numInts; |
| 210 | native_handle_t const* const h = handle; |
| 211 | memcpy(fds, h->data, h->numFds*sizeof(int)); |
| 212 | memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int)); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 213 | } |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 214 | |
| 215 | return NO_ERROR; |
| 216 | } |
| 217 | |
| 218 | status_t GraphicBuffer::unflatten(void const* buffer, size_t size, |
| 219 | int fds[], size_t count) |
| 220 | { |
| 221 | if (size < 8*sizeof(int)) return NO_MEMORY; |
| 222 | |
| 223 | int const* buf = static_cast<int const*>(buffer); |
| 224 | if (buf[0] != 'GBFR') return BAD_TYPE; |
| 225 | |
| 226 | const size_t numFds = buf[6]; |
| 227 | const size_t numInts = buf[7]; |
| 228 | |
| 229 | const size_t sizeNeeded = (8 + numInts) * sizeof(int); |
| 230 | if (size < sizeNeeded) return NO_MEMORY; |
| 231 | |
| 232 | size_t fdCountNeeded = 0; |
| 233 | if (count < fdCountNeeded) return NO_MEMORY; |
| 234 | |
| 235 | if (handle) { |
| 236 | // free previous handle if any |
| 237 | free_handle(); |
| 238 | } |
| 239 | |
| 240 | if (numFds || numInts) { |
| 241 | width = buf[1]; |
| 242 | height = buf[2]; |
| 243 | stride = buf[3]; |
| 244 | format = buf[4]; |
| 245 | usage = buf[5]; |
| 246 | native_handle* h = native_handle_create(numFds, numInts); |
| 247 | memcpy(h->data, fds, numFds*sizeof(int)); |
| 248 | memcpy(h->data + numFds, &buf[8], numInts*sizeof(int)); |
| 249 | handle = h; |
| 250 | } else { |
| 251 | width = height = stride = format = usage = 0; |
| 252 | handle = NULL; |
| 253 | } |
| 254 | |
| 255 | mOwner = ownHandle; |
| 256 | return NO_ERROR; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| 260 | void GraphicBuffer::setIndex(int index) { |
| 261 | mIndex = index; |
| 262 | } |
| 263 | |
| 264 | int GraphicBuffer::getIndex() const { |
| 265 | return mIndex; |
| 266 | } |
| 267 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 268 | // --------------------------------------------------------------------------- |
| 269 | |
| 270 | }; // namespace android |