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