blob: 3ebc95960849e651f43b17e78cfce3b3733a05df [file] [log] [blame]
Chia-I Wuf1405182017-11-27 11:29:21 -08001/*
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
Chia-I Wuda5c7302017-11-27 14:51:06 -080024#include "DispSync.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080025#include "Layer.h"
26
Chia-I Wuf1405182017-11-27 11:29:21 -080027#include <inttypes.h>
28
29#include <EGL/egl.h>
30#include <EGL/eglext.h>
31#include <GLES2/gl2.h>
32#include <GLES2/gl2ext.h>
33#include <cutils/compiler.h>
34
35#include <hardware/hardware.h>
36
37#include <math/mat4.h>
38
39#include <gui/BufferItem.h>
40#include <gui/GLConsumer.h>
41#include <gui/ISurfaceComposer.h>
42#include <gui/SurfaceComposerClient.h>
43
44#include <private/gui/ComposerService.h>
45#include <private/gui/SyncFeatures.h>
46
47#include <utils/Log.h>
48#include <utils/String8.h>
49#include <utils/Trace.h>
50
51extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
52#define CROP_EXT_STR "EGL_ANDROID_image_crop"
53#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
54#define EGL_PROTECTED_CONTENT_EXT 0x32C0
55
56namespace android {
57
58// Macros for including the BufferLayerConsumer name in log messages
59#define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
60#define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
61//#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
62#define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
63#define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
64
Chia-I Wuf1405182017-11-27 11:29:21 -080065static const mat4 mtxIdentity;
66
Chia-I Wuf1405182017-11-27 11:29:21 -080067static bool hasEglAndroidImageCropImpl() {
68 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
69 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
70 size_t cropExtLen = strlen(CROP_EXT_STR);
71 size_t extsLen = strlen(exts);
72 bool equal = !strcmp(CROP_EXT_STR, exts);
73 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen + 1);
74 bool atEnd = (cropExtLen + 1) < extsLen &&
75 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen + 1));
76 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
77 return equal || atStart || atEnd || inMiddle;
78}
79
80static bool hasEglAndroidImageCrop() {
81 // Only compute whether the extension is present once the first time this
82 // function is called.
83 static bool hasIt = hasEglAndroidImageCropImpl();
84 return hasIt;
85}
86
87static bool hasEglProtectedContentImpl() {
88 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
89 const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
90 size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR);
91 size_t extsLen = strlen(exts);
92 bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts);
93 bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen + 1);
94 bool atEnd = (cropExtLen + 1) < extsLen &&
95 !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen + 1));
96 bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " ");
97 return equal || atStart || atEnd || inMiddle;
98}
99
100static bool hasEglProtectedContent() {
101 // Only compute whether the extension is present once the first time this
102 // function is called.
103 static bool hasIt = hasEglProtectedContentImpl();
104 return hasIt;
105}
106
107static bool isEglImageCroppable(const Rect& crop) {
108 return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
109}
110
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800111BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
112 Layer* layer)
Chia-I Wubd854bf2017-11-27 13:41:26 -0800113 : ConsumerBase(bq, false),
Chia-I Wuf1405182017-11-27 11:29:21 -0800114 mCurrentCrop(Rect::EMPTY_RECT),
115 mCurrentTransform(0),
116 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
117 mCurrentFence(Fence::NO_FENCE),
118 mCurrentTimestamp(0),
119 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
120 mCurrentFrameNumber(0),
Chia-I Wu67dcc692017-11-27 14:51:06 -0800121 mCurrentTransformToDisplayInverse(false),
122 mCurrentSurfaceDamage(),
Chia-I Wuf1405182017-11-27 11:29:21 -0800123 mDefaultWidth(1),
124 mDefaultHeight(1),
125 mFilteringEnabled(true),
126 mTexName(tex),
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800127 mLayer(layer),
Chia-I Wuf1405182017-11-27 11:29:21 -0800128 mEglDisplay(EGL_NO_DISPLAY),
129 mEglContext(EGL_NO_CONTEXT),
Chia-I Wuc91077c2017-11-27 13:32:04 -0800130 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800131 BLC_LOGV("BufferLayerConsumer");
132
133 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
134
135 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
136}
137
138status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) {
139 Mutex::Autolock lock(mMutex);
140 if (mAbandoned) {
141 BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!");
142 return NO_INIT;
143 }
144 mDefaultWidth = w;
145 mDefaultHeight = h;
146 return mConsumer->setDefaultBufferSize(w, h);
147}
148
Chia-I Wufd257f82017-11-27 14:51:06 -0800149void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) {
150 setFrameAvailableListener(listener);
151 Mutex::Autolock lock(mMutex);
152 mContentsChangedListener = listener;
153}
154
Chia-I Wuda5c7302017-11-27 14:51:06 -0800155// We need to determine the time when a buffer acquired now will be
156// displayed. This can be calculated:
157// time when previous buffer's actual-present fence was signaled
158// + current display refresh rate * HWC latency
159// + a little extra padding
160//
161// Buffer producers are expected to set their desired presentation time
162// based on choreographer time stamps, which (coming from vsync events)
163// will be slightly later then the actual-present timing. If we get a
164// desired-present time that is unintentionally a hair after the next
165// vsync, we'll hold the frame when we really want to display it. We
166// need to take the offset between actual-present and reported-vsync
167// into account.
168//
169// If the system is configured without a DispSync phase offset for the app,
170// we also want to throw in a bit of padding to avoid edge cases where we
171// just barely miss. We want to do it here, not in every app. A major
172// source of trouble is the app's use of the display's ideal refresh time
173// (via Display.getRefreshRate()), which could be off of the actual refresh
174// by a few percent, with the error multiplied by the number of frames
175// between now and when the buffer should be displayed.
176//
177// If the refresh reported to the app has a phase offset, we shouldn't need
178// to tweak anything here.
179nsecs_t BufferLayerConsumer::computeExpectedPresent(const DispSync& dispSync) {
180 // The HWC doesn't currently have a way to report additional latency.
181 // Assume that whatever we submit now will appear right after the flip.
182 // For a smart panel this might be 1. This is expressed in frames,
183 // rather than time, because we expect to have a constant frame delay
184 // regardless of the refresh rate.
185 const uint32_t hwcLatency = 0;
186
187 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
188 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
189
190 // The DispSync time is already adjusted for the difference between
191 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
192 // we don't need to factor that in here. Pad a little to avoid
193 // weird effects if apps might be requesting times right on the edge.
194 nsecs_t extraPadding = 0;
195 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
196 extraPadding = 1000000; // 1ms (6% of 60Hz)
197 }
198
199 return nextRefresh + extraPadding;
200}
201
202status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync,
203 bool* autoRefresh, bool* queuedBuffer,
204 uint64_t maxFrameNumber) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800205 ATRACE_CALL();
206 BLC_LOGV("updateTexImage");
207 Mutex::Autolock lock(mMutex);
208
209 if (mAbandoned) {
210 BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!");
211 return NO_INIT;
212 }
213
214 // Make sure the EGL state is the same as in previous calls.
215 status_t err = checkAndUpdateEglStateLocked();
216 if (err != NO_ERROR) {
217 return err;
218 }
219
220 BufferItem item;
221
222 // Acquire the next buffer.
223 // In asynchronous mode the list is guaranteed to be one buffer
224 // deep, while in synchronous mode we use the oldest buffer.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800225 err = acquireBufferLocked(&item, computeExpectedPresent(dispSync), maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -0800226 if (err != NO_ERROR) {
227 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800228 err = NO_ERROR;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800229 } else if (err == BufferQueue::PRESENT_LATER) {
230 // return the error, without logging
Chia-I Wuf1405182017-11-27 11:29:21 -0800231 } else {
232 BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
233 }
234 return err;
235 }
236
Chia-I Wuda5c7302017-11-27 14:51:06 -0800237 if (autoRefresh) {
238 *autoRefresh = item.mAutoRefresh;
239 }
240
241 if (queuedBuffer) {
242 *queuedBuffer = item.mQueuedBuffer;
243 }
244
245 // We call the rejecter here, in case the caller has a reason to
246 // not accept this buffer. This is used by SurfaceFlinger to
247 // reject buffers which have the wrong size
248 int slot = item.mSlot;
249 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
250 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
251 return BUFFER_REJECTED;
252 }
253
Chia-I Wuf1405182017-11-27 11:29:21 -0800254 // Release the previous buffer.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800255 err = updateAndReleaseLocked(item, &mPendingRelease);
Chia-I Wuf1405182017-11-27 11:29:21 -0800256 if (err != NO_ERROR) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800257 return err;
258 }
259
Chia-I Wuda5c7302017-11-27 14:51:06 -0800260 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
261 // Bind the new buffer to the GL texture.
262 //
263 // Older devices require the "implicit" synchronization provided
264 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
265 // devices will either call this in Layer::onDraw, or (if it's not
266 // a GL-composited layer) not at all.
267 err = bindTextureImageLocked();
268 }
269
270 return err;
271}
272
273void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
274 if (!fence->isValid()) {
275 return;
276 }
277
278 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
279 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
280 return;
281 }
282
283 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer
284 : mCurrentTextureImage->graphicBuffer();
285 auto err = addReleaseFence(slot, buffer, fence);
286 if (err != OK) {
287 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
288 }
289}
290
291bool BufferLayerConsumer::releasePendingBuffer() {
292 if (!mPendingRelease.isPending) {
293 BLC_LOGV("Pending buffer already released");
294 return false;
295 }
296 BLC_LOGV("Releasing pending buffer");
297 Mutex::Autolock lock(mMutex);
298 status_t result =
299 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
300 if (result < NO_ERROR) {
301 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
302 }
303 mPendingRelease = PendingRelease();
304 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800305}
306
Chia-I Wuf1405182017-11-27 11:29:21 -0800307status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
308 uint64_t maxFrameNumber) {
309 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
310 if (err != NO_ERROR) {
311 return err;
312 }
313
314 // If item->mGraphicBuffer is not null, this buffer has not been acquired
315 // before, so any prior EglImage created is using a stale buffer. This
316 // replaces any old EglImage with a new one (using the new buffer).
317 if (item->mGraphicBuffer != NULL) {
318 int slot = item->mSlot;
319 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
320 }
321
322 return NO_ERROR;
323}
324
Chia-I Wuf1405182017-11-27 11:29:21 -0800325status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
326 PendingRelease* pendingRelease) {
327 status_t err = NO_ERROR;
328
329 int slot = item.mSlot;
330
Chia-I Wuf1405182017-11-27 11:29:21 -0800331 // Confirm state.
332 err = checkAndUpdateEglStateLocked();
333 if (err != NO_ERROR) {
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800334 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800335 return err;
336 }
337
338 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
339 // if nessessary, for the gralloc buffer currently in the slot in
340 // ConsumerBase.
341 // We may have to do this even when item.mGraphicBuffer == NULL (which
342 // means the buffer was previously acquired).
343 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
344 if (err != NO_ERROR) {
345 BLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d", mEglDisplay,
346 slot);
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800347 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800348 return UNKNOWN_ERROR;
349 }
350
351 // Do whatever sync ops we need to do before releasing the old slot.
352 if (slot != mCurrentTexture) {
353 err = syncForReleaseLocked(mEglDisplay);
354 if (err != NO_ERROR) {
355 // Release the buffer we just acquired. It's not safe to
356 // release the old buffer, so instead we just drop the new frame.
357 // As we are still under lock since acquireBuffer, it is safe to
358 // release by slot.
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800359 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800360 return err;
361 }
362 }
363
364 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
365 mCurrentTextureImage != NULL ? mCurrentTextureImage->graphicBufferHandle() : 0, slot,
366 mSlots[slot].mGraphicBuffer->handle);
367
368 // Hang onto the pointer so that it isn't freed in the call to
369 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
370 // the same.
371 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
372
373 // release old buffer
374 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
375 if (pendingRelease == nullptr) {
376 status_t status =
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800377 releaseBufferLocked(mCurrentTexture, mCurrentTextureImage->graphicBuffer());
Chia-I Wuf1405182017-11-27 11:29:21 -0800378 if (status < NO_ERROR) {
379 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
380 status);
381 err = status;
382 // keep going, with error raised [?]
383 }
384 } else {
385 pendingRelease->currentTexture = mCurrentTexture;
386 pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800387 pendingRelease->isPending = true;
388 }
389 }
390
391 // Update the BufferLayerConsumer state.
392 mCurrentTexture = slot;
393 mCurrentTextureImage = nextTextureImage;
394 mCurrentCrop = item.mCrop;
395 mCurrentTransform = item.mTransform;
396 mCurrentScalingMode = item.mScalingMode;
397 mCurrentTimestamp = item.mTimestamp;
398 mCurrentDataSpace = item.mDataSpace;
399 mCurrentFence = item.mFence;
400 mCurrentFenceTime = item.mFenceTime;
401 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800402 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
403 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wuf1405182017-11-27 11:29:21 -0800404
405 computeCurrentTransformMatrixLocked();
406
407 return err;
408}
409
410status_t BufferLayerConsumer::bindTextureImageLocked() {
411 if (mEglDisplay == EGL_NO_DISPLAY) {
412 ALOGE("bindTextureImage: invalid display");
413 return INVALID_OPERATION;
414 }
415
416 GLenum error;
417 while ((error = glGetError()) != GL_NO_ERROR) {
418 BLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
419 }
420
Chia-I Wubd854bf2017-11-27 13:41:26 -0800421 glBindTexture(sTexTarget, mTexName);
Chia-I Wuf1405182017-11-27 11:29:21 -0800422 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == NULL) {
423 BLC_LOGE("bindTextureImage: no currently-bound texture");
424 return NO_INIT;
425 }
426
427 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay, mCurrentCrop);
428 if (err != NO_ERROR) {
429 BLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", mEglDisplay,
430 mCurrentTexture);
431 return UNKNOWN_ERROR;
432 }
Chia-I Wubd854bf2017-11-27 13:41:26 -0800433 mCurrentTextureImage->bindToTextureTarget(sTexTarget);
Chia-I Wuf1405182017-11-27 11:29:21 -0800434
Chia-I Wuf1405182017-11-27 11:29:21 -0800435 // Wait for the new buffer to be ready.
436 return doGLFenceWaitLocked();
437}
438
Chia-I Wuc91077c2017-11-27 13:32:04 -0800439status_t BufferLayerConsumer::checkAndUpdateEglStateLocked() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800440 EGLDisplay dpy = eglGetCurrentDisplay();
441 EGLContext ctx = eglGetCurrentContext();
442
Chia-I Wuc91077c2017-11-27 13:32:04 -0800443 // if this is the first time we're called, mEglDisplay/mEglContext have
444 // never been set, so don't error out (below).
445 if (mEglDisplay == EGL_NO_DISPLAY) {
446 mEglDisplay = dpy;
447 }
448 if (mEglContext == EGL_NO_CONTEXT) {
449 mEglContext = ctx;
Chia-I Wuf1405182017-11-27 11:29:21 -0800450 }
451
452 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
453 BLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
454 return INVALID_OPERATION;
455 }
456
457 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
458 BLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
459 return INVALID_OPERATION;
460 }
461
Chia-I Wuf1405182017-11-27 11:29:21 -0800462 return NO_ERROR;
463}
464
Chia-I Wuf1405182017-11-27 11:29:21 -0800465status_t BufferLayerConsumer::syncForReleaseLocked(EGLDisplay dpy) {
466 BLC_LOGV("syncForReleaseLocked");
467
468 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
469 if (SyncFeatures::getInstance().useNativeFenceSync()) {
470 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
471 if (sync == EGL_NO_SYNC_KHR) {
472 BLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x", eglGetError());
473 return UNKNOWN_ERROR;
474 }
475 glFlush();
476 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
477 eglDestroySyncKHR(dpy, sync);
478 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
479 BLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
480 "fd: %#x",
481 eglGetError());
482 return UNKNOWN_ERROR;
483 }
484 sp<Fence> fence(new Fence(fenceFd));
485 status_t err = addReleaseFenceLocked(mCurrentTexture,
486 mCurrentTextureImage->graphicBuffer(), fence);
487 if (err != OK) {
488 BLC_LOGE("syncForReleaseLocked: error adding release fence: "
489 "%s (%d)",
490 strerror(-err), err);
491 return err;
492 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800493 }
494 }
495
496 return OK;
497}
498
Chia-I Wuf1405182017-11-27 11:29:21 -0800499void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
500 Mutex::Autolock lock(mMutex);
501 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
502}
503
504void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
505 Mutex::Autolock lock(mMutex);
506 if (mAbandoned) {
507 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
508 return;
509 }
510 bool needsRecompute = mFilteringEnabled != enabled;
511 mFilteringEnabled = enabled;
512
513 if (needsRecompute && mCurrentTextureImage == NULL) {
514 BLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
515 }
516
517 if (needsRecompute && mCurrentTextureImage != NULL) {
518 computeCurrentTransformMatrixLocked();
519 }
520}
521
522void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
523 BLC_LOGV("computeCurrentTransformMatrixLocked");
524 sp<GraphicBuffer> buf =
525 (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer();
526 if (buf == nullptr) {
527 BLC_LOGD("computeCurrentTransformMatrixLocked: "
528 "mCurrentTextureImage is NULL");
529 }
Chia-I Wu243b74a2017-11-27 14:24:14 -0800530 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, buf,
531 isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT
532 : mCurrentCrop,
533 mCurrentTransform, mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800534}
535
Chia-I Wuf1405182017-11-27 11:29:21 -0800536nsecs_t BufferLayerConsumer::getTimestamp() {
537 BLC_LOGV("getTimestamp");
538 Mutex::Autolock lock(mMutex);
539 return mCurrentTimestamp;
540}
541
542android_dataspace BufferLayerConsumer::getCurrentDataSpace() {
543 BLC_LOGV("getCurrentDataSpace");
544 Mutex::Autolock lock(mMutex);
545 return mCurrentDataSpace;
546}
547
548uint64_t BufferLayerConsumer::getFrameNumber() {
549 BLC_LOGV("getFrameNumber");
550 Mutex::Autolock lock(mMutex);
551 return mCurrentFrameNumber;
552}
553
Chia-I Wu67dcc692017-11-27 14:51:06 -0800554bool BufferLayerConsumer::getTransformToDisplayInverse() const {
555 Mutex::Autolock lock(mMutex);
556 return mCurrentTransformToDisplayInverse;
557}
558
559const Region& BufferLayerConsumer::getSurfaceDamage() const {
560 return mCurrentSurfaceDamage;
561}
562
Chia-I Wuf1405182017-11-27 11:29:21 -0800563sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const {
564 Mutex::Autolock lock(mMutex);
565
566 if (outSlot != nullptr) {
567 *outSlot = mCurrentTexture;
568 }
569
570 return (mCurrentTextureImage == nullptr) ? NULL : mCurrentTextureImage->graphicBuffer();
571}
572
573Rect BufferLayerConsumer::getCurrentCrop() const {
574 Mutex::Autolock lock(mMutex);
575 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Chia-I Wue1e11872017-12-01 09:21:59 -0800576 ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
Chia-I Wuf1405182017-11-27 11:29:21 -0800577 : mCurrentCrop;
578}
579
580uint32_t BufferLayerConsumer::getCurrentTransform() const {
581 Mutex::Autolock lock(mMutex);
582 return mCurrentTransform;
583}
584
585uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
586 Mutex::Autolock lock(mMutex);
587 return mCurrentScalingMode;
588}
589
590sp<Fence> BufferLayerConsumer::getCurrentFence() const {
591 Mutex::Autolock lock(mMutex);
592 return mCurrentFence;
593}
594
595std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
596 Mutex::Autolock lock(mMutex);
597 return mCurrentFenceTime;
598}
599
600status_t BufferLayerConsumer::doGLFenceWaitLocked() const {
601 EGLDisplay dpy = eglGetCurrentDisplay();
602 EGLContext ctx = eglGetCurrentContext();
603
604 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
605 BLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
606 return INVALID_OPERATION;
607 }
608
609 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
610 BLC_LOGE("doGLFenceWait: invalid current EGLContext");
611 return INVALID_OPERATION;
612 }
613
614 if (mCurrentFence->isValid()) {
615 if (SyncFeatures::getInstance().useWaitSync()) {
616 // Create an EGLSyncKHR from the current fence.
617 int fenceFd = mCurrentFence->dup();
618 if (fenceFd == -1) {
619 BLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
620 return -errno;
621 }
622 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
623 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
624 if (sync == EGL_NO_SYNC_KHR) {
625 close(fenceFd);
626 BLC_LOGE("doGLFenceWait: error creating EGL fence: %#x", eglGetError());
627 return UNKNOWN_ERROR;
628 }
629
630 // XXX: The spec draft is inconsistent as to whether this should
631 // return an EGLint or void. Ignore the return value for now, as
632 // it's not strictly needed.
633 eglWaitSyncKHR(dpy, sync, 0);
634 EGLint eglErr = eglGetError();
635 eglDestroySyncKHR(dpy, sync);
636 if (eglErr != EGL_SUCCESS) {
637 BLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x", eglErr);
638 return UNKNOWN_ERROR;
639 }
640 } else {
641 status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doGLFenceWaitLocked");
642 if (err != NO_ERROR) {
643 BLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
644 return err;
645 }
646 }
647 }
648
649 return NO_ERROR;
650}
651
652void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
653 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
654 if (slotIndex == mCurrentTexture) {
655 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
656 }
657 mEglSlots[slotIndex].mEglImage.clear();
658 ConsumerBase::freeBufferLocked(slotIndex);
659}
660
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800661void BufferLayerConsumer::onDisconnect() {
662 sp<Layer> l = mLayer.promote();
663 if (l.get()) {
664 l->onDisconnect();
665 }
666}
667
Chia-I Wufd257f82017-11-27 14:51:06 -0800668void BufferLayerConsumer::onSidebandStreamChanged() {
669 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
670 {
671 Mutex::Autolock lock(mFrameAvailableMutex);
672 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
673 }
674 sp<ContentsChangedListener> listener;
675 { // scope for the lock
676 Mutex::Autolock lock(mMutex);
677 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
678 listener = mContentsChangedListener.promote();
679 }
680
681 if (listener != NULL) {
682 listener->onSidebandStreamChanged();
683 }
684}
685
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800686void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
687 FrameEventHistoryDelta* outDelta) {
688 sp<Layer> l = mLayer.promote();
689 if (l.get()) {
690 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
691 }
692}
693
Chia-I Wuf1405182017-11-27 11:29:21 -0800694void BufferLayerConsumer::abandonLocked() {
695 BLC_LOGV("abandonLocked");
696 mCurrentTextureImage.clear();
697 ConsumerBase::abandonLocked();
698}
699
700status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
701 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
702}
703
704void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
705 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
706 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
707 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
708 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
709 mCurrentTransform);
710
711 ConsumerBase::dumpLocked(result, prefix);
712}
713
714BufferLayerConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer)
715 : mGraphicBuffer(graphicBuffer),
716 mEglImage(EGL_NO_IMAGE_KHR),
717 mEglDisplay(EGL_NO_DISPLAY),
718 mCropRect(Rect::EMPTY_RECT) {}
719
720BufferLayerConsumer::EglImage::~EglImage() {
721 if (mEglImage != EGL_NO_IMAGE_KHR) {
722 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
723 ALOGE("~EglImage: eglDestroyImageKHR failed");
724 }
725 eglTerminate(mEglDisplay);
726 }
727}
728
Chia-I Wuc91077c2017-11-27 13:32:04 -0800729status_t BufferLayerConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
730 const Rect& cropRect) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800731 // If there's an image and it's no longer valid, destroy it.
732 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
733 bool displayInvalid = mEglDisplay != eglDisplay;
734 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Chia-I Wuc91077c2017-11-27 13:32:04 -0800735 if (haveImage && (displayInvalid || cropInvalid)) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800736 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
737 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
738 }
739 eglTerminate(mEglDisplay);
740 mEglImage = EGL_NO_IMAGE_KHR;
741 mEglDisplay = EGL_NO_DISPLAY;
742 }
743
744 // If there's no image, create one.
745 if (mEglImage == EGL_NO_IMAGE_KHR) {
746 mEglDisplay = eglDisplay;
747 mCropRect = cropRect;
748 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
749 }
750
751 // Fail if we can't create a valid image.
752 if (mEglImage == EGL_NO_IMAGE_KHR) {
753 mEglDisplay = EGL_NO_DISPLAY;
754 mCropRect.makeInvalid();
755 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
756 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
757 buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(),
758 buffer->getPixelFormat());
759 return UNKNOWN_ERROR;
760 }
761
762 return OK;
763}
764
765void BufferLayerConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
766 glEGLImageTargetTexture2DOES(texTarget, static_cast<GLeglImageOES>(mEglImage));
767}
768
769EGLImageKHR BufferLayerConsumer::EglImage::createImage(EGLDisplay dpy,
770 const sp<GraphicBuffer>& graphicBuffer,
771 const Rect& crop) {
772 EGLClientBuffer cbuf = static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
773 const bool createProtectedImage =
774 (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) && hasEglProtectedContent();
775 EGLint attrs[] = {
776 EGL_IMAGE_PRESERVED_KHR,
777 EGL_TRUE,
778 EGL_IMAGE_CROP_LEFT_ANDROID,
779 crop.left,
780 EGL_IMAGE_CROP_TOP_ANDROID,
781 crop.top,
782 EGL_IMAGE_CROP_RIGHT_ANDROID,
783 crop.right,
784 EGL_IMAGE_CROP_BOTTOM_ANDROID,
785 crop.bottom,
786 createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
787 createProtectedImage ? EGL_TRUE : EGL_NONE,
788 EGL_NONE,
789 };
790 if (!crop.isValid()) {
791 // No crop rect to set, so leave the crop out of the attrib array. Make
792 // sure to propagate the protected content attrs if they are set.
793 attrs[2] = attrs[10];
794 attrs[3] = attrs[11];
795 attrs[4] = EGL_NONE;
796 } else if (!isEglImageCroppable(crop)) {
797 // The crop rect is not at the origin, so we can't set the crop on the
798 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
799 // extension. In the future we can add a layered extension that
800 // removes this restriction if there is hardware that can support it.
801 attrs[2] = attrs[10];
802 attrs[3] = attrs[11];
803 attrs[4] = EGL_NONE;
804 }
805 eglInitialize(dpy, 0, 0);
806 EGLImageKHR image =
807 eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
808 if (image == EGL_NO_IMAGE_KHR) {
809 EGLint error = eglGetError();
810 ALOGE("error creating EGLImage: %#x", error);
811 eglTerminate(dpy);
812 }
813 return image;
814}
815
816}; // namespace android