GraphicBuffer.cpp revision a0612e41dd5e6e9ec0a89e8e7437a3dafec0ee10
1/*
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
17#define LOG_TAG "GraphicBuffer"
18
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
23#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()
40    : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
41      mInitCheck(NO_ERROR),  mVStride(0), mIndex(-1)
42{
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)
53    : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
54      mInitCheck(NO_ERROR),  mVStride(0), mIndex(-1)
55{
56    width  =
57    height =
58    stride =
59    format =
60    usage  = 0;
61    handle = NULL;
62    mInitCheck = initSize(w, h, reqFormat, reqUsage);
63}
64
65GraphicBuffer::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()),
70      mInitCheck(NO_ERROR),  mVStride(0), mIndex(-1)
71{
72    width  = w;
73    height = h;
74    stride = inStride;
75    format = inFormat;
76    usage  = inUsage;
77    handle = inHandle;
78}
79
80GraphicBuffer::~GraphicBuffer()
81{
82    if (handle) {
83        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);
95    }
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{
111    if (mOwner != ownData)
112        return INVALID_OPERATION;
113
114    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{
125    GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
126    status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
127
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
139    if (err == NO_ERROR) {
140        this->width  = w;
141        this->height = h;
142        this->format = format;
143        this->usage  = reqUsage;
144        mVStride = 0;
145    }
146    return err;
147}
148
149status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
150{
151    const Rect lockBounds(width, height);
152    status_t res = lock(usage, lockBounds, vaddr);
153    return res;
154}
155
156status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
157{
158    if (rect.left < 0 || rect.right  > this->width ||
159        rect.top  < 0 || rect.bottom > this->height) {
160        LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
161                rect.left, rect.top, rect.right, rect.bottom,
162                this->width, this->height);
163        return BAD_VALUE;
164    }
165    status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
166    return res;
167}
168
169status_t GraphicBuffer::unlock()
170{
171    status_t res = getBufferMapper().unlock(handle);
172    return res;
173}
174
175status_t GraphicBuffer::lock(GGLSurface* sur, uint32_t usage)
176{
177    void* vaddr;
178    status_t res = GraphicBuffer::lock(usage, &vaddr);
179    if (res == NO_ERROR && sur) {
180        sur->version = sizeof(GGLSurface);
181        sur->width = width;
182        sur->height = height;
183        sur->stride = stride;
184        sur->format = format;
185        sur->vstride = mVStride;
186        sur->data = static_cast<GGLubyte*>(vaddr);
187    }
188    return res;
189}
190
191size_t GraphicBuffer::getFlattenedSize() const {
192    return (8 + (handle ? handle->numInts : 0))*sizeof(int);
193}
194
195size_t GraphicBuffer::getFdCount() const {
196    return handle ? handle->numFds : 0;
197}
198
199status_t GraphicBuffer::flatten(void* buffer, size_t size,
200        int fds[], size_t count) const
201{
202    size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
203    if (size < sizeNeeded) return NO_MEMORY;
204
205    size_t fdCountNeeded = GraphicBuffer::getFdCount();
206    if (count < fdCountNeeded) return NO_MEMORY;
207
208    int* buf = static_cast<int*>(buffer);
209    buf[0] = 'GBFR';
210    buf[1] = width;
211    buf[2] = height;
212    buf[3] = stride;
213    buf[4] = format;
214    buf[5] = usage;
215    buf[6] = 0;
216    buf[7] = 0;
217
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));
223        memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
224    }
225
226    return NO_ERROR;
227}
228
229status_t GraphicBuffer::unflatten(void const* buffer, size_t size,
230        int fds[], size_t count)
231{
232    if (size < 8*sizeof(int)) return NO_MEMORY;
233
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
240    const size_t sizeNeeded = (8 + numInts) * sizeof(int);
241    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];
257        native_handle* h = native_handle_create(numFds, numInts);
258        memcpy(h->data,          fds,     numFds*sizeof(int));
259        memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
260        handle = h;
261    } else {
262        width = height = stride = format = usage = 0;
263        handle = NULL;
264    }
265
266    mOwner = ownHandle;
267    return NO_ERROR;
268}
269
270
271void GraphicBuffer::setIndex(int index) {
272    mIndex = index;
273}
274
275int GraphicBuffer::getIndex() const {
276    return mIndex;
277}
278
279void GraphicBuffer::setVerticalStride(uint32_t vstride) {
280    mVStride = vstride;
281}
282
283uint32_t GraphicBuffer::getVerticalStride() const {
284    return mVStride;
285}
286
287// ---------------------------------------------------------------------------
288
289}; // namespace android
290