blob: d9efeab4bf4cb6a778edbef4547bd30f33d0b0a9 [file] [log] [blame]
Mathias Agopian3330b202009-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 Agopian98e71dd2010-02-11 17:30:52 -080017#define LOG_TAG "GraphicBuffer"
18
Mathias Agopian3330b202009-10-05 17:07:12 -070019#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian3330b202009-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 Agopian54ba51d2009-10-26 20:12:37 -070040 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070041 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian3330b202009-10-05 17:07:12 -070042{
43 width =
44 height =
45 stride =
46 format =
47 usage = 0;
Mathias Agopian30eb1b12010-11-02 20:57:14 -070048 transform = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070049 handle = NULL;
50}
51
Jean-Baptiste Queru3f3f5af2011-06-18 09:42:04 -070052GraphicBuffer::GraphicBuffer(android_native_buffer_t*, bool)
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 transform = 0;
62 handle = NULL;
63}
64
Mathias Agopian3330b202009-10-05 17:07:12 -070065GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
66 PixelFormat reqFormat, uint32_t reqUsage)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070067 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070068 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian3330b202009-10-05 17:07:12 -070069{
70 width =
71 height =
72 stride =
73 format =
Mathias Agopian30eb1b12010-11-02 20:57:14 -070074 usage =
75 transform = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070076 handle = NULL;
77 mInitCheck = initSize(w, h, reqFormat, reqUsage);
78}
79
Mathias Agopian54ba51d2009-10-26 20:12:37 -070080GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
81 PixelFormat inFormat, uint32_t inUsage,
82 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
83 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
84 mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070085 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070086{
87 width = w;
88 height = h;
89 stride = inStride;
90 format = inFormat;
91 usage = inUsage;
Mathias Agopian30eb1b12010-11-02 20:57:14 -070092 transform = 0;
Mathias Agopian54ba51d2009-10-26 20:12:37 -070093 handle = inHandle;
94}
95
Mathias Agopian3330b202009-10-05 17:07:12 -070096GraphicBuffer::~GraphicBuffer()
97{
98 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -080099 free_handle();
100 }
101}
102
103void GraphicBuffer::free_handle()
104{
105 if (mOwner == ownHandle) {
106 native_handle_close(handle);
107 native_handle_delete(const_cast<native_handle*>(handle));
108 } else if (mOwner == ownData) {
109 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
110 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700111 }
112}
113
114status_t GraphicBuffer::initCheck() const {
115 return mInitCheck;
116}
117
Mathias Agopian678bdd62010-12-03 17:33:09 -0800118void GraphicBuffer::dumpAllocationsToSystemLog()
119{
120 GraphicBufferAllocator::dumpToSystemLog();
121}
122
Mathias Agopian3330b202009-10-05 17:07:12 -0700123android_native_buffer_t* GraphicBuffer::getNativeBuffer() const
124{
125 return static_cast<android_native_buffer_t*>(
126 const_cast<GraphicBuffer*>(this));
127}
128
129status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
130 uint32_t reqUsage)
131{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700132 if (mOwner != ownData)
133 return INVALID_OPERATION;
134
Mathias Agopian579b3f82010-06-08 19:54:15 -0700135 if (handle && w==width && h==height && f==format && reqUsage==usage)
136 return NO_ERROR;
137
Mathias Agopian3330b202009-10-05 17:07:12 -0700138 if (handle) {
139 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
140 allocator.free(handle);
141 handle = 0;
142 }
143 return initSize(w, h, f, reqUsage);
144}
145
146status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
147 uint32_t reqUsage)
148{
Mathias Agopian3330b202009-10-05 17:07:12 -0700149 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
150 status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
151 if (err == NO_ERROR) {
152 this->width = w;
153 this->height = h;
154 this->format = format;
155 this->usage = reqUsage;
Mathias Agopian3330b202009-10-05 17:07:12 -0700156 }
157 return err;
158}
159
160status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
161{
162 const Rect lockBounds(width, height);
163 status_t res = lock(usage, lockBounds, vaddr);
164 return res;
165}
166
167status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
168{
169 if (rect.left < 0 || rect.right > this->width ||
170 rect.top < 0 || rect.bottom > this->height) {
171 LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
172 rect.left, rect.top, rect.right, rect.bottom,
173 this->width, this->height);
174 return BAD_VALUE;
175 }
176 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
177 return res;
178}
179
180status_t GraphicBuffer::unlock()
181{
182 status_t res = getBufferMapper().unlock(handle);
183 return res;
184}
185
186status_t GraphicBuffer::lock(GGLSurface* sur, uint32_t usage)
187{
188 void* vaddr;
189 status_t res = GraphicBuffer::lock(usage, &vaddr);
190 if (res == NO_ERROR && sur) {
191 sur->version = sizeof(GGLSurface);
192 sur->width = width;
193 sur->height = height;
194 sur->stride = stride;
195 sur->format = format;
Mathias Agopian3330b202009-10-05 17:07:12 -0700196 sur->data = static_cast<GGLubyte*>(vaddr);
197 }
198 return res;
199}
200
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700201const int kFlattenFdsOffset = 9;
202
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800203size_t GraphicBuffer::getFlattenedSize() const {
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700204 return (kFlattenFdsOffset + (handle ? handle->numInts : 0))*sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800205}
Mathias Agopian3330b202009-10-05 17:07:12 -0700206
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800207size_t GraphicBuffer::getFdCount() const {
208 return handle ? handle->numFds : 0;
209}
210
211status_t GraphicBuffer::flatten(void* buffer, size_t size,
212 int fds[], size_t count) const
Mathias Agopian3330b202009-10-05 17:07:12 -0700213{
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800214 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
215 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700216
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800217 size_t fdCountNeeded = GraphicBuffer::getFdCount();
218 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700219
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800220 int* buf = static_cast<int*>(buffer);
221 buf[0] = 'GBFR';
222 buf[1] = width;
223 buf[2] = height;
224 buf[3] = stride;
225 buf[4] = format;
226 buf[5] = usage;
227 buf[6] = 0;
228 buf[7] = 0;
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700229 buf[8] = transform;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800230
231 if (handle) {
232 buf[6] = handle->numFds;
233 buf[7] = handle->numInts;
234 native_handle_t const* const h = handle;
235 memcpy(fds, h->data, h->numFds*sizeof(int));
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700236 memcpy(&buf[kFlattenFdsOffset], h->data + h->numFds, h->numInts*sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700237 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800238
239 return NO_ERROR;
240}
241
242status_t GraphicBuffer::unflatten(void const* buffer, size_t size,
243 int fds[], size_t count)
244{
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700245 if (size < kFlattenFdsOffset*sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800246
247 int const* buf = static_cast<int const*>(buffer);
248 if (buf[0] != 'GBFR') return BAD_TYPE;
249
250 const size_t numFds = buf[6];
251 const size_t numInts = buf[7];
252
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700253 const size_t sizeNeeded = (kFlattenFdsOffset + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800254 if (size < sizeNeeded) return NO_MEMORY;
255
256 size_t fdCountNeeded = 0;
257 if (count < fdCountNeeded) return NO_MEMORY;
258
259 if (handle) {
260 // free previous handle if any
261 free_handle();
262 }
263
264 if (numFds || numInts) {
265 width = buf[1];
266 height = buf[2];
267 stride = buf[3];
268 format = buf[4];
269 usage = buf[5];
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700270 transform = buf[8];
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800271 native_handle* h = native_handle_create(numFds, numInts);
272 memcpy(h->data, fds, numFds*sizeof(int));
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700273 memcpy(h->data + numFds, &buf[kFlattenFdsOffset], numInts*sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800274 handle = h;
275 } else {
276 width = height = stride = format = usage = 0;
277 handle = NULL;
278 }
279
280 mOwner = ownHandle;
281 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700282}
283
284
285void GraphicBuffer::setIndex(int index) {
286 mIndex = index;
287}
288
289int GraphicBuffer::getIndex() const {
290 return mIndex;
291}
292
Mathias Agopian3330b202009-10-05 17:07:12 -0700293// ---------------------------------------------------------------------------
294
295}; // namespace android