18ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis/* 28ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * Copyright (C) 2010 The Android Open Source Project 38ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * 48ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * Licensed under the Apache License, Version 2.0 (the "License"); 58ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * you may not use this file except in compliance with the License. 68ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * You may obtain a copy of the License at 78ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * 88ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * http://www.apache.org/licenses/LICENSE-2.0 98ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * 108ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * Unless required by applicable law or agreed to in writing, software 118ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * distributed under the License is distributed on an "AS IS" BASIS, 128ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * See the License for the specific language governing permissions and 148ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * limitations under the License. 158ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis */ 168ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis 172adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden#define LOG_TAG "GLConsumer" 181c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#define ATRACE_TAG ATRACE_TAG_GRAPHICS 19e70d8b43938e17beebcc0c97a9373a1906f6f2bfJamie Gennis//#define LOG_NDEBUG 0 208ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis 218ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#define GL_GLEXT_PROTOTYPES 228ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#define EGL_EGLEXT_PROTOTYPES 238ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis 248ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <EGL/egl.h> 258ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <EGL/eglext.h> 268ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <GLES2/gl2.h> 278ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <GLES2/gl2ext.h> 28ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian#include <cutils/compiler.h> 298ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis 309fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis#include <hardware/hardware.h> 319fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis 32dd26416fe135f93ef2c8570738f8e1ca5e2ca3a3Dan Stoza#include <gui/BufferItem.h> 33ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#include <gui/GLConsumer.h> 3490ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/IGraphicBufferAlloc.h> 3590ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/ISurfaceComposer.h> 3690ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/SurfaceComposerClient.h> 3741f673c9b3aac0d96e41c928845c39186d565212Mathias Agopian 3890ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <private/gui/ComposerService.h> 39ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#include <private/gui/SyncFeatures.h> 408ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis 418ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <utils/Log.h> 4268c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian#include <utils/String8.h> 431c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#include <utils/Trace.h> 448ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis 45dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie GennisEGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); 46dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis#define CROP_EXT_STR "EGL_ANDROID_image_crop" 47aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content" 48aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner#define EGL_PROTECTED_CONTENT_EXT 0x32C0 49dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis 5097eba8904c2f221c42a9473407223a4c3a213f75Andy McFaddennamespace android { 5197eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden 522adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden// Macros for including the GLConsumer name in log messages 53d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__) 54d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__) 55d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__) 56d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__) 57d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) 5829b5762efc359022168e5099c1d17925444d3147Mathias Agopian 59ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopianstatic const struct { 60d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza uint32_t width, height; 61ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian char const* bits; 6245263e2475ac6a885dbd78eff7d4e44f374e5237Mathias Agopian} kDebugData = { 15, 12, 63d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "_______________" 64d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "_______________" 65d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "_____XX_XX_____" 66d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "__X_X_____X_X__" 67d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "__X_XXXXXXX_X__" 68d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "__XXXXXXXXXXX__" 69d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "___XX_XXX_XX___" 70d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "____XXXXXXX____" 71d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "_____X___X_____" 72d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "____X_____X____" 73d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "_______________" 74d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza "_______________" 7545263e2475ac6a885dbd78eff7d4e44f374e5237Mathias Agopian}; 76ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 77f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis// Transform matrices 78f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennisstatic float mtxIdentity[16] = { 79f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 1, 0, 0, 0, 80f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, 1, 0, 0, 81f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, 0, 1, 0, 82f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, 0, 0, 1, 83f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis}; 84f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennisstatic float mtxFlipH[16] = { 85f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis -1, 0, 0, 0, 86f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, 1, 0, 0, 87f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, 0, 1, 0, 88f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 1, 0, 0, 1, 89f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis}; 90f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennisstatic float mtxFlipV[16] = { 91f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 1, 0, 0, 0, 92f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, -1, 0, 0, 93f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, 0, 1, 0, 94f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, 1, 0, 1, 95f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis}; 96f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennisstatic float mtxRot90[16] = { 97f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, 1, 0, 0, 98f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis -1, 0, 0, 0, 99f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 0, 0, 1, 0, 100f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 1, 0, 0, 1, 101f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis}; 102f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 103f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennisstatic void mtxMul(float out[16], const float a[16], const float b[16]); 104f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 1059870c9b66cc73ee31aabba23aa06deaf673ee5efMathias AgopianMutex GLConsumer::sStaticInitLock; 1069870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopiansp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer; 107fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis 108dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennisstatic bool hasEglAndroidImageCropImpl() { 109dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); 110dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS); 111dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis size_t cropExtLen = strlen(CROP_EXT_STR); 112dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis size_t extsLen = strlen(exts); 113dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis bool equal = !strcmp(CROP_EXT_STR, exts); 114dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1); 115dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis bool atEnd = (cropExtLen+1) < extsLen && 116dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1)); 117dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis bool inMiddle = strstr(exts, " " CROP_EXT_STR " "); 118dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis return equal || atStart || atEnd || inMiddle; 119dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis} 120dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis 121dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennisstatic bool hasEglAndroidImageCrop() { 122dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis // Only compute whether the extension is present once the first time this 123dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis // function is called. 124dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis static bool hasIt = hasEglAndroidImageCropImpl(); 125dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis return hasIt; 126dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis} 127dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis 128aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donnerstatic bool hasEglProtectedContentImpl() { 129aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); 130aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner const char* exts = eglQueryString(dpy, EGL_EXTENSIONS); 131aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR); 132aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner size_t extsLen = strlen(exts); 133aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts); 134aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1); 135aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner bool atEnd = (cropExtLen+1) < extsLen && 136aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen+1)); 137aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " "); 1384df76b2f5f7e8be128b5ec7a2d8d03845effaea0Craig Donner return equal || atStart || atEnd || inMiddle; 139aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner} 140aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner 141aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donnerstatic bool hasEglProtectedContent() { 142aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner // Only compute whether the extension is present once the first time this 143aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner // function is called. 144aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner static bool hasIt = hasEglProtectedContentImpl(); 145aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner return hasIt; 146aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner} 147aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner 148dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennisstatic bool isEglImageCroppable(const Rect& crop) { 149dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0); 150dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis} 151dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis 1523f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias AgopianGLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex, 1533f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian uint32_t texTarget, bool useFenceSync, bool isControlledByApp) : 154595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian ConsumerBase(bq, isControlledByApp), 15560d6922a011fe18c111b8d30fb6ef1f655b6b15ePablo Ceballos mCurrentCrop(Rect::EMPTY_RECT), 1561d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala mCurrentTransform(0), 157e692ab9a6be63193c5b52a6562d85d06c40463b8Mathias Agopian mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), 1581df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis mCurrentFence(Fence::NO_FENCE), 1591d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala mCurrentTimestamp(0), 160d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala mCurrentFrameNumber(0), 161e692ab9a6be63193c5b52a6562d85d06c40463b8Mathias Agopian mDefaultWidth(1), 162e692ab9a6be63193c5b52a6562d85d06c40463b8Mathias Agopian mDefaultHeight(1), 1635c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis mFilteringEnabled(true), 164b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian mTexName(tex), 16586edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis mUseFenceSync(useFenceSync), 166eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam mTexTarget(texTarget), 167ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis mEglDisplay(EGL_NO_DISPLAY), 168ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis mEglContext(EGL_NO_CONTEXT), 16974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), 17074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis mAttached(true) 1716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam{ 172d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("GLConsumer"); 173b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam 174fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis memcpy(mCurrentTransformMatrix, mtxIdentity, 175fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis sizeof(mCurrentTransformMatrix)); 176fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis 177db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); 1788ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis} 1798ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis 180ab57491de3a89a2d454d3060d36adef71741a7aeDan StozaGLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget, 181ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza bool useFenceSync, bool isControlledByApp) : 182ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza ConsumerBase(bq, isControlledByApp), 18360d6922a011fe18c111b8d30fb6ef1f655b6b15ePablo Ceballos mCurrentCrop(Rect::EMPTY_RECT), 184ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mCurrentTransform(0), 185ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), 186ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mCurrentFence(Fence::NO_FENCE), 187ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mCurrentTimestamp(0), 188ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mCurrentFrameNumber(0), 189ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mDefaultWidth(1), 190ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mDefaultHeight(1), 191ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mFilteringEnabled(true), 192d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza mTexName(0), 193ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mUseFenceSync(useFenceSync), 194ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mTexTarget(texTarget), 195ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mEglDisplay(EGL_NO_DISPLAY), 196ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mEglContext(EGL_NO_CONTEXT), 197ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), 198ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mAttached(false) 199ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza{ 200d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("GLConsumer"); 201ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza 202ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza memcpy(mCurrentTransformMatrix, mtxIdentity, 203ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza sizeof(mCurrentTransformMatrix)); 204ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza 205ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); 206ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza} 207ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza 2082adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenstatus_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) 209a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian{ 210b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam Mutex::Autolock lock(mMutex); 21165d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos if (mAbandoned) { 21265d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos GLC_LOGE("setDefaultBufferSize: GLConsumer is abandoned!"); 21365d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos return NO_INIT; 21465d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos } 215016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam mDefaultWidth = w; 216016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam mDefaultHeight = h; 217db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian return mConsumer->setDefaultBufferSize(w, h); 218a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian} 219a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian 2202adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenstatus_t GLConsumer::updateTexImage() { 221bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden ATRACE_CALL(); 222d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("updateTexImage"); 223bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden Mutex::Autolock lock(mMutex); 224bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 225bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden if (mAbandoned) { 226d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("updateTexImage: GLConsumer is abandoned!"); 227bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return NO_INIT; 228bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 229bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 230bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // Make sure the EGL state is the same as in previous calls. 231bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden status_t err = checkAndUpdateEglStateLocked(); 232bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden if (err != NO_ERROR) { 233bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return err; 234bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 235bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 236dd26416fe135f93ef2c8570738f8e1ca5e2ca3a3Dan Stoza BufferItem item; 237bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 238bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // Acquire the next buffer. 239bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // In asynchronous mode the list is guaranteed to be one buffer 240bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // deep, while in synchronous mode we use the oldest buffer. 2411585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden err = acquireBufferLocked(&item, 0); 242bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden if (err != NO_ERROR) { 243bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden if (err == BufferQueue::NO_BUFFER_AVAILABLE) { 244bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // We always bind the texture even if we don't update its contents. 245d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("updateTexImage: no buffers were available"); 246bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden glBindTexture(mTexTarget, mTexName); 247bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden err = NO_ERROR; 248bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } else { 249d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("updateTexImage: acquire failed: %s (%d)", 250bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden strerror(-err), err); 251bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 252bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return err; 253bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 254bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 255bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // Release the previous buffer. 256ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian err = updateAndReleaseLocked(item); 257bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden if (err != NO_ERROR) { 258bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // We always bind the texture. 259bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden glBindTexture(mTexTarget, mTexName); 260bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return err; 261bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 262bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 26397eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden // Bind the new buffer to the GL texture, and wait until it's ready. 26497eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden return bindTextureImageLocked(); 2652c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian} 2662c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian 267ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 268ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopianstatus_t GLConsumer::releaseTexImage() { 269ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian ATRACE_CALL(); 270d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("releaseTexImage"); 271ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian Mutex::Autolock lock(mMutex); 272ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 273ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian if (mAbandoned) { 274d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("releaseTexImage: GLConsumer is abandoned!"); 275ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian return NO_INIT; 276ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian } 277ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 278ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian // Make sure the EGL state is the same as in previous calls. 27945155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian status_t err = NO_ERROR; 28045155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian 28145155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian if (mAttached) { 28245155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian err = checkAndUpdateEglStateLocked(true); 28345155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian if (err != NO_ERROR) { 28445155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian return err; 28545155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian } 28645155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian } else { 28745155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian // if we're detached, no need to validate EGL's state -- we won't use it. 288ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian } 289ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 290ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian // Update the GLConsumer state. 291ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian int buf = mCurrentTexture; 292ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian if (buf != BufferQueue::INVALID_BUFFER_SLOT) { 293ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 294d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached); 295ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 29645155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian if (mAttached) { 29745155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian // Do whatever sync ops we need to do before releasing the slot. 29845155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian err = syncForReleaseLocked(mEglDisplay); 29945155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian if (err != NO_ERROR) { 300d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err); 30145155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian return err; 30245155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian } 30345155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian } else { 30445155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian // if we're detached, we just use the fence that was created in detachFromContext() 30545155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian // so... basically, nothing more to do here. 306ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian } 307ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 30845155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR); 309ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian if (err < NO_ERROR) { 310d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)", 311ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian strerror(-err), err); 312ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian return err; 313ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian } 314ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 3155c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (mReleasedTexImage == NULL) { 3165c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mReleasedTexImage = new EglImage(getDebugTexImageBuffer()); 3175c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 3185c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 319ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; 3205c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCurrentTextureImage = mReleasedTexImage; 321ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian mCurrentCrop.makeInvalid(); 322ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian mCurrentTransform = 0; 323ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian mCurrentTimestamp = 0; 324ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian mCurrentFence = Fence::NO_FENCE; 325ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 32645155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian if (mAttached) { 3275c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // This binds a dummy buffer (mReleasedTexImage). 328d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza status_t result = bindTextureImageLocked(); 329d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza if (result != NO_ERROR) { 330d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza return result; 3315c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 33245155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian } else { 33345155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian // detached, don't touch the texture (and we may not even have an 33445155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian // EGLDisplay here. 33545155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian } 336ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian } 337ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 338ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian return NO_ERROR; 339ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian} 340ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian 3419870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopiansp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() { 3429870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian Mutex::Autolock _l(sStaticInitLock); 3439870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) { 3449870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian // The first time, create the debug texture in case the application 3459870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian // continues to use it. 3469870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian sp<GraphicBuffer> buffer = new GraphicBuffer( 3479870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888, 3489870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian GraphicBuffer::USAGE_SW_WRITE_RARELY); 3499870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian uint32_t* bits; 3509870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits)); 351d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza uint32_t stride = buffer->getStride(); 352d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza uint32_t height = buffer->getHeight(); 353d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza memset(bits, 0, stride * height * 4); 354d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza for (uint32_t y = 0; y < kDebugData.height; y++) { 355d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza for (uint32_t x = 0; x < kDebugData.width; x++) { 356d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ? 357d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza 0xFF000000 : 0xFFFFFFFF; 3589870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian } 359d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza bits += stride; 3609870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian } 3619870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian buffer->unlock(); 3629870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian sReleasedTexImageBuffer = buffer; 3639870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian } 3649870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian return sReleasedTexImageBuffer; 3659870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian} 3669870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian 367dd26416fe135f93ef2c8570738f8e1ca5e2ca3a3Dan Stozastatus_t GLConsumer::acquireBufferLocked(BufferItem *item, 368a4650a50a0b35e9e4342d6600b6eb24fd94bb8e5Dan Stoza nsecs_t presentWhen, uint64_t maxFrameNumber) { 369a4650a50a0b35e9e4342d6600b6eb24fd94bb8e5Dan Stoza status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, 370a4650a50a0b35e9e4342d6600b6eb24fd94bb8e5Dan Stoza maxFrameNumber); 3719fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis if (err != NO_ERROR) { 3729fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis return err; 3739fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis } 3749fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis 3755c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // If item->mGraphicBuffer is not null, this buffer has not been acquired 3765c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // before, so any prior EglImage created is using a stale buffer. This 3775c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // replaces any old EglImage with a new one (using the new buffer). 3785c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (item->mGraphicBuffer != NULL) { 37947650f4f66a49e1815ad08ca4fb12a661d133abcPablo Ceballos int slot = item->mSlot; 3805c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer); 3819fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis } 3829fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis 3839fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis return NO_ERROR; 3849fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis} 3859fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis 386c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnarstatus_t GLConsumer::releaseBufferLocked(int buf, 387c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar sp<GraphicBuffer> graphicBuffer, 388c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar EGLDisplay display, EGLSyncKHR eglFence) { 389c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar // release the buffer if it hasn't already been discarded by the 390c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar // BufferQueue. This can happen, for example, when the producer of this 391c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar // buffer has reallocated the original buffer slot after this buffer 392c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar // was acquired. 393c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar status_t err = ConsumerBase::releaseBufferLocked( 394c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar buf, graphicBuffer, display, eglFence); 395d1b330de416adff0d178a5cb7271419d9ed7a89aJamie Gennis mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR; 3969fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis return err; 3979fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis} 3989fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis 3993ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stozastatus_t GLConsumer::updateAndReleaseLocked(const BufferItem& item, 4003ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza PendingRelease* pendingRelease) 401bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden{ 4029abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam status_t err = NO_ERROR; 4039abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam 40447650f4f66a49e1815ad08ca4fb12a661d133abcPablo Ceballos int slot = item.mSlot; 40587a678478005026d950bedec49ee80b693777b95Andy McFadden 40674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (!mAttached) { 407d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL " 40874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis "ES context"); 40947650f4f66a49e1815ad08ca4fb12a661d133abcPablo Ceballos releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, 41087a678478005026d950bedec49ee80b693777b95Andy McFadden mEglDisplay, EGL_NO_SYNC_KHR); 41174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return INVALID_OPERATION; 41274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 41374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 414bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // Confirm state. 415bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden err = checkAndUpdateEglStateLocked(); 416bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden if (err != NO_ERROR) { 41747650f4f66a49e1815ad08ca4fb12a661d133abcPablo Ceballos releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, 41887a678478005026d950bedec49ee80b693777b95Andy McFadden mEglDisplay, EGL_NO_SYNC_KHR); 419bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return err; 420ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis } 421ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis 4225c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // Ensure we have a valid EglImageKHR for the slot, creating an EglImage 4235c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // if nessessary, for the gralloc buffer currently in the slot in 4245c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // ConsumerBase. 425bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // We may have to do this even when item.mGraphicBuffer == NULL (which 4265c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // means the buffer was previously acquired). 42747650f4f66a49e1815ad08ca4fb12a661d133abcPablo Ceballos err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop); 4285c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (err != NO_ERROR) { 429d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d", 43047650f4f66a49e1815ad08ca4fb12a661d133abcPablo Ceballos mEglDisplay, slot); 43147650f4f66a49e1815ad08ca4fb12a661d133abcPablo Ceballos releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, 4325c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglDisplay, EGL_NO_SYNC_KHR); 4335c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner return UNKNOWN_ERROR; 434ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis } 435ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis 436bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // Do whatever sync ops we need to do before releasing the old slot. 437ff95aabbcc6e8606acbd7933c90eeb9b8b382a21Pablo Ceballos if (slot != mCurrentTexture) { 43833fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos err = syncForReleaseLocked(mEglDisplay); 43933fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos if (err != NO_ERROR) { 44033fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos // Release the buffer we just acquired. It's not safe to 44133fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos // release the old buffer, so instead we just drop the new frame. 44233fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos // As we are still under lock since acquireBuffer, it is safe to 44333fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos // release by slot. 44433fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, 44533fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos mEglDisplay, EGL_NO_SYNC_KHR); 44633fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos return err; 44733fcc2ecf707bfa15360d6d3ec95c778423d3032Pablo Ceballos } 448bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 449ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis 450d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", 4515c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCurrentTexture, mCurrentTextureImage != NULL ? 4525c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCurrentTextureImage->graphicBufferHandle() : 0, 45347650f4f66a49e1815ad08ca4fb12a661d133abcPablo Ceballos slot, mSlots[slot].mGraphicBuffer->handle); 454eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam 455ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos // Hang onto the pointer so that it isn't freed in the call to 4563559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos // releaseBufferLocked() if we're in shared buffer mode and both buffers are 457ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos // the same. 458ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage; 459ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos 460bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden // release old buffer 461bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { 4623ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza if (pendingRelease == nullptr) { 4633ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza status_t status = releaseBufferLocked( 4643ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza mCurrentTexture, mCurrentTextureImage->graphicBuffer(), 4653ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza mEglDisplay, mEglSlots[mCurrentTexture].mEglFence); 4663ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza if (status < NO_ERROR) { 4673ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", 4683ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza strerror(-status), status); 4693ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza err = status; 4703ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza // keep going, with error raised [?] 4713ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza } 4723ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza } else { 4733ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza pendingRelease->currentTexture = mCurrentTexture; 4743ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza pendingRelease->graphicBuffer = 4753ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza mCurrentTextureImage->graphicBuffer(); 4763ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza pendingRelease->display = mEglDisplay; 4773ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence; 4783ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza pendingRelease->isPending = true; 4792c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian } 480bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 4812c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian 4822adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden // Update the GLConsumer state. 48347650f4f66a49e1815ad08ca4fb12a661d133abcPablo Ceballos mCurrentTexture = slot; 484ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos mCurrentTextureImage = nextTextureImage; 485bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden mCurrentCrop = item.mCrop; 486bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden mCurrentTransform = item.mTransform; 487bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden mCurrentScalingMode = item.mScalingMode; 488bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden mCurrentTimestamp = item.mTimestamp; 489bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden mCurrentFence = item.mFence; 490d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala mCurrentFrameNumber = item.mFrameNumber; 491bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 492bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden computeCurrentTransformMatrixLocked(); 493bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 494bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return err; 495bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden} 496bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 4972adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenstatus_t GLConsumer::bindTextureImageLocked() { 498bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden if (mEglDisplay == EGL_NO_DISPLAY) { 499bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden ALOGE("bindTextureImage: invalid display"); 500bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return INVALID_OPERATION; 501bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 502bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 503d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLenum error; 504bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden while ((error = glGetError()) != GL_NO_ERROR) { 505d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error); 506bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 507bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 508bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden glBindTexture(mTexTarget, mTexName); 5095c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && 5105c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCurrentTextureImage == NULL) { 511d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("bindTextureImage: no currently-bound texture"); 5125c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner return NO_INIT; 5135c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 5140eb8851e8cc35f443646000220e42dba3adfab8bJamie Gennis 5155c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay, 5162d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner mCurrentCrop); 5175c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (err != NO_ERROR) { 518d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", 5195c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglDisplay, mCurrentTexture); 5205c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner return UNKNOWN_ERROR; 5215c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 5225c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCurrentTextureImage->bindToTextureTarget(mTexTarget); 5235c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 5242d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner // In the rare case that the display is terminated and then initialized 5252d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner // again, we can't detect that the display changed (it didn't), but the 5262d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner // image is invalid. In this case, repeat the exact same steps while 5272d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner // forcing the creation of a new image. 5282d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner if ((error = glGetError()) != GL_NO_ERROR) { 5292d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner glBindTexture(mTexTarget, mTexName); 530d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay, 531d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza mCurrentCrop, 532d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza true); 533d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza if (result != NO_ERROR) { 534d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", 5352d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner mEglDisplay, mCurrentTexture); 5362d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner return UNKNOWN_ERROR; 5372d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner } 5382d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner mCurrentTextureImage->bindToTextureTarget(mTexTarget); 5392d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner if ((error = glGetError()) != GL_NO_ERROR) { 540d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("bindTextureImage: error binding external image: %#04x", error); 5412d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner return UNKNOWN_ERROR; 5422d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner } 543bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 54497eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden 54597eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden // Wait for the new buffer to be ready. 54697eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden return doGLFenceWaitLocked(); 547bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden} 5489a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis 54945155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopianstatus_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) { 550bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden EGLDisplay dpy = eglGetCurrentDisplay(); 551bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden EGLContext ctx = eglGetCurrentContext(); 55286edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis 55345155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian if (!contextCheck) { 55445155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian // if this is the first time we're called, mEglDisplay/mEglContext have 55545155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian // never been set, so don't error out (below). 55645155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian if (mEglDisplay == EGL_NO_DISPLAY) { 55745155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian mEglDisplay = dpy; 55845155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian } 55946a1f6b40e1f7677cd41cd30f729ff66c7c21517Jesse Hall if (mEglContext == EGL_NO_CONTEXT) { 56045155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian mEglContext = ctx; 56145155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian } 56245155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian } 56345155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian 56445155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) { 565d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay"); 566bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return INVALID_OPERATION; 567bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 568b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian 56945155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) { 570d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext"); 571bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return INVALID_OPERATION; 5728ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis } 57350c4efc2a44fd312febeda76c8d1a6dc42cee8f8Jamie Gennis 574bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden mEglDisplay = dpy; 575bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden mEglContext = ctx; 576bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden return NO_ERROR; 5778ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis} 5788ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis 57913f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hallvoid GLConsumer::setReleaseFence(const sp<Fence>& fence) { 58013f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall if (fence->isValid() && 58113f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { 582c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar status_t err = addReleaseFence(mCurrentTexture, 5835c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCurrentTextureImage->graphicBuffer(), fence); 58413f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall if (err != OK) { 585d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", 58613f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall strerror(-err), err); 58713f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall } 588ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall } 589ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall} 590ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall 5912adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenstatus_t GLConsumer::detachFromContext() { 59274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis ATRACE_CALL(); 593d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("detachFromContext"); 59474bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis Mutex::Autolock lock(mMutex); 59574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 59674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (mAbandoned) { 597d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("detachFromContext: abandoned GLConsumer"); 59874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return NO_INIT; 59974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 60074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 60174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (!mAttached) { 602d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("detachFromContext: GLConsumer is not attached to a " 60374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis "context"); 60474bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return INVALID_OPERATION; 60574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 60674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 60774bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis EGLDisplay dpy = eglGetCurrentDisplay(); 60874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis EGLContext ctx = eglGetCurrentContext(); 60974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 61074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) { 611d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("detachFromContext: invalid current EGLDisplay"); 61274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return INVALID_OPERATION; 61374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 61474bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 61574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) { 616d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("detachFromContext: invalid current EGLContext"); 61774bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return INVALID_OPERATION; 61874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 61974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 62074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) { 62174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis status_t err = syncForReleaseLocked(dpy); 62274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (err != OK) { 62374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return err; 62474bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 62574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 62674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis glDeleteTextures(1, &mTexName); 62774bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 62874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 62974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis mEglDisplay = EGL_NO_DISPLAY; 63074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis mEglContext = EGL_NO_CONTEXT; 63174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis mAttached = false; 63274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 63374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return OK; 63474bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis} 63574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 6363f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianstatus_t GLConsumer::attachToContext(uint32_t tex) { 63774bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis ATRACE_CALL(); 638d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("attachToContext"); 63974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis Mutex::Autolock lock(mMutex); 64074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 64174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (mAbandoned) { 642d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("attachToContext: abandoned GLConsumer"); 64374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return NO_INIT; 64474bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 64574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 64674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (mAttached) { 647d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("attachToContext: GLConsumer is already attached to a " 64874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis "context"); 64974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return INVALID_OPERATION; 65074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 65174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 65274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis EGLDisplay dpy = eglGetCurrentDisplay(); 65374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis EGLContext ctx = eglGetCurrentContext(); 65474bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 65574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (dpy == EGL_NO_DISPLAY) { 656d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("attachToContext: invalid current EGLDisplay"); 65774bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return INVALID_OPERATION; 65874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 65974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 66074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis if (ctx == EGL_NO_CONTEXT) { 661d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("attachToContext: invalid current EGLContext"); 66274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return INVALID_OPERATION; 66374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 66474bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 66574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis // We need to bind the texture regardless of whether there's a current 66674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis // buffer. 6673f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian glBindTexture(mTexTarget, GLuint(tex)); 66874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 66974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis mEglDisplay = dpy; 67074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis mEglContext = ctx; 67174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis mTexName = tex; 67274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis mAttached = true; 67374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 6745c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (mCurrentTextureImage != NULL) { 6755c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // This may wait for a buffer a second time. This is likely required if 6765c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // this is a different context, since otherwise the wait could be skipped 6775c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // by bouncing through another context. For the same context the extra 6785c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // wait is redundant. 6795c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner status_t err = bindTextureImageLocked(); 6805c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (err != NO_ERROR) { 6815c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner return err; 6825c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 683bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden } 684bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 6855c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner return OK; 686bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden} 687bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 688bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden 6892adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenstatus_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) { 690d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("syncForReleaseLocked"); 69174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 69201dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { 693ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian if (SyncFeatures::getInstance().useNativeFenceSync()) { 69401dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis EGLSyncKHR sync = eglCreateSyncKHR(dpy, 69501dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis EGL_SYNC_NATIVE_FENCE_ANDROID, NULL); 69601dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis if (sync == EGL_NO_SYNC_KHR) { 697d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x", 69801dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis eglGetError()); 69974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return UNKNOWN_ERROR; 70074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 70101dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis glFlush(); 70201dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync); 70398ff0597bd9e57ba133d54f8f09841f96955cba1Jamie Gennis eglDestroySyncKHR(dpy, sync); 70401dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { 705d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("syncForReleaseLocked: error dup'ing native fence " 70601dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis "fd: %#x", eglGetError()); 70701dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis return UNKNOWN_ERROR; 70801dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis } 70901dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis sp<Fence> fence(new Fence(fenceFd)); 710c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar status_t err = addReleaseFenceLocked(mCurrentTexture, 7115c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCurrentTextureImage->graphicBuffer(), fence); 71201dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis if (err != OK) { 713d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("syncForReleaseLocked: error adding release fence: " 71401dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis "%s (%d)", strerror(-err), err); 71501dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis return err; 71601dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis } 717ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) { 71801dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence; 71901dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis if (fence != EGL_NO_SYNC_KHR) { 72001dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis // There is already a fence for the current slot. We need to 72101dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis // wait on that before replacing it with another fence to 72201dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis // ensure that all outstanding buffer accesses have completed 72301dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis // before the producer accesses it. 72401dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000); 72501dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis if (result == EGL_FALSE) { 726d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("syncForReleaseLocked: error waiting for previous " 72701dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis "fence: %#x", eglGetError()); 72801dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis return UNKNOWN_ERROR; 72901dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { 730d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("syncForReleaseLocked: timeout waiting for previous " 73101dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis "fence"); 73201dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis return TIMED_OUT; 73301dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis } 73401dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis eglDestroySyncKHR(dpy, fence); 73501dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis } 73674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 73701dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis // Create a fence for the outstanding accesses in the current 73801dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis // OpenGL ES context. 73901dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL); 74001dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis if (fence == EGL_NO_SYNC_KHR) { 741d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("syncForReleaseLocked: error creating fence: %#x", 74201dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis eglGetError()); 74301dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis return UNKNOWN_ERROR; 74401dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis } 74501dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis glFlush(); 74601dbf5539bff4bece3f9bbfa55046b04a8a45e99Jamie Gennis mEglSlots[mCurrentTexture].mEglFence = fence; 74774bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 74874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis } 74974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 75074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis return OK; 75174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis} 75274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis 753d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stozabool GLConsumer::isExternalFormat(PixelFormat format) 7547a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian{ 7557a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian switch (format) { 7567a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian // supported YUV formats 7577a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian case HAL_PIXEL_FORMAT_YV12: 7587a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian // Legacy/deprecated YUV formats 7597a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian case HAL_PIXEL_FORMAT_YCbCr_422_SP: 7607a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian case HAL_PIXEL_FORMAT_YCrCb_420_SP: 7617a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian case HAL_PIXEL_FORMAT_YCbCr_422_I: 7627a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian return true; 7637a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian } 7647a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian 7657a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian // Any OEM format needs to be considered 7667a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian if (format>=0x100 && format<=0x1FF) 7677a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian return true; 7687a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian 7697a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian return false; 7707a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian} 7717a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian 7723f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianuint32_t GLConsumer::getCurrentTextureTarget() const { 773fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis return mTexTarget; 7747a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian} 7757a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian 7762adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenvoid GLConsumer::getTransformMatrix(float mtx[16]) { 777f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis Mutex::Autolock lock(mMutex); 778736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); 779736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis} 780736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis 7812adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenvoid GLConsumer::setFilteringEnabled(bool enabled) { 7825c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis Mutex::Autolock lock(mMutex); 783e96e9e1093b5700e9f403a6e2479da7dc36d3b71Mathias Agopian if (mAbandoned) { 784d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!"); 785e96e9e1093b5700e9f403a6e2479da7dc36d3b71Mathias Agopian return; 786e96e9e1093b5700e9f403a6e2479da7dc36d3b71Mathias Agopian } 7875c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis bool needsRecompute = mFilteringEnabled != enabled; 7885c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis mFilteringEnabled = enabled; 789e96e9e1093b5700e9f403a6e2479da7dc36d3b71Mathias Agopian 7905c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (needsRecompute && mCurrentTextureImage==NULL) { 791d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL"); 792e96e9e1093b5700e9f403a6e2479da7dc36d3b71Mathias Agopian } 793e96e9e1093b5700e9f403a6e2479da7dc36d3b71Mathias Agopian 7945c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (needsRecompute && mCurrentTextureImage != NULL) { 795e96e9e1093b5700e9f403a6e2479da7dc36d3b71Mathias Agopian computeCurrentTransformMatrixLocked(); 7965c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis } 7975c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis} 7985c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis 7992adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenvoid GLConsumer::computeCurrentTransformMatrixLocked() { 800d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("computeCurrentTransformMatrixLocked"); 8011a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ? 8021a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck nullptr : mCurrentTextureImage->graphicBuffer(); 8031a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck if (buf == nullptr) { 8041a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck GLC_LOGD("computeCurrentTransformMatrixLocked: " 8051a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck "mCurrentTextureImage is NULL"); 8061a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck } 8071a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck computeTransformMatrix(mCurrentTransformMatrix, buf, 8081a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop, 8091a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck mCurrentTransform, mFilteringEnabled); 8101a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck} 8111a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck 8121a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reckvoid GLConsumer::computeTransformMatrix(float outTransform[16], 8131a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck const sp<GraphicBuffer>& buf, const Rect& cropRect, uint32_t transform, 8141a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck bool filtering) { 815f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 816a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis float xform[16]; 817a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis for (int i = 0; i < 16; i++) { 818a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis xform[i] = mtxIdentity[i]; 819a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis } 8201a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { 821a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis float result[16]; 822a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis mtxMul(result, xform, mtxFlipH); 823a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis for (int i = 0; i < 16; i++) { 824a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis xform[i] = result[i]; 825a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis } 826a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis } 8271a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { 828a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis float result[16]; 829a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis mtxMul(result, xform, mtxFlipV); 830a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis for (int i = 0; i < 16; i++) { 831a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis xform[i] = result[i]; 832a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis } 833a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis } 8341a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) { 835a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis float result[16]; 836a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis mtxMul(result, xform, mtxRot90); 837a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis for (int i = 0; i < 16; i++) { 838a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis xform[i] = result[i]; 839a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis } 840f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis } 841f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 842dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis float mtxBeforeFlipV[16]; 8431a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck if (!cropRect.isEmpty()) { 844dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f; 845dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis float bufferWidth = buf->getWidth(); 846dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis float bufferHeight = buf->getHeight(); 8471a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck float shrinkAmount = 0.0f; 8481a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck if (filtering) { 8491a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // In order to prevent bilinear sampling beyond the edge of the 8501a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // crop rectangle we may need to shrink it by 2 texels in each 8511a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // dimension. Normally this would just need to take 1/2 a texel 8521a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // off each end, but because the chroma channels of YUV420 images 8531a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // are subsampled we may need to shrink the crop region by a whole 8541a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // texel on each side. 8551a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck switch (buf->getPixelFormat()) { 8561a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck case PIXEL_FORMAT_RGBA_8888: 8571a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck case PIXEL_FORMAT_RGBX_8888: 8581a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck case PIXEL_FORMAT_RGB_888: 8591a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck case PIXEL_FORMAT_RGB_565: 8601a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck case PIXEL_FORMAT_BGRA_8888: 8611a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // We know there's no subsampling of any channels, so we 8621a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // only need to shrink by a half a pixel. 8631a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck shrinkAmount = 0.5; 8641a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck break; 8651a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck 8661a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck default: 8671a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // If we don't recognize the format, we must assume the 8681a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // worst case (that we care about), which is YUV420. 8691a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck shrinkAmount = 1.0; 8701a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck break; 8715c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis } 8721a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck } 8735c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis 8741a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck // Only shrink the dimensions that are not the size of the buffer. 8751a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck if (cropRect.width() < bufferWidth) { 8761a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck tx = (float(cropRect.left) + shrinkAmount) / bufferWidth; 8771a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) / 8781a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck bufferWidth; 8791a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck } 8801a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck if (cropRect.height() < bufferHeight) { 8811a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) / 8821a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck bufferHeight; 8831a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) / 8841a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck bufferHeight; 8855c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis } 886dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis float crop[16] = { 887dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis sx, 0, 0, 0, 888dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis 0, sy, 0, 0, 889dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis 0, 0, 1, 0, 890dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis tx, ty, 0, 1, 891dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis }; 892dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis 893dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis mtxMul(mtxBeforeFlipV, crop, xform); 894dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis } else { 895dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis for (int i = 0; i < 16; i++) { 896dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis mtxBeforeFlipV[i] = xform[i]; 8975c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis } 898a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis } 899a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis 900a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis // SurfaceFlinger expects the top of its window textures to be at a Y 9012adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden // coordinate of 0, so GLConsumer must behave the same way. We don't 902a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis // want to expose this to applications, however, so we must add an 903a214c64d7fb3baf9138cc5314f2111ecf5056c6cJamie Gennis // additional vertical flip to the transform after all the other transforms. 9041a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck mtxMul(outTransform, mtxFlipV, mtxBeforeFlipV); 905f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis} 906f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 9072adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddennsecs_t GLConsumer::getTimestamp() { 908d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("getTimestamp"); 9091d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala Mutex::Autolock lock(mMutex); 9101d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala return mCurrentTimestamp; 9111d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala} 9121d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala 913d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stozauint64_t GLConsumer::getFrameNumber() { 914d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("getFrameNumber"); 915d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala Mutex::Autolock lock(mMutex); 916d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala return mCurrentFrameNumber; 917d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala} 918d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala 9192adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddensp<GraphicBuffer> GLConsumer::getCurrentBuffer() const { 9207a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian Mutex::Autolock lock(mMutex); 9215c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner return (mCurrentTextureImage == NULL) ? 9225c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner NULL : mCurrentTextureImage->graphicBuffer(); 9237a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian} 9247a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian 9252adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenRect GLConsumer::getCurrentCrop() const { 9267a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian Mutex::Autolock lock(mMutex); 927016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam 928016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam Rect outCrop = mCurrentCrop; 929016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) { 930d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width()); 931d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height()); 932016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam 933016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) { 934016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam newWidth = newHeight * mDefaultWidth / mDefaultHeight; 935d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("too wide: newWidth = %d", newWidth); 936016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) { 937016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam newHeight = newWidth * mDefaultHeight / mDefaultWidth; 938d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("too tall: newHeight = %d", newHeight); 939016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam } 940016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam 941d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width()); 942d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height()); 943d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza 944016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam // The crop is too wide 945d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza if (newWidth < currentWidth) { 946ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza uint32_t dw = currentWidth - newWidth; 947ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza auto halfdw = dw / 2; 948ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza outCrop.left += halfdw; 949ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza // Not halfdw because it would subtract 1 too few when dw is odd 950ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza outCrop.right -= (dw - halfdw); 951016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam // The crop is too tall 952d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza } else if (newHeight < currentHeight) { 953ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza uint32_t dh = currentHeight - newHeight; 954ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza auto halfdh = dh / 2; 955ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza outCrop.top += halfdh; 956ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza // Not halfdh because it would subtract 1 too few when dh is odd 957ec4cb387502d5bd09e49b55605cf679fe3a0207aDan Stoza outCrop.bottom -= (dh - halfdh); 958016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam } 959016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam 960d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]", 961016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam outCrop.left, outCrop.top, 962016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam outCrop.right,outCrop.bottom); 963016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam } 964016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam 965016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam return outCrop; 9667a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian} 9677a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian 9682adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenuint32_t GLConsumer::getCurrentTransform() const { 9697a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian Mutex::Autolock lock(mMutex); 9707a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian return mCurrentTransform; 9717a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian} 9727a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian 9732adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenuint32_t GLConsumer::getCurrentScalingMode() const { 9747734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian Mutex::Autolock lock(mMutex); 9757734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian return mCurrentScalingMode; 9767734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian} 9777734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian 9782adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddensp<Fence> GLConsumer::getCurrentFence() const { 979dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall Mutex::Autolock lock(mMutex); 980dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall return mCurrentFence; 981dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall} 982dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall 9832adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenstatus_t GLConsumer::doGLFenceWait() const { 98461e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis Mutex::Autolock lock(mMutex); 9853941cb240d438bfdebe24920bb2ada86456a0bf9Jamie Gennis return doGLFenceWaitLocked(); 9863941cb240d438bfdebe24920bb2ada86456a0bf9Jamie Gennis} 9873941cb240d438bfdebe24920bb2ada86456a0bf9Jamie Gennis 9882adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenstatus_t GLConsumer::doGLFenceWaitLocked() const { 98961e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis 99061e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis EGLDisplay dpy = eglGetCurrentDisplay(); 99161e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis EGLContext ctx = eglGetCurrentContext(); 99261e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis 99361e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) { 994d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("doGLFenceWait: invalid current EGLDisplay"); 99561e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis return INVALID_OPERATION; 99661e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis } 99761e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis 99861e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) { 999d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("doGLFenceWait: invalid current EGLContext"); 100061e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis return INVALID_OPERATION; 100161e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis } 100261e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis 10031df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis if (mCurrentFence->isValid()) { 1004ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian if (SyncFeatures::getInstance().useWaitSync()) { 100561e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis // Create an EGLSyncKHR from the current fence. 100661e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis int fenceFd = mCurrentFence->dup(); 100761e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis if (fenceFd == -1) { 1008d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno); 100961e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis return -errno; 101061e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis } 101161e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis EGLint attribs[] = { 101261e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, 101361e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis EGL_NONE 101461e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis }; 101561e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis EGLSyncKHR sync = eglCreateSyncKHR(dpy, 101661e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); 101761e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis if (sync == EGL_NO_SYNC_KHR) { 101861e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis close(fenceFd); 1019d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x", 102061e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis eglGetError()); 102161e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis return UNKNOWN_ERROR; 102261e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis } 102361e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis 102461e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis // XXX: The spec draft is inconsistent as to whether this should 102561e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis // return an EGLint or void. Ignore the return value for now, as 102661e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis // it's not strictly needed. 10272bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian eglWaitSyncKHR(dpy, sync, 0); 102861e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis EGLint eglErr = eglGetError(); 102961e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis eglDestroySyncKHR(dpy, sync); 103061e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis if (eglErr != EGL_SUCCESS) { 1031d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x", 103261e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis eglErr); 103361e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis return UNKNOWN_ERROR; 103461e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis } 103561e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis } else { 1036ea74d3b78d607cde17790a7bb83e6f68ffd34cfdMathias Agopian status_t err = mCurrentFence->waitForever( 10372adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden "GLConsumer::doGLFenceWaitLocked"); 103861e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis if (err != NO_ERROR) { 1039d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err); 104061e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis return err; 104161e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis } 104261e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis } 104361e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis } 104461e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis 104561e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis return NO_ERROR; 104661e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis} 104761e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis 10482adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenvoid GLConsumer::freeBufferLocked(int slotIndex) { 1049d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); 10509abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam if (slotIndex == mCurrentTexture) { 10519abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; 10529abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam } 10535c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglSlots[slotIndex].mEglImage.clear(); 10549fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis ConsumerBase::freeBufferLocked(slotIndex); 1055fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis} 1056fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis 10572adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenvoid GLConsumer::abandonLocked() { 1058d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza GLC_LOGV("abandonLocked"); 10595c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCurrentTextureImage.clear(); 10609fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis ConsumerBase::abandonLocked(); 10617b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis} 10627b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis 10632adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenvoid GLConsumer::setName(const String8& name) { 1064eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam Mutex::Autolock _l(mMutex); 106565d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos if (mAbandoned) { 106665d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos GLC_LOGE("setName: GLConsumer is abandoned!"); 106765d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos return; 106865d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos } 1069fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis mName = name; 1070db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian mConsumer->setConsumerName(name); 1071b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam} 1072b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam 1073d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stozastatus_t GLConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) { 1074b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam Mutex::Autolock lock(mMutex); 107565d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos if (mAbandoned) { 107665d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos GLC_LOGE("setDefaultBufferFormat: GLConsumer is abandoned!"); 107765d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos return NO_INIT; 107865d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos } 1079db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian return mConsumer->setDefaultBufferFormat(defaultFormat); 1080b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam} 1081b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam 10825b75a513e431c097ae704cba2f7affa6bfaecec9Eino-Ville Talvalastatus_t GLConsumer::setDefaultBufferDataSpace( 10835b75a513e431c097ae704cba2f7affa6bfaecec9Eino-Ville Talvala android_dataspace defaultDataSpace) { 10845b75a513e431c097ae704cba2f7affa6bfaecec9Eino-Ville Talvala Mutex::Autolock lock(mMutex); 108565d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos if (mAbandoned) { 108665d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos GLC_LOGE("setDefaultBufferDataSpace: GLConsumer is abandoned!"); 108765d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos return NO_INIT; 108865d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos } 10895b75a513e431c097ae704cba2f7affa6bfaecec9Eino-Ville Talvala return mConsumer->setDefaultBufferDataSpace(defaultDataSpace); 10905b75a513e431c097ae704cba2f7affa6bfaecec9Eino-Ville Talvala} 10915b75a513e431c097ae704cba2f7affa6bfaecec9Eino-Ville Talvala 10922adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenstatus_t GLConsumer::setConsumerUsageBits(uint32_t usage) { 1093b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam Mutex::Autolock lock(mMutex); 109465d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos if (mAbandoned) { 109565d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos GLC_LOGE("setConsumerUsageBits: GLConsumer is abandoned!"); 109665d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos return NO_INIT; 109765d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos } 109885b217668d6840c8e6a109adfb99461313676f8dEino-Ville Talvala usage |= DEFAULT_USAGE_FLAGS; 1099db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian return mConsumer->setConsumerUsageBits(usage); 1100b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam} 1101b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam 11022adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenstatus_t GLConsumer::setTransformHint(uint32_t hint) { 1103b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam Mutex::Autolock lock(mMutex); 110465d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos if (mAbandoned) { 110565d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos GLC_LOGE("setTransformHint: GLConsumer is abandoned!"); 110665d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos return NO_INIT; 110765d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos } 1108db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian return mConsumer->setTransformHint(hint); 1109b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam} 1110b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam 111119e3e06e3c65a7c001a6fe0971744ba5ff536515Pablo Ceballosstatus_t GLConsumer::setMaxAcquiredBufferCount(int maxAcquiredBuffers) { 111219e3e06e3c65a7c001a6fe0971744ba5ff536515Pablo Ceballos Mutex::Autolock lock(mMutex); 111365d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos if (mAbandoned) { 111465d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos GLC_LOGE("setMaxAcquiredBufferCount: GLConsumer is abandoned!"); 111565d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos return NO_INIT; 111665d9f6d63ab3bad1d835df14c662028a748eb3c5Pablo Ceballos } 111719e3e06e3c65a7c001a6fe0971744ba5ff536515Pablo Ceballos return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers); 111819e3e06e3c65a7c001a6fe0971744ba5ff536515Pablo Ceballos} 111919e3e06e3c65a7c001a6fe0971744ba5ff536515Pablo Ceballos 112074d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopianvoid GLConsumer::dumpLocked(String8& result, const char* prefix) const 112168c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian{ 112274d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian result.appendFormat( 11239fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis "%smTexName=%d mCurrentTexture=%d\n" 11249fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", 11259fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, 11269fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, 11279fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis mCurrentTransform); 112868c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian 112974d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian ConsumerBase::dumpLocked(result, prefix); 113068c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian} 113168c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian 1132f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennisstatic void mtxMul(float out[16], const float a[16], const float b[16]) { 1133f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; 1134f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; 1135f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; 1136f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; 1137f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 1138f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; 1139f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; 1140f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; 1141f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; 1142f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 1143f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; 1144f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; 1145f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; 1146f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; 1147f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 1148f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; 1149f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; 1150f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; 1151f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; 1152f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis} 1153f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis 11545c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric PennerGLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) : 11555c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mGraphicBuffer(graphicBuffer), 11565c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglImage(EGL_NO_IMAGE_KHR), 115760d6922a011fe18c111b8d30fb6ef1f655b6b15ePablo Ceballos mEglDisplay(EGL_NO_DISPLAY), 115860d6922a011fe18c111b8d30fb6ef1f655b6b15ePablo Ceballos mCropRect(Rect::EMPTY_RECT) { 11595c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner} 11605c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 11615c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric PennerGLConsumer::EglImage::~EglImage() { 11625c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (mEglImage != EGL_NO_IMAGE_KHR) { 11635c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) { 11645c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner ALOGE("~EglImage: eglDestroyImageKHR failed"); 11655c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 116678be65e7bf6898c6afa55c9016f331ab1aa2503aMichael Lentine eglTerminate(mEglDisplay); 11675c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 11685c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner} 11695c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 11705c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Pennerstatus_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay, 11712d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner const Rect& cropRect, 11722d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner bool forceCreation) { 11735c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // If there's an image and it's no longer valid, destroy it. 11745c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner bool haveImage = mEglImage != EGL_NO_IMAGE_KHR; 11755c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner bool displayInvalid = mEglDisplay != eglDisplay; 11765c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect; 11772d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner if (haveImage && (displayInvalid || cropInvalid || forceCreation)) { 11785c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) { 11795c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner ALOGE("createIfNeeded: eglDestroyImageKHR failed"); 11805c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 118178be65e7bf6898c6afa55c9016f331ab1aa2503aMichael Lentine eglTerminate(mEglDisplay); 11825c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglImage = EGL_NO_IMAGE_KHR; 11835c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglDisplay = EGL_NO_DISPLAY; 11845c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 11855c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 11865c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // If there's no image, create one. 11875c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (mEglImage == EGL_NO_IMAGE_KHR) { 11885c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglDisplay = eglDisplay; 11895c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCropRect = cropRect; 11905c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect); 11915c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 11925c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 11935c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // Fail if we can't create a valid image. 11945c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (mEglImage == EGL_NO_IMAGE_KHR) { 11955c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mEglDisplay = EGL_NO_DISPLAY; 11965c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner mCropRect.makeInvalid(); 11975c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner const sp<GraphicBuffer>& buffer = mGraphicBuffer; 11985c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d", 11995c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner buffer->getWidth(), buffer->getHeight(), buffer->getStride(), 12005c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner buffer->getUsage(), buffer->getPixelFormat()); 12015c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner return UNKNOWN_ERROR; 12025c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 12035c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 12045c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner return OK; 12055c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner} 12065c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 12075c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Pennervoid GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) { 1208d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza glEGLImageTargetTexture2DOES(texTarget, 1209d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza static_cast<GLeglImageOES>(mEglImage)); 12105c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner} 12115c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 12125c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric PennerEGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy, 12135c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) { 1214d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza EGLClientBuffer cbuf = 1215d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer()); 1216aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner const bool createProtectedImage = 1217aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) && 1218aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner hasEglProtectedContent(); 12195c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGLint attrs[] = { 12205c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, 12215c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGL_IMAGE_CROP_LEFT_ANDROID, crop.left, 12225c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGL_IMAGE_CROP_TOP_ANDROID, crop.top, 12235c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right, 12245c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom, 1225aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE, 1226aec8697fcaa8bb30e335f7850cffdd0364c35532Craig Donner createProtectedImage ? EGL_TRUE : EGL_NONE, 12275c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGL_NONE, 12285c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner }; 12295c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (!crop.isValid()) { 12305c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // No crop rect to set, so terminate the attrib array before the crop. 12315c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner attrs[2] = EGL_NONE; 12325c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } else if (!isEglImageCroppable(crop)) { 12335c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // The crop rect is not at the origin, so we can't set the crop on the 12345c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // EGLImage because that's not allowed by the EGL_ANDROID_image_crop 12355c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // extension. In the future we can add a layered extension that 12365c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner // removes this restriction if there is hardware that can support it. 12375c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner attrs[2] = EGL_NONE; 12385c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 123978be65e7bf6898c6afa55c9016f331ab1aa2503aMichael Lentine eglInitialize(dpy, 0, 0); 12405c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, 12415c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); 12425c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner if (image == EGL_NO_IMAGE_KHR) { 12435c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner EGLint error = eglGetError(); 12445c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner ALOGE("error creating EGLImage: %#x", error); 124578be65e7bf6898c6afa55c9016f331ab1aa2503aMichael Lentine eglTerminate(dpy); 12465c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner } 12475c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner return image; 12485c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner} 12495c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner 12508ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis}; // namespace android 1251