2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "GraphicBuffer"
21 #include <sys/types.h>
23 #include <utils/Errors.h>
24 #include <utils/Log.h>
26 #include <ui/GraphicBuffer.h>
27 #include <ui/GraphicBufferAllocator.h>
28 #include <ui/GraphicBufferMapper.h>
29 #include <ui/PixelFormat.h>
31 #include <pixelflinger/pixelflinger.h>
35 // ===========================================================================
36 // Buffer and implementation of android_native_buffer_t
37 // ===========================================================================
39 GraphicBuffer::GraphicBuffer()
40 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
41 mInitCheck(NO_ERROR), mIndex(-1)
52 GraphicBuffer::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)
64 mInitCheck = initSize(w, h, reqFormat, reqUsage);
67 GraphicBuffer::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)
83 GraphicBuffer::~GraphicBuffer()
90 void GraphicBuffer::free_handle()
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);
101 status_t GraphicBuffer::initCheck() const {
105 void GraphicBuffer::dumpAllocationsToSystemLog()
107 GraphicBufferAllocator::dumpToSystemLog();
110 android_native_buffer_t* GraphicBuffer::getNativeBuffer() const
112 return static_cast<android_native_buffer_t*>(
113 const_cast<GraphicBuffer*>(this));
116 status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
119 if (mOwner != ownData)
120 return INVALID_OPERATION;
122 if (handle && w==width && h==height && f==format && reqUsage==usage)
126 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
127 allocator.free(handle);
130 return initSize(w, h, f, reqUsage);
133 status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
136 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
137 status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
138 if (err == NO_ERROR) {
141 this->format = format;
142 this->usage = reqUsage;
147 status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
149 const Rect lockBounds(width, height);
150 status_t res = lock(usage, lockBounds, vaddr);
154 status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
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);
163 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
167 status_t GraphicBuffer::unlock()
169 status_t res = getBufferMapper().unlock(handle);
173 status_t GraphicBuffer::lock(GGLSurface* sur, uint32_t usage)
176 status_t res = GraphicBuffer::lock(usage, &vaddr);
177 if (res == NO_ERROR && sur) {
178 sur->version = sizeof(GGLSurface);
180 sur->height = height;
181 sur->stride = stride;
182 sur->format = format;
183 sur->data = static_cast<GGLubyte*>(vaddr);
188 const int kFlattenFdsOffset = 9;
190 size_t GraphicBuffer::getFlattenedSize() const {
191 return (kFlattenFdsOffset + (handle ? handle->numInts : 0))*sizeof(int);
194 size_t GraphicBuffer::getFdCount() const {
195 return handle ? handle->numFds : 0;
198 status_t GraphicBuffer::flatten(void* buffer, size_t size,
199 int fds[], size_t count) const
201 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
202 if (size < sizeNeeded) return NO_MEMORY;
204 size_t fdCountNeeded = GraphicBuffer::getFdCount();
205 if (count < fdCountNeeded) return NO_MEMORY;
207 int* buf = static_cast<int*>(buffer);
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));
229 status_t GraphicBuffer::unflatten(void const* buffer, size_t size,
230 int fds[], size_t count)
232 if (size < kFlattenFdsOffset*sizeof(int)) return NO_MEMORY;
234 int const* buf = static_cast<int const*>(buffer);
235 if (buf[0] != 'GBFR') return BAD_TYPE;
237 const size_t numFds = buf[6];
238 const size_t numInts = buf[7];
240 const size_t sizeNeeded = (kFlattenFdsOffset + numInts) * sizeof(int);
241 if (size < sizeNeeded) return NO_MEMORY;
243 size_t fdCountNeeded = 0;
244 if (count < fdCountNeeded) return NO_MEMORY;
247 // free previous handle if any
251 if (numFds || numInts) {
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));
263 width = height = stride = format = usage = 0;
272 void GraphicBuffer::setIndex(int index) {
276 int GraphicBuffer::getIndex() const {
280 // ---------------------------------------------------------------------------
282 }; // namespace android