blob: 99601edc70302b2b84f40b6bc54e2ea45429c5db [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
Dan Stozab1363d32014-03-28 15:10:52 -070037static uint64_t getUniqueId() {
38 static volatile int32_t nextId = 0;
39 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
40 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
41 return id;
42}
43
Mathias Agopian3330b202009-10-05 17:07:12 -070044GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070045 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Dan Stozab1363d32014-03-28 15:10:52 -070046 mInitCheck(NO_ERROR), mId(getUniqueId())
47{
Mathias Agopian3330b202009-10-05 17:07:12 -070048 width =
49 height =
50 stride =
51 format =
52 usage = 0;
53 handle = NULL;
54}
55
56GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
57 PixelFormat reqFormat, uint32_t reqUsage)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070058 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Dan Stozab1363d32014-03-28 15:10:52 -070059 mInitCheck(NO_ERROR), mId(getUniqueId())
Mathias Agopian3330b202009-10-05 17:07:12 -070060{
61 width =
62 height =
63 stride =
64 format =
65 usage = 0;
66 handle = NULL;
67 mInitCheck = initSize(w, h, reqFormat, reqUsage);
68}
69
Mathias Agopian54ba51d2009-10-26 20:12:37 -070070GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
71 PixelFormat inFormat, uint32_t inUsage,
72 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
73 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
74 mBufferMapper(GraphicBufferMapper::get()),
Dan Stozab1363d32014-03-28 15:10:52 -070075 mInitCheck(NO_ERROR), mId(getUniqueId())
Mathias Agopian54ba51d2009-10-26 20:12:37 -070076{
77 width = w;
78 height = h;
79 stride = inStride;
80 format = inFormat;
81 usage = inUsage;
82 handle = inHandle;
83}
84
Iliyan Malchev697526b2011-05-01 11:33:26 -070085GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070086 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
87 mBufferMapper(GraphicBufferMapper::get()),
Dan Stozab1363d32014-03-28 15:10:52 -070088 mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId())
Jamie Gennis309d3bb2010-10-07 13:46:55 -070089{
90 width = buffer->width;
91 height = buffer->height;
92 stride = buffer->stride;
93 format = buffer->format;
94 usage = buffer->usage;
95 handle = buffer->handle;
96}
97
Mathias Agopian3330b202009-10-05 17:07:12 -070098GraphicBuffer::~GraphicBuffer()
99{
100 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800101 free_handle();
102 }
103}
104
105void GraphicBuffer::free_handle()
106{
107 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700108 mBufferMapper.unregisterBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800109 native_handle_close(handle);
110 native_handle_delete(const_cast<native_handle*>(handle));
111 } else if (mOwner == ownData) {
112 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
113 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700114 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700115 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700116}
117
118status_t GraphicBuffer::initCheck() const {
119 return mInitCheck;
120}
121
Mathias Agopian678bdd62010-12-03 17:33:09 -0800122void GraphicBuffer::dumpAllocationsToSystemLog()
123{
124 GraphicBufferAllocator::dumpToSystemLog();
125}
126
Iliyan Malchev697526b2011-05-01 11:33:26 -0700127ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700128{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700129 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700130 const_cast<GraphicBuffer*>(this));
131}
132
133status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
134 uint32_t reqUsage)
135{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700136 if (mOwner != ownData)
137 return INVALID_OPERATION;
138
Mathias Agopian579b3f82010-06-08 19:54:15 -0700139 if (handle && w==width && h==height && f==format && reqUsage==usage)
140 return NO_ERROR;
141
Mathias Agopian3330b202009-10-05 17:07:12 -0700142 if (handle) {
143 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
144 allocator.free(handle);
145 handle = 0;
146 }
147 return initSize(w, h, f, reqUsage);
148}
149
150status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
151 uint32_t reqUsage)
152{
Mathias Agopian3330b202009-10-05 17:07:12 -0700153 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
154 status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
155 if (err == NO_ERROR) {
156 this->width = w;
157 this->height = h;
158 this->format = format;
159 this->usage = reqUsage;
Mathias Agopian3330b202009-10-05 17:07:12 -0700160 }
161 return err;
162}
163
164status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
165{
166 const Rect lockBounds(width, height);
167 status_t res = lock(usage, lockBounds, vaddr);
168 return res;
169}
170
171status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
172{
173 if (rect.left < 0 || rect.right > this->width ||
174 rect.top < 0 || rect.bottom > this->height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000175 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Mathias Agopian3330b202009-10-05 17:07:12 -0700176 rect.left, rect.top, rect.right, rect.bottom,
177 this->width, this->height);
178 return BAD_VALUE;
179 }
180 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
181 return res;
182}
183
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700184status_t GraphicBuffer::lockYCbCr(uint32_t usage, android_ycbcr *ycbcr)
185{
186 const Rect lockBounds(width, height);
187 status_t res = lockYCbCr(usage, lockBounds, ycbcr);
188 return res;
189}
190
191status_t GraphicBuffer::lockYCbCr(uint32_t usage, const Rect& rect,
192 android_ycbcr *ycbcr)
193{
194 if (rect.left < 0 || rect.right > this->width ||
195 rect.top < 0 || rect.bottom > this->height) {
196 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
197 rect.left, rect.top, rect.right, rect.bottom,
198 this->width, this->height);
199 return BAD_VALUE;
200 }
201 status_t res = getBufferMapper().lockYCbCr(handle, usage, rect, ycbcr);
202 return res;
203}
204
Mathias Agopian3330b202009-10-05 17:07:12 -0700205status_t GraphicBuffer::unlock()
206{
207 status_t res = getBufferMapper().unlock(handle);
208 return res;
209}
210
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800211size_t GraphicBuffer::getFlattenedSize() const {
Dan Stozab1363d32014-03-28 15:10:52 -0700212 return (10 + (handle ? handle->numInts : 0))*sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800213}
Mathias Agopian3330b202009-10-05 17:07:12 -0700214
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800215size_t GraphicBuffer::getFdCount() const {
216 return handle ? handle->numFds : 0;
217}
218
Mathias Agopiane1424282013-07-29 21:24:40 -0700219status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800220 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
221 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700222
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800223 size_t fdCountNeeded = GraphicBuffer::getFdCount();
224 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700225
Dan Stozab1363d32014-03-28 15:10:52 -0700226 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800227 buf[0] = 'GBFR';
228 buf[1] = width;
229 buf[2] = height;
230 buf[3] = stride;
231 buf[4] = format;
232 buf[5] = usage;
Dan Stozab1363d32014-03-28 15:10:52 -0700233 buf[6] = static_cast<int32_t>(mId >> 32);
234 buf[7] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
235 buf[8] = 0;
236 buf[9] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800237
238 if (handle) {
Dan Stozab1363d32014-03-28 15:10:52 -0700239 buf[8] = handle->numFds;
240 buf[9] = handle->numInts;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800241 native_handle_t const* const h = handle;
242 memcpy(fds, h->data, h->numFds*sizeof(int));
Dan Stozab1363d32014-03-28 15:10:52 -0700243 memcpy(&buf[10], h->data + h->numFds, h->numInts*sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700244 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800245
Mathias Agopiane1424282013-07-29 21:24:40 -0700246 buffer = reinterpret_cast<void*>(static_cast<int*>(buffer) + sizeNeeded);
247 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700248 if (handle) {
249 fds += handle->numFds;
250 count -= handle->numFds;
251 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700252
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800253 return NO_ERROR;
254}
255
Mathias Agopiane1424282013-07-29 21:24:40 -0700256status_t GraphicBuffer::unflatten(
257 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800258 if (size < 8*sizeof(int)) return NO_MEMORY;
259
260 int const* buf = static_cast<int const*>(buffer);
261 if (buf[0] != 'GBFR') return BAD_TYPE;
262
Dan Stozab1363d32014-03-28 15:10:52 -0700263 const size_t numFds = buf[8];
264 const size_t numInts = buf[9];
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800265
Dan Stozab1363d32014-03-28 15:10:52 -0700266 const size_t sizeNeeded = (10 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800267 if (size < sizeNeeded) return NO_MEMORY;
268
269 size_t fdCountNeeded = 0;
270 if (count < fdCountNeeded) return NO_MEMORY;
271
272 if (handle) {
273 // free previous handle if any
274 free_handle();
275 }
276
277 if (numFds || numInts) {
278 width = buf[1];
279 height = buf[2];
280 stride = buf[3];
281 format = buf[4];
282 usage = buf[5];
283 native_handle* h = native_handle_create(numFds, numInts);
284 memcpy(h->data, fds, numFds*sizeof(int));
Dan Stozab1363d32014-03-28 15:10:52 -0700285 memcpy(h->data + numFds, &buf[10], numInts*sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800286 handle = h;
287 } else {
288 width = height = stride = format = usage = 0;
289 handle = NULL;
290 }
291
Dan Stozab1363d32014-03-28 15:10:52 -0700292 mId = static_cast<uint64_t>(buf[6]) << 32;
293 mId |= static_cast<uint32_t>(buf[7]);
294
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800295 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700296
297 if (handle != 0) {
Jamie Gennisd69097f2012-08-30 13:28:23 -0700298 status_t err = mBufferMapper.registerBuffer(handle);
299 if (err != NO_ERROR) {
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800300 width = height = stride = format = usage = 0;
301 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700302 ALOGE("unflatten: registerBuffer failed: %s (%d)",
303 strerror(-err), err);
304 return err;
305 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700306 }
307
Mathias Agopiane1424282013-07-29 21:24:40 -0700308 buffer = reinterpret_cast<void const*>(static_cast<int const*>(buffer) + sizeNeeded);
309 size -= sizeNeeded;
310 fds += numFds;
311 count -= numFds;
312
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800313 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700314}
315
Mathias Agopian3330b202009-10-05 17:07:12 -0700316// ---------------------------------------------------------------------------
317
318}; // namespace android