blob: 57063e52462045b878c5864322e24e8eea38b268 [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
Mathias Agopian3330b202009-10-05 17:07:12 -070031namespace android {
32
33// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070034// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070035// ===========================================================================
36
37GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070038 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070039 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian3330b202009-10-05 17:07:12 -070040{
41 width =
42 height =
43 stride =
44 format =
45 usage = 0;
46 handle = NULL;
47}
48
49GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
50 PixelFormat reqFormat, uint32_t reqUsage)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070051 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070052 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian3330b202009-10-05 17:07:12 -070053{
54 width =
55 height =
56 stride =
57 format =
58 usage = 0;
59 handle = NULL;
60 mInitCheck = initSize(w, h, reqFormat, reqUsage);
61}
62
Mathias Agopian54ba51d2009-10-26 20:12:37 -070063GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
64 PixelFormat inFormat, uint32_t inUsage,
65 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
66 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
67 mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070068 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070069{
70 width = w;
71 height = h;
72 stride = inStride;
73 format = inFormat;
74 usage = inUsage;
75 handle = inHandle;
76}
77
Iliyan Malchev697526b2011-05-01 11:33:26 -070078GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070079 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
80 mBufferMapper(GraphicBufferMapper::get()),
81 mInitCheck(NO_ERROR), mIndex(-1), mWrappedBuffer(buffer)
82{
83 width = buffer->width;
84 height = buffer->height;
85 stride = buffer->stride;
86 format = buffer->format;
87 usage = buffer->usage;
88 handle = buffer->handle;
89}
90
Mathias Agopian3330b202009-10-05 17:07:12 -070091GraphicBuffer::~GraphicBuffer()
92{
93 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -080094 free_handle();
95 }
96}
97
98void GraphicBuffer::free_handle()
99{
100 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700101 mBufferMapper.unregisterBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800102 native_handle_close(handle);
103 native_handle_delete(const_cast<native_handle*>(handle));
104 } else if (mOwner == ownData) {
105 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
106 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700107 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700108 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700109}
110
111status_t GraphicBuffer::initCheck() const {
112 return mInitCheck;
113}
114
Mathias Agopian678bdd62010-12-03 17:33:09 -0800115void GraphicBuffer::dumpAllocationsToSystemLog()
116{
117 GraphicBufferAllocator::dumpToSystemLog();
118}
119
Iliyan Malchev697526b2011-05-01 11:33:26 -0700120ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700121{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700122 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700123 const_cast<GraphicBuffer*>(this));
124}
125
126status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
127 uint32_t reqUsage)
128{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700129 if (mOwner != ownData)
130 return INVALID_OPERATION;
131
Mathias Agopian579b3f82010-06-08 19:54:15 -0700132 if (handle && w==width && h==height && f==format && reqUsage==usage)
133 return NO_ERROR;
134
Mathias Agopian3330b202009-10-05 17:07:12 -0700135 if (handle) {
136 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
137 allocator.free(handle);
138 handle = 0;
139 }
140 return initSize(w, h, f, reqUsage);
141}
142
143status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
144 uint32_t reqUsage)
145{
Mathias Agopian3330b202009-10-05 17:07:12 -0700146 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
147 status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
148 if (err == NO_ERROR) {
149 this->width = w;
150 this->height = h;
151 this->format = format;
152 this->usage = reqUsage;
Mathias Agopian3330b202009-10-05 17:07:12 -0700153 }
154 return err;
155}
156
157status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
158{
159 const Rect lockBounds(width, height);
160 status_t res = lock(usage, lockBounds, vaddr);
161 return res;
162}
163
164status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
165{
166 if (rect.left < 0 || rect.right > this->width ||
167 rect.top < 0 || rect.bottom > this->height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000168 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Mathias Agopian3330b202009-10-05 17:07:12 -0700169 rect.left, rect.top, rect.right, rect.bottom,
170 this->width, this->height);
171 return BAD_VALUE;
172 }
173 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
174 return res;
175}
176
177status_t GraphicBuffer::unlock()
178{
179 status_t res = getBufferMapper().unlock(handle);
180 return res;
181}
182
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800183size_t GraphicBuffer::getFlattenedSize() const {
184 return (8 + (handle ? handle->numInts : 0))*sizeof(int);
185}
Mathias Agopian3330b202009-10-05 17:07:12 -0700186
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800187size_t GraphicBuffer::getFdCount() const {
188 return handle ? handle->numFds : 0;
189}
190
191status_t GraphicBuffer::flatten(void* buffer, size_t size,
192 int fds[], size_t count) const
Mathias Agopian3330b202009-10-05 17:07:12 -0700193{
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800194 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
195 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700196
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800197 size_t fdCountNeeded = GraphicBuffer::getFdCount();
198 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700199
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800200 int* buf = static_cast<int*>(buffer);
201 buf[0] = 'GBFR';
202 buf[1] = width;
203 buf[2] = height;
204 buf[3] = stride;
205 buf[4] = format;
206 buf[5] = usage;
207 buf[6] = 0;
208 buf[7] = 0;
209
210 if (handle) {
211 buf[6] = handle->numFds;
212 buf[7] = handle->numInts;
213 native_handle_t const* const h = handle;
214 memcpy(fds, h->data, h->numFds*sizeof(int));
215 memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700216 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800217
218 return NO_ERROR;
219}
220
221status_t GraphicBuffer::unflatten(void const* buffer, size_t size,
222 int fds[], size_t count)
223{
224 if (size < 8*sizeof(int)) return NO_MEMORY;
225
226 int const* buf = static_cast<int const*>(buffer);
227 if (buf[0] != 'GBFR') return BAD_TYPE;
228
229 const size_t numFds = buf[6];
230 const size_t numInts = buf[7];
231
232 const size_t sizeNeeded = (8 + numInts) * sizeof(int);
233 if (size < sizeNeeded) return NO_MEMORY;
234
235 size_t fdCountNeeded = 0;
236 if (count < fdCountNeeded) return NO_MEMORY;
237
238 if (handle) {
239 // free previous handle if any
240 free_handle();
241 }
242
243 if (numFds || numInts) {
244 width = buf[1];
245 height = buf[2];
246 stride = buf[3];
247 format = buf[4];
248 usage = buf[5];
249 native_handle* h = native_handle_create(numFds, numInts);
250 memcpy(h->data, fds, numFds*sizeof(int));
251 memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
252 handle = h;
253 } else {
254 width = height = stride = format = usage = 0;
255 handle = NULL;
256 }
257
258 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700259
260 if (handle != 0) {
261 mBufferMapper.registerBuffer(handle);
262 }
263
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800264 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700265}
266
267
268void GraphicBuffer::setIndex(int index) {
269 mIndex = index;
270}
271
272int GraphicBuffer::getIndex() const {
273 return mIndex;
274}
275
Mathias Agopian3330b202009-10-05 17:07:12 -0700276// ---------------------------------------------------------------------------
277
278}; // namespace android