blob: 367195439763bef0858edca34423c104c1b6e405 [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
52GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
53 PixelFormat reqFormat, uint32_t reqUsage)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070054 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070055 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian3330b202009-10-05 17:07:12 -070056{
57 width =
58 height =
59 stride =
60 format =
Mathias Agopian30eb1b12010-11-02 20:57:14 -070061 usage =
62 transform = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070063 handle = NULL;
64 mInitCheck = initSize(w, h, reqFormat, reqUsage);
65}
66
Mathias Agopian54ba51d2009-10-26 20:12:37 -070067GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
68 PixelFormat inFormat, uint32_t inUsage,
69 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
70 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
71 mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070072 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070073{
74 width = w;
75 height = h;
76 stride = inStride;
77 format = inFormat;
78 usage = inUsage;
Mathias Agopian30eb1b12010-11-02 20:57:14 -070079 transform = 0;
Mathias Agopian54ba51d2009-10-26 20:12:37 -070080 handle = inHandle;
81}
82
Mathias Agopian3330b202009-10-05 17:07:12 -070083GraphicBuffer::~GraphicBuffer()
84{
85 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -080086 free_handle();
87 }
88}
89
90void GraphicBuffer::free_handle()
91{
92 if (mOwner == ownHandle) {
93 native_handle_close(handle);
94 native_handle_delete(const_cast<native_handle*>(handle));
95 } else if (mOwner == ownData) {
96 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
97 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -070098 }
99}
100
101status_t GraphicBuffer::initCheck() const {
102 return mInitCheck;
103}
104
Mathias Agopian678bdd62010-12-03 17:33:09 -0800105void GraphicBuffer::dumpAllocationsToSystemLog()
106{
107 GraphicBufferAllocator::dumpToSystemLog();
108}
109
Mathias Agopian3330b202009-10-05 17:07:12 -0700110android_native_buffer_t* GraphicBuffer::getNativeBuffer() const
111{
112 return static_cast<android_native_buffer_t*>(
113 const_cast<GraphicBuffer*>(this));
114}
115
116status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
117 uint32_t reqUsage)
118{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700119 if (mOwner != ownData)
120 return INVALID_OPERATION;
121
Mathias Agopian579b3f82010-06-08 19:54:15 -0700122 if (handle && w==width && h==height && f==format && reqUsage==usage)
123 return NO_ERROR;
124
Mathias Agopian3330b202009-10-05 17:07:12 -0700125 if (handle) {
126 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
127 allocator.free(handle);
128 handle = 0;
129 }
130 return initSize(w, h, f, reqUsage);
131}
132
133status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
134 uint32_t reqUsage)
135{
Mathias Agopian3330b202009-10-05 17:07:12 -0700136 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
137 status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
138 if (err == NO_ERROR) {
139 this->width = w;
140 this->height = h;
141 this->format = format;
142 this->usage = reqUsage;
Mathias Agopian3330b202009-10-05 17:07:12 -0700143 }
144 return err;
145}
146
147status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
148{
149 const Rect lockBounds(width, height);
150 status_t res = lock(usage, lockBounds, vaddr);
151 return res;
152}
153
154status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
155{
156 if (rect.left < 0 || rect.right > this->width ||
157 rect.top < 0 || rect.bottom > this->height) {
158 LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
159 rect.left, rect.top, rect.right, rect.bottom,
160 this->width, this->height);
161 return BAD_VALUE;
162 }
163 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
164 return res;
165}
166
167status_t GraphicBuffer::unlock()
168{
169 status_t res = getBufferMapper().unlock(handle);
170 return res;
171}
172
173status_t GraphicBuffer::lock(GGLSurface* sur, uint32_t usage)
174{
175 void* vaddr;
176 status_t res = GraphicBuffer::lock(usage, &vaddr);
177 if (res == NO_ERROR && sur) {
178 sur->version = sizeof(GGLSurface);
179 sur->width = width;
180 sur->height = height;
181 sur->stride = stride;
182 sur->format = format;
Mathias Agopian3330b202009-10-05 17:07:12 -0700183 sur->data = static_cast<GGLubyte*>(vaddr);
184 }
185 return res;
186}
187
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700188const int kFlattenFdsOffset = 9;
189
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800190size_t GraphicBuffer::getFlattenedSize() const {
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700191 return (kFlattenFdsOffset + (handle ? handle->numInts : 0))*sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800192}
Mathias Agopian3330b202009-10-05 17:07:12 -0700193
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800194size_t GraphicBuffer::getFdCount() const {
195 return handle ? handle->numFds : 0;
196}
197
198status_t GraphicBuffer::flatten(void* buffer, size_t size,
199 int fds[], size_t count) const
Mathias Agopian3330b202009-10-05 17:07:12 -0700200{
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800201 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
202 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700203
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800204 size_t fdCountNeeded = GraphicBuffer::getFdCount();
205 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700206
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800207 int* buf = static_cast<int*>(buffer);
208 buf[0] = 'GBFR';
209 buf[1] = width;
210 buf[2] = height;
211 buf[3] = stride;
212 buf[4] = format;
213 buf[5] = usage;
214 buf[6] = 0;
215 buf[7] = 0;
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700216 buf[8] = transform;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800217
218 if (handle) {
219 buf[6] = handle->numFds;
220 buf[7] = handle->numInts;
221 native_handle_t const* const h = handle;
222 memcpy(fds, h->data, h->numFds*sizeof(int));
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700223 memcpy(&buf[kFlattenFdsOffset], h->data + h->numFds, h->numInts*sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700224 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800225
226 return NO_ERROR;
227}
228
229status_t GraphicBuffer::unflatten(void const* buffer, size_t size,
230 int fds[], size_t count)
231{
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700232 if (size < kFlattenFdsOffset*sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800233
234 int const* buf = static_cast<int const*>(buffer);
235 if (buf[0] != 'GBFR') return BAD_TYPE;
236
237 const size_t numFds = buf[6];
238 const size_t numInts = buf[7];
239
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700240 const size_t sizeNeeded = (kFlattenFdsOffset + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800241 if (size < sizeNeeded) return NO_MEMORY;
242
243 size_t fdCountNeeded = 0;
244 if (count < fdCountNeeded) return NO_MEMORY;
245
246 if (handle) {
247 // free previous handle if any
248 free_handle();
249 }
250
251 if (numFds || numInts) {
252 width = buf[1];
253 height = buf[2];
254 stride = buf[3];
255 format = buf[4];
256 usage = buf[5];
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700257 transform = buf[8];
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800258 native_handle* h = native_handle_create(numFds, numInts);
259 memcpy(h->data, fds, numFds*sizeof(int));
Mathias Agopian30eb1b12010-11-02 20:57:14 -0700260 memcpy(h->data + numFds, &buf[kFlattenFdsOffset], numInts*sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800261 handle = h;
262 } else {
263 width = height = stride = format = usage = 0;
264 handle = NULL;
265 }
266
267 mOwner = ownHandle;
268 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700269}
270
271
272void GraphicBuffer::setIndex(int index) {
273 mIndex = index;
274}
275
276int GraphicBuffer::getIndex() const {
277 return mIndex;
278}
279
Mathias Agopian3330b202009-10-05 17:07:12 -0700280// ---------------------------------------------------------------------------
281
282}; // namespace android