blob: 0e639cf81b6b0cb7b21c2f76b9b46a8fb44dc4d2 [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>
19#include <genlock.h>
20
Naseer Ahmed29a26812012-06-14 00:56:20 -070021// -----------------------------------------------------------------------------
22// QueuedBufferStore
23//This class holds currently and previously queued buffers.
24//Provides utilities to store, lock, remove, unlock.
25
26namespace qhwc{
27static const int MAX_QUEUED_BUFS = 4;
28class QueuedBufferStore {
29 public:
30 QueuedBufferStore() {
31 clearCurrent();
32 clearPrevious();
33 }
34 ~QueuedBufferStore() {}
35 void lockAndAdd(private_handle_t*);
Saurabh Shah83523d82012-08-16 10:19:07 -070036 //Unlocks only previous and makes the current as previous
Naseer Ahmed29a26812012-06-14 00:56:20 -070037 void unlockAllPrevious();
Saurabh Shah83523d82012-08-16 10:19:07 -070038 //Unlocks previous as well as current, useful in suspend case
39 void unlockAll();
Naseer Ahmed29a26812012-06-14 00:56:20 -070040
41 private:
42 QueuedBufferStore& operator=(const QueuedBufferStore&);
43 QueuedBufferStore(const QueuedBufferStore&);
44 bool lockBuffer(private_handle_t *hnd);
45 void unlockBuffer(private_handle_t *hnd);
46 void clearCurrent();
47 void clearPrevious();
48 void mvCurrToPrev();
49
50 //members
51 private_handle_t *current[MAX_QUEUED_BUFS]; //holds buf being queued
52 private_handle_t *previous[MAX_QUEUED_BUFS]; //holds bufs queued in prev round
53 int curCount;
54 int prevCount;
55};
56
57//Store and lock current drawing round buffers
58inline void QueuedBufferStore::lockAndAdd(private_handle_t *hnd) {
59 if(lockBuffer(hnd))
60 current[curCount++] = hnd;
61}
62
63//Unlock all previous drawing round buffers
64inline void QueuedBufferStore::unlockAllPrevious() {
65 //Unlock
66 for(int i = 0; i < prevCount; i++) {
67 unlockBuffer(previous[i]);
68 previous[i] = NULL;
69 }
70 //Move current hnd to previous
71 mvCurrToPrev();
72 //Clear current
73 clearCurrent();
74}
75
Saurabh Shah83523d82012-08-16 10:19:07 -070076inline void QueuedBufferStore::unlockAll() {
77 //Unlocks prev and moves current to prev
78 unlockAllPrevious();
79 //Unlocks the newly populated prev if any.
80 unlockAllPrevious();
81}
82
Naseer Ahmed29a26812012-06-14 00:56:20 -070083//Clear currentbuf store
84inline void QueuedBufferStore::clearCurrent() {
85 for(int i = 0; i < MAX_QUEUED_BUFS; i++)
86 current[i] = NULL;
87 curCount = 0;
88}
89
90//Clear previousbuf store
91inline void QueuedBufferStore::clearPrevious() {
92 for(int i = 0; i < MAX_QUEUED_BUFS; i++)
93 previous[i] = NULL;
94 prevCount = 0;
95}
96
97//Copy from current to previous
98inline void QueuedBufferStore::mvCurrToPrev() {
99 for(int i = 0; i < curCount; i++)
100 previous[i] = current[i];
101 prevCount = curCount;
102}
103
104inline bool QueuedBufferStore::lockBuffer(private_handle_t *hnd) {
105 if (GENLOCK_FAILURE == genlock_lock_buffer(hnd, GENLOCK_READ_LOCK,
106 GENLOCK_MAX_TIMEOUT)) {
107 ALOGE("%s: genlock_lock_buffer(READ) failed", __func__);
108 return false;
109 }
110 return true;
111}
112
113inline void QueuedBufferStore::unlockBuffer(private_handle_t *hnd) {
114 //Check if buffer is still around
115 if(private_handle_t::validate(hnd) != 0) {
116 ALOGE("%s Invalid Handle", __func__);
117 return;
118 }
119 //Actually try to unlock
120 if (GENLOCK_FAILURE == genlock_unlock_buffer(hnd)) {
121 ALOGE("%s: genlock_unlock_buffer failed", __func__);
122 return;
123 }
124}
125// -----------------------------------------------------------------------------
126};//namespace
127