blob: 433e1eba9241711c69bdc118bc9e0856036ba76f [file] [log] [blame]
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001/*
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
Jesse Hall99c7dbb2013-03-14 14:29:29 -070017#include "VirtualDisplaySurface.h"
Jesse Hall80e0a392013-03-15 12:32:10 -070018#include "HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070019
20// ---------------------------------------------------------------------------
21namespace android {
22// ---------------------------------------------------------------------------
23
24VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int disp,
25 const sp<IGraphicBufferProducer>& sink, const String8& name)
26: mHwc(hwc),
27 mDisplayId(disp),
Jesse Hall80e0a392013-03-15 12:32:10 -070028 mSource(new BufferQueueInterposer(sink, name)),
29 mName(name),
30 mReleaseFence(Fence::NO_FENCE)
Jesse Hall99c7dbb2013-03-14 14:29:29 -070031{}
32
33VirtualDisplaySurface::~VirtualDisplaySurface() {
Jesse Hall80e0a392013-03-15 12:32:10 -070034 if (mAcquiredBuffer != NULL) {
35 status_t result = mSource->releaseBuffer(mReleaseFence);
36 ALOGE_IF(result != NO_ERROR, "VirtualDisplaySurface \"%s\": "
37 "failed to release previous buffer: %d",
38 mName.string(), result);
39 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -070040}
41
42sp<IGraphicBufferProducer> VirtualDisplaySurface::getIGraphicBufferProducer() const {
Jesse Hall80e0a392013-03-15 12:32:10 -070043 return mSource;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070044}
45
46status_t VirtualDisplaySurface::compositionComplete() {
47 return NO_ERROR;
48}
49
50status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall80e0a392013-03-15 12:32:10 -070051 Mutex::Autolock lock(mMutex);
52 status_t result = NO_ERROR;
53
54 if (mAcquiredBuffer != NULL) {
55 result = mSource->releaseBuffer(mReleaseFence);
56 ALOGE_IF(result != NO_ERROR, "VirtualDisplaySurface \"%s\": "
57 "failed to release previous buffer: %d",
58 mName.string(), result);
59 mAcquiredBuffer.clear();
60 mReleaseFence = Fence::NO_FENCE;
61 }
62
63 sp<Fence> fence;
64 result = mSource->acquireBuffer(&mAcquiredBuffer, &fence);
65 if (result == BufferQueueInterposer::NO_BUFFER_AVAILABLE) {
66 result = mSource->pullEmptyBuffer();
67 if (result != NO_ERROR)
68 return result;
69 result = mSource->acquireBuffer(&mAcquiredBuffer, &fence);
70 }
71 if (result != NO_ERROR)
72 return result;
73
74 return mHwc.fbPost(mDisplayId, fence, mAcquiredBuffer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070075}
76
77status_t VirtualDisplaySurface::setReleaseFenceFd(int fenceFd) {
78 if (fenceFd >= 0) {
Jesse Hall80e0a392013-03-15 12:32:10 -070079 sp<Fence> fence(new Fence(fenceFd));
80 Mutex::Autolock lock(mMutex);
81 sp<Fence> mergedFence = Fence::merge(
82 String8::format("VirtualDisplaySurface \"%s\"",
83 mName.string()),
84 mReleaseFence, fence);
85 if (!mergedFence->isValid()) {
86 ALOGE("VirtualDisplaySurface \"%s\": failed to merge release fence",
87 mName.string());
88 // synchronization is broken, the best we can do is hope fences
89 // signal in order so the new fence will act like a union
90 mReleaseFence = fence;
91 return BAD_VALUE;
92 }
93 mReleaseFence = mergedFence;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070094 }
95 return NO_ERROR;
96}
97
98void VirtualDisplaySurface::dump(String8& result) const {
99}
100
101// ---------------------------------------------------------------------------
102} // namespace android
103// ---------------------------------------------------------------------------