GraphicBuffer.cpp revision 309d3bb2f902163356f9d40b6d45c11b435d77a9
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 17#define LOG_TAG "GraphicBuffer" 18 19#include <stdlib.h> 20#include <stdint.h> 21#include <sys/types.h> 22 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 33namespace android { 34 35// =========================================================================== 36// Buffer and implementation of android_native_buffer_t 37// =========================================================================== 38 39GraphicBuffer::GraphicBuffer() 40 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()), 41 mInitCheck(NO_ERROR), mIndex(-1) 42{ 43 width = 44 height = 45 stride = 46 format = 47 usage = 0; 48 handle = NULL; 49} 50 51GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h, 52 PixelFormat reqFormat, uint32_t reqUsage) 53 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()), 54 mInitCheck(NO_ERROR), mIndex(-1) 55{ 56 width = 57 height = 58 stride = 59 format = 60 usage = 0; 61 handle = NULL; 62 mInitCheck = initSize(w, h, reqFormat, reqUsage); 63} 64 65GraphicBuffer::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()), 70 mInitCheck(NO_ERROR), mIndex(-1) 71{ 72 width = w; 73 height = h; 74 stride = inStride; 75 format = inFormat; 76 usage = inUsage; 77 handle = inHandle; 78} 79 80GraphicBuffer::GraphicBuffer(android_native_buffer_t* buffer, bool keepOwnership) 81 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone), 82 mBufferMapper(GraphicBufferMapper::get()), 83 mInitCheck(NO_ERROR), mIndex(-1), mWrappedBuffer(buffer) 84{ 85 width = buffer->width; 86 height = buffer->height; 87 stride = buffer->stride; 88 format = buffer->format; 89 usage = buffer->usage; 90 handle = buffer->handle; 91} 92 93GraphicBuffer::~GraphicBuffer() 94{ 95 if (handle) { 96 free_handle(); 97 } 98} 99 100void GraphicBuffer::free_handle() 101{ 102 if (mOwner == ownHandle) { 103 mBufferMapper.unregisterBuffer(handle); 104 native_handle_close(handle); 105 native_handle_delete(const_cast<native_handle*>(handle)); 106 } else if (mOwner == ownData) { 107 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get()); 108 allocator.free(handle); 109 } 110 mWrappedBuffer = 0; 111} 112 113status_t GraphicBuffer::initCheck() const { 114 return mInitCheck; 115} 116 117android_native_buffer_t* GraphicBuffer::getNativeBuffer() const 118{ 119 return static_cast<android_native_buffer_t*>( 120 const_cast<GraphicBuffer*>(this)); 121} 122 123status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f, 124 uint32_t reqUsage) 125{ 126 if (mOwner != ownData) 127 return INVALID_OPERATION; 128 129 if (handle && w==width && h==height && f==format && reqUsage==usage) 130 return NO_ERROR; 131 132 if (handle) { 133 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get()); 134 allocator.free(handle); 135 handle = 0; 136 } 137 return initSize(w, h, f, reqUsage); 138} 139 140status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format, 141 uint32_t reqUsage) 142{ 143 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get(); 144 status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride); 145 if (err == NO_ERROR) { 146 this->width = w; 147 this->height = h; 148 this->format = format; 149 this->usage = reqUsage; 150 } 151 return err; 152} 153 154status_t GraphicBuffer::lock(uint32_t usage, void** vaddr) 155{ 156 const Rect lockBounds(width, height); 157 status_t res = lock(usage, lockBounds, vaddr); 158 return res; 159} 160 161status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr) 162{ 163 if (rect.left < 0 || rect.right > this->width || 164 rect.top < 0 || rect.bottom > this->height) { 165 LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", 166 rect.left, rect.top, rect.right, rect.bottom, 167 this->width, this->height); 168 return BAD_VALUE; 169 } 170 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr); 171 return res; 172} 173 174status_t GraphicBuffer::unlock() 175{ 176 status_t res = getBufferMapper().unlock(handle); 177 return res; 178} 179 180status_t GraphicBuffer::lock(GGLSurface* sur, uint32_t usage) 181{ 182 void* vaddr; 183 status_t res = GraphicBuffer::lock(usage, &vaddr); 184 if (res == NO_ERROR && sur) { 185 sur->version = sizeof(GGLSurface); 186 sur->width = width; 187 sur->height = height; 188 sur->stride = stride; 189 sur->format = format; 190 sur->data = static_cast<GGLubyte*>(vaddr); 191 } 192 return res; 193} 194 195size_t GraphicBuffer::getFlattenedSize() const { 196 return (8 + (handle ? handle->numInts : 0))*sizeof(int); 197} 198 199size_t GraphicBuffer::getFdCount() const { 200 return handle ? handle->numFds : 0; 201} 202 203status_t GraphicBuffer::flatten(void* buffer, size_t size, 204 int fds[], size_t count) const 205{ 206 size_t sizeNeeded = GraphicBuffer::getFlattenedSize(); 207 if (size < sizeNeeded) return NO_MEMORY; 208 209 size_t fdCountNeeded = GraphicBuffer::getFdCount(); 210 if (count < fdCountNeeded) return NO_MEMORY; 211 212 int* buf = static_cast<int*>(buffer); 213 buf[0] = 'GBFR'; 214 buf[1] = width; 215 buf[2] = height; 216 buf[3] = stride; 217 buf[4] = format; 218 buf[5] = usage; 219 buf[6] = 0; 220 buf[7] = 0; 221 222 if (handle) { 223 buf[6] = handle->numFds; 224 buf[7] = handle->numInts; 225 native_handle_t const* const h = handle; 226 memcpy(fds, h->data, h->numFds*sizeof(int)); 227 memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int)); 228 } 229 230 return NO_ERROR; 231} 232 233status_t GraphicBuffer::unflatten(void const* buffer, size_t size, 234 int fds[], size_t count) 235{ 236 if (size < 8*sizeof(int)) return NO_MEMORY; 237 238 int const* buf = static_cast<int const*>(buffer); 239 if (buf[0] != 'GBFR') return BAD_TYPE; 240 241 const size_t numFds = buf[6]; 242 const size_t numInts = buf[7]; 243 244 const size_t sizeNeeded = (8 + numInts) * sizeof(int); 245 if (size < sizeNeeded) return NO_MEMORY; 246 247 size_t fdCountNeeded = 0; 248 if (count < fdCountNeeded) return NO_MEMORY; 249 250 if (handle) { 251 // free previous handle if any 252 free_handle(); 253 } 254 255 if (numFds || numInts) { 256 width = buf[1]; 257 height = buf[2]; 258 stride = buf[3]; 259 format = buf[4]; 260 usage = buf[5]; 261 native_handle* h = native_handle_create(numFds, numInts); 262 memcpy(h->data, fds, numFds*sizeof(int)); 263 memcpy(h->data + numFds, &buf[8], numInts*sizeof(int)); 264 handle = h; 265 } else { 266 width = height = stride = format = usage = 0; 267 handle = NULL; 268 } 269 270 mOwner = ownHandle; 271 272 if (handle != 0) { 273 mBufferMapper.registerBuffer(handle); 274 } 275 276 return NO_ERROR; 277} 278 279 280void GraphicBuffer::setIndex(int index) { 281 mIndex = index; 282} 283 284int GraphicBuffer::getIndex() const { 285 return mIndex; 286} 287 288// --------------------------------------------------------------------------- 289 290}; // namespace android 291