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