blob: c99ab297dfca053d17d069ee96ac22071f84f994 [file] [log] [blame]
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -07001/*
2 * Copyright (C) 2012 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_GUI_CPUCONSUMER_H
18#define ANDROID_GUI_CPUCONSUMER_H
19
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -070020#include <gui/ConsumerBase.h>
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070021
22#include <ui/GraphicBuffer.h>
23
24#include <utils/String8.h>
25#include <utils/Vector.h>
26#include <utils/threads.h>
27
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070028
29namespace android {
30
Mathias Agopian8f938a52013-07-12 22:06:26 -070031class BufferQueue;
32
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070033/**
34 * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU
35 * access to the underlying gralloc buffers provided by BufferQueue. Multiple
36 * buffers may be acquired by it at once, to be used concurrently by the
37 * CpuConsumer owner. Sets gralloc usage flags to be software-read-only.
38 * This queue is synchronous by default.
39 */
40
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080041class CpuConsumer : public ConsumerBase
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070042{
43 public:
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -070044 typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070045
46 struct LockedBuffer {
47 uint8_t *data;
48 uint32_t width;
49 uint32_t height;
50 PixelFormat format;
51 uint32_t stride;
52 Rect crop;
53 uint32_t transform;
54 uint32_t scalingMode;
55 int64_t timestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080056 android_dataspace dataSpace;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070057 uint64_t frameNumber;
Lajos Molnar6a26be72015-01-22 16:37:37 -080058 // this is the same as format, except for formats that are compatible with
59 // a flexible format (e.g. HAL_PIXEL_FORMAT_YCbCr_420_888). In the latter
60 // case this contains that flexible format
61 PixelFormat flexFormat;
62 // Values below are only valid when using HAL_PIXEL_FORMAT_YCbCr_420_888
63 // or compatible format, in which case LockedBuffer::data
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070064 // contains the Y channel, and stride is the Y channel stride. For other
65 // formats, these will all be 0.
66 uint8_t *dataCb;
67 uint8_t *dataCr;
68 uint32_t chromaStride;
69 uint32_t chromaStep;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070070 };
71
72 // Create a new CPU consumer. The maxLockedBuffers parameter specifies
73 // how many buffers can be locked for user access at the same time.
Mathias Agopiandb89edc2013-08-02 01:40:18 -070074 CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
Dan Stoza3be1c6b2014-11-18 10:24:03 -080075 size_t maxLockedBuffers, bool controlledByApp = false);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070076
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070077 virtual ~CpuConsumer();
78
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070079 // set the name of the CpuConsumer that will be used to identify it in
80 // log messages.
81 void setName(const String8& name);
82
Zhijun Heb4b63702013-06-06 11:59:21 -070083 // setDefaultBufferSize is used to set the size of buffers returned by
84 // requestBuffers when a width and height of zero is requested.
85 // A call to setDefaultBufferSize() may trigger requestBuffers() to
86 // be called from the client. Default size is 1x1.
87 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
88
89 // setDefaultBufferFormat allows CpuConsumer's BufferQueue to create buffers
Dan Stoza3be1c6b2014-11-18 10:24:03 -080090 // of a defaultFormat if no format is specified by producer.
91 // The initial default is PIXEL_FORMAT_RGBA_8888.
92 status_t setDefaultBufferFormat(PixelFormat defaultFormat);
Zhijun Heb4b63702013-06-06 11:59:21 -070093
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080094 // setDefaultBufferDataSpace allows the BufferQueue to create
95 // GraphicBuffers of a defaultDataSpace if no data space is specified
96 // in queueBuffer.
97 // The initial default is HAL_DATASPACE_UNKNOWN
98 status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace);
99
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700100 // Gets the next graphics buffer from the producer and locks it for CPU use,
101 // filling out the passed-in locked buffer structure with the native pointer
102 // and metadata. Returns BAD_VALUE if no new buffer is available, and
Igor Murashkina5b75132013-08-14 18:49:12 -0700103 // NOT_ENOUGH_DATA if the maximum number of buffers is already locked.
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700104 //
105 // Only a fixed number of buffers can be locked at a time, determined by the
106 // construction-time maxLockedBuffers parameter. If INVALID_OPERATION is
107 // returned by lockNextBuffer, then old buffers must be returned to the queue
108 // by calling unlockBuffer before more buffers can be acquired.
109 status_t lockNextBuffer(LockedBuffer *nativeBuffer);
110
111 // Returns a locked buffer to the queue, allowing it to be reused. Since
112 // only a fixed number of buffers may be locked at a time, old buffers must
113 // be released by calling unlockBuffer to ensure new buffers can be acquired by
114 // lockNextBuffer.
115 status_t unlockBuffer(const LockedBuffer &nativeBuffer);
116
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700117 private:
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700118 // Maximum number of buffers that can be locked at a time
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800119 size_t mMaxLockedBuffers;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700120
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800121 status_t releaseAcquiredBufferLocked(size_t lockedIdx);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800122
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700123 virtual void freeBufferLocked(int slotIndex);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700124
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800125 // Tracking for buffers acquired by the user
126 struct AcquiredBuffer {
127 // Need to track the original mSlot index and the buffer itself because
128 // the mSlot entry may be freed/reused before the acquired buffer is
129 // released.
130 int mSlot;
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800131 sp<GraphicBuffer> mGraphicBuffer;
132 void *mBufferPointer;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800133
134 AcquiredBuffer() :
135 mSlot(BufferQueue::INVALID_BUFFER_SLOT),
136 mBufferPointer(NULL) {
137 }
138 };
139 Vector<AcquiredBuffer> mAcquiredBuffers;
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800140
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700141 // Count of currently locked buffers
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800142 size_t mCurrentLockedBuffers;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700143
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700144};
145
146} // namespace android
147
148#endif // ANDROID_GUI_CPUCONSUMER_H