summaryrefslogtreecommitdiff
path: root/libs/gui/BufferItemConsumer.cpp
blob: 4926ceb619f62c99989aa3d2395127beabbb2950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "BufferItemConsumer"
//#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include <utils/Errors.h>
#include <utils/Log.h>

#include <inttypes.h>

#include <com_android_graphics_libgui_flags.h>
#include <gui/BufferItem.h>
#include <gui/BufferItemConsumer.h>
#include <gui/Surface.h>
#include <ui/BufferQueueDefs.h>
#include <ui/GraphicBuffer.h>

#define BI_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
// #define BI_LOGD(x, ...) ALOGD("[%s] " x, mName.c_str(), ##__VA_ARGS__)
// #define BI_LOGI(x, ...) ALOGI("[%s] " x, mName.c_str(), ##__VA_ARGS__)
// #define BI_LOGW(x, ...) ALOGW("[%s] " x, mName.c_str(), ##__VA_ARGS__)
#define BI_LOGE(x, ...) ALOGE("[%s] " x, mName.c_str(), ##__VA_ARGS__)

namespace android {

std::tuple<sp<BufferItemConsumer>, sp<Surface>> BufferItemConsumer::create(
        uint64_t consumerUsage, int bufferCount, bool controlledByApp,
        bool isConsumerSurfaceFlinger) {
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
    sp<BufferItemConsumer> bufferItemConsumer =
            sp<BufferItemConsumer>::make(consumerUsage, bufferCount, controlledByApp,
                                         isConsumerSurfaceFlinger);
    return {bufferItemConsumer, bufferItemConsumer->getSurface()};
#else
    sp<IGraphicBufferProducer> igbp;
    sp<IGraphicBufferConsumer> igbc;
    BufferQueue::createBufferQueue(&igbp, &igbc, isConsumerSurfaceFlinger);
    sp<BufferItemConsumer> bufferItemConsumer =
            sp<BufferItemConsumer>::make(igbc, consumerUsage, bufferCount, controlledByApp);
    return {bufferItemConsumer, sp<Surface>::make(igbp, controlledByApp)};
#endif
}

sp<BufferItemConsumer> BufferItemConsumer::create(const sp<IGraphicBufferConsumer>& consumer,
                                                  uint64_t consumerUsage, int bufferCount,
                                                  bool controlledByApp) {
    return sp<BufferItemConsumer>::make(consumer, consumerUsage, bufferCount, controlledByApp);
}

#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
BufferItemConsumer::BufferItemConsumer(uint64_t consumerUsage, int bufferCount,
                                       bool controlledByApp, bool isConsumerSurfaceFlinger)
      : ConsumerBase(controlledByApp, isConsumerSurfaceFlinger) {
    initialize(consumerUsage, bufferCount);
}

BufferItemConsumer::BufferItemConsumer(const sp<IGraphicBufferProducer>& producer,
                                       const sp<IGraphicBufferConsumer>& consumer,
                                       uint64_t consumerUsage, int bufferCount,
                                       bool controlledByApp)
      : ConsumerBase(producer, consumer, controlledByApp) {
    initialize(consumerUsage, bufferCount);
}
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

BufferItemConsumer::BufferItemConsumer(
        const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
        int bufferCount, bool controlledByApp) :
    ConsumerBase(consumer, controlledByApp)
{
    initialize(consumerUsage, bufferCount);
}

void BufferItemConsumer::initialize(uint64_t consumerUsage, int bufferCount) {
    status_t err = mConsumer->setConsumerUsageBits(consumerUsage);
    LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set consumer usage bits to %#" PRIx64, consumerUsage);
    if (bufferCount != DEFAULT_MAX_BUFFERS) {
        err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
        LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set max acquired buffer count to %d",
                            bufferCount);
    }
}

BufferItemConsumer::~BufferItemConsumer() {}

void BufferItemConsumer::setBufferFreedListener(
        const wp<BufferFreedListener>& listener) {
    Mutex::Autolock _l(mMutex);
    mBufferFreedListener = listener;
}

status_t BufferItemConsumer::acquireBuffer(BufferItem *item,
        nsecs_t presentWhen, bool waitForFence) {
    status_t err;

    if (!item) return BAD_VALUE;

    Mutex::Autolock _l(mMutex);

    err = acquireBufferLocked(item, presentWhen);
    if (err != OK) {
        if (err != NO_BUFFER_AVAILABLE) {
            BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
        }
        return err;
    }

    if (waitForFence) {
        err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer");
        if (err != OK) {
            BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)",
                    strerror(-err), err);
            return err;
        }
    }

    item->mGraphicBuffer = mSlots[item->mSlot].mGraphicBuffer;

    return OK;
}

status_t BufferItemConsumer::attachBuffer(const sp<GraphicBuffer>& buffer) {
    if (!buffer) {
        BI_LOGE("BufferItemConsumer::attachBuffer no input buffer specified.");
        return BAD_VALUE;
    }

    Mutex::Autolock _l(mMutex);

    int slot = INVALID_BUFFER_SLOT;
    status_t status = mConsumer->attachBuffer(&slot, buffer);
    if (status != OK) {
        BI_LOGE("BufferItemConsumer::attachBuffer unable to attach buffer %d", status);
        return status;
    }

    mSlots[slot] = {
            .mGraphicBuffer = buffer,
            .mFence = nullptr,
            .mFrameNumber = 0,
    };

    return OK;
}

status_t BufferItemConsumer::releaseBuffer(const BufferItem &item,
        const sp<Fence>& releaseFence) {
    Mutex::Autolock _l(mMutex);
    return releaseBufferSlotLocked(item.mSlot, item.mGraphicBuffer, releaseFence);
}

status_t BufferItemConsumer::releaseBuffer(const sp<GraphicBuffer>& buffer,
                                           const sp<Fence>& releaseFence) {
    Mutex::Autolock _l(mMutex);

    if (buffer == nullptr) {
        return BAD_VALUE;
    }

    int slotIndex = getSlotForBufferLocked(buffer);
    if (slotIndex == INVALID_BUFFER_SLOT) {
        return BAD_VALUE;
    }

    return releaseBufferSlotLocked(slotIndex, buffer, releaseFence);
}

status_t BufferItemConsumer::releaseBufferSlotLocked(int slotIndex, const sp<GraphicBuffer>& buffer,
                                                     const sp<Fence>& releaseFence) {
    status_t err;

    err = addReleaseFenceLocked(slotIndex, buffer, releaseFence);
    if (err != OK) {
        BI_LOGE("Failed to addReleaseFenceLocked");
    }

    err = releaseBufferLocked(slotIndex, buffer);
    if (err != OK && err != IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
        BI_LOGE("Failed to release buffer: %s (%d)",
                strerror(-err), err);
    }
    return err;
}

void BufferItemConsumer::freeBufferLocked(int slotIndex) {
    sp<BufferFreedListener> listener = mBufferFreedListener.promote();
    if (listener != nullptr && mSlots[slotIndex].mGraphicBuffer != nullptr) {
        // Fire callback if we have a listener registered and the buffer being freed is valid.
        BI_LOGV("actually calling onBufferFreed");
        listener->onBufferFreed(mSlots[slotIndex].mGraphicBuffer);
    }
    ConsumerBase::freeBufferLocked(slotIndex);
}

} // namespace android