blob: d2b3edb58b992df311c3490ca7ae5e3cf28bee24 [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
Jesse Hallffe1f192013-03-22 15:13:48 -070024VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070025 const sp<IGraphicBufferProducer>& sink, const String8& name)
26: mHwc(hwc),
Jesse Hallffe1f192013-03-22 15:13:48 -070027 mDisplayId(dispId),
Jesse Hall74149652013-03-19 17:18:09 -070028 mName(name)
Jesse Hallffe1f192013-03-22 15:13:48 -070029{
30 if (mDisplayId >= 0) {
31 mInterposer = new BufferQueueInterposer(sink, name);
32 mSourceProducer = mInterposer;
33 } else {
34 mSourceProducer = sink;
35 }
36}
Jesse Hall99c7dbb2013-03-14 14:29:29 -070037
38VirtualDisplaySurface::~VirtualDisplaySurface() {
Jesse Hall80e0a392013-03-15 12:32:10 -070039 if (mAcquiredBuffer != NULL) {
Jesse Hallffe1f192013-03-22 15:13:48 -070040 status_t result = mInterposer->releaseBuffer(Fence::NO_FENCE);
Jesse Hall80e0a392013-03-15 12:32:10 -070041 ALOGE_IF(result != NO_ERROR, "VirtualDisplaySurface \"%s\": "
Jesse Hall74149652013-03-19 17:18:09 -070042 "failed to release buffer: %d", mName.string(), result);
Jesse Hall80e0a392013-03-15 12:32:10 -070043 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -070044}
45
46sp<IGraphicBufferProducer> VirtualDisplaySurface::getIGraphicBufferProducer() const {
Jesse Hallffe1f192013-03-22 15:13:48 -070047 return mSourceProducer;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070048}
49
50status_t VirtualDisplaySurface::compositionComplete() {
51 return NO_ERROR;
52}
53
54status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hallffe1f192013-03-22 15:13:48 -070055 if (mInterposer == NULL)
56 return NO_ERROR;
57
Jesse Hall80e0a392013-03-15 12:32:10 -070058 Mutex::Autolock lock(mMutex);
59 status_t result = NO_ERROR;
60
61 if (mAcquiredBuffer != NULL) {
Jesse Hall74149652013-03-19 17:18:09 -070062 ALOGE("VirtualDisplaySurface \"%s\": "
63 "advanceFrame called twice without onFrameCommitted",
64 mName.string());
65 return INVALID_OPERATION;
Jesse Hall80e0a392013-03-15 12:32:10 -070066 }
67
68 sp<Fence> fence;
Jesse Hallffe1f192013-03-22 15:13:48 -070069 result = mInterposer->acquireBuffer(&mAcquiredBuffer, &fence);
Jesse Hall80e0a392013-03-15 12:32:10 -070070 if (result == BufferQueueInterposer::NO_BUFFER_AVAILABLE) {
Jesse Hallffe1f192013-03-22 15:13:48 -070071 result = mInterposer->pullEmptyBuffer();
Jesse Hall80e0a392013-03-15 12:32:10 -070072 if (result != NO_ERROR)
73 return result;
Jesse Hallffe1f192013-03-22 15:13:48 -070074 result = mInterposer->acquireBuffer(&mAcquiredBuffer, &fence);
Jesse Hall80e0a392013-03-15 12:32:10 -070075 }
76 if (result != NO_ERROR)
77 return result;
78
79 return mHwc.fbPost(mDisplayId, fence, mAcquiredBuffer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070080}
81
Jesse Hall851cfe82013-03-20 13:44:00 -070082void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hallffe1f192013-03-22 15:13:48 -070083 if (mInterposer == NULL)
84 return;
85
Jesse Hall74149652013-03-19 17:18:09 -070086 Mutex::Autolock lock(mMutex);
Jesse Hall74149652013-03-19 17:18:09 -070087 if (mAcquiredBuffer != NULL) {
Jesse Hall851cfe82013-03-20 13:44:00 -070088 // 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
Jesse Hallffe1f192013-03-22 15:13:48 -0700100 status_t result = mInterposer->releaseBuffer(fence);
Jesse Hall74149652013-03-19 17:18:09 -0700101 ALOGE_IF(result != NO_ERROR, "VirtualDisplaySurface \"%s\": "
102 "failed to release buffer: %d", mName.string(), result);
103 mAcquiredBuffer.clear();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700104 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700105}
106
107void VirtualDisplaySurface::dump(String8& result) const {
108}
109
110// ---------------------------------------------------------------------------
111} // namespace android
112// ---------------------------------------------------------------------------