blob: 7e14d955a73fa8017258f2ae36c99ae5d0adef33 [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
Jesse Hall2ba647e2013-04-04 12:59:37 -070079 result = mHwc.fbPost(mDisplayId, fence, mAcquiredBuffer);
80 if (result == NO_ERROR) {
81 result = mHwc.setOutputBuffer(mDisplayId, fence, mAcquiredBuffer);
82 }
83 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070084}
85
Jesse Hall851cfe82013-03-20 13:44:00 -070086void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hallffe1f192013-03-22 15:13:48 -070087 if (mInterposer == NULL)
88 return;
89
Jesse Hall74149652013-03-19 17:18:09 -070090 Mutex::Autolock lock(mMutex);
Jesse Hall74149652013-03-19 17:18:09 -070091 if (mAcquiredBuffer != NULL) {
Jesse Hall851cfe82013-03-20 13:44:00 -070092 // 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
Jesse Hallffe1f192013-03-22 15:13:48 -0700104 status_t result = mInterposer->releaseBuffer(fence);
Jesse Hall74149652013-03-19 17:18:09 -0700105 ALOGE_IF(result != NO_ERROR, "VirtualDisplaySurface \"%s\": "
106 "failed to release buffer: %d", mName.string(), result);
107 mAcquiredBuffer.clear();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700108 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700109}
110
111void VirtualDisplaySurface::dump(String8& result) const {
112}
113
114// ---------------------------------------------------------------------------
115} // namespace android
116// ---------------------------------------------------------------------------