blob: 3ddde38f2c3c3c77df8d3e8110cab70133ebcee1 [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;
48 handle = NULL;
49}
50
51GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
52 PixelFormat reqFormat, uint32_t reqUsage)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070053 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070054 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian3330b202009-10-05 17:07:12 -070055{
56 width =
57 height =
58 stride =
59 format =
60 usage = 0;
61 handle = NULL;
62 mInitCheck = initSize(w, h, reqFormat, reqUsage);
63}
64
Mathias Agopian54ba51d2009-10-26 20:12:37 -070065GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
66 PixelFormat inFormat, uint32_t inUsage,
67 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
68 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
69 mBufferMapper(GraphicBufferMapper::get()),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070070 mInitCheck(NO_ERROR), mIndex(-1)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070071{
72 width = w;
73 height = h;
74 stride = inStride;
75 format = inFormat;
76 usage = inUsage;
77 handle = inHandle;
78}
79
Mathias Agopian3330b202009-10-05 17:07:12 -070080GraphicBuffer::~GraphicBuffer()
81{
82 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -080083 free_handle();
84 }
85}
86
87void GraphicBuffer::free_handle()
88{
89 if (mOwner == ownHandle) {
90 native_handle_close(handle);
91 native_handle_delete(const_cast<native_handle*>(handle));
92 } else if (mOwner == ownData) {
93 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
94 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -070095 }
96}
97
98status_t GraphicBuffer::initCheck() const {
99 return mInitCheck;
100}
101
102android_native_buffer_t* GraphicBuffer::getNativeBuffer() const
103{
104 return static_cast<android_native_buffer_t*>(
105 const_cast<GraphicBuffer*>(this));
106}
107
108status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
109 uint32_t reqUsage)
110{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700111 if (mOwner != ownData)
112 return INVALID_OPERATION;
113
Mathias Agopian3330b202009-10-05 17:07:12 -0700114 if (handle) {
115 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
116 allocator.free(handle);
117 handle = 0;
118 }
119 return initSize(w, h, f, reqUsage);
120}
121
122status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
123 uint32_t reqUsage)
124{
Mathias Agopian3330b202009-10-05 17:07:12 -0700125 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
126 status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
Mathias Agopian49753262010-04-12 15:34:55 -0700127
128 if (err<0 && format == PIXEL_FORMAT_RGBX_8888) {
129 /*
130 * There is currently a bug with some gralloc implementations
131 * not supporting RGBX_8888. In this case, we revert to using RGBA_8888
132 * which is not exactly the same, as GL_REPLACE will yield a different
133 * result.
134 */
135 format = PIXEL_FORMAT_RGBA_8888;
136 err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
137 }
138
Mathias Agopian3330b202009-10-05 17:07:12 -0700139 if (err == NO_ERROR) {
140 this->width = w;
141 this->height = h;
142 this->format = format;
143 this->usage = reqUsage;
Mathias Agopian3330b202009-10-05 17:07:12 -0700144 }
145 return err;
146}
147
148status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
149{
150 const Rect lockBounds(width, height);
151 status_t res = lock(usage, lockBounds, vaddr);
152 return res;
153}
154
155status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
156{
157 if (rect.left < 0 || rect.right > this->width ||
158 rect.top < 0 || rect.bottom > this->height) {
159 LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
160 rect.left, rect.top, rect.right, rect.bottom,
161 this->width, this->height);
162 return BAD_VALUE;
163 }
164 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
165 return res;
166}
167
168status_t GraphicBuffer::unlock()
169{
170 status_t res = getBufferMapper().unlock(handle);
171 return res;
172}
173
174status_t GraphicBuffer::lock(GGLSurface* sur, uint32_t usage)
175{
176 void* vaddr;
177 status_t res = GraphicBuffer::lock(usage, &vaddr);
178 if (res == NO_ERROR && sur) {
179 sur->version = sizeof(GGLSurface);
180 sur->width = width;
181 sur->height = height;
182 sur->stride = stride;
183 sur->format = format;
Mathias Agopian3330b202009-10-05 17:07:12 -0700184 sur->data = static_cast<GGLubyte*>(vaddr);
185 }
186 return res;
187}
188
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800189size_t GraphicBuffer::getFlattenedSize() const {
190 return (8 + (handle ? handle->numInts : 0))*sizeof(int);
191}
Mathias Agopian3330b202009-10-05 17:07:12 -0700192
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800193size_t GraphicBuffer::getFdCount() const {
194 return handle ? handle->numFds : 0;
195}
196
197status_t GraphicBuffer::flatten(void* buffer, size_t size,
198 int fds[], size_t count) const
Mathias Agopian3330b202009-10-05 17:07:12 -0700199{
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800200 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
201 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700202
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800203 size_t fdCountNeeded = GraphicBuffer::getFdCount();
204 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700205
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800206 int* buf = static_cast<int*>(buffer);
207 buf[0] = 'GBFR';
208 buf[1] = width;
209 buf[2] = height;
210 buf[3] = stride;
211 buf[4] = format;
212 buf[5] = usage;
213 buf[6] = 0;
214 buf[7] = 0;
215
216 if (handle) {
217 buf[6] = handle->numFds;
218 buf[7] = handle->numInts;
219 native_handle_t const* const h = handle;
220 memcpy(fds, h->data, h->numFds*sizeof(int));
221 memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700222 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800223
224 return NO_ERROR;
225}
226
227status_t GraphicBuffer::unflatten(void const* buffer, size_t size,
228 int fds[], size_t count)
229{
230 if (size < 8*sizeof(int)) return NO_MEMORY;
231
232 int const* buf = static_cast<int const*>(buffer);
233 if (buf[0] != 'GBFR') return BAD_TYPE;
234
235 const size_t numFds = buf[6];
236 const size_t numInts = buf[7];
237
238 const size_t sizeNeeded = (8 + numInts) * sizeof(int);
239 if (size < sizeNeeded) return NO_MEMORY;
240
241 size_t fdCountNeeded = 0;
242 if (count < fdCountNeeded) return NO_MEMORY;
243
244 if (handle) {
245 // free previous handle if any
246 free_handle();
247 }
248
249 if (numFds || numInts) {
250 width = buf[1];
251 height = buf[2];
252 stride = buf[3];
253 format = buf[4];
254 usage = buf[5];
255 native_handle* h = native_handle_create(numFds, numInts);
256 memcpy(h->data, fds, numFds*sizeof(int));
257 memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
258 handle = h;
259 } else {
260 width = height = stride = format = usage = 0;
261 handle = NULL;
262 }
263
264 mOwner = ownHandle;
265 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700266}
267
268
269void GraphicBuffer::setIndex(int index) {
270 mIndex = index;
271}
272
273int GraphicBuffer::getIndex() const {
274 return mIndex;
275}
276
Mathias Agopian3330b202009-10-05 17:07:12 -0700277// ---------------------------------------------------------------------------
278
279}; // namespace android