blob: 6d75964b6624cd57c98a04125f29618f4f6da8a6 [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*);
36 void unlockAllPrevious();
37
38 private:
39 QueuedBufferStore& operator=(const QueuedBufferStore&);
40 QueuedBufferStore(const QueuedBufferStore&);
41 bool lockBuffer(private_handle_t *hnd);
42 void unlockBuffer(private_handle_t *hnd);
43 void clearCurrent();
44 void clearPrevious();
45 void mvCurrToPrev();
46
47 //members
48 private_handle_t *current[MAX_QUEUED_BUFS]; //holds buf being queued
49 private_handle_t *previous[MAX_QUEUED_BUFS]; //holds bufs queued in prev round
50 int curCount;
51 int prevCount;
52};
53
54//Store and lock current drawing round buffers
55inline void QueuedBufferStore::lockAndAdd(private_handle_t *hnd) {
56 if(lockBuffer(hnd))
57 current[curCount++] = hnd;
58}
59
60//Unlock all previous drawing round buffers
61inline void QueuedBufferStore::unlockAllPrevious() {
62 //Unlock
63 for(int i = 0; i < prevCount; i++) {
64 unlockBuffer(previous[i]);
65 previous[i] = NULL;
66 }
67 //Move current hnd to previous
68 mvCurrToPrev();
69 //Clear current
70 clearCurrent();
71}
72
73//Clear currentbuf store
74inline void QueuedBufferStore::clearCurrent() {
75 for(int i = 0; i < MAX_QUEUED_BUFS; i++)
76 current[i] = NULL;
77 curCount = 0;
78}
79
80//Clear previousbuf store
81inline void QueuedBufferStore::clearPrevious() {
82 for(int i = 0; i < MAX_QUEUED_BUFS; i++)
83 previous[i] = NULL;
84 prevCount = 0;
85}
86
87//Copy from current to previous
88inline void QueuedBufferStore::mvCurrToPrev() {
89 for(int i = 0; i < curCount; i++)
90 previous[i] = current[i];
91 prevCount = curCount;
92}
93
94inline bool QueuedBufferStore::lockBuffer(private_handle_t *hnd) {
95 if (GENLOCK_FAILURE == genlock_lock_buffer(hnd, GENLOCK_READ_LOCK,
96 GENLOCK_MAX_TIMEOUT)) {
97 ALOGE("%s: genlock_lock_buffer(READ) failed", __func__);
98 return false;
99 }
100 return true;
101}
102
103inline void QueuedBufferStore::unlockBuffer(private_handle_t *hnd) {
104 //Check if buffer is still around
105 if(private_handle_t::validate(hnd) != 0) {
106 ALOGE("%s Invalid Handle", __func__);
107 return;
108 }
109 //Actually try to unlock
110 if (GENLOCK_FAILURE == genlock_unlock_buffer(hnd)) {
111 ALOGE("%s: genlock_unlock_buffer failed", __func__);
112 return;
113 }
114}
115// -----------------------------------------------------------------------------
116};//namespace
117