VirtualDisplaySurface.cpp revision ffe1f19ca9707f84cb9fdb06209bf36cd8c2ef0a
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    return mHwc.fbPost(mDisplayId, fence, mAcquiredBuffer);
80}
81
82void VirtualDisplaySurface::onFrameCommitted() {
83    if (mInterposer == NULL)
84        return;
85
86    Mutex::Autolock lock(mMutex);
87    if (mAcquiredBuffer != NULL) {
88        // fbFence signals when reads from the framebuffer are finished
89        // outFence signals when writes to the output buffer are finished
90        // It's unlikely that there will be an implementation where fbFence
91        // signals after outFence (in fact they'll typically be the same
92        // sync_pt), but just to be pedantic we merge them so the sink will
93        // be sure to wait until both are complete.
94        sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
95        sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
96        sp<Fence> fence = Fence::merge(
97                String8::format("HWC done: %.21s", mName.string()),
98                fbFence, outFence);
99
100        status_t result = mInterposer->releaseBuffer(fence);
101        ALOGE_IF(result != NO_ERROR, "VirtualDisplaySurface \"%s\": "
102                "failed to release buffer: %d", mName.string(), result);
103        mAcquiredBuffer.clear();
104    }
105}
106
107void VirtualDisplaySurface::dump(String8& result) const {
108}
109
110// ---------------------------------------------------------------------------
111} // namespace android
112// ---------------------------------------------------------------------------
113