GraphicBuffer.cpp revision e142428a9c8b9d2380032cd4d7b55ee440fe8770
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::lockYCbCr(uint32_t usage, android_ycbcr *ycbcr)
178{
179    const Rect lockBounds(width, height);
180    status_t res = lockYCbCr(usage, lockBounds, ycbcr);
181    return res;
182}
183
184status_t GraphicBuffer::lockYCbCr(uint32_t usage, const Rect& rect,
185        android_ycbcr *ycbcr)
186{
187    if (rect.left < 0 || rect.right  > this->width ||
188        rect.top  < 0 || rect.bottom > this->height) {
189        ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
190                rect.left, rect.top, rect.right, rect.bottom,
191                this->width, this->height);
192        return BAD_VALUE;
193    }
194    status_t res = getBufferMapper().lockYCbCr(handle, usage, rect, ycbcr);
195    return res;
196}
197
198status_t GraphicBuffer::unlock()
199{
200    status_t res = getBufferMapper().unlock(handle);
201    return res;
202}
203
204size_t GraphicBuffer::getFlattenedSize() const {
205    return (8 + (handle ? handle->numInts : 0))*sizeof(int);
206}
207
208size_t GraphicBuffer::getFdCount() const {
209    return handle ? handle->numFds : 0;
210}
211
212status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
213    size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
214    if (size < sizeNeeded) return NO_MEMORY;
215
216    size_t fdCountNeeded = GraphicBuffer::getFdCount();
217    if (count < fdCountNeeded) return NO_MEMORY;
218
219    int* buf = static_cast<int*>(buffer);
220    buf[0] = 'GBFR';
221    buf[1] = width;
222    buf[2] = height;
223    buf[3] = stride;
224    buf[4] = format;
225    buf[5] = usage;
226    buf[6] = 0;
227    buf[7] = 0;
228
229    if (handle) {
230        buf[6] = handle->numFds;
231        buf[7] = handle->numInts;
232        native_handle_t const* const h = handle;
233        memcpy(fds,     h->data,             h->numFds*sizeof(int));
234        memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
235    }
236
237    buffer = reinterpret_cast<void*>(static_cast<int*>(buffer) + sizeNeeded);
238    size -= sizeNeeded;
239    fds += handle->numFds;
240    count -= handle->numFds;
241
242    return NO_ERROR;
243}
244
245status_t GraphicBuffer::unflatten(
246        void const*& buffer, size_t& size, int const*& fds, size_t& count) {
247    if (size < 8*sizeof(int)) return NO_MEMORY;
248
249    int const* buf = static_cast<int const*>(buffer);
250    if (buf[0] != 'GBFR') return BAD_TYPE;
251
252    const size_t numFds  = buf[6];
253    const size_t numInts = buf[7];
254
255    const size_t sizeNeeded = (8 + numInts) * sizeof(int);
256    if (size < sizeNeeded) return NO_MEMORY;
257
258    size_t fdCountNeeded = 0;
259    if (count < fdCountNeeded) return NO_MEMORY;
260
261    if (handle) {
262        // free previous handle if any
263        free_handle();
264    }
265
266    if (numFds || numInts) {
267        width  = buf[1];
268        height = buf[2];
269        stride = buf[3];
270        format = buf[4];
271        usage  = buf[5];
272        native_handle* h = native_handle_create(numFds, numInts);
273        memcpy(h->data,          fds,     numFds*sizeof(int));
274        memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
275        handle = h;
276    } else {
277        width = height = stride = format = usage = 0;
278        handle = NULL;
279    }
280
281    mOwner = ownHandle;
282
283    if (handle != 0) {
284        status_t err = mBufferMapper.registerBuffer(handle);
285        if (err != NO_ERROR) {
286            ALOGE("unflatten: registerBuffer failed: %s (%d)",
287                    strerror(-err), err);
288            return err;
289        }
290    }
291
292    buffer = reinterpret_cast<void const*>(static_cast<int const*>(buffer) + sizeNeeded);
293    size -= sizeNeeded;
294    fds += numFds;
295    count -= numFds;
296
297    return NO_ERROR;
298}
299
300
301void GraphicBuffer::setIndex(int index) {
302    mIndex = index;
303}
304
305int GraphicBuffer::getIndex() const {
306    return mIndex;
307}
308
309// ---------------------------------------------------------------------------
310
311}; // namespace android
312