blob: c4e4efa1ca9874888a969ebc1cdc6adc60e906c9 [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 Agopian87f9b872013-07-31 19:18:22 -070039 mInitCheck(NO_ERROR) {
Mathias Agopian3330b202009-10-05 17:07:12 -070040 width =
41 height =
42 stride =
43 format =
44 usage = 0;
45 handle = NULL;
46}
47
48GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
49 PixelFormat reqFormat, uint32_t reqUsage)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070050 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopian87f9b872013-07-31 19:18:22 -070051 mInitCheck(NO_ERROR)
Mathias Agopian3330b202009-10-05 17:07:12 -070052{
53 width =
54 height =
55 stride =
56 format =
57 usage = 0;
58 handle = NULL;
59 mInitCheck = initSize(w, h, reqFormat, reqUsage);
60}
61
Mathias Agopian54ba51d2009-10-26 20:12:37 -070062GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
63 PixelFormat inFormat, uint32_t inUsage,
64 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
65 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
66 mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopian87f9b872013-07-31 19:18:22 -070067 mInitCheck(NO_ERROR)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070068{
69 width = w;
70 height = h;
71 stride = inStride;
72 format = inFormat;
73 usage = inUsage;
74 handle = inHandle;
75}
76
Iliyan Malchev697526b2011-05-01 11:33:26 -070077GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070078 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
79 mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopian87f9b872013-07-31 19:18:22 -070080 mInitCheck(NO_ERROR), mWrappedBuffer(buffer)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070081{
82 width = buffer->width;
83 height = buffer->height;
84 stride = buffer->stride;
85 format = buffer->format;
86 usage = buffer->usage;
87 handle = buffer->handle;
88}
89
Mathias Agopian3330b202009-10-05 17:07:12 -070090GraphicBuffer::~GraphicBuffer()
91{
92 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -080093 free_handle();
94 }
95}
96
97void GraphicBuffer::free_handle()
98{
99 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700100 mBufferMapper.unregisterBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800101 native_handle_close(handle);
102 native_handle_delete(const_cast<native_handle*>(handle));
103 } else if (mOwner == ownData) {
104 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
105 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700106 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700107 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700108}
109
110status_t GraphicBuffer::initCheck() const {
111 return mInitCheck;
112}
113
Mathias Agopian678bdd62010-12-03 17:33:09 -0800114void GraphicBuffer::dumpAllocationsToSystemLog()
115{
116 GraphicBufferAllocator::dumpToSystemLog();
117}
118
Iliyan Malchev697526b2011-05-01 11:33:26 -0700119ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700120{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700121 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700122 const_cast<GraphicBuffer*>(this));
123}
124
125status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
126 uint32_t reqUsage)
127{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700128 if (mOwner != ownData)
129 return INVALID_OPERATION;
130
Mathias Agopian579b3f82010-06-08 19:54:15 -0700131 if (handle && w==width && h==height && f==format && reqUsage==usage)
132 return NO_ERROR;
133
Mathias Agopian3330b202009-10-05 17:07:12 -0700134 if (handle) {
135 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
136 allocator.free(handle);
137 handle = 0;
138 }
139 return initSize(w, h, f, reqUsage);
140}
141
142status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
143 uint32_t reqUsage)
144{
Mathias Agopian3330b202009-10-05 17:07:12 -0700145 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
146 status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
147 if (err == NO_ERROR) {
148 this->width = w;
149 this->height = h;
150 this->format = format;
151 this->usage = reqUsage;
Mathias Agopian3330b202009-10-05 17:07:12 -0700152 }
153 return err;
154}
155
156status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
157{
158 const Rect lockBounds(width, height);
159 status_t res = lock(usage, lockBounds, vaddr);
160 return res;
161}
162
163status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
164{
165 if (rect.left < 0 || rect.right > this->width ||
166 rect.top < 0 || rect.bottom > this->height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000167 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Mathias Agopian3330b202009-10-05 17:07:12 -0700168 rect.left, rect.top, rect.right, rect.bottom,
169 this->width, this->height);
170 return BAD_VALUE;
171 }
172 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
173 return res;
174}
175
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700176status_t GraphicBuffer::lockYCbCr(uint32_t usage, android_ycbcr *ycbcr)
177{
178 const Rect lockBounds(width, height);
179 status_t res = lockYCbCr(usage, lockBounds, ycbcr);
180 return res;
181}
182
183status_t GraphicBuffer::lockYCbCr(uint32_t usage, const Rect& rect,
184 android_ycbcr *ycbcr)
185{
186 if (rect.left < 0 || rect.right > this->width ||
187 rect.top < 0 || rect.bottom > this->height) {
188 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
189 rect.left, rect.top, rect.right, rect.bottom,
190 this->width, this->height);
191 return BAD_VALUE;
192 }
193 status_t res = getBufferMapper().lockYCbCr(handle, usage, rect, ycbcr);
194 return res;
195}
196
Mathias Agopian3330b202009-10-05 17:07:12 -0700197status_t GraphicBuffer::unlock()
198{
199 status_t res = getBufferMapper().unlock(handle);
200 return res;
201}
202
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800203size_t GraphicBuffer::getFlattenedSize() const {
204 return (8 + (handle ? handle->numInts : 0))*sizeof(int);
205}
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
Mathias Agopiane1424282013-07-29 21:24:40 -0700211status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800212 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
213 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700214
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800215 size_t fdCountNeeded = GraphicBuffer::getFdCount();
216 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700217
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800218 int* buf = static_cast<int*>(buffer);
219 buf[0] = 'GBFR';
220 buf[1] = width;
221 buf[2] = height;
222 buf[3] = stride;
223 buf[4] = format;
224 buf[5] = usage;
225 buf[6] = 0;
226 buf[7] = 0;
227
228 if (handle) {
229 buf[6] = handle->numFds;
230 buf[7] = handle->numInts;
231 native_handle_t const* const h = handle;
232 memcpy(fds, h->data, h->numFds*sizeof(int));
233 memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700234 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800235
Mathias Agopiane1424282013-07-29 21:24:40 -0700236 buffer = reinterpret_cast<void*>(static_cast<int*>(buffer) + sizeNeeded);
237 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700238 if (handle) {
239 fds += handle->numFds;
240 count -= handle->numFds;
241 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700242
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800243 return NO_ERROR;
244}
245
Mathias Agopiane1424282013-07-29 21:24:40 -0700246status_t GraphicBuffer::unflatten(
247 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800248 if (size < 8*sizeof(int)) return NO_MEMORY;
249
250 int const* buf = static_cast<int const*>(buffer);
251 if (buf[0] != 'GBFR') return BAD_TYPE;
252
253 const size_t numFds = buf[6];
254 const size_t numInts = buf[7];
255
256 const size_t sizeNeeded = (8 + numInts) * sizeof(int);
257 if (size < sizeNeeded) return NO_MEMORY;
258
259 size_t fdCountNeeded = 0;
260 if (count < fdCountNeeded) return NO_MEMORY;
261
262 if (handle) {
263 // free previous handle if any
264 free_handle();
265 }
266
267 if (numFds || numInts) {
268 width = buf[1];
269 height = buf[2];
270 stride = buf[3];
271 format = buf[4];
272 usage = buf[5];
273 native_handle* h = native_handle_create(numFds, numInts);
274 memcpy(h->data, fds, numFds*sizeof(int));
275 memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
276 handle = h;
277 } else {
278 width = height = stride = format = usage = 0;
279 handle = NULL;
280 }
281
282 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700283
284 if (handle != 0) {
Jamie Gennisd69097f2012-08-30 13:28:23 -0700285 status_t err = mBufferMapper.registerBuffer(handle);
286 if (err != NO_ERROR) {
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800287 width = height = stride = format = usage = 0;
288 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700289 ALOGE("unflatten: registerBuffer failed: %s (%d)",
290 strerror(-err), err);
291 return err;
292 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700293 }
294
Mathias Agopiane1424282013-07-29 21:24:40 -0700295 buffer = reinterpret_cast<void const*>(static_cast<int const*>(buffer) + sizeNeeded);
296 size -= sizeNeeded;
297 fds += numFds;
298 count -= numFds;
299
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800300 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700301}
302
Mathias Agopian3330b202009-10-05 17:07:12 -0700303// ---------------------------------------------------------------------------
304
305}; // namespace android