Home | History | Annotate | Download | only in ui
      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 namespace android {
     32 
     33 // ===========================================================================
     34 // Buffer and implementation of ANativeWindowBuffer
     35 // ===========================================================================
     36 
     37 GraphicBuffer::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 
     49 GraphicBuffer::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 
     63 GraphicBuffer::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 
     78 GraphicBuffer::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 
     91 GraphicBuffer::~GraphicBuffer()
     92 {
     93     if (handle) {
     94         free_handle();
     95     }
     96 }
     97 
     98 void 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 
    111 status_t GraphicBuffer::initCheck() const {
    112     return mInitCheck;
    113 }
    114 
    115 void GraphicBuffer::dumpAllocationsToSystemLog()
    116 {
    117     GraphicBufferAllocator::dumpToSystemLog();
    118 }
    119 
    120 ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
    121 {
    122     return static_cast<ANativeWindowBuffer*>(
    123             const_cast<GraphicBuffer*>(this));
    124 }
    125 
    126 status_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 
    143 status_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 
    157 status_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 
    164 status_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 
    177 status_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 
    184 status_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 
    198 status_t GraphicBuffer::unlock()
    199 {
    200     status_t res = getBufferMapper().unlock(handle);
    201     return res;
    202 }
    203 
    204 size_t GraphicBuffer::getFlattenedSize() const {
    205     return (8 + (handle ? handle->numInts : 0))*sizeof(int);
    206 }
    207 
    208 size_t GraphicBuffer::getFdCount() const {
    209     return handle ? handle->numFds : 0;
    210 }
    211 
    212 status_t GraphicBuffer::flatten(void* buffer, size_t size,
    213         int fds[], size_t count) const
    214 {
    215     size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
    216     if (size < sizeNeeded) return NO_MEMORY;
    217 
    218     size_t fdCountNeeded = GraphicBuffer::getFdCount();
    219     if (count < fdCountNeeded) return NO_MEMORY;
    220 
    221     int* buf = static_cast<int*>(buffer);
    222     buf[0] = 'GBFR';
    223     buf[1] = width;
    224     buf[2] = height;
    225     buf[3] = stride;
    226     buf[4] = format;
    227     buf[5] = usage;
    228     buf[6] = 0;
    229     buf[7] = 0;
    230 
    231     if (handle) {
    232         buf[6] = handle->numFds;
    233         buf[7] = handle->numInts;
    234         native_handle_t const* const h = handle;
    235         memcpy(fds,     h->data,             h->numFds*sizeof(int));
    236         memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int));
    237     }
    238 
    239     return NO_ERROR;
    240 }
    241 
    242 status_t GraphicBuffer::unflatten(void const* buffer, size_t size,
    243         int fds[], size_t count)
    244 {
    245     if (size < 8*sizeof(int)) return NO_MEMORY;
    246 
    247     int const* buf = static_cast<int const*>(buffer);
    248     if (buf[0] != 'GBFR') return BAD_TYPE;
    249 
    250     const size_t numFds  = buf[6];
    251     const size_t numInts = buf[7];
    252 
    253     const size_t sizeNeeded = (8 + numInts) * sizeof(int);
    254     if (size < sizeNeeded) return NO_MEMORY;
    255 
    256     size_t fdCountNeeded = 0;
    257     if (count < fdCountNeeded) return NO_MEMORY;
    258 
    259     if (handle) {
    260         // free previous handle if any
    261         free_handle();
    262     }
    263 
    264     if (numFds || numInts) {
    265         width  = buf[1];
    266         height = buf[2];
    267         stride = buf[3];
    268         format = buf[4];
    269         usage  = buf[5];
    270         native_handle* h = native_handle_create(numFds, numInts);
    271         memcpy(h->data,          fds,     numFds*sizeof(int));
    272         memcpy(h->data + numFds, &buf[8], numInts*sizeof(int));
    273         handle = h;
    274     } else {
    275         width = height = stride = format = usage = 0;
    276         handle = NULL;
    277     }
    278 
    279     mOwner = ownHandle;
    280 
    281     if (handle != 0) {
    282         status_t err = mBufferMapper.registerBuffer(handle);
    283         if (err != NO_ERROR) {
    284             ALOGE("unflatten: registerBuffer failed: %s (%d)",
    285                     strerror(-err), err);
    286             return err;
    287         }
    288     }
    289 
    290     return NO_ERROR;
    291 }
    292 
    293 
    294 void GraphicBuffer::setIndex(int index) {
    295     mIndex = index;
    296 }
    297 
    298 int GraphicBuffer::getIndex() const {
    299     return mIndex;
    300 }
    301 
    302 // ---------------------------------------------------------------------------
    303 
    304 }; // namespace android
    305