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