blob: 7630faa7625772400e2c142ffd2d43dc34f3dc58 [file] [log] [blame]
Mathias Agopian3330b202009-10-05 17:07:12 -07001/*
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
Mathias Agopian5f2165f2012-02-24 18:25:41 -080023#include <ui/ANativeObjectBase.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070024#include <ui/PixelFormat.h>
25#include <ui/Rect.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080026#include <utils/Flattenable.h>
Mathias Agopiane0417162013-03-06 18:50:52 -080027#include <utils/RefBase.h>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080028
Mathias Agopian3330b202009-10-05 17:07:12 -070029
Iliyan Malchev697526b2011-05-01 11:33:26 -070030struct ANativeWindowBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -070031
32namespace android {
33
34class GraphicBufferMapper;
Mathias Agopian3330b202009-10-05 17:07:12 -070035
36// ===========================================================================
37// GraphicBuffer
38// ===========================================================================
39
40class GraphicBuffer
Mathias Agopiane0417162013-03-06 18:50:52 -080041 : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >,
Mathias Agopiane1424282013-07-29 21:24:40 -070042 public Flattenable<GraphicBuffer>
Mathias Agopian3330b202009-10-05 17:07:12 -070043{
Mathias Agopiane1424282013-07-29 21:24:40 -070044 friend class Flattenable<GraphicBuffer>;
Mathias Agopian3330b202009-10-05 17:07:12 -070045public:
46
47 enum {
48 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
49 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
50 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
51 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
52
53 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
54 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
55 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
56 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
Glenn Kasten16f04532011-01-19 15:27:27 -080057
Mathias Agopian3330b202009-10-05 17:07:12 -070058 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
Glenn Kasten16f04532011-01-19 15:27:27 -080059
60 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED,
61
Mathias Agopian3330b202009-10-05 17:07:12 -070062 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
63 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
64 USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
Jamie Gennis3599bf22011-08-10 11:48:07 -070065 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER,
Jamie Gennisb7d87c42011-11-21 16:51:47 -080066 USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER,
Riley Andrews03414a12014-07-01 14:22:59 -070067 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK,
68
69 USAGE_CURSOR = GRALLOC_USAGE_CURSOR,
Mathias Agopian3330b202009-10-05 17:07:12 -070070 };
71
72 GraphicBuffer();
73
74 // creates w * h buffer
Mathias Agopian54ba51d2009-10-26 20:12:37 -070075 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
76
77 // create a buffer from an existing handle
78 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
79 uint32_t stride, native_handle_t* handle, bool keepOwnership);
Mathias Agopian3330b202009-10-05 17:07:12 -070080
Iliyan Malchev697526b2011-05-01 11:33:26 -070081 // create a buffer from an existing ANativeWindowBuffer
82 GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
Jamie Gennis309d3bb2010-10-07 13:46:55 -070083
Mathias Agopian3330b202009-10-05 17:07:12 -070084 // return status
85 status_t initCheck() const;
86
87 uint32_t getWidth() const { return width; }
88 uint32_t getHeight() const { return height; }
89 uint32_t getStride() const { return stride; }
90 uint32_t getUsage() const { return usage; }
91 PixelFormat getPixelFormat() const { return format; }
92 Rect getBounds() const { return Rect(width, height); }
Dan Stozab1363d32014-03-28 15:10:52 -070093 uint64_t getId() const { return mId; }
94
Mathias Agopian3330b202009-10-05 17:07:12 -070095 status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage);
96
97 status_t lock(uint32_t usage, void** vaddr);
98 status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070099 // For HAL_PIXEL_FORMAT_YCbCr_420_888
100 status_t lockYCbCr(uint32_t usage, android_ycbcr *ycbcr);
101 status_t lockYCbCr(uint32_t usage, const Rect& rect, android_ycbcr *ycbcr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700102 status_t unlock();
Francis Hart8f396012014-04-01 15:30:53 +0300103 status_t lockAsync(uint32_t usage, void** vaddr, int fenceFd);
104 status_t lockAsync(uint32_t usage, const Rect& rect, void** vaddr, int fenceFd);
105 status_t lockAsyncYCbCr(uint32_t usage, android_ycbcr *ycbcr, int fenceFd);
106 status_t lockAsyncYCbCr(uint32_t usage, const Rect& rect, android_ycbcr *ycbcr, int fenceFd);
107 status_t unlockAsync(int *fenceFd);
Mathias Agopian678bdd62010-12-03 17:33:09 -0800108
Iliyan Malchev697526b2011-05-01 11:33:26 -0700109 ANativeWindowBuffer* getNativeBuffer() const;
Mathias Agopian3330b202009-10-05 17:07:12 -0700110
Mathias Agopian678bdd62010-12-03 17:33:09 -0800111 // for debugging
112 static void dumpAllocationsToSystemLog();
113
Mathias Agopian87f9b872013-07-31 19:18:22 -0700114 // Flattenable protocol
115 size_t getFlattenedSize() const;
116 size_t getFdCount() const;
117 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
118 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
119
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700120private:
Mathias Agopiane1424282013-07-29 21:24:40 -0700121 ~GraphicBuffer();
Mathias Agopian3330b202009-10-05 17:07:12 -0700122
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700123 enum {
124 ownNone = 0,
125 ownHandle = 1,
126 ownData = 2,
127 };
128
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700129 inline const GraphicBufferMapper& getBufferMapper() const {
130 return mBufferMapper;
131 }
132 inline GraphicBufferMapper& getBufferMapper() {
133 return mBufferMapper;
134 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700135 uint8_t mOwner;
Mathias Agopian3330b202009-10-05 17:07:12 -0700136
137private:
138 friend class Surface;
139 friend class BpSurface;
140 friend class BnSurface;
141 friend class LightRefBase<GraphicBuffer>;
142 GraphicBuffer(const GraphicBuffer& rhs);
143 GraphicBuffer& operator = (const GraphicBuffer& rhs);
144 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
145
146 status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
147 uint32_t usage);
148
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800149 void free_handle();
150
Mathias Agopian3330b202009-10-05 17:07:12 -0700151 GraphicBufferMapper& mBufferMapper;
152 ssize_t mInitCheck;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700153
154 // If we're wrapping another buffer then this reference will make sure it
155 // doesn't get freed.
Iliyan Malchev697526b2011-05-01 11:33:26 -0700156 sp<ANativeWindowBuffer> mWrappedBuffer;
Dan Stozab1363d32014-03-28 15:10:52 -0700157
158 uint64_t mId;
Mathias Agopian3330b202009-10-05 17:07:12 -0700159};
160
161}; // namespace android
162
163#endif // ANDROID_GRAPHIC_BUFFER_H