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 Agopiannamespace android {
323330b203039dea366d4981db1408a460134b2d2cMathias Agopian
333330b203039dea366d4981db1408a460134b2d2cMathias Agopian// ===========================================================================
34697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev// Buffer and implementation of ANativeWindowBuffer
353330b203039dea366d4981db1408a460134b2d2cMathias Agopian// ===========================================================================
363330b203039dea366d4981db1408a460134b2d2cMathias Agopian
373330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBuffer::GraphicBuffer()
3854ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
3987f9b877964b7d7db9b59ba45b73b4b1a779ecf8Mathias Agopian      mInitCheck(NO_ERROR) {
403330b203039dea366d4981db1408a460134b2d2cMathias Agopian    width  =
413330b203039dea366d4981db1408a460134b2d2cMathias Agopian    height =
423330b203039dea366d4981db1408a460134b2d2cMathias Agopian    stride =
433330b203039dea366d4981db1408a460134b2d2cMathias Agopian    format =
443330b203039dea366d4981db1408a460134b2d2cMathias Agopian    usage  = 0;
453330b203039dea366d4981db1408a460134b2d2cMathias Agopian    handle = NULL;
463330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
473330b203039dea366d4981db1408a460134b2d2cMathias Agopian
483330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
493330b203039dea366d4981db1408a460134b2d2cMathias Agopian        PixelFormat reqFormat, uint32_t reqUsage)
5054ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
5187f9b877964b7d7db9b59ba45b73b4b1a779ecf8Mathias Agopian      mInitCheck(NO_ERROR)
523330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
533330b203039dea366d4981db1408a460134b2d2cMathias Agopian    width  =
543330b203039dea366d4981db1408a460134b2d2cMathias Agopian    height =
553330b203039dea366d4981db1408a460134b2d2cMathias Agopian    stride =
563330b203039dea366d4981db1408a460134b2d2cMathias Agopian    format =
573330b203039dea366d4981db1408a460134b2d2cMathias Agopian    usage  = 0;
583330b203039dea366d4981db1408a460134b2d2cMathias Agopian    handle = NULL;
593330b203039dea366d4981db1408a460134b2d2cMathias Agopian    mInitCheck = initSize(w, h, reqFormat, reqUsage);
603330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
613330b203039dea366d4981db1408a460134b2d2cMathias Agopian
6254ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias AgopianGraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h,
6354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian        PixelFormat inFormat, uint32_t inUsage,
6454ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian        uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
6554ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
6654ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian      mBufferMapper(GraphicBufferMapper::get()),
6787f9b877964b7d7db9b59ba45b73b4b1a779ecf8Mathias Agopian      mInitCheck(NO_ERROR)
6854ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian{
6954ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    width  = w;
7054ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    height = h;
7154ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    stride = inStride;
7254ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    format = inFormat;
7354ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    usage  = inUsage;
7454ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    handle = inHandle;
7554ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian}
7654ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian
77697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan MalchevGraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
78309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
79309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis      mBufferMapper(GraphicBufferMapper::get()),
8087f9b877964b7d7db9b59ba45b73b4b1a779ecf8Mathias Agopian      mInitCheck(NO_ERROR), mWrappedBuffer(buffer)
81309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis{
82309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    width  = buffer->width;
83309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    height = buffer->height;
84309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    stride = buffer->stride;
85309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    format = buffer->format;
86309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    usage  = buffer->usage;
87309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    handle = buffer->handle;
88309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis}
89309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis
9098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias AgopianGraphicBuffer::~GraphicBuffer()
913330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
9298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (handle) {
9398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        free_handle();
943330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
953330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
963330b203039dea366d4981db1408a460134b2d2cMathias Agopian
9798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopianvoid GraphicBuffer::free_handle()
983330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
9998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (mOwner == ownHandle) {
100309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis        mBufferMapper.unregisterBuffer(handle);
10198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        native_handle_close(handle);
10298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        native_handle_delete(const_cast<native_handle*>(handle));
10398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    } else if (mOwner == ownData) {
10498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
10598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        allocator.free(handle);
1063330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
107309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    mWrappedBuffer = 0;
1083330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1093330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1103330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::initCheck() const {
1113330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return mInitCheck;
1123330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1133330b203039dea366d4981db1408a460134b2d2cMathias Agopian
114678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopianvoid GraphicBuffer::dumpAllocationsToSystemLog()
115678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian{
116678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian    GraphicBufferAllocator::dumpToSystemLog();
117678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian}
118678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian
119697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan MalchevANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
1203330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
121697526bc9e44ce61c88614f98387ae8bbf0a187eIliyan Malchev    return static_cast<ANativeWindowBuffer*>(
1223330b203039dea366d4981db1408a460134b2d2cMathias Agopian            const_cast<GraphicBuffer*>(this));
1233330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1243330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1253330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
1263330b203039dea366d4981db1408a460134b2d2cMathias Agopian        uint32_t reqUsage)
1273330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
12854ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian    if (mOwner != ownData)
12954ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian        return INVALID_OPERATION;
13054ba51dff21de666c5ae3bf3abd4f0634ebb0676Mathias Agopian
131579b3f88d03d06b897b778bd11818f5104677d1dMathias Agopian    if (handle && w==width && h==height && f==format && reqUsage==usage)
132579b3f88d03d06b897b778bd11818f5104677d1dMathias Agopian        return NO_ERROR;
133579b3f88d03d06b897b778bd11818f5104677d1dMathias Agopian
1343330b203039dea366d4981db1408a460134b2d2cMathias Agopian    if (handle) {
1353330b203039dea366d4981db1408a460134b2d2cMathias Agopian        GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
1363330b203039dea366d4981db1408a460134b2d2cMathias Agopian        allocator.free(handle);
1373330b203039dea366d4981db1408a460134b2d2cMathias Agopian        handle = 0;
1383330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
1393330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return initSize(w, h, f, reqUsage);
1403330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1413330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1423330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
1433330b203039dea366d4981db1408a460134b2d2cMathias Agopian        uint32_t reqUsage)
1443330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1453330b203039dea366d4981db1408a460134b2d2cMathias Agopian    GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
1463330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
1473330b203039dea366d4981db1408a460134b2d2cMathias Agopian    if (err == NO_ERROR) {
1483330b203039dea366d4981db1408a460134b2d2cMathias Agopian        this->width  = w;
1493330b203039dea366d4981db1408a460134b2d2cMathias Agopian        this->height = h;
1503330b203039dea366d4981db1408a460134b2d2cMathias Agopian        this->format = format;
1513330b203039dea366d4981db1408a460134b2d2cMathias Agopian        this->usage  = reqUsage;
1523330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
1533330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return err;
1543330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1553330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1563330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
1573330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1583330b203039dea366d4981db1408a460134b2d2cMathias Agopian    const Rect lockBounds(width, height);
1593330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t res = lock(usage, lockBounds, vaddr);
1603330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return res;
1613330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1623330b203039dea366d4981db1408a460134b2d2cMathias Agopian
1633330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
1643330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1653330b203039dea366d4981db1408a460134b2d2cMathias Agopian    if (rect.left < 0 || rect.right  > this->width ||
1663330b203039dea366d4981db1408a460134b2d2cMathias Agopian        rect.top  < 0 || rect.bottom > this->height) {
167e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
1683330b203039dea366d4981db1408a460134b2d2cMathias Agopian                rect.left, rect.top, rect.right, rect.bottom,
1693330b203039dea366d4981db1408a460134b2d2cMathias Agopian                this->width, this->height);
1703330b203039dea366d4981db1408a460134b2d2cMathias Agopian        return BAD_VALUE;
1713330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
1723330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
1733330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return res;
1743330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
1753330b203039dea366d4981db1408a460134b2d2cMathias Agopian
176c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvalastatus_t GraphicBuffer::lockYCbCr(uint32_t usage, android_ycbcr *ycbcr)
177c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala{
178c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala    const Rect lockBounds(width, height);
179c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala    status_t res = lockYCbCr(usage, lockBounds, ycbcr);
180c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala    return res;
181c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala}
182c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala
183c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvalastatus_t GraphicBuffer::lockYCbCr(uint32_t usage, const Rect& rect,
184c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala        android_ycbcr *ycbcr)
185c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala{
186c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala    if (rect.left < 0 || rect.right  > this->width ||
187c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala        rect.top  < 0 || rect.bottom > this->height) {
188c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala        ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
189c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala                rect.left, rect.top, rect.right, rect.bottom,
190c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala                this->width, this->height);
191c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala        return BAD_VALUE;
192c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala    }
193c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala    status_t res = getBufferMapper().lockYCbCr(handle, usage, rect, ycbcr);
194c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala    return res;
195c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala}
196c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala
1973330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBuffer::unlock()
1983330b203039dea366d4981db1408a460134b2d2cMathias Agopian{
1993330b203039dea366d4981db1408a460134b2d2cMathias Agopian    status_t res = getBufferMapper().unlock(handle);
2003330b203039dea366d4981db1408a460134b2d2cMathias Agopian    return res;
2013330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
2023330b203039dea366d4981db1408a460134b2d2cMathias Agopian
20398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopiansize_t GraphicBuffer::getFlattenedSize() const {
20498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    return (8 + (handle ? handle->numInts : 0))*sizeof(int);
20598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian}
20698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
20798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopiansize_t GraphicBuffer::getFdCount() const {
20898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    return handle ? handle->numFds : 0;
20998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian}
2103330b203039dea366d4981db1408a460134b2d2cMathias Agopian
211e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopianstatus_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
21298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
21398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (size < sizeNeeded) return NO_MEMORY;
21498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
21598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    size_t fdCountNeeded = GraphicBuffer::getFdCount();
21698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (count < fdCountNeeded) return NO_MEMORY;
21798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
21898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    int* buf = static_cast<int*>(buffer);
21998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[0] = 'GBFR';
22098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[1] = width;
22198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[2] = height;
22298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[3] = stride;
22398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[4] = format;
22498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[5] = usage;
22598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[6] = 0;
22698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    buf[7] = 0;
2273330b203039dea366d4981db1408a460134b2d2cMathias Agopian
22898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (handle) {
22998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        buf[6] = handle->numFds;
23098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        buf[7] = handle->numInts;
23198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        native_handle_t const* const h = handle;
23298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        memcpy(fds,     h->data,             h->numFds*sizeof(int));
23398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
23498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    }
23598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
236e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    buffer = reinterpret_cast<void*>(static_cast<int*>(buffer) + sizeNeeded);
237e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    size -= sizeNeeded;
238e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    fds += handle->numFds;
239e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    count -= handle->numFds;
240e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian
24198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    return NO_ERROR;
24298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian}
24398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
244e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopianstatus_t GraphicBuffer::unflatten(
245e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian        void const*& buffer, size_t& size, int const*& fds, size_t& count) {
24698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (size < 8*sizeof(int)) return NO_MEMORY;
24798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
24898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    int const* buf = static_cast<int const*>(buffer);
24998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (buf[0] != 'GBFR') return BAD_TYPE;
25098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
25198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    const size_t numFds  = buf[6];
25298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    const size_t numInts = buf[7];
2533330b203039dea366d4981db1408a460134b2d2cMathias Agopian
25498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    const size_t sizeNeeded = (8 + numInts) * sizeof(int);
25598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (size < sizeNeeded) return NO_MEMORY;
25698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
25798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    size_t fdCountNeeded = 0;
25898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (count < fdCountNeeded) return NO_MEMORY;
25998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
26098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (handle) {
26198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        // free previous handle if any
26298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        free_handle();
26398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    }
26498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
26598e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    if (numFds || numInts) {
26698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        width  = buf[1];
26798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        height = buf[2];
26898e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        stride = buf[3];
26998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        format = buf[4];
27098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        usage  = buf[5];
27198e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        native_handle* h = native_handle_create(numFds, numInts);
27298e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        memcpy(h->data,          fds,     numFds*sizeof(int));
27398e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
27498e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        handle = h;
2753330b203039dea366d4981db1408a460134b2d2cMathias Agopian    } else {
27698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        width = height = stride = format = usage = 0;
27798e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian        handle = NULL;
2783330b203039dea366d4981db1408a460134b2d2cMathias Agopian    }
27998e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian
28098e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    mOwner = ownHandle;
281309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis
282309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    if (handle != 0) {
283d69097f936d9780957a51ce77335ae409b32aaa3Jamie Gennis        status_t err = mBufferMapper.registerBuffer(handle);
284d69097f936d9780957a51ce77335ae409b32aaa3Jamie Gennis        if (err != NO_ERROR) {
285d69097f936d9780957a51ce77335ae409b32aaa3Jamie Gennis            ALOGE("unflatten: registerBuffer failed: %s (%d)",
286d69097f936d9780957a51ce77335ae409b32aaa3Jamie Gennis                    strerror(-err), err);
287d69097f936d9780957a51ce77335ae409b32aaa3Jamie Gennis            return err;
288d69097f936d9780957a51ce77335ae409b32aaa3Jamie Gennis        }
289309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis    }
290309d3bb2f902163356f9d40b6d45c11b435d77a9Jamie Gennis
291e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    buffer = reinterpret_cast<void const*>(static_cast<int const*>(buffer) + sizeNeeded);
292e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    size -= sizeNeeded;
293e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    fds += numFds;
294e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian    count -= numFds;
295e142428a9c8b9d2380032cd4d7b55ee440fe8770Mathias Agopian
29698e71ddaede9a0bfb681fd237bec1f66c6c53193Mathias Agopian    return NO_ERROR;
2973330b203039dea366d4981db1408a460134b2d2cMathias Agopian}
2983330b203039dea366d4981db1408a460134b2d2cMathias Agopian
2993330b203039dea366d4981db1408a460134b2d2cMathias Agopian// ---------------------------------------------------------------------------
3003330b203039dea366d4981db1408a460134b2d2cMathias Agopian
3013330b203039dea366d4981db1408a460134b2d2cMathias Agopian}; // namespace android
302