1 /*
2 * Copyright (C) 2010 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 #undef LOG_TAG
18 #define LOG_TAG "BufferLayerConsumer"
19 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20 //#define LOG_NDEBUG 0
21
22 #include "BufferLayerConsumer.h"
23 #include "Layer.h"
24 #include "Scheduler/DispSync.h"
25
26 #include <inttypes.h>
27
28 #include <cutils/compiler.h>
29
30 #include <hardware/hardware.h>
31
32 #include <math/mat4.h>
33
34 #include <gui/BufferItem.h>
35 #include <gui/GLConsumer.h>
36 #include <gui/ISurfaceComposer.h>
37 #include <gui/SurfaceComposerClient.h>
38 #include <private/gui/ComposerService.h>
39 #include <renderengine/Image.h>
40 #include <renderengine/RenderEngine.h>
41 #include <utils/Log.h>
42 #include <utils/String8.h>
43 #include <utils/Trace.h>
44
45 namespace android {
46
47 // Macros for including the BufferLayerConsumer name in log messages
48 #define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
49 #define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
50 //#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
51 #define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
52 #define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
53
54 static const mat4 mtxIdentity;
55
BufferLayerConsumer(const sp<IGraphicBufferConsumer> & bq,renderengine::RenderEngine & engine,uint32_t tex,Layer * layer)56 BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq,
57 renderengine::RenderEngine& engine, uint32_t tex,
58 Layer* layer)
59 : ConsumerBase(bq, false),
60 mCurrentCrop(Rect::EMPTY_RECT),
61 mCurrentTransform(0),
62 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
63 mCurrentFence(Fence::NO_FENCE),
64 mCurrentTimestamp(0),
65 mCurrentDataSpace(ui::Dataspace::UNKNOWN),
66 mCurrentFrameNumber(0),
67 mCurrentTransformToDisplayInverse(false),
68 mCurrentSurfaceDamage(),
69 mCurrentApi(0),
70 mDefaultWidth(1),
71 mDefaultHeight(1),
72 mFilteringEnabled(true),
73 mRE(engine),
74 mTexName(tex),
75 mLayer(layer),
76 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) {
77 BLC_LOGV("BufferLayerConsumer");
78
79 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
80
81 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
82 }
83
setDefaultBufferSize(uint32_t w,uint32_t h)84 status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) {
85 Mutex::Autolock lock(mMutex);
86 if (mAbandoned) {
87 BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!");
88 return NO_INIT;
89 }
90 mDefaultWidth = w;
91 mDefaultHeight = h;
92 return mConsumer->setDefaultBufferSize(w, h);
93 }
94
setContentsChangedListener(const wp<ContentsChangedListener> & listener)95 void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) {
96 setFrameAvailableListener(listener);
97 Mutex::Autolock lock(mMutex);
98 mContentsChangedListener = listener;
99 }
100
updateTexImage(BufferRejecter * rejecter,nsecs_t expectedPresentTime,bool * autoRefresh,bool * queuedBuffer,uint64_t maxFrameNumber)101 status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime,
102 bool* autoRefresh, bool* queuedBuffer,
103 uint64_t maxFrameNumber) {
104 ATRACE_CALL();
105 BLC_LOGV("updateTexImage");
106 Mutex::Autolock lock(mMutex);
107
108 if (mAbandoned) {
109 BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!");
110 return NO_INIT;
111 }
112
113 BufferItem item;
114
115 // Acquire the next buffer.
116 // In asynchronous mode the list is guaranteed to be one buffer
117 // deep, while in synchronous mode we use the oldest buffer.
118 status_t err = acquireBufferLocked(&item, expectedPresentTime, maxFrameNumber);
119 if (err != NO_ERROR) {
120 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
121 err = NO_ERROR;
122 } else if (err == BufferQueue::PRESENT_LATER) {
123 // return the error, without logging
124 } else {
125 BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
126 }
127 return err;
128 }
129
130 if (autoRefresh) {
131 *autoRefresh = item.mAutoRefresh;
132 }
133
134 if (queuedBuffer) {
135 *queuedBuffer = item.mQueuedBuffer;
136 }
137
138 // We call the rejecter here, in case the caller has a reason to
139 // not accept this buffer. This is used by SurfaceFlinger to
140 // reject buffers which have the wrong size
141 int slot = item.mSlot;
142 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
143 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
144 return BUFFER_REJECTED;
145 }
146
147 // Release the previous buffer.
148 err = updateAndReleaseLocked(item, &mPendingRelease);
149 if (err != NO_ERROR) {
150 return err;
151 }
152
153 if (!mRE.useNativeFenceSync()) {
154 // Bind the new buffer to the GL texture.
155 //
156 // Older devices require the "implicit" synchronization provided
157 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
158 // devices will either call this in Layer::onDraw, or (if it's not
159 // a GL-composited layer) not at all.
160 err = bindTextureImageLocked();
161 }
162
163 return err;
164 }
165
bindTextureImage()166 status_t BufferLayerConsumer::bindTextureImage() {
167 Mutex::Autolock lock(mMutex);
168 return bindTextureImageLocked();
169 }
170
setReleaseFence(const sp<Fence> & fence)171 void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
172 if (!fence->isValid()) {
173 return;
174 }
175
176 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
177 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
178 return;
179 }
180
181 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer
182 : mCurrentTextureBuffer->graphicBuffer();
183 auto err = addReleaseFence(slot, buffer, fence);
184 if (err != OK) {
185 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
186 }
187 }
188
releasePendingBuffer()189 bool BufferLayerConsumer::releasePendingBuffer() {
190 if (!mPendingRelease.isPending) {
191 BLC_LOGV("Pending buffer already released");
192 return false;
193 }
194 BLC_LOGV("Releasing pending buffer");
195 Mutex::Autolock lock(mMutex);
196 status_t result =
197 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
198 if (result < NO_ERROR) {
199 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
200 }
201 mPendingRelease = PendingRelease();
202 return true;
203 }
204
getPrevFinalReleaseFence() const205 sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const {
206 Mutex::Autolock lock(mMutex);
207 return ConsumerBase::mPrevFinalReleaseFence;
208 }
209
acquireBufferLocked(BufferItem * item,nsecs_t presentWhen,uint64_t maxFrameNumber)210 status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
211 uint64_t maxFrameNumber) {
212 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
213 if (err != NO_ERROR) {
214 return err;
215 }
216
217 // If item->mGraphicBuffer is not null, this buffer has not been acquired
218 // before, so we need to clean up old references.
219 if (item->mGraphicBuffer != nullptr) {
220 std::lock_guard<std::mutex> lock(mImagesMutex);
221 if (mImages[item->mSlot] == nullptr || mImages[item->mSlot]->graphicBuffer() == nullptr ||
222 mImages[item->mSlot]->graphicBuffer()->getId() != item->mGraphicBuffer->getId()) {
223 mImages[item->mSlot] = std::make_shared<Image>(item->mGraphicBuffer, mRE);
224 }
225 }
226
227 return NO_ERROR;
228 }
229
updateAndReleaseLocked(const BufferItem & item,PendingRelease * pendingRelease)230 status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
231 PendingRelease* pendingRelease) {
232 status_t err = NO_ERROR;
233
234 int slot = item.mSlot;
235
236 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
237 (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr)
238 ? mCurrentTextureBuffer->graphicBuffer()->handle
239 : 0,
240 slot, mSlots[slot].mGraphicBuffer->handle);
241
242 // Hang onto the pointer so that it isn't freed in the call to
243 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
244 // the same.
245
246 std::shared_ptr<Image> nextTextureBuffer;
247 {
248 std::lock_guard<std::mutex> lock(mImagesMutex);
249 nextTextureBuffer = mImages[slot];
250 }
251
252 // release old buffer
253 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
254 if (pendingRelease == nullptr) {
255 status_t status =
256 releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer->graphicBuffer());
257 if (status < NO_ERROR) {
258 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
259 status);
260 err = status;
261 // keep going, with error raised [?]
262 }
263 } else {
264 pendingRelease->currentTexture = mCurrentTexture;
265 pendingRelease->graphicBuffer = mCurrentTextureBuffer->graphicBuffer();
266 pendingRelease->isPending = true;
267 }
268 }
269
270 // Update the BufferLayerConsumer state.
271 mCurrentTexture = slot;
272 mCurrentTextureBuffer = nextTextureBuffer;
273 mCurrentCrop = item.mCrop;
274 mCurrentTransform = item.mTransform;
275 mCurrentScalingMode = item.mScalingMode;
276 mCurrentTimestamp = item.mTimestamp;
277 mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace);
278 mCurrentHdrMetadata = item.mHdrMetadata;
279 mCurrentFence = item.mFence;
280 mCurrentFenceTime = item.mFenceTime;
281 mCurrentFrameNumber = item.mFrameNumber;
282 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
283 mCurrentSurfaceDamage = item.mSurfaceDamage;
284 mCurrentApi = item.mApi;
285
286 computeCurrentTransformMatrixLocked();
287
288 return err;
289 }
290
bindTextureImageLocked()291 status_t BufferLayerConsumer::bindTextureImageLocked() {
292 ATRACE_CALL();
293
294 if (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr) {
295 return mRE.bindExternalTextureBuffer(mTexName, mCurrentTextureBuffer->graphicBuffer(),
296 mCurrentFence);
297 }
298
299 return NO_INIT;
300 }
301
getTransformMatrix(float mtx[16])302 void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
303 Mutex::Autolock lock(mMutex);
304 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
305 }
306
setFilteringEnabled(bool enabled)307 void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
308 Mutex::Autolock lock(mMutex);
309 if (mAbandoned) {
310 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
311 return;
312 }
313 bool needsRecompute = mFilteringEnabled != enabled;
314 mFilteringEnabled = enabled;
315
316 if (needsRecompute && mCurrentTextureBuffer == nullptr) {
317 BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr");
318 }
319
320 if (needsRecompute && mCurrentTextureBuffer != nullptr) {
321 computeCurrentTransformMatrixLocked();
322 }
323 }
324
computeCurrentTransformMatrixLocked()325 void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
326 BLC_LOGV("computeCurrentTransformMatrixLocked");
327 if (mCurrentTextureBuffer == nullptr || mCurrentTextureBuffer->graphicBuffer() == nullptr) {
328 BLC_LOGD("computeCurrentTransformMatrixLocked: "
329 "mCurrentTextureBuffer is nullptr");
330 }
331 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix,
332 mCurrentTextureBuffer == nullptr
333 ? nullptr
334 : mCurrentTextureBuffer->graphicBuffer(),
335 getCurrentCropLocked(), mCurrentTransform,
336 mFilteringEnabled);
337 }
338
getTimestamp()339 nsecs_t BufferLayerConsumer::getTimestamp() {
340 BLC_LOGV("getTimestamp");
341 Mutex::Autolock lock(mMutex);
342 return mCurrentTimestamp;
343 }
344
getCurrentDataSpace()345 ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() {
346 BLC_LOGV("getCurrentDataSpace");
347 Mutex::Autolock lock(mMutex);
348 return mCurrentDataSpace;
349 }
350
getCurrentHdrMetadata() const351 const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
352 BLC_LOGV("getCurrentHdrMetadata");
353 Mutex::Autolock lock(mMutex);
354 return mCurrentHdrMetadata;
355 }
356
getFrameNumber()357 uint64_t BufferLayerConsumer::getFrameNumber() {
358 BLC_LOGV("getFrameNumber");
359 Mutex::Autolock lock(mMutex);
360 return mCurrentFrameNumber;
361 }
362
getTransformToDisplayInverse() const363 bool BufferLayerConsumer::getTransformToDisplayInverse() const {
364 Mutex::Autolock lock(mMutex);
365 return mCurrentTransformToDisplayInverse;
366 }
367
getSurfaceDamage() const368 const Region& BufferLayerConsumer::getSurfaceDamage() const {
369 return mCurrentSurfaceDamage;
370 }
371
getCurrentApi() const372 int BufferLayerConsumer::getCurrentApi() const {
373 Mutex::Autolock lock(mMutex);
374 return mCurrentApi;
375 }
376
getCurrentBuffer(int * outSlot,sp<Fence> * outFence) const377 sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot, sp<Fence>* outFence) const {
378 Mutex::Autolock lock(mMutex);
379
380 if (outSlot != nullptr) {
381 *outSlot = mCurrentTexture;
382 }
383
384 if (outFence != nullptr) {
385 *outFence = mCurrentFence;
386 }
387
388 return mCurrentTextureBuffer == nullptr ? nullptr : mCurrentTextureBuffer->graphicBuffer();
389 }
390
getCurrentCrop() const391 Rect BufferLayerConsumer::getCurrentCrop() const {
392 Mutex::Autolock lock(mMutex);
393 return getCurrentCropLocked();
394 }
395
getCurrentCropLocked() const396 Rect BufferLayerConsumer::getCurrentCropLocked() const {
397 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
398 ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
399 : mCurrentCrop;
400 }
401
getCurrentTransform() const402 uint32_t BufferLayerConsumer::getCurrentTransform() const {
403 Mutex::Autolock lock(mMutex);
404 return mCurrentTransform;
405 }
406
getCurrentScalingMode() const407 uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
408 Mutex::Autolock lock(mMutex);
409 return mCurrentScalingMode;
410 }
411
getCurrentFence() const412 sp<Fence> BufferLayerConsumer::getCurrentFence() const {
413 Mutex::Autolock lock(mMutex);
414 return mCurrentFence;
415 }
416
getCurrentFenceTime() const417 std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
418 Mutex::Autolock lock(mMutex);
419 return mCurrentFenceTime;
420 }
421
doFenceWaitLocked() const422 status_t BufferLayerConsumer::doFenceWaitLocked() const {
423 if (mCurrentFence->isValid()) {
424 if (mRE.useWaitSync()) {
425 base::unique_fd fenceFd(mCurrentFence->dup());
426 if (fenceFd == -1) {
427 BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno);
428 return -errno;
429 }
430 if (!mRE.waitFence(std::move(fenceFd))) {
431 BLC_LOGE("doFenceWait: failed to wait on fence fd");
432 return UNKNOWN_ERROR;
433 }
434 } else {
435 status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked");
436 if (err != NO_ERROR) {
437 BLC_LOGE("doFenceWait: error waiting for fence: %d", err);
438 return err;
439 }
440 }
441 }
442
443 return NO_ERROR;
444 }
445
freeBufferLocked(int slotIndex)446 void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
447 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
448 std::lock_guard<std::mutex> lock(mImagesMutex);
449 if (slotIndex == mCurrentTexture) {
450 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
451 }
452 mImages[slotIndex] = nullptr;
453 ConsumerBase::freeBufferLocked(slotIndex);
454 }
455
onDisconnect()456 void BufferLayerConsumer::onDisconnect() {
457 sp<Layer> l = mLayer.promote();
458 if (l.get()) {
459 l->onDisconnect();
460 }
461 }
462
onSidebandStreamChanged()463 void BufferLayerConsumer::onSidebandStreamChanged() {
464 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
465 {
466 Mutex::Autolock lock(mFrameAvailableMutex);
467 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
468 }
469 sp<ContentsChangedListener> listener;
470 { // scope for the lock
471 Mutex::Autolock lock(mMutex);
472 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
473 listener = mContentsChangedListener.promote();
474 }
475
476 if (listener != nullptr) {
477 listener->onSidebandStreamChanged();
478 }
479 }
480
onBufferAvailable(const BufferItem & item)481 void BufferLayerConsumer::onBufferAvailable(const BufferItem& item) {
482 if (item.mGraphicBuffer != nullptr && item.mSlot != BufferQueue::INVALID_BUFFER_SLOT) {
483 std::lock_guard<std::mutex> lock(mImagesMutex);
484 const std::shared_ptr<Image>& oldImage = mImages[item.mSlot];
485 if (oldImage == nullptr || oldImage->graphicBuffer() == nullptr ||
486 oldImage->graphicBuffer()->getId() != item.mGraphicBuffer->getId()) {
487 mImages[item.mSlot] = std::make_shared<Image>(item.mGraphicBuffer, mRE);
488 mRE.cacheExternalTextureBuffer(item.mGraphicBuffer);
489 }
490 }
491 }
492
addAndGetFrameTimestamps(const NewFrameEventsEntry * newTimestamps,FrameEventHistoryDelta * outDelta)493 void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
494 FrameEventHistoryDelta* outDelta) {
495 sp<Layer> l = mLayer.promote();
496 if (l.get()) {
497 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
498 }
499 }
500
abandonLocked()501 void BufferLayerConsumer::abandonLocked() {
502 BLC_LOGV("abandonLocked");
503 mCurrentTextureBuffer = nullptr;
504 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
505 std::lock_guard<std::mutex> lock(mImagesMutex);
506 mImages[i] = nullptr;
507 }
508 ConsumerBase::abandonLocked();
509 }
510
setConsumerUsageBits(uint64_t usage)511 status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
512 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
513 }
514
dumpLocked(String8 & result,const char * prefix) const515 void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
516 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
517 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
518 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
519 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
520 mCurrentTransform);
521
522 ConsumerBase::dumpLocked(result, prefix);
523 }
524
~Image()525 BufferLayerConsumer::Image::~Image() {
526 if (mGraphicBuffer != nullptr) {
527 ALOGV("Destroying buffer: %" PRId64, mGraphicBuffer->getId());
528 mRE.unbindExternalTextureBuffer(mGraphicBuffer->getId());
529 }
530 }
531 }; // namespace android
532