VirtualDisplaySurface.cpp revision 2ba647e9f5249d9ed97739d29d879064e31ba34a
1/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "VirtualDisplaySurface.h"
18#include "HWComposer.h"
19
20// ---------------------------------------------------------------------------
21namespace android {
22// ---------------------------------------------------------------------------
23
24VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
25        const sp<IGraphicBufferProducer>& sink, const String8& name)
26:   mHwc(hwc),
27    mDisplayId(dispId),
28    mName(name)
29{
30    if (mDisplayId >= 0) {
31        mInterposer = new BufferQueueInterposer(sink, name);
32        mSourceProducer = mInterposer;
33    } else {
34        mSourceProducer = sink;
35    }
36}
37
38VirtualDisplaySurface::~VirtualDisplaySurface() {
39    if (mAcquiredBuffer != NULL) {
40        status_t result = mInterposer->releaseBuffer(Fence::NO_FENCE);
41        ALOGE_IF(result != NO_ERROR, "VirtualDisplaySurface \"%s\": "
42                "failed to release buffer: %d", mName.string(), result);
43    }
44}
45
46sp<IGraphicBufferProducer> VirtualDisplaySurface::getIGraphicBufferProducer() const {
47    return mSourceProducer;
48}
49
50status_t VirtualDisplaySurface::compositionComplete() {
51    return NO_ERROR;
52}
53
54status_t VirtualDisplaySurface::advanceFrame() {
55    if (mInterposer == NULL)
56        return NO_ERROR;
57
58    Mutex::Autolock lock(mMutex);
59    status_t result = NO_ERROR;
60
61    if (mAcquiredBuffer != NULL) {
62        ALOGE("VirtualDisplaySurface \"%s\": "
63                "advanceFrame called twice without onFrameCommitted",
64                mName.string());
65        return INVALID_OPERATION;
66    }
67
68    sp<Fence> fence;
69    result = mInterposer->acquireBuffer(&mAcquiredBuffer, &fence);
70    if (result == BufferQueueInterposer::NO_BUFFER_AVAILABLE) {
71        result = mInterposer->pullEmptyBuffer();
72        if (result != NO_ERROR)
73            return result;
74        result = mInterposer->acquireBuffer(&mAcquiredBuffer, &fence);
75    }
76    if (result != NO_ERROR)
77        return result;
78
79    result = mHwc.fbPost(mDisplayId, fence, mAcquiredBuffer);
80    if (result == NO_ERROR) {
81        result = mHwc.setOutputBuffer(mDisplayId, fence, mAcquiredBuffer);
82    }
83    return result;
84}
85
86void VirtualDisplaySurface::onFrameCommitted() {
87    if (mInterposer == NULL)
88        return;
89
90    Mutex::Autolock lock(mMutex);
91    if (mAcquiredBuffer != NULL) {
92        // fbFence signals when reads from the framebuffer are finished
93        // outFence signals when writes to the output buffer are finished
94        // It's unlikely that there will be an implementation where fbFence
95        // signals after outFence (in fact they'll typically be the same
96        // sync_pt), but just to be pedantic we merge them so the sink will
97        // be sure to wait until both are complete.
98        sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
99        sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
100        sp<Fence> fence = Fence::merge(
101                String8::format("HWC done: %.21s", mName.string()),
102                fbFence, outFence);
103
104        status_t result = mInterposer->releaseBuffer(fence);
105        ALOGE_IF(result != NO_ERROR, "VirtualDisplaySurface \"%s\": "
106                "failed to release buffer: %d", mName.string(), result);
107        mAcquiredBuffer.clear();
108    }
109}
110
111void VirtualDisplaySurface::dump(String8& result) const {
112}
113
114// ---------------------------------------------------------------------------
115} // namespace android
116// ---------------------------------------------------------------------------
117