GraphicBuffer.cpp revision b7e930db175c192464cebdeb49eb56cf6dd60114
13330b203039dea366d4981db1408a460134b2d2cMathias Agopian/*
23330b203039dea366d4981db1408a460134b2d2cMathias Agopian * Copyright (C) 2007 The Android Open Source Project
33330b203039dea366d4981db1408a460134b2d2cMathias Agopian *
43330b203039dea366d4981db1408a460134b2d2cMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
53330b203039dea366d4981db1408a460134b2d2cMathias Agopian * you may not use this file except in compliance with the License.
63330b203039dea366d4981db1408a460134b2d2cMathias Agopian * You may obtain a copy of the License at
73330b203039dea366d4981db1408a460134b2d2cMathias Agopian *
83330b203039dea366d4981db1408a460134b2d2cMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
93330b203039dea366d4981db1408a460134b2d2cMathias Agopian *
103330b203039dea366d4981db1408a460134b2d2cMathias Agopian * Unless required by applicable law or agreed to in writing, software
113330b203039dea366d4981db1408a460134b2d2cMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
123330b203039dea366d4981db1408a460134b2d2cMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133330b203039dea366d4981db1408a460134b2d2cMathias Agopian * See the License for the specific language governing permissions and
143330b203039dea366d4981db1408a460134b2d2cMathias Agopian * limitations under the License.
153330b203039dea366d4981db1408a460134b2d2cMathias Agopian */
163330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian#define LOG_TAG "GraphicBuffer"
1898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
193330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <stdlib.h>
203330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <stdint.h>
213330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <sys/types.h>
223330b203039dea366d4981db1408a460134b2d2cMathias Agopian
233330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <utils/Errors.h>
243330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <utils/Log.h>
253330b203039dea366d4981db1408a460134b2d2cMathias Agopian
263330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBuffer.h>
273330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBufferAllocator.h>
283330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBufferMapper.h>
293330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/PixelFormat.h>
303330b203039dea366d4981db1408a460134b2d2cMathias Agopian
313330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <pixelflinger/pixelflinger.h>
323330b203039dea366d4981db1408a460134b2d2cMathias Agopian
333330b203039dea366d4981db1408a460134b2d2cMathias Agopiannamespace android {
343330b203039dea366d4981db1408a460134b2d2cMathias Agopian
353330b203039dea366d4981db1408a460134b2d2cMathias Agopian// ===========================================================================
363330b203039dea366d4981db1408a460134b2d2cMathias Agopian// Buffer and implementation of android_native_buffer_t
373330b203039dea366d4981db1408a460134b2d2cMathias Agopian// ===========================================================================
383330b203039dea366d4981db1408a460134b2d2cMathias Agopian
393330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBuffer::GraphicBuffer()
4054ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
41b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian      mInitCheck(NO_ERROR), mIndex(-1)
423330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
433330b203039dea366d4981db1408a460134b2d2cMathias Agopian    width  =
443330b203039dea366d4981db1408a460134b2d2cMathias Agopian    height =
453330b203039dea366d4981db1408a460134b2d2cMathias Agopian    stride =
463330b203039dea366d4981db1408a460134b2d2cMathias Agopian    format =
473330b203039dea366d4981db1408a460134b2d2cMathias Agopian    usage  = 0;
483330b203039dea366d4981db1408a460134b2d2cMathias Agopian    handle = NULL;
493330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
503330b203039dea366d4981db1408a460134b2d2cMathias Agopian
513330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
523330b203039dea366d4981db1408a460134b2d2cMathias Agopian        PixelFormat reqFormat, uint32_t reqUsage)
5354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
54b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian      mInitCheck(NO_ERROR), mIndex(-1)
553330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
563330b203039dea366d4981db1408a460134b2d2cMathias Agopian    width  =
573330b203039dea366d4981db1408a460134b2d2cMathias Agopian    height =
583330b203039dea366d4981db1408a460134b2d2cMathias Agopian    stride =
593330b203039dea366d4981db1408a460134b2d2cMathias Agopian    format =
603330b203039dea366d4981db1408a460134b2d2cMathias Agopian    usage  = 0;
613330b203039dea366d4981db1408a460134b2d2cMathias Agopian    handle = NULL;
623330b203039dea366d4981db1408a460134b2d2cMathias Agopian    mInitCheck = initSize(w, h, reqFormat, reqUsage);
633330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
643330b203039dea366d4981db1408a460134b2d2cMathias Agopian
6554ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias AgopianGraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
6654ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian        PixelFormat inFormat, uint32_t inUsage,
6754ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian        uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
6854ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
6954ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian      mBufferMapper(GraphicBufferMapper::get()),
70b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian      mInitCheck(NO_ERROR), mIndex(-1)
7154ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian{
7254ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    width  = w;
7354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    height = h;
7454ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    stride = inStride;
7554ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    format = inFormat;
7654ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    usage  = inUsage;
7754ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    handle = inHandle;
7854ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian}
7954ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian
8098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias AgopianGraphicBuffer::~GraphicBuffer()
813330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
8298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (handle) {
8398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        free_handle();
843330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
853330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
863330b203039dea366d4981db1408a460134b2d2cMathias Agopian
8798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopianvoid GraphicBuffer::free_handle()
883330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
8998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (mOwner == ownHandle) {
9098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        native_handle_close(handle);
9198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        native_handle_delete(const_cast<native_handle*>(handle));
9298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    } else if (mOwner == ownData) {
9398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
9498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        allocator.free(handle);
953330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
963330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
973330b203039dea366d4981db1408a460134b2d2cMathias Agopian
983330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::initCheck() const {
993330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return mInitCheck;
1003330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1013330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1023330b203039dea366d4981db1408a460134b2d2cMathias Agopianandroid_native_buffer_t* GraphicBuffer::getNativeBuffer() const
1033330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1043330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return static_cast<android_native_buffer_t*>(
1053330b203039dea366d4981db1408a460134b2d2cMathias Agopian            const_cast<GraphicBuffer*>(this));
1063330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1073330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1083330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
1093330b203039dea366d4981db1408a460134b2d2cMathias Agopian        uint32_t reqUsage)
1103330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
11154ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    if (mOwner != ownData)
11254ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian        return INVALID_OPERATION;
11354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian
1143330b203039dea366d4981db1408a460134b2d2cMathias Agopian    if (handle) {
1153330b203039dea366d4981db1408a460134b2d2cMathias Agopian        GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
1163330b203039dea366d4981db1408a460134b2d2cMathias Agopian        allocator.free(handle);
1173330b203039dea366d4981db1408a460134b2d2cMathias Agopian        handle = 0;
1183330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
1193330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return initSize(w, h, f, reqUsage);
1203330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1213330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1223330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
1233330b203039dea366d4981db1408a460134b2d2cMathias Agopian        uint32_t reqUsage)
1243330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1253330b203039dea366d4981db1408a460134b2d2cMathias Agopian    GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
1263330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
12749753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian
12849753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian    if (err<0 && format == PIXEL_FORMAT_RGBX_8888) {
12949753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian        /*
13049753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian         * There is currently a bug with some gralloc implementations
13149753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian         * not supporting RGBX_8888. In this case, we revert to using RGBA_8888
13249753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian         * which is not exactly the same, as GL_REPLACE will yield a different
13349753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian         * result.
13449753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian         */
13549753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian        format = PIXEL_FORMAT_RGBA_8888;
13649753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian        err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
13749753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian    }
13849753266d2219d2fbf5c33dc4626f299dffcdd76Mathias Agopian
1393330b203039dea366d4981db1408a460134b2d2cMathias Agopian    if (err == NO_ERROR) {
1403330b203039dea366d4981db1408a460134b2d2cMathias Agopian        this->width  = w;
1413330b203039dea366d4981db1408a460134b2d2cMathias Agopian        this->height = h;
1423330b203039dea366d4981db1408a460134b2d2cMathias Agopian        this->format = format;
1433330b203039dea366d4981db1408a460134b2d2cMathias Agopian        this->usage  = reqUsage;
1443330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
1453330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return err;
1463330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1473330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1483330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
1493330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1503330b203039dea366d4981db1408a460134b2d2cMathias Agopian    const Rect lockBounds(width, height);
1513330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t res = lock(usage, lockBounds, vaddr);
1523330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return res;
1533330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1543330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1553330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
1563330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1573330b203039dea366d4981db1408a460134b2d2cMathias Agopian    if (rect.left < 0 || rect.right  > this->width ||
1583330b203039dea366d4981db1408a460134b2d2cMathias Agopian        rect.top  < 0 || rect.bottom > this->height) {
1593330b203039dea366d4981db1408a460134b2d2cMathias Agopian        LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
1603330b203039dea366d4981db1408a460134b2d2cMathias Agopian                rect.left, rect.top, rect.right, rect.bottom,
1613330b203039dea366d4981db1408a460134b2d2cMathias Agopian                this->width, this->height);
1623330b203039dea366d4981db1408a460134b2d2cMathias Agopian        return BAD_VALUE;
1633330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
1643330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
1653330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return res;
1663330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1673330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1683330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::unlock()
1693330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1703330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t res = getBufferMapper().unlock(handle);
1713330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return res;
1723330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1733330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1743330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::lock(GGLSurface* sur, uint32_t usage)
1753330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1763330b203039dea366d4981db1408a460134b2d2cMathias Agopian    void* vaddr;
1773330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t res = GraphicBuffer::lock(usage, &vaddr);
1783330b203039dea366d4981db1408a460134b2d2cMathias Agopian    if (res == NO_ERROR && sur) {
1793330b203039dea366d4981db1408a460134b2d2cMathias Agopian        sur->version = sizeof(GGLSurface);
1803330b203039dea366d4981db1408a460134b2d2cMathias Agopian        sur->width = width;
1813330b203039dea366d4981db1408a460134b2d2cMathias Agopian        sur->height = height;
1823330b203039dea366d4981db1408a460134b2d2cMathias Agopian        sur->stride = stride;
1833330b203039dea366d4981db1408a460134b2d2cMathias Agopian        sur->format = format;
1843330b203039dea366d4981db1408a460134b2d2cMathias Agopian        sur->data = static_cast<GGLubyte*>(vaddr);
1853330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
1863330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return res;
1873330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1883330b203039dea366d4981db1408a460134b2d2cMathias Agopian
18998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopiansize_t GraphicBuffer::getFlattenedSize() const {
19098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    return (8 + (handle ? handle->numInts : 0))*sizeof(int);
19198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian}
19298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
19398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopiansize_t GraphicBuffer::getFdCount() const {
19498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    return handle ? handle->numFds : 0;
19598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian}
1963330b203039dea366d4981db1408a460134b2d2cMathias Agopian
19798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopianstatus_t GraphicBuffer::flatten(void* buffer, size_t size,
19898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        int fds[], size_t count) const
1993330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
20098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
20198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (size < sizeNeeded) return NO_MEMORY;
20298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
20398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    size_t fdCountNeeded = GraphicBuffer::getFdCount();
20498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (count < fdCountNeeded) return NO_MEMORY;
20598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
20698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    int* buf = static_cast<int*>(buffer);
20798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[0] = 'GBFR';
20898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[1] = width;
20998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[2] = height;
21098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[3] = stride;
21198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[4] = format;
21298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[5] = usage;
21398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[6] = 0;
21498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[7] = 0;
2153330b203039dea366d4981db1408a460134b2d2cMathias Agopian
21698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (handle) {
21798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        buf[6] = handle->numFds;
21898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        buf[7] = handle->numInts;
21998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        native_handle_t const* const h = handle;
22098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        memcpy(fds,     h->data,             h->numFds*sizeof(int));
22198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
22298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    }
22398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
22498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    return NO_ERROR;
22598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian}
22698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
22798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopianstatus_t GraphicBuffer::unflatten(void const* buffer, size_t size,
22898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        int fds[], size_t count)
22998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian{
23098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (size < 8*sizeof(int)) return NO_MEMORY;
23198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
23298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    int const* buf = static_cast<int const*>(buffer);
23398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (buf[0] != 'GBFR') return BAD_TYPE;
23498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
23598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    const size_t numFds  = buf[6];
23698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    const size_t numInts = buf[7];
2373330b203039dea366d4981db1408a460134b2d2cMathias Agopian
23898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    const size_t sizeNeeded = (8 + numInts) * sizeof(int);
23998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (size < sizeNeeded) return NO_MEMORY;
24098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
24198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    size_t fdCountNeeded = 0;
24298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (count < fdCountNeeded) return NO_MEMORY;
24398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
24498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (handle) {
24598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        // free previous handle if any
24698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        free_handle();
24798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    }
24898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
24998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (numFds || numInts) {
25098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        width  = buf[1];
25198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        height = buf[2];
25298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        stride = buf[3];
25398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        format = buf[4];
25498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        usage  = buf[5];
25598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        native_handle* h = native_handle_create(numFds, numInts);
25698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        memcpy(h->data,          fds,     numFds*sizeof(int));
25798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
25898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        handle = h;
2593330b203039dea366d4981db1408a460134b2d2cMathias Agopian    } else {
26098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        width = height = stride = format = usage = 0;
26198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        handle = NULL;
2623330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
26398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
26498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    mOwner = ownHandle;
26598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    return NO_ERROR;
2663330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
2673330b203039dea366d4981db1408a460134b2d2cMathias Agopian
2683330b203039dea366d4981db1408a460134b2d2cMathias Agopian
2693330b203039dea366d4981db1408a460134b2d2cMathias Agopianvoid GraphicBuffer::setIndex(int index) {
2703330b203039dea366d4981db1408a460134b2d2cMathias Agopian    mIndex = index;
2713330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
2723330b203039dea366d4981db1408a460134b2d2cMathias Agopian
2733330b203039dea366d4981db1408a460134b2d2cMathias Agopianint GraphicBuffer::getIndex() const {
2743330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return mIndex;
2753330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
2763330b203039dea366d4981db1408a460134b2d2cMathias Agopian
2773330b203039dea366d4981db1408a460134b2d2cMathias Agopian// ---------------------------------------------------------------------------
2783330b203039dea366d4981db1408a460134b2d2cMathias Agopian
2793330b203039dea366d4981db1408a460134b2d2cMathias Agopian}; // namespace android
280