GraphicBuffer.cpp revision 5f2165f9455d4893b581b73a67c5431f4344b47e
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
31namespace android {
32
33// ===========================================================================
34// Buffer and implementation of ANativeWindowBuffer
35// ===========================================================================
36
37GraphicBuffer::GraphicBuffer()
38    : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
39      mInitCheck(NO_ERROR), mIndex(-1)
40{
41    width  =
42    height =
43    stride =
44    format =
45    usage  = 0;
46    handle = NULL;
47}
48
49GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
50        PixelFormat reqFormat, uint32_t reqUsage)
51    : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
52      mInitCheck(NO_ERROR), mIndex(-1)
53{
54    width  =
55    height =
56    stride =
57    format =
58    usage  = 0;
59    handle = NULL;
60    mInitCheck = initSize(w, h, reqFormat, reqUsage);
61}
62
63GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
64        PixelFormat inFormat, uint32_t inUsage,
65        uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
66    : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
67      mBufferMapper(GraphicBufferMapper::get()),
68      mInitCheck(NO_ERROR), mIndex(-1)
69{
70    width  = w;
71    height = h;
72    stride = inStride;
73    format = inFormat;
74    usage  = inUsage;
75    handle = inHandle;
76}
77
78GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
79    : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
80      mBufferMapper(GraphicBufferMapper::get()),
81      mInitCheck(NO_ERROR), mIndex(-1), mWrappedBuffer(buffer)
82{
83    width  = buffer->width;
84    height = buffer->height;
85    stride = buffer->stride;
86    format = buffer->format;
87    usage  = buffer->usage;
88    handle = buffer->handle;
89}
90
91GraphicBuffer::~GraphicBuffer()
92{
93    if (handle) {
94        free_handle();
95    }
96}
97
98void GraphicBuffer::free_handle()
99{
100    if (mOwner == ownHandle) {
101        mBufferMapper.unregisterBuffer(handle);
102        native_handle_close(handle);
103        native_handle_delete(const_cast<native_handle*>(handle));
104    } else if (mOwner == ownData) {
105        GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
106        allocator.free(handle);
107    }
108    mWrappedBuffer = 0;
109}
110
111status_t GraphicBuffer::initCheck() const {
112    return mInitCheck;
113}
114
115void GraphicBuffer::dumpAllocationsToSystemLog()
116{
117    GraphicBufferAllocator::dumpToSystemLog();
118}
119
120ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
121{
122    return static_cast<ANativeWindowBuffer*>(
123            const_cast<GraphicBuffer*>(this));
124}
125
126status_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
127        uint32_t reqUsage)
128{
129    if (mOwner != ownData)
130        return INVALID_OPERATION;
131
132    if (handle && w==width && h==height && f==format && reqUsage==usage)
133        return NO_ERROR;
134
135    if (handle) {
136        GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
137        allocator.free(handle);
138        handle = 0;
139    }
140    return initSize(w, h, f, reqUsage);
141}
142
143status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
144        uint32_t reqUsage)
145{
146    GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
147    status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
148    if (err == NO_ERROR) {
149        this->width  = w;
150        this->height = h;
151        this->format = format;
152        this->usage  = reqUsage;
153    }
154    return err;
155}
156
157status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
158{
159    const Rect lockBounds(width, height);
160    status_t res = lock(usage, lockBounds, vaddr);
161    return res;
162}
163
164status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
165{
166    if (rect.left < 0 || rect.right  > this->width ||
167        rect.top  < 0 || rect.bottom > this->height) {
168        ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
169                rect.left, rect.top, rect.right, rect.bottom,
170                this->width, this->height);
171        return BAD_VALUE;
172    }
173    status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
174    return res;
175}
176
177status_t GraphicBuffer::unlock()
178{
179    status_t res = getBufferMapper().unlock(handle);
180    return res;
181}
182
183size_t GraphicBuffer::getFlattenedSize() const {
184    return (8 + (handle ? handle->numInts : 0))*sizeof(int);
185}
186
187size_t GraphicBuffer::getFdCount() const {
188    return handle ? handle->numFds : 0;
189}
190
191status_t GraphicBuffer::flatten(void* buffer, size_t size,
192        int fds[], size_t count) const
193{
194    size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
195    if (size < sizeNeeded) return NO_MEMORY;
196
197    size_t fdCountNeeded = GraphicBuffer::getFdCount();
198    if (count < fdCountNeeded) return NO_MEMORY;
199
200    int* buf = static_cast<int*>(buffer);
201    buf[0] = 'GBFR';
202    buf[1] = width;
203    buf[2] = height;
204    buf[3] = stride;
205    buf[4] = format;
206    buf[5] = usage;
207    buf[6] = 0;
208    buf[7] = 0;
209
210    if (handle) {
211        buf[6] = handle->numFds;
212        buf[7] = handle->numInts;
213        native_handle_t const* const h = handle;
214        memcpy(fds,     h->data,             h->numFds*sizeof(int));
215        memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
216    }
217
218    return NO_ERROR;
219}
220
221status_t GraphicBuffer::unflatten(void const* buffer, size_t size,
222        int fds[], size_t count)
223{
224    if (size < 8*sizeof(int)) return NO_MEMORY;
225
226    int const* buf = static_cast<int const*>(buffer);
227    if (buf[0] != 'GBFR') return BAD_TYPE;
228
229    const size_t numFds  = buf[6];
230    const size_t numInts = buf[7];
231
232    const size_t sizeNeeded = (8 + numInts) * sizeof(int);
233    if (size < sizeNeeded) return NO_MEMORY;
234
235    size_t fdCountNeeded = 0;
236    if (count < fdCountNeeded) return NO_MEMORY;
237
238    if (handle) {
239        // free previous handle if any
240        free_handle();
241    }
242
243    if (numFds || numInts) {
244        width  = buf[1];
245        height = buf[2];
246        stride = buf[3];
247        format = buf[4];
248        usage  = buf[5];
249        native_handle* h = native_handle_create(numFds, numInts);
250        memcpy(h->data,          fds,     numFds*sizeof(int));
251        memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
252        handle = h;
253    } else {
254        width = height = stride = format = usage = 0;
255        handle = NULL;
256    }
257
258    mOwner = ownHandle;
259
260    if (handle != 0) {
261        mBufferMapper.registerBuffer(handle);
262    }
263
264    return NO_ERROR;
265}
266
267
268void GraphicBuffer::setIndex(int index) {
269    mIndex = index;
270}
271
272int GraphicBuffer::getIndex() const {
273    return mIndex;
274}
275
276// ---------------------------------------------------------------------------
277
278}; // namespace android
279