blob: 436e06471624784bc723e277682fb1bc48a64e02 [file] [log] [blame]
Mathias Agopian6950e422009-10-05 17:07:12 -07001/*
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
Mathias Agopianc86727f2010-02-11 17:30:52 -080017#define LOG_TAG "GraphicBuffer"
18
Mathias Agopian6950e422009-10-05 17:07:12 -070019#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian6950e422009-10-05 17:07:12 -070023#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()
Mathias Agopian9042b452009-10-26 20:12:37 -070040 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopian7623da42010-06-01 15:12:58 -070041 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian6950e422009-10-05 17:07:12 -070042{
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)
Mathias Agopian9042b452009-10-26 20:12:37 -070053 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopian7623da42010-06-01 15:12:58 -070054 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian6950e422009-10-05 17:07:12 -070055{
56 width =
57 height =
58 stride =
59 format =
60 usage = 0;
61 handle = NULL;
62 mInitCheck = initSize(w, h, reqFormat, reqUsage);
63}
64
Mathias Agopian9042b452009-10-26 20:12:37 -070065GraphicBuffer::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()),
Mathias Agopian7623da42010-06-01 15:12:58 -070070 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian9042b452009-10-26 20:12:37 -070071{
72 width = w;
73 height = h;
74 stride = inStride;
75 format = inFormat;
76 usage = inUsage;
77 handle = inHandle;
78}
79
Jamie Gennis1ef773f2010-10-07 13:46:55 -070080GraphicBuffer::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
Mathias Agopian6950e422009-10-05 17:07:12 -070093GraphicBuffer::~GraphicBuffer()
94{
95 if (handle) {
Mathias Agopianc86727f2010-02-11 17:30:52 -080096 free_handle();
97 }
98}
99
100void GraphicBuffer::free_handle()
101{
102 if (mOwner == ownHandle) {
Jamie Gennis1ef773f2010-10-07 13:46:55 -0700103 mBufferMapper.unregisterBuffer(handle);
Mathias Agopianc86727f2010-02-11 17:30:52 -0800104 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);
Mathias Agopian6950e422009-10-05 17:07:12 -0700109 }
Jamie Gennis1ef773f2010-10-07 13:46:55 -0700110 mWrappedBuffer = 0;
Mathias Agopian6950e422009-10-05 17:07:12 -0700111}
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{
Mathias Agopian9042b452009-10-26 20:12:37 -0700126 if (mOwner != ownData)
127 return INVALID_OPERATION;
128
Mathias Agopian5e140102010-06-08 19:54:15 -0700129 if (handle && w==width && h==height && f==format && reqUsage==usage)
130 return NO_ERROR;
131
Mathias Agopian6950e422009-10-05 17:07:12 -0700132 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{
Mathias Agopian6950e422009-10-05 17:07:12 -0700143 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;
Mathias Agopian6950e422009-10-05 17:07:12 -0700150 }
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;
Mathias Agopian6950e422009-10-05 17:07:12 -0700190 sur->data = static_cast<GGLubyte*>(vaddr);
191 }
192 return res;
193}
194
Mathias Agopianc86727f2010-02-11 17:30:52 -0800195size_t GraphicBuffer::getFlattenedSize() const {
196 return (8 + (handle ? handle->numInts : 0))*sizeof(int);
197}
Mathias Agopian6950e422009-10-05 17:07:12 -0700198
Mathias Agopianc86727f2010-02-11 17:30:52 -0800199size_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
Mathias Agopian6950e422009-10-05 17:07:12 -0700205{
Mathias Agopianc86727f2010-02-11 17:30:52 -0800206 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
207 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian6950e422009-10-05 17:07:12 -0700208
Mathias Agopianc86727f2010-02-11 17:30:52 -0800209 size_t fdCountNeeded = GraphicBuffer::getFdCount();
210 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian6950e422009-10-05 17:07:12 -0700211
Mathias Agopianc86727f2010-02-11 17:30:52 -0800212 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));
Mathias Agopian6950e422009-10-05 17:07:12 -0700228 }
Mathias Agopianc86727f2010-02-11 17:30:52 -0800229
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;
Jamie Gennis1ef773f2010-10-07 13:46:55 -0700271
272 if (handle != 0) {
273 mBufferMapper.registerBuffer(handle);
274 }
275
Mathias Agopianc86727f2010-02-11 17:30:52 -0800276 return NO_ERROR;
Mathias Agopian6950e422009-10-05 17:07:12 -0700277}
278
279
280void GraphicBuffer::setIndex(int index) {
281 mIndex = index;
282}
283
284int GraphicBuffer::getIndex() const {
285 return mIndex;
286}
287
Mathias Agopian6950e422009-10-05 17:07:12 -0700288// ---------------------------------------------------------------------------
289
290}; // namespace android