blob: bf9918eecfc022227fddd8259056c5fcddc4413a [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
28#define ANDROID_GRAPHICS_CPUCONSUMER_JNI_ID "mCpuConsumer"
29
30namespace android {
31
32/**
33 * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU
34 * access to the underlying gralloc buffers provided by BufferQueue. Multiple
35 * buffers may be acquired by it at once, to be used concurrently by the
36 * CpuConsumer owner. Sets gralloc usage flags to be software-read-only.
37 * This queue is synchronous by default.
38 */
39
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080040class CpuConsumer : public ConsumerBase
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070041{
42 public:
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -070043 typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070044
45 struct LockedBuffer {
46 uint8_t *data;
47 uint32_t width;
48 uint32_t height;
49 PixelFormat format;
50 uint32_t stride;
51 Rect crop;
52 uint32_t transform;
53 uint32_t scalingMode;
54 int64_t timestamp;
55 uint64_t frameNumber;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070056 // Values below are only valid when using
57 // HAL_PIXEL_FORMAT_YCbCr_420_888, in which case LockedBuffer::data
58 // contains the Y channel, and stride is the Y channel stride. For other
59 // formats, these will all be 0.
60 uint8_t *dataCb;
61 uint8_t *dataCr;
62 uint32_t chromaStride;
63 uint32_t chromaStep;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070064 };
65
66 // Create a new CPU consumer. The maxLockedBuffers parameter specifies
67 // how many buffers can be locked for user access at the same time.
Eino-Ville Talvalaeb0d1292013-02-28 11:01:32 -080068 CpuConsumer(uint32_t maxLockedBuffers, bool synchronousMode = true);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070069
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070070 virtual ~CpuConsumer();
71
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070072 // set the name of the CpuConsumer that will be used to identify it in
73 // log messages.
74 void setName(const String8& name);
75
76 // Gets the next graphics buffer from the producer and locks it for CPU use,
77 // filling out the passed-in locked buffer structure with the native pointer
78 // and metadata. Returns BAD_VALUE if no new buffer is available, and
79 // INVALID_OPERATION if the maximum number of buffers is already locked.
80 //
81 // Only a fixed number of buffers can be locked at a time, determined by the
82 // construction-time maxLockedBuffers parameter. If INVALID_OPERATION is
83 // returned by lockNextBuffer, then old buffers must be returned to the queue
84 // by calling unlockBuffer before more buffers can be acquired.
85 status_t lockNextBuffer(LockedBuffer *nativeBuffer);
86
87 // Returns a locked buffer to the queue, allowing it to be reused. Since
88 // only a fixed number of buffers may be locked at a time, old buffers must
89 // be released by calling unlockBuffer to ensure new buffers can be acquired by
90 // lockNextBuffer.
91 status_t unlockBuffer(const LockedBuffer &nativeBuffer);
92
Andy McFadden2adaf042012-12-18 09:49:45 -080093 sp<IGraphicBufferProducer> getProducerInterface() const { return getBufferQueue(); }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070094
95 private:
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070096 // Maximum number of buffers that can be locked at a time
97 uint32_t mMaxLockedBuffers;
98
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080099 status_t releaseAcquiredBufferLocked(int lockedIdx);
100
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700101 virtual void freeBufferLocked(int slotIndex);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700102
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800103 // Tracking for buffers acquired by the user
104 struct AcquiredBuffer {
105 // Need to track the original mSlot index and the buffer itself because
106 // the mSlot entry may be freed/reused before the acquired buffer is
107 // released.
108 int mSlot;
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800109 sp<GraphicBuffer> mGraphicBuffer;
110 void *mBufferPointer;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800111
112 AcquiredBuffer() :
113 mSlot(BufferQueue::INVALID_BUFFER_SLOT),
114 mBufferPointer(NULL) {
115 }
116 };
117 Vector<AcquiredBuffer> mAcquiredBuffers;
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800118
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700119 // Count of currently locked buffers
120 uint32_t mCurrentLockedBuffers;
121
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700122};
123
124} // namespace android
125
126#endif // ANDROID_GUI_CPUCONSUMER_H