blob: 90f7143fda1efe89aadfdcd467576e190e35ec3b [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, Code Aurora Forum. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Naseer Ahmed72cf9762012-07-21 12:17:13 -070018#include <gralloc_priv.h>
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070019
20#ifndef USE_FENCE_SYNC
Naseer Ahmed72cf9762012-07-21 12:17:13 -070021#include <genlock.h>
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070022#endif
Naseer Ahmed72cf9762012-07-21 12:17:13 -070023
Naseer Ahmed29a26812012-06-14 00:56:20 -070024// -----------------------------------------------------------------------------
25// QueuedBufferStore
26//This class holds currently and previously queued buffers.
27//Provides utilities to store, lock, remove, unlock.
28
29namespace qhwc{
30static const int MAX_QUEUED_BUFS = 4;
31class QueuedBufferStore {
32 public:
33 QueuedBufferStore() {
34 clearCurrent();
35 clearPrevious();
36 }
37 ~QueuedBufferStore() {}
38 void lockAndAdd(private_handle_t*);
Saurabh Shah83523d82012-08-16 10:19:07 -070039 //Unlocks only previous and makes the current as previous
Naseer Ahmed29a26812012-06-14 00:56:20 -070040 void unlockAllPrevious();
Saurabh Shah83523d82012-08-16 10:19:07 -070041 //Unlocks previous as well as current, useful in suspend case
42 void unlockAll();
Naseer Ahmed29a26812012-06-14 00:56:20 -070043
44 private:
45 QueuedBufferStore& operator=(const QueuedBufferStore&);
46 QueuedBufferStore(const QueuedBufferStore&);
47 bool lockBuffer(private_handle_t *hnd);
48 void unlockBuffer(private_handle_t *hnd);
49 void clearCurrent();
50 void clearPrevious();
51 void mvCurrToPrev();
52
53 //members
54 private_handle_t *current[MAX_QUEUED_BUFS]; //holds buf being queued
55 private_handle_t *previous[MAX_QUEUED_BUFS]; //holds bufs queued in prev round
56 int curCount;
57 int prevCount;
58};
59
60//Store and lock current drawing round buffers
61inline void QueuedBufferStore::lockAndAdd(private_handle_t *hnd) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070062#ifndef USE_FENCE_SYNC
Naseer Ahmed29a26812012-06-14 00:56:20 -070063 if(lockBuffer(hnd))
64 current[curCount++] = hnd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070065#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -070066}
67
68//Unlock all previous drawing round buffers
69inline void QueuedBufferStore::unlockAllPrevious() {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070070#ifndef USE_FENCE_SYNC
Naseer Ahmed29a26812012-06-14 00:56:20 -070071 //Unlock
72 for(int i = 0; i < prevCount; i++) {
73 unlockBuffer(previous[i]);
74 previous[i] = NULL;
75 }
76 //Move current hnd to previous
77 mvCurrToPrev();
78 //Clear current
79 clearCurrent();
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070080#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -070081}
82
Saurabh Shah83523d82012-08-16 10:19:07 -070083inline void QueuedBufferStore::unlockAll() {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070084#ifndef USE_FENCE_SYNC
Saurabh Shah83523d82012-08-16 10:19:07 -070085 //Unlocks prev and moves current to prev
86 unlockAllPrevious();
87 //Unlocks the newly populated prev if any.
88 unlockAllPrevious();
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070089#endif
Saurabh Shah83523d82012-08-16 10:19:07 -070090}
91
Naseer Ahmed29a26812012-06-14 00:56:20 -070092//Clear currentbuf store
93inline void QueuedBufferStore::clearCurrent() {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070094#ifndef USE_FENCE_SYNC
Naseer Ahmed29a26812012-06-14 00:56:20 -070095 for(int i = 0; i < MAX_QUEUED_BUFS; i++)
96 current[i] = NULL;
97 curCount = 0;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -070098#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -070099}
100
101//Clear previousbuf store
102inline void QueuedBufferStore::clearPrevious() {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700103#ifndef USE_FENCE_SYNC
Naseer Ahmed29a26812012-06-14 00:56:20 -0700104 for(int i = 0; i < MAX_QUEUED_BUFS; i++)
105 previous[i] = NULL;
106 prevCount = 0;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700107#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700108}
109
110//Copy from current to previous
111inline void QueuedBufferStore::mvCurrToPrev() {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700112#ifndef USE_FENCE_SYNC
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113 for(int i = 0; i < curCount; i++)
114 previous[i] = current[i];
115 prevCount = curCount;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700116#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700117}
118
119inline bool QueuedBufferStore::lockBuffer(private_handle_t *hnd) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700120#ifndef USE_FENCE_SYNC
Naseer Ahmed29a26812012-06-14 00:56:20 -0700121 if (GENLOCK_FAILURE == genlock_lock_buffer(hnd, GENLOCK_READ_LOCK,
122 GENLOCK_MAX_TIMEOUT)) {
123 ALOGE("%s: genlock_lock_buffer(READ) failed", __func__);
124 return false;
125 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700126#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700127 return true;
128}
129
130inline void QueuedBufferStore::unlockBuffer(private_handle_t *hnd) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700131#ifndef USE_FENCE_SYNC
Naseer Ahmed29a26812012-06-14 00:56:20 -0700132 //Check if buffer is still around
133 if(private_handle_t::validate(hnd) != 0) {
134 ALOGE("%s Invalid Handle", __func__);
135 return;
136 }
137 //Actually try to unlock
138 if (GENLOCK_FAILURE == genlock_unlock_buffer(hnd)) {
139 ALOGE("%s: genlock_unlock_buffer failed", __func__);
140 return;
141 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700142#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700143}
144// -----------------------------------------------------------------------------
145};//namespace
146