199c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall/*
299c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall * Copyright 2013 The Android Open Source Project
399c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall *
499c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall * Licensed under the Apache License, Version 2.0 (the "License");
599c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall * you may not use this file except in compliance with the License.
699c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall * You may obtain a copy of the License at
799c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall *
899c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall *      http://www.apache.org/licenses/LICENSE-2.0
999c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall *
1099c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall * Unless required by applicable law or agreed to in writing, software
1199c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall * distributed under the License is distributed on an "AS IS" BASIS,
1299c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1399c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall * See the License for the specific language governing permissions and
1499c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall * limitations under the License.
1599c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall */
1699c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
1738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall// #define LOG_NDEBUG 0
1899c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall#include "VirtualDisplaySurface.h"
1938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall#include "HWComposer.h"
2099c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
2199c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall// ---------------------------------------------------------------------------
2299c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hallnamespace android {
2399c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall// ---------------------------------------------------------------------------
2499c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
25c354effe97be26638618e3bc0da81a3f8b384b20Jesse Hall#if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS)
26c354effe97be26638618e3bc0da81a3f8b384b20Jesse Hallstatic const bool sForceHwcCopy = true;
27c354effe97be26638618e3bc0da81a3f8b384b20Jesse Hall#else
28c354effe97be26638618e3bc0da81a3f8b384b20Jesse Hallstatic const bool sForceHwcCopy = false;
29c354effe97be26638618e3bc0da81a3f8b384b20Jesse Hall#endif
306a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed
3124cd98eef88ac93f80c327f8d74f0a1ae0aceee4Jesse Hall#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
3238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        mDisplayName.string(), ##__VA_ARGS__)
3324cd98eef88ac93f80c327f8d74f0a1ae0aceee4Jesse Hall#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
3438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        mDisplayName.string(), ##__VA_ARGS__)
3524cd98eef88ac93f80c327f8d74f0a1ae0aceee4Jesse Hall#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
3638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        mDisplayName.string(), ##__VA_ARGS__)
3738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
3838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallstatic const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
3938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    switch (type) {
4038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
4138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case DisplaySurface::COMPOSITION_GLES:    return "GLES";
4238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case DisplaySurface::COMPOSITION_HWC:     return "HWC";
4338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case DisplaySurface::COMPOSITION_MIXED:   return "MIXED";
4438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        default:                                  return "<INVALID>";
4538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
4638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
4738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
48ffe1f19ca9707f84cb9fdb06209bf36cd8c2ef0aJesse HallVirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
49db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian        const sp<IGraphicBufferProducer>& sink,
50b9b088375d33a87b201cdbe18be71802e2607717Dan Stoza        const sp<IGraphicBufferProducer>& bqProducer,
51b9b088375d33a87b201cdbe18be71802e2607717Dan Stoza        const sp<IGraphicBufferConsumer>& bqConsumer,
52db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian        const String8& name)
53b9b088375d33a87b201cdbe18be71802e2607717Dan Stoza:   ConsumerBase(bqConsumer),
5438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mHwc(hwc),
5538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mDisplayId(dispId),
5638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mDisplayName(name),
571e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall    mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
5838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mProducerSlotSource(0),
5938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mDbgState(DBG_STATE_IDLE),
607143316af216fa92c31a60d4407b707637382da1Dan Stoza    mDbgLastCompositionType(COMPOSITION_UNKNOWN),
617143316af216fa92c31a60d4407b707637382da1Dan Stoza    mMustRecompose(false)
62ffe1f19ca9707f84cb9fdb06209bf36cd8c2ef0aJesse Hall{
6338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mSource[SOURCE_SINK] = sink;
64b9b088375d33a87b201cdbe18be71802e2607717Dan Stoza    mSource[SOURCE_SCRATCH] = bqProducer;
6538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
6638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    resetPerFrameState();
6738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
6838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    int sinkWidth, sinkHeight;
69497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
70497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
7147e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    mSinkBufferWidth = sinkWidth;
7247e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    mSinkBufferHeight = sinkHeight;
73497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall
74497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    // Pick the buffer format to request from the sink when not rendering to it
75497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    // with GLES. If the consumer needs CPU access, use the default format
76497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    // set by the consumer. Otherwise allow gralloc to decide the format based
77497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    // on usage bits.
78497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    int sinkUsage;
79497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
80497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
81497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall        int sinkFormat;
82497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall        sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
83497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall        mDefaultOutputFormat = sinkFormat;
84497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    } else {
85497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall        mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
86497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    }
87497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall    mOutputFormat = mDefaultOutputFormat;
8838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
8938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
90db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    mConsumer->setConsumerName(ConsumerBase::mName);
91db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
92db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
93db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    mConsumer->setDefaultMaxBufferCount(2);
94ffe1f19ca9707f84cb9fdb06209bf36cd8c2ef0aJesse Hall}
9599c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
9699c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse HallVirtualDisplaySurface::~VirtualDisplaySurface() {
9799c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall}
9899c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
997143316af216fa92c31a60d4407b707637382da1Dan Stozastatus_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
10038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (mDisplayId < 0)
10138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        return NO_ERROR;
10238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
1037143316af216fa92c31a60d4407b707637382da1Dan Stoza    mMustRecompose = mustRecompose;
1047143316af216fa92c31a60d4407b707637382da1Dan Stoza
10538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
106028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall            "Unexpected beginFrame() in %s state", dbgStateStr());
107028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    mDbgState = DBG_STATE_BEGUN;
108028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall
109028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    return refreshOutputBuffer();
110028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall}
111028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall
112028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hallstatus_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
113028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    if (mDisplayId < 0)
114028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        return NO_ERROR;
115028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall
116028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
11738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            "Unexpected prepareFrame() in %s state", dbgStateStr());
11838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mDbgState = DBG_STATE_PREPARED;
11938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
12038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mCompositionType = compositionType;
1216a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed    if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
1226a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed        // Some hardware can do RGB->YUV conversion more efficiently in hardware
1236a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed        // controlled by HWC than in hardware controlled by the video encoder.
1246a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed        // Forcing GLES-composed frames to go through an extra copy by the HWC
1256a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed        // allows the format conversion to happen there, rather than passing RGB
1266a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed        // directly to the consumer.
1276a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed        //
1286a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed        // On the other hand, when the consumer prefers RGB or can consume RGB
1296a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed        // inexpensively, this forces an unnecessary copy.
1306a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed        mCompositionType = COMPOSITION_MIXED;
1316a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed    }
1326a968462f9ce4d93d81fcc13672073e3e6eb2e0fNaseer Ahmed
13338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (mCompositionType != mDbgLastCompositionType) {
13438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        VDS_LOGV("prepareFrame: composition type changed to %s",
13538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                dbgCompositionTypeStr(mCompositionType));
13638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        mDbgLastCompositionType = mCompositionType;
13738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
13838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
1391e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall    if (mCompositionType != COMPOSITION_GLES &&
140497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall            (mOutputFormat != mDefaultOutputFormat ||
1411e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall             mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
1421e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        // We must have just switched from GLES-only to MIXED or HWC
1431e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        // composition. Stop using the format and usage requested by the GLES
1441e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        // driver; they may be suboptimal when HWC is writing to the output
1451e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        // buffer. For example, if the output is going to a video encoder, and
1461e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        // HWC can write directly to YUV, some hardware can skip a
1471e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        // memory-to-memory RGB-to-YUV conversion step.
1481e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        //
1491e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        // If we just switched *to* GLES-only mode, we'll change the
1501e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        // format/usage and get a new buffer when the GLES driver calls
1511e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        // dequeueBuffer().
152497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall        mOutputFormat = mDefaultOutputFormat;
1531e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
1541e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        refreshOutputBuffer();
1551e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall    }
1561e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall
15738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return NO_ERROR;
15899c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall}
15999c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
16099c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hallstatus_t VirtualDisplaySurface::compositionComplete() {
16199c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    return NO_ERROR;
16299c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall}
16399c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
16499c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hallstatus_t VirtualDisplaySurface::advanceFrame() {
16538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (mDisplayId < 0)
16638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        return NO_ERROR;
16738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
16838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (mCompositionType == COMPOSITION_HWC) {
16938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
17038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                "Unexpected advanceFrame() in %s state on HWC frame",
17138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                dbgStateStr());
17238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    } else {
17338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
17438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
17538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                dbgStateStr());
17638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
17738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mDbgState = DBG_STATE_HWC;
17838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
17914e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall    if (mOutputProducerSlot < 0 ||
18014e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall            (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
18138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // Last chance bailout if something bad happened earlier. For example,
18238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // in a GLES configuration, if the sink disappears then dequeueBuffer
18338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
18438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // will soldier on. So we end up here without a buffer. There should
18538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // be lots of scary messages in the log just before this.
18638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        VDS_LOGE("advanceFrame: no buffer, bailing out");
18738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        return NO_MEMORY;
18838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
18938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
19014e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall    sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
19114e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall            mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
19238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
19338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
19438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            mFbProducerSlot, fbBuffer.get(),
19538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            mOutputProducerSlot, outBuffer.get());
19638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
197b716e57048e945d5bceda33c2434c65d06464dbaJesse Hall    // At this point we know the output buffer acquire fence,
198b716e57048e945d5bceda33c2434c65d06464dbaJesse Hall    // so update HWC state with it.
199b716e57048e945d5bceda33c2434c65d06464dbaJesse Hall    mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
200b716e57048e945d5bceda33c2434c65d06464dbaJesse Hall
20114e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall    status_t result = NO_ERROR;
20214e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall    if (fbBuffer != NULL) {
20314e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall        result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
20414e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall    }
20514e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall
20614e8b01a761180250671f3d6494532d8bd04fa8eJesse Hall    return result;
20799c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall}
20899c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
209851cfe834295224cd64bdd499872b95b19c4de8cJesse Hallvoid VirtualDisplaySurface::onFrameCommitted() {
21038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (mDisplayId < 0)
21138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        return;
21238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
21338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
21438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            "Unexpected onFrameCommitted() in %s state", dbgStateStr());
21538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mDbgState = DBG_STATE_IDLE;
21638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
21738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
21838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
21938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // release the scratch buffer back to the pool
22038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        Mutex::Autolock lock(mMutex);
22138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
22238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
22338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
22438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
22538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
22638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
22738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
22838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (mOutputProducerSlot >= 0) {
22938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
23038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        QueueBufferOutput qbo;
23138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
23238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
2337143316af216fa92c31a60d4407b707637382da1Dan Stoza        if (mMustRecompose) {
2347143316af216fa92c31a60d4407b707637382da1Dan Stoza            status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
2357143316af216fa92c31a60d4407b707637382da1Dan Stoza                    QueueBufferInput(
2367143316af216fa92c31a60d4407b707637382da1Dan Stoza                        systemTime(), false /* isAutoTimestamp */,
2377143316af216fa92c31a60d4407b707637382da1Dan Stoza                        Rect(mSinkBufferWidth, mSinkBufferHeight),
2387143316af216fa92c31a60d4407b707637382da1Dan Stoza                        NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
2397143316af216fa92c31a60d4407b707637382da1Dan Stoza                        true /* async*/,
2407143316af216fa92c31a60d4407b707637382da1Dan Stoza                        outFence),
2417143316af216fa92c31a60d4407b707637382da1Dan Stoza                    &qbo);
2427143316af216fa92c31a60d4407b707637382da1Dan Stoza            if (result == NO_ERROR) {
2437143316af216fa92c31a60d4407b707637382da1Dan Stoza                updateQueueBufferOutput(qbo);
2447143316af216fa92c31a60d4407b707637382da1Dan Stoza            }
2457143316af216fa92c31a60d4407b707637382da1Dan Stoza        } else {
2467143316af216fa92c31a60d4407b707637382da1Dan Stoza            // If the surface hadn't actually been updated, then we only went
2477143316af216fa92c31a60d4407b707637382da1Dan Stoza            // through the motions of updating the display to keep our state
2487143316af216fa92c31a60d4407b707637382da1Dan Stoza            // machine happy. We cancel the buffer to avoid triggering another
2497143316af216fa92c31a60d4407b707637382da1Dan Stoza            // re-composition and causing an infinite loop.
2507143316af216fa92c31a60d4407b707637382da1Dan Stoza            mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence);
25138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        }
25238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
25338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
25438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    resetPerFrameState();
25599c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall}
25699c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
257febd4f4f462444bfcb3f0618d07ac77e3fc1f6adDan Stozavoid VirtualDisplaySurface::dump(String8& /* result */) const {
25899c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall}
25999c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall
26047e45405d1533aa73307014f7bf371e118695cf3Michael Lentinevoid VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
26147e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    uint32_t tmpW, tmpH, transformHint, numPendingBuffers;
26247e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    mQueueBufferOutput.deflate(&tmpW, &tmpH, &transformHint, &numPendingBuffers);
26347e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    mQueueBufferOutput.inflate(w, h, transformHint, numPendingBuffers);
26447e45405d1533aa73307014f7bf371e118695cf3Michael Lentine
26547e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    mSinkBufferWidth = w;
26647e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    mSinkBufferHeight = h;
26747e45405d1533aa73307014f7bf371e118695cf3Michael Lentine}
26847e45405d1533aa73307014f7bf371e118695cf3Michael Lentine
26938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallstatus_t VirtualDisplaySurface::requestBuffer(int pslot,
27038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        sp<GraphicBuffer>* outBuf) {
27147e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    if (mDisplayId < 0)
27247e45405d1533aa73307014f7bf371e118695cf3Michael Lentine        return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
27347e45405d1533aa73307014f7bf371e118695cf3Michael Lentine
27438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
27538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            "Unexpected requestBuffer pslot=%d in %s state",
27638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            pslot, dbgStateStr());
27738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
27838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    *outBuf = mProducerBuffers[pslot];
27938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return NO_ERROR;
28038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
28138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
28238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallstatus_t VirtualDisplaySurface::setBufferCount(int bufferCount) {
28338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return mSource[SOURCE_SINK]->setBufferCount(bufferCount);
28438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
28538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
28638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallstatus_t VirtualDisplaySurface::dequeueBuffer(Source source,
2871e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        uint32_t format, uint32_t usage, int* sslot, sp<Fence>* fence) {
28847e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
2898db92553e9e6263cd41eaf1a1ebc9e3f88f92b5fJesse Hall    // Don't let a slow consumer block us
2908db92553e9e6263cd41eaf1a1ebc9e3f88f92b5fJesse Hall    bool async = (source == SOURCE_SINK);
2918db92553e9e6263cd41eaf1a1ebc9e3f88f92b5fJesse Hall
2927cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
2931e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall            mSinkBufferWidth, mSinkBufferHeight, format, usage);
29438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (result < 0)
29538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        return result;
29638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    int pslot = mapSource2ProducerSlot(source, *sslot);
29738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
29838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            dbgSourceStr(source), *sslot, pslot, result);
299febd4f4f462444bfcb3f0618d07ac77e3fc1f6adDan Stoza    uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
30038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
301febd4f4f462444bfcb3f0618d07ac77e3fc1f6adDan Stoza    if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
30238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // This slot was previously dequeued from the other source; must
30338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // re-request the buffer.
30438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        result |= BUFFER_NEEDS_REALLOCATION;
305febd4f4f462444bfcb3f0618d07ac77e3fc1f6adDan Stoza        mProducerSlotSource &= ~(1ULL << pslot);
30638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        mProducerSlotSource |= sourceBit;
30738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
30838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
30938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (result & RELEASE_ALL_BUFFERS) {
31038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
311febd4f4f462444bfcb3f0618d07ac77e3fc1f6adDan Stoza            if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
31238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                mProducerBuffers[i].clear();
31338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        }
31438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
31538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (result & BUFFER_NEEDS_REALLOCATION) {
3160b63cd105cae094c21aafaa2bac8322427e75827Jesse Hall        result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
3170b63cd105cae094c21aafaa2bac8322427e75827Jesse Hall        if (result < 0) {
3180b63cd105cae094c21aafaa2bac8322427e75827Jesse Hall            mProducerBuffers[pslot].clear();
3190b63cd105cae094c21aafaa2bac8322427e75827Jesse Hall            mSource[source]->cancelBuffer(*sslot, *fence);
3200b63cd105cae094c21aafaa2bac8322427e75827Jesse Hall            return result;
3210b63cd105cae094c21aafaa2bac8322427e75827Jesse Hall        }
322497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall        VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
323497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall                dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
324497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall                mProducerBuffers[pslot]->getPixelFormat(),
325497ba0e08503806571b52ebe27cc7eee4c0e71a7Jesse Hall                mProducerBuffers[pslot]->getUsage());
32638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
32738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
32838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return result;
32938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
33038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
3317cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopianstatus_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
33238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
33347e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    if (mDisplayId < 0)
33447e45405d1533aa73307014f7bf371e118695cf3Michael Lentine        return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, async, w, h, format, usage);
33547e45405d1533aa73307014f7bf371e118695cf3Michael Lentine
33638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
33738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            "Unexpected dequeueBuffer() in %s state", dbgStateStr());
33838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mDbgState = DBG_STATE_GLES;
33938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
3408db92553e9e6263cd41eaf1a1ebc9e3f88f92b5fJesse Hall    VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)");
34138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
34238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
343028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    status_t result = NO_ERROR;
34438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    Source source = fbSourceForCompositionType(mCompositionType);
345028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall
34638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (source == SOURCE_SINK) {
3476da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian
3486da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian        if (mOutputProducerSlot < 0) {
3496da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian            // Last chance bailout if something bad happened earlier. For example,
3506da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian            // in a GLES configuration, if the sink disappears then dequeueBuffer
3516da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian            // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
3526da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian            // will soldier on. So we end up here without a buffer. There should
3536da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian            // be lots of scary messages in the log just before this.
3546da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian            VDS_LOGE("dequeueBuffer: no buffer, bailing out");
3556da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian            return NO_MEMORY;
3566da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian        }
3576da15f46f5f8b38e31384d641f8d3db2c3c6ea30Mathias Agopian
358028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        // We already dequeued the output buffer. If the GLES driver wants
359028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        // something incompatible, we have to cancel and get a new one. This
360028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        // will mean that HWC will see a different output buffer between
361028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        // prepare and set, but since we're in GLES-only mode already it
362028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        // shouldn't matter.
363028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall
3641e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        usage |= GRALLOC_USAGE_HW_COMPOSER;
365028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
3661e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        if ((usage & ~buf->getUsage()) != 0 ||
367028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall                (format != 0 && format != (uint32_t)buf->getPixelFormat()) ||
368028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall                (w != 0 && w != mSinkBufferWidth) ||
369028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall                (h != 0 && h != mSinkBufferHeight)) {
3701e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall            VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
3711e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall                    "want %dx%d fmt=%d use=%#x, "
3721e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall                    "have %dx%d fmt=%d use=%#x",
3731e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall                    w, h, format, usage,
3741e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall                    mSinkBufferWidth, mSinkBufferHeight,
3751e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall                    buf->getPixelFormat(), buf->getUsage());
3761e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall            mOutputFormat = format;
3771e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall            mOutputUsage = usage;
378028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall            result = refreshOutputBuffer();
379028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall            if (result < 0)
380028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall                return result;
381028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        }
38238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
38338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
384028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    if (source == SOURCE_SINK) {
385028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        *pslot = mOutputProducerSlot;
386028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        *fence = mOutputFence;
387028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    } else {
388028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        int sslot;
3891e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall        result = dequeueBuffer(source, format, usage, &sslot, fence);
390028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        if (result >= 0) {
391028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall            *pslot = mapSource2ProducerSlot(source, sslot);
392028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        }
39338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
39438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return result;
39538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
39638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
39788a459a9b37f3b4eb3c9a044ad7af30616ad66c9Dan Stozastatus_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
39888a459a9b37f3b4eb3c9a044ad7af30616ad66c9Dan Stoza    VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
39988a459a9b37f3b4eb3c9a044ad7af30616ad66c9Dan Stoza    return INVALID_OPERATION;
4009f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza}
4019f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza
402d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stozastatus_t VirtualDisplaySurface::detachNextBuffer(
403d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stoza        sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
404d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stoza    VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
405d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stoza    return INVALID_OPERATION;
406d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stoza}
407d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stoza
40888a459a9b37f3b4eb3c9a044ad7af30616ad66c9Dan Stozastatus_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
40988a459a9b37f3b4eb3c9a044ad7af30616ad66c9Dan Stoza        const sp<GraphicBuffer>& /* buffer */) {
41088a459a9b37f3b4eb3c9a044ad7af30616ad66c9Dan Stoza    VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
41188a459a9b37f3b4eb3c9a044ad7af30616ad66c9Dan Stoza    return INVALID_OPERATION;
4129f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza}
4139f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza
41438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallstatus_t VirtualDisplaySurface::queueBuffer(int pslot,
41538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        const QueueBufferInput& input, QueueBufferOutput* output) {
41647e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    if (mDisplayId < 0)
41747e45405d1533aa73307014f7bf371e118695cf3Michael Lentine        return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
41847e45405d1533aa73307014f7bf371e118695cf3Michael Lentine
41938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
42038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
42138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            dbgStateStr());
42238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mDbgState = DBG_STATE_GLES_DONE;
42338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
42438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGV("queueBuffer pslot=%d", pslot);
42538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
42638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    status_t result;
42738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (mCompositionType == COMPOSITION_MIXED) {
42838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // Queue the buffer back into the scratch pool
42938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        QueueBufferOutput scratchQBO;
43038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
431db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian        result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
43238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        if (result != NO_ERROR)
43338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            return result;
43438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
43538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // Now acquire the buffer from the scratch pool -- should be the same
43638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // slot and fence as we just queued.
43738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        Mutex::Autolock lock(mMutex);
43838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        BufferQueue::BufferItem item;
439bce7611e8e4514026bf12f96baaf9168f6d5894eJesse Hall        result = acquireBufferLocked(&item, 0);
44038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        if (result != NO_ERROR)
44138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            return result;
44238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        VDS_LOGW_IF(item.mBuf != sslot,
44338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
44438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                item.mBuf, sslot);
44538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf);
44638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        mFbFence = mSlots[item.mBuf].mFence;
44738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
44838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    } else {
44938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
45038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                "Unexpected queueBuffer in state %s for compositionType %s",
45138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall                dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
45238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
45338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        // Extract the GLES release fence for HWC to acquire
45438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        int64_t timestamp;
4553c25621ad7d13f64d3ab95a27fa970fbc9998f73Andy McFadden        bool isAutoTimestamp;
45638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        Rect crop;
45738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        int scalingMode;
45838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        uint32_t transform;
4597cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian        bool async;
4603c25621ad7d13f64d3ab95a27fa970fbc9998f73Andy McFadden        input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode,
4613c25621ad7d13f64d3ab95a27fa970fbc9998f73Andy McFadden                &transform, &async, &mFbFence);
46238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
46338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        mFbProducerSlot = pslot;
464028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        mOutputFence = mFbFence;
46538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
46638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
46738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    *output = mQueueBufferOutput;
46838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return NO_ERROR;
46938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
47038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
47138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallvoid VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) {
47247e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    if (mDisplayId < 0)
473fd9d183cd8c859ab8480dea572430398438ba8b5Michael Lentine        return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
47447e45405d1533aa73307014f7bf371e118695cf3Michael Lentine
47538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
47638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
47738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            dbgStateStr());
47838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    VDS_LOGV("cancelBuffer pslot=%d", pslot);
47938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    Source source = fbSourceForCompositionType(mCompositionType);
48038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return mSource[source]->cancelBuffer(
48138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            mapProducer2SourceSlot(source, pslot), fence);
48238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
48338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
48438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallint VirtualDisplaySurface::query(int what, int* value) {
48547e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    switch (what) {
48647e45405d1533aa73307014f7bf371e118695cf3Michael Lentine        case NATIVE_WINDOW_WIDTH:
48747e45405d1533aa73307014f7bf371e118695cf3Michael Lentine            *value = mSinkBufferWidth;
48847e45405d1533aa73307014f7bf371e118695cf3Michael Lentine            break;
48947e45405d1533aa73307014f7bf371e118695cf3Michael Lentine        case NATIVE_WINDOW_HEIGHT:
49047e45405d1533aa73307014f7bf371e118695cf3Michael Lentine            *value = mSinkBufferHeight;
49147e45405d1533aa73307014f7bf371e118695cf3Michael Lentine            break;
49247e45405d1533aa73307014f7bf371e118695cf3Michael Lentine        default:
49347e45405d1533aa73307014f7bf371e118695cf3Michael Lentine            return mSource[SOURCE_SINK]->query(what, value);
49447e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    }
49547e45405d1533aa73307014f7bf371e118695cf3Michael Lentine    return NO_ERROR;
49638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
49738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
498f0eaf25e9247edf4d124bedaeb863f7abdf35a3eDan Stozastatus_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
499365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian        int api, bool producerControlledByApp,
500595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian        QueueBufferOutput* output) {
50138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    QueueBufferOutput qbo;
502f0eaf25e9247edf4d124bedaeb863f7abdf35a3eDan Stoza    status_t result = mSource[SOURCE_SINK]->connect(listener, api,
503f0eaf25e9247edf4d124bedaeb863f7abdf35a3eDan Stoza            producerControlledByApp, &qbo);
50438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (result == NO_ERROR) {
50538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        updateQueueBufferOutput(qbo);
50638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        *output = mQueueBufferOutput;
50738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
50838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return result;
50938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
51038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
51138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallstatus_t VirtualDisplaySurface::disconnect(int api) {
51238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return mSource[SOURCE_SINK]->disconnect(api);
51338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
51438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
515399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hallstatus_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
516399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    return INVALID_OPERATION;
517399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall}
518399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall
51929a3e90879fd96404c971e7187cd0e05927bbce0Dan Stozavoid VirtualDisplaySurface::allocateBuffers(bool /* async */,
52029a3e90879fd96404c971e7187cd0e05927bbce0Dan Stoza        uint32_t /* width */, uint32_t /* height */, uint32_t /* format */,
52129a3e90879fd96404c971e7187cd0e05927bbce0Dan Stoza        uint32_t /* usage */) {
52229a3e90879fd96404c971e7187cd0e05927bbce0Dan Stoza    // TODO: Should we actually allocate buffers for a virtual display?
52329a3e90879fd96404c971e7187cd0e05927bbce0Dan Stoza}
52429a3e90879fd96404c971e7187cd0e05927bbce0Dan Stoza
52538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallvoid VirtualDisplaySurface::updateQueueBufferOutput(
52638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        const QueueBufferOutput& qbo) {
52738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    uint32_t w, h, transformHint, numPendingBuffers;
52838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
52938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
53038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
53138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
53238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallvoid VirtualDisplaySurface::resetPerFrameState() {
53338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mCompositionType = COMPOSITION_UNKNOWN;
534b988f85fc44efd24f11f2e5ab8fb9b653916f666mayank parshar    mFbFence = Fence::NO_FENCE;
535028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    mOutputFence = Fence::NO_FENCE;
53638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    mOutputProducerSlot = -1;
537fdfde88d0bcd0b293c9deb37f49254bdd97e4dacmayank parshar    mFbProducerSlot = -1;
53838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
53938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
540028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hallstatus_t VirtualDisplaySurface::refreshOutputBuffer() {
541028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    if (mOutputProducerSlot >= 0) {
542028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        mSource[SOURCE_SINK]->cancelBuffer(
543028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall                mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
544028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall                mOutputFence);
545028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    }
546028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall
547028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    int sslot;
5481e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall    status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
5491e27ba234c5bfd4f6535ee221a6c20407a50f868Jesse Hall            &sslot, &mOutputFence);
550028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    if (result < 0)
551028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall        return result;
552028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
553028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall
554b716e57048e945d5bceda33c2434c65d06464dbaJesse Hall    // On GLES-only frames, we don't have the right output buffer acquire fence
555b716e57048e945d5bceda33c2434c65d06464dbaJesse Hall    // until after GLES calls queueBuffer(). So here we just set the buffer
556b716e57048e945d5bceda33c2434c65d06464dbaJesse Hall    // (for use in HWC prepare) but not the fence; we'll call this again with
557b716e57048e945d5bceda33c2434c65d06464dbaJesse Hall    // the proper fence once we have it.
558b716e57048e945d5bceda33c2434c65d06464dbaJesse Hall    result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
559028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall            mProducerBuffers[mOutputProducerSlot]);
560028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall
561028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall    return result;
562028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall}
563028dc8f2d72bc7cd4fbe7808781443125a742f78Jesse Hall
56438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall// This slot mapping function is its own inverse, so two copies are unnecessary.
56538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall// Both are kept to make the intent clear where the function is called, and for
56638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall// the (unlikely) chance that we switch to a different mapping function.
56738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallint VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
56838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    if (source == SOURCE_SCRATCH) {
56938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
57038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    } else {
57138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        return sslot;
57238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
57338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
57438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallint VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
57538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return mapSource2ProducerSlot(source, pslot);
57638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
57738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
57838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse HallVirtualDisplaySurface::Source
57938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse HallVirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
58038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
58138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
58238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
58338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallconst char* VirtualDisplaySurface::dbgStateStr() const {
58438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    switch (mDbgState) {
58538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case DBG_STATE_IDLE:      return "IDLE";
58638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case DBG_STATE_PREPARED:  return "PREPARED";
58738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case DBG_STATE_GLES:      return "GLES";
58838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case DBG_STATE_GLES_DONE: return "GLES_DONE";
58938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case DBG_STATE_HWC:       return "HWC";
59038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        default:                  return "INVALID";
59138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
59238efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
59338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
59438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hallconst char* VirtualDisplaySurface::dbgSourceStr(Source s) {
59538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    switch (s) {
59638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case SOURCE_SINK:    return "SINK";
59738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        case SOURCE_SCRATCH: return "SCRATCH";
59838efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        default:             return "INVALID";
59938efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall    }
60038efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall}
60138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall
60299c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall// ---------------------------------------------------------------------------
60399c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall} // namespace android
60499c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall// ---------------------------------------------------------------------------
605