blob: 110b7de41a92e9e4bb1d838d9a7d89dcea767189 [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),
121 mDefaultWidth(1),
122 mDefaultHeight(1),
123 mFilteringEnabled(true),
124 mTexName(tex),
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800125 mLayer(layer),
Chia-I Wuf1405182017-11-27 11:29:21 -0800126 mEglDisplay(EGL_NO_DISPLAY),
127 mEglContext(EGL_NO_CONTEXT),
Chia-I Wuc91077c2017-11-27 13:32:04 -0800128 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800129 BLC_LOGV("BufferLayerConsumer");
130
131 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
132
133 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
134}
135
136status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) {
137 Mutex::Autolock lock(mMutex);
138 if (mAbandoned) {
139 BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!");
140 return NO_INIT;
141 }
142 mDefaultWidth = w;
143 mDefaultHeight = h;
144 return mConsumer->setDefaultBufferSize(w, h);
145}
146
Chia-I Wufd257f82017-11-27 14:51:06 -0800147void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) {
148 setFrameAvailableListener(listener);
149 Mutex::Autolock lock(mMutex);
150 mContentsChangedListener = listener;
151}
152
Chia-I Wuda5c7302017-11-27 14:51:06 -0800153// We need to determine the time when a buffer acquired now will be
154// displayed. This can be calculated:
155// time when previous buffer's actual-present fence was signaled
156// + current display refresh rate * HWC latency
157// + a little extra padding
158//
159// Buffer producers are expected to set their desired presentation time
160// based on choreographer time stamps, which (coming from vsync events)
161// will be slightly later then the actual-present timing. If we get a
162// desired-present time that is unintentionally a hair after the next
163// vsync, we'll hold the frame when we really want to display it. We
164// need to take the offset between actual-present and reported-vsync
165// into account.
166//
167// If the system is configured without a DispSync phase offset for the app,
168// we also want to throw in a bit of padding to avoid edge cases where we
169// just barely miss. We want to do it here, not in every app. A major
170// source of trouble is the app's use of the display's ideal refresh time
171// (via Display.getRefreshRate()), which could be off of the actual refresh
172// by a few percent, with the error multiplied by the number of frames
173// between now and when the buffer should be displayed.
174//
175// If the refresh reported to the app has a phase offset, we shouldn't need
176// to tweak anything here.
177nsecs_t BufferLayerConsumer::computeExpectedPresent(const DispSync& dispSync) {
178 // The HWC doesn't currently have a way to report additional latency.
179 // Assume that whatever we submit now will appear right after the flip.
180 // For a smart panel this might be 1. This is expressed in frames,
181 // rather than time, because we expect to have a constant frame delay
182 // regardless of the refresh rate.
183 const uint32_t hwcLatency = 0;
184
185 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
186 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
187
188 // The DispSync time is already adjusted for the difference between
189 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
190 // we don't need to factor that in here. Pad a little to avoid
191 // weird effects if apps might be requesting times right on the edge.
192 nsecs_t extraPadding = 0;
193 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
194 extraPadding = 1000000; // 1ms (6% of 60Hz)
195 }
196
197 return nextRefresh + extraPadding;
198}
199
200status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync,
201 bool* autoRefresh, bool* queuedBuffer,
202 uint64_t maxFrameNumber) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800203 ATRACE_CALL();
204 BLC_LOGV("updateTexImage");
205 Mutex::Autolock lock(mMutex);
206
207 if (mAbandoned) {
208 BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!");
209 return NO_INIT;
210 }
211
212 // Make sure the EGL state is the same as in previous calls.
213 status_t err = checkAndUpdateEglStateLocked();
214 if (err != NO_ERROR) {
215 return err;
216 }
217
218 BufferItem item;
219
220 // Acquire the next buffer.
221 // In asynchronous mode the list is guaranteed to be one buffer
222 // deep, while in synchronous mode we use the oldest buffer.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800223 err = acquireBufferLocked(&item, computeExpectedPresent(dispSync), maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -0800224 if (err != NO_ERROR) {
225 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800226 err = NO_ERROR;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800227 } else if (err == BufferQueue::PRESENT_LATER) {
228 // return the error, without logging
Chia-I Wuf1405182017-11-27 11:29:21 -0800229 } else {
230 BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
231 }
232 return err;
233 }
234
Chia-I Wuda5c7302017-11-27 14:51:06 -0800235 if (autoRefresh) {
236 *autoRefresh = item.mAutoRefresh;
237 }
238
239 if (queuedBuffer) {
240 *queuedBuffer = item.mQueuedBuffer;
241 }
242
243 // We call the rejecter here, in case the caller has a reason to
244 // not accept this buffer. This is used by SurfaceFlinger to
245 // reject buffers which have the wrong size
246 int slot = item.mSlot;
247 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
248 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
249 return BUFFER_REJECTED;
250 }
251
Chia-I Wuf1405182017-11-27 11:29:21 -0800252 // Release the previous buffer.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800253 err = updateAndReleaseLocked(item, &mPendingRelease);
Chia-I Wuf1405182017-11-27 11:29:21 -0800254 if (err != NO_ERROR) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800255 return err;
256 }
257
Chia-I Wuda5c7302017-11-27 14:51:06 -0800258 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
259 // Bind the new buffer to the GL texture.
260 //
261 // Older devices require the "implicit" synchronization provided
262 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
263 // devices will either call this in Layer::onDraw, or (if it's not
264 // a GL-composited layer) not at all.
265 err = bindTextureImageLocked();
266 }
267
268 return err;
269}
270
271void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
272 if (!fence->isValid()) {
273 return;
274 }
275
276 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
277 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
278 return;
279 }
280
281 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer
282 : mCurrentTextureImage->graphicBuffer();
283 auto err = addReleaseFence(slot, buffer, fence);
284 if (err != OK) {
285 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
286 }
287}
288
289bool BufferLayerConsumer::releasePendingBuffer() {
290 if (!mPendingRelease.isPending) {
291 BLC_LOGV("Pending buffer already released");
292 return false;
293 }
294 BLC_LOGV("Releasing pending buffer");
295 Mutex::Autolock lock(mMutex);
296 status_t result =
297 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
298 if (result < NO_ERROR) {
299 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
300 }
301 mPendingRelease = PendingRelease();
302 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800303}
304
Chia-I Wuf1405182017-11-27 11:29:21 -0800305status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
306 uint64_t maxFrameNumber) {
307 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
308 if (err != NO_ERROR) {
309 return err;
310 }
311
312 // If item->mGraphicBuffer is not null, this buffer has not been acquired
313 // before, so any prior EglImage created is using a stale buffer. This
314 // replaces any old EglImage with a new one (using the new buffer).
315 if (item->mGraphicBuffer != NULL) {
316 int slot = item->mSlot;
317 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
318 }
319
320 return NO_ERROR;
321}
322
Chia-I Wuf1405182017-11-27 11:29:21 -0800323status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
324 PendingRelease* pendingRelease) {
325 status_t err = NO_ERROR;
326
327 int slot = item.mSlot;
328
Chia-I Wuf1405182017-11-27 11:29:21 -0800329 // Confirm state.
330 err = checkAndUpdateEglStateLocked();
331 if (err != NO_ERROR) {
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800332 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800333 return err;
334 }
335
336 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
337 // if nessessary, for the gralloc buffer currently in the slot in
338 // ConsumerBase.
339 // We may have to do this even when item.mGraphicBuffer == NULL (which
340 // means the buffer was previously acquired).
341 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
342 if (err != NO_ERROR) {
343 BLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d", mEglDisplay,
344 slot);
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800345 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800346 return UNKNOWN_ERROR;
347 }
348
349 // Do whatever sync ops we need to do before releasing the old slot.
350 if (slot != mCurrentTexture) {
351 err = syncForReleaseLocked(mEglDisplay);
352 if (err != NO_ERROR) {
353 // Release the buffer we just acquired. It's not safe to
354 // release the old buffer, so instead we just drop the new frame.
355 // As we are still under lock since acquireBuffer, it is safe to
356 // release by slot.
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800357 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800358 return err;
359 }
360 }
361
362 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
363 mCurrentTextureImage != NULL ? mCurrentTextureImage->graphicBufferHandle() : 0, slot,
364 mSlots[slot].mGraphicBuffer->handle);
365
366 // Hang onto the pointer so that it isn't freed in the call to
367 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
368 // the same.
369 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
370
371 // release old buffer
372 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
373 if (pendingRelease == nullptr) {
374 status_t status =
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800375 releaseBufferLocked(mCurrentTexture, mCurrentTextureImage->graphicBuffer());
Chia-I Wuf1405182017-11-27 11:29:21 -0800376 if (status < NO_ERROR) {
377 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
378 status);
379 err = status;
380 // keep going, with error raised [?]
381 }
382 } else {
383 pendingRelease->currentTexture = mCurrentTexture;
384 pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800385 pendingRelease->isPending = true;
386 }
387 }
388
389 // Update the BufferLayerConsumer state.
390 mCurrentTexture = slot;
391 mCurrentTextureImage = nextTextureImage;
392 mCurrentCrop = item.mCrop;
393 mCurrentTransform = item.mTransform;
394 mCurrentScalingMode = item.mScalingMode;
395 mCurrentTimestamp = item.mTimestamp;
396 mCurrentDataSpace = item.mDataSpace;
397 mCurrentFence = item.mFence;
398 mCurrentFenceTime = item.mFenceTime;
399 mCurrentFrameNumber = item.mFrameNumber;
400
401 computeCurrentTransformMatrixLocked();
402
403 return err;
404}
405
406status_t BufferLayerConsumer::bindTextureImageLocked() {
407 if (mEglDisplay == EGL_NO_DISPLAY) {
408 ALOGE("bindTextureImage: invalid display");
409 return INVALID_OPERATION;
410 }
411
412 GLenum error;
413 while ((error = glGetError()) != GL_NO_ERROR) {
414 BLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
415 }
416
Chia-I Wubd854bf2017-11-27 13:41:26 -0800417 glBindTexture(sTexTarget, mTexName);
Chia-I Wuf1405182017-11-27 11:29:21 -0800418 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == NULL) {
419 BLC_LOGE("bindTextureImage: no currently-bound texture");
420 return NO_INIT;
421 }
422
423 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay, mCurrentCrop);
424 if (err != NO_ERROR) {
425 BLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", mEglDisplay,
426 mCurrentTexture);
427 return UNKNOWN_ERROR;
428 }
Chia-I Wubd854bf2017-11-27 13:41:26 -0800429 mCurrentTextureImage->bindToTextureTarget(sTexTarget);
Chia-I Wuf1405182017-11-27 11:29:21 -0800430
Chia-I Wuf1405182017-11-27 11:29:21 -0800431 // Wait for the new buffer to be ready.
432 return doGLFenceWaitLocked();
433}
434
Chia-I Wuc91077c2017-11-27 13:32:04 -0800435status_t BufferLayerConsumer::checkAndUpdateEglStateLocked() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800436 EGLDisplay dpy = eglGetCurrentDisplay();
437 EGLContext ctx = eglGetCurrentContext();
438
Chia-I Wuc91077c2017-11-27 13:32:04 -0800439 // if this is the first time we're called, mEglDisplay/mEglContext have
440 // never been set, so don't error out (below).
441 if (mEglDisplay == EGL_NO_DISPLAY) {
442 mEglDisplay = dpy;
443 }
444 if (mEglContext == EGL_NO_CONTEXT) {
445 mEglContext = ctx;
Chia-I Wuf1405182017-11-27 11:29:21 -0800446 }
447
448 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
449 BLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
450 return INVALID_OPERATION;
451 }
452
453 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
454 BLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
455 return INVALID_OPERATION;
456 }
457
Chia-I Wuf1405182017-11-27 11:29:21 -0800458 return NO_ERROR;
459}
460
Chia-I Wuf1405182017-11-27 11:29:21 -0800461status_t BufferLayerConsumer::syncForReleaseLocked(EGLDisplay dpy) {
462 BLC_LOGV("syncForReleaseLocked");
463
464 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
465 if (SyncFeatures::getInstance().useNativeFenceSync()) {
466 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
467 if (sync == EGL_NO_SYNC_KHR) {
468 BLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x", eglGetError());
469 return UNKNOWN_ERROR;
470 }
471 glFlush();
472 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
473 eglDestroySyncKHR(dpy, sync);
474 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
475 BLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
476 "fd: %#x",
477 eglGetError());
478 return UNKNOWN_ERROR;
479 }
480 sp<Fence> fence(new Fence(fenceFd));
481 status_t err = addReleaseFenceLocked(mCurrentTexture,
482 mCurrentTextureImage->graphicBuffer(), fence);
483 if (err != OK) {
484 BLC_LOGE("syncForReleaseLocked: error adding release fence: "
485 "%s (%d)",
486 strerror(-err), err);
487 return err;
488 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800489 }
490 }
491
492 return OK;
493}
494
Chia-I Wuf1405182017-11-27 11:29:21 -0800495void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
496 Mutex::Autolock lock(mMutex);
497 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
498}
499
500void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
501 Mutex::Autolock lock(mMutex);
502 if (mAbandoned) {
503 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
504 return;
505 }
506 bool needsRecompute = mFilteringEnabled != enabled;
507 mFilteringEnabled = enabled;
508
509 if (needsRecompute && mCurrentTextureImage == NULL) {
510 BLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
511 }
512
513 if (needsRecompute && mCurrentTextureImage != NULL) {
514 computeCurrentTransformMatrixLocked();
515 }
516}
517
518void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
519 BLC_LOGV("computeCurrentTransformMatrixLocked");
520 sp<GraphicBuffer> buf =
521 (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer();
522 if (buf == nullptr) {
523 BLC_LOGD("computeCurrentTransformMatrixLocked: "
524 "mCurrentTextureImage is NULL");
525 }
Chia-I Wu243b74a2017-11-27 14:24:14 -0800526 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, buf,
527 isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT
528 : mCurrentCrop,
529 mCurrentTransform, mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800530}
531
Chia-I Wuf1405182017-11-27 11:29:21 -0800532nsecs_t BufferLayerConsumer::getTimestamp() {
533 BLC_LOGV("getTimestamp");
534 Mutex::Autolock lock(mMutex);
535 return mCurrentTimestamp;
536}
537
538android_dataspace BufferLayerConsumer::getCurrentDataSpace() {
539 BLC_LOGV("getCurrentDataSpace");
540 Mutex::Autolock lock(mMutex);
541 return mCurrentDataSpace;
542}
543
544uint64_t BufferLayerConsumer::getFrameNumber() {
545 BLC_LOGV("getFrameNumber");
546 Mutex::Autolock lock(mMutex);
547 return mCurrentFrameNumber;
548}
549
550sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const {
551 Mutex::Autolock lock(mMutex);
552
553 if (outSlot != nullptr) {
554 *outSlot = mCurrentTexture;
555 }
556
557 return (mCurrentTextureImage == nullptr) ? NULL : mCurrentTextureImage->graphicBuffer();
558}
559
560Rect BufferLayerConsumer::getCurrentCrop() const {
561 Mutex::Autolock lock(mMutex);
562 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Chia-I Wue1e11872017-12-01 09:21:59 -0800563 ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
Chia-I Wuf1405182017-11-27 11:29:21 -0800564 : mCurrentCrop;
565}
566
567uint32_t BufferLayerConsumer::getCurrentTransform() const {
568 Mutex::Autolock lock(mMutex);
569 return mCurrentTransform;
570}
571
572uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
573 Mutex::Autolock lock(mMutex);
574 return mCurrentScalingMode;
575}
576
577sp<Fence> BufferLayerConsumer::getCurrentFence() const {
578 Mutex::Autolock lock(mMutex);
579 return mCurrentFence;
580}
581
582std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
583 Mutex::Autolock lock(mMutex);
584 return mCurrentFenceTime;
585}
586
587status_t BufferLayerConsumer::doGLFenceWaitLocked() const {
588 EGLDisplay dpy = eglGetCurrentDisplay();
589 EGLContext ctx = eglGetCurrentContext();
590
591 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
592 BLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
593 return INVALID_OPERATION;
594 }
595
596 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
597 BLC_LOGE("doGLFenceWait: invalid current EGLContext");
598 return INVALID_OPERATION;
599 }
600
601 if (mCurrentFence->isValid()) {
602 if (SyncFeatures::getInstance().useWaitSync()) {
603 // Create an EGLSyncKHR from the current fence.
604 int fenceFd = mCurrentFence->dup();
605 if (fenceFd == -1) {
606 BLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
607 return -errno;
608 }
609 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
610 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
611 if (sync == EGL_NO_SYNC_KHR) {
612 close(fenceFd);
613 BLC_LOGE("doGLFenceWait: error creating EGL fence: %#x", eglGetError());
614 return UNKNOWN_ERROR;
615 }
616
617 // XXX: The spec draft is inconsistent as to whether this should
618 // return an EGLint or void. Ignore the return value for now, as
619 // it's not strictly needed.
620 eglWaitSyncKHR(dpy, sync, 0);
621 EGLint eglErr = eglGetError();
622 eglDestroySyncKHR(dpy, sync);
623 if (eglErr != EGL_SUCCESS) {
624 BLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x", eglErr);
625 return UNKNOWN_ERROR;
626 }
627 } else {
628 status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doGLFenceWaitLocked");
629 if (err != NO_ERROR) {
630 BLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
631 return err;
632 }
633 }
634 }
635
636 return NO_ERROR;
637}
638
639void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
640 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
641 if (slotIndex == mCurrentTexture) {
642 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
643 }
644 mEglSlots[slotIndex].mEglImage.clear();
645 ConsumerBase::freeBufferLocked(slotIndex);
646}
647
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800648void BufferLayerConsumer::onDisconnect() {
649 sp<Layer> l = mLayer.promote();
650 if (l.get()) {
651 l->onDisconnect();
652 }
653}
654
Chia-I Wufd257f82017-11-27 14:51:06 -0800655void BufferLayerConsumer::onSidebandStreamChanged() {
656 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
657 {
658 Mutex::Autolock lock(mFrameAvailableMutex);
659 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
660 }
661 sp<ContentsChangedListener> listener;
662 { // scope for the lock
663 Mutex::Autolock lock(mMutex);
664 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
665 listener = mContentsChangedListener.promote();
666 }
667
668 if (listener != NULL) {
669 listener->onSidebandStreamChanged();
670 }
671}
672
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800673void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
674 FrameEventHistoryDelta* outDelta) {
675 sp<Layer> l = mLayer.promote();
676 if (l.get()) {
677 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
678 }
679}
680
Chia-I Wuf1405182017-11-27 11:29:21 -0800681void BufferLayerConsumer::abandonLocked() {
682 BLC_LOGV("abandonLocked");
683 mCurrentTextureImage.clear();
684 ConsumerBase::abandonLocked();
685}
686
687status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
688 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
689}
690
691void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
692 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
693 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
694 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
695 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
696 mCurrentTransform);
697
698 ConsumerBase::dumpLocked(result, prefix);
699}
700
701BufferLayerConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer)
702 : mGraphicBuffer(graphicBuffer),
703 mEglImage(EGL_NO_IMAGE_KHR),
704 mEglDisplay(EGL_NO_DISPLAY),
705 mCropRect(Rect::EMPTY_RECT) {}
706
707BufferLayerConsumer::EglImage::~EglImage() {
708 if (mEglImage != EGL_NO_IMAGE_KHR) {
709 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
710 ALOGE("~EglImage: eglDestroyImageKHR failed");
711 }
712 eglTerminate(mEglDisplay);
713 }
714}
715
Chia-I Wuc91077c2017-11-27 13:32:04 -0800716status_t BufferLayerConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
717 const Rect& cropRect) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800718 // If there's an image and it's no longer valid, destroy it.
719 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
720 bool displayInvalid = mEglDisplay != eglDisplay;
721 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Chia-I Wuc91077c2017-11-27 13:32:04 -0800722 if (haveImage && (displayInvalid || cropInvalid)) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800723 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
724 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
725 }
726 eglTerminate(mEglDisplay);
727 mEglImage = EGL_NO_IMAGE_KHR;
728 mEglDisplay = EGL_NO_DISPLAY;
729 }
730
731 // If there's no image, create one.
732 if (mEglImage == EGL_NO_IMAGE_KHR) {
733 mEglDisplay = eglDisplay;
734 mCropRect = cropRect;
735 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
736 }
737
738 // Fail if we can't create a valid image.
739 if (mEglImage == EGL_NO_IMAGE_KHR) {
740 mEglDisplay = EGL_NO_DISPLAY;
741 mCropRect.makeInvalid();
742 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
743 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
744 buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(),
745 buffer->getPixelFormat());
746 return UNKNOWN_ERROR;
747 }
748
749 return OK;
750}
751
752void BufferLayerConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
753 glEGLImageTargetTexture2DOES(texTarget, static_cast<GLeglImageOES>(mEglImage));
754}
755
756EGLImageKHR BufferLayerConsumer::EglImage::createImage(EGLDisplay dpy,
757 const sp<GraphicBuffer>& graphicBuffer,
758 const Rect& crop) {
759 EGLClientBuffer cbuf = static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
760 const bool createProtectedImage =
761 (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) && hasEglProtectedContent();
762 EGLint attrs[] = {
763 EGL_IMAGE_PRESERVED_KHR,
764 EGL_TRUE,
765 EGL_IMAGE_CROP_LEFT_ANDROID,
766 crop.left,
767 EGL_IMAGE_CROP_TOP_ANDROID,
768 crop.top,
769 EGL_IMAGE_CROP_RIGHT_ANDROID,
770 crop.right,
771 EGL_IMAGE_CROP_BOTTOM_ANDROID,
772 crop.bottom,
773 createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
774 createProtectedImage ? EGL_TRUE : EGL_NONE,
775 EGL_NONE,
776 };
777 if (!crop.isValid()) {
778 // No crop rect to set, so leave the crop out of the attrib array. Make
779 // sure to propagate the protected content attrs if they are set.
780 attrs[2] = attrs[10];
781 attrs[3] = attrs[11];
782 attrs[4] = EGL_NONE;
783 } else if (!isEglImageCroppable(crop)) {
784 // The crop rect is not at the origin, so we can't set the crop on the
785 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
786 // extension. In the future we can add a layered extension that
787 // removes this restriction if there is hardware that can support it.
788 attrs[2] = attrs[10];
789 attrs[3] = attrs[11];
790 attrs[4] = EGL_NONE;
791 }
792 eglInitialize(dpy, 0, 0);
793 EGLImageKHR image =
794 eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
795 if (image == EGL_NO_IMAGE_KHR) {
796 EGLint error = eglGetError();
797 ALOGE("error creating EGLImage: %#x", error);
798 eglTerminate(dpy);
799 }
800 return image;
801}
802
803}; // namespace android