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 #ifndef ANDROID_GRAPHIC_BUFFER_H
     18 #define ANDROID_GRAPHIC_BUFFER_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <ui/ANativeObjectBase.h>
     24 #include <ui/PixelFormat.h>
     25 #include <ui/Rect.h>
     26 #include <utils/Flattenable.h>
     27 #include <utils/RefBase.h>
     28 
     29 #include <string>
     30 
     31 struct ANativeWindowBuffer;
     32 
     33 namespace android {
     34 
     35 class GraphicBufferMapper;
     36 
     37 // ===========================================================================
     38 // GraphicBuffer
     39 // ===========================================================================
     40 
     41 class GraphicBuffer
     42     : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >,
     43       public Flattenable<GraphicBuffer>
     44 {
     45     friend class Flattenable<GraphicBuffer>;
     46 public:
     47 
     48     enum {
     49         USAGE_SW_READ_NEVER     = GRALLOC_USAGE_SW_READ_NEVER,
     50         USAGE_SW_READ_RARELY    = GRALLOC_USAGE_SW_READ_RARELY,
     51         USAGE_SW_READ_OFTEN     = GRALLOC_USAGE_SW_READ_OFTEN,
     52         USAGE_SW_READ_MASK      = GRALLOC_USAGE_SW_READ_MASK,
     53 
     54         USAGE_SW_WRITE_NEVER    = GRALLOC_USAGE_SW_WRITE_NEVER,
     55         USAGE_SW_WRITE_RARELY   = GRALLOC_USAGE_SW_WRITE_RARELY,
     56         USAGE_SW_WRITE_OFTEN    = GRALLOC_USAGE_SW_WRITE_OFTEN,
     57         USAGE_SW_WRITE_MASK     = GRALLOC_USAGE_SW_WRITE_MASK,
     58 
     59         USAGE_SOFTWARE_MASK     = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
     60 
     61         USAGE_PROTECTED         = GRALLOC_USAGE_PROTECTED,
     62 
     63         USAGE_HW_TEXTURE        = GRALLOC_USAGE_HW_TEXTURE,
     64         USAGE_HW_RENDER         = GRALLOC_USAGE_HW_RENDER,
     65         USAGE_HW_2D             = GRALLOC_USAGE_HW_2D,
     66         USAGE_HW_COMPOSER       = GRALLOC_USAGE_HW_COMPOSER,
     67         USAGE_HW_VIDEO_ENCODER  = GRALLOC_USAGE_HW_VIDEO_ENCODER,
     68         USAGE_HW_MASK           = GRALLOC_USAGE_HW_MASK,
     69 
     70         USAGE_CURSOR            = GRALLOC_USAGE_CURSOR,
     71     };
     72 
     73     GraphicBuffer();
     74 
     75     // creates w * h buffer
     76     GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
     77             uint32_t inUsage, std::string requestorName = "<Unknown>");
     78 
     79     // create a buffer from an existing handle
     80     GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
     81             uint32_t inUsage, uint32_t inStride, native_handle_t* inHandle,
     82             bool keepOwnership);
     83 
     84     // create a buffer from an existing ANativeWindowBuffer
     85     GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
     86 
     87     // return status
     88     status_t initCheck() const;
     89 
     90     uint32_t getWidth() const           { return static_cast<uint32_t>(width); }
     91     uint32_t getHeight() const          { return static_cast<uint32_t>(height); }
     92     uint32_t getStride() const          { return static_cast<uint32_t>(stride); }
     93     uint32_t getUsage() const           { return static_cast<uint32_t>(usage); }
     94     PixelFormat getPixelFormat() const  { return format; }
     95     Rect getBounds() const              { return Rect(width, height); }
     96     uint64_t getId() const              { return mId; }
     97 
     98     uint32_t getGenerationNumber() const { return mGenerationNumber; }
     99     void setGenerationNumber(uint32_t generation) {
    100         mGenerationNumber = generation;
    101     }
    102 
    103     status_t reallocate(uint32_t inWidth, uint32_t inHeight,
    104             PixelFormat inFormat, uint32_t inUsage);
    105 
    106     bool needsReallocation(uint32_t inWidth, uint32_t inHeight,
    107             PixelFormat inFormat, uint32_t inUsage);
    108 
    109     status_t lock(uint32_t inUsage, void** vaddr);
    110     status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr);
    111     // For HAL_PIXEL_FORMAT_YCbCr_420_888
    112     status_t lockYCbCr(uint32_t inUsage, android_ycbcr *ycbcr);
    113     status_t lockYCbCr(uint32_t inUsage, const Rect& rect,
    114             android_ycbcr *ycbcr);
    115     status_t unlock();
    116     status_t lockAsync(uint32_t inUsage, void** vaddr, int fenceFd);
    117     status_t lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr,
    118             int fenceFd);
    119     status_t lockAsyncYCbCr(uint32_t inUsage, android_ycbcr *ycbcr,
    120             int fenceFd);
    121     status_t lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
    122             android_ycbcr *ycbcr, int fenceFd);
    123     status_t unlockAsync(int *fenceFd);
    124 
    125     ANativeWindowBuffer* getNativeBuffer() const;
    126 
    127     // for debugging
    128     static void dumpAllocationsToSystemLog();
    129 
    130     // Flattenable protocol
    131     size_t getFlattenedSize() const;
    132     size_t getFdCount() const;
    133     status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
    134     status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
    135 
    136 private:
    137     ~GraphicBuffer();
    138 
    139     enum {
    140         ownNone   = 0,
    141         ownHandle = 1,
    142         ownData   = 2,
    143     };
    144 
    145     inline const GraphicBufferMapper& getBufferMapper() const {
    146         return mBufferMapper;
    147     }
    148     inline GraphicBufferMapper& getBufferMapper() {
    149         return mBufferMapper;
    150     }
    151     uint8_t mOwner;
    152 
    153 private:
    154     friend class Surface;
    155     friend class BpSurface;
    156     friend class BnSurface;
    157     friend class LightRefBase<GraphicBuffer>;
    158     GraphicBuffer(const GraphicBuffer& rhs);
    159     GraphicBuffer& operator = (const GraphicBuffer& rhs);
    160     const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
    161 
    162     status_t initSize(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
    163             uint32_t inUsage, std::string requestorName);
    164 
    165     void free_handle();
    166 
    167     GraphicBufferMapper& mBufferMapper;
    168     ssize_t mInitCheck;
    169 
    170     // If we're wrapping another buffer then this reference will make sure it
    171     // doesn't get freed.
    172     sp<ANativeWindowBuffer> mWrappedBuffer;
    173 
    174     uint64_t mId;
    175 
    176     // Stores the generation number of this buffer. If this number does not
    177     // match the BufferQueue's internal generation number (set through
    178     // IGBP::setGenerationNumber), attempts to attach the buffer will fail.
    179     uint32_t mGenerationNumber;
    180 };
    181 
    182 }; // namespace android
    183 
    184 #endif // ANDROID_GRAPHIC_BUFFER_H
    185