Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 1 | /* |
| 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 Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 18 | #include <gralloc_priv.h> |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 19 | |
| 20 | #ifndef USE_FENCE_SYNC |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 21 | #include <genlock.h> |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 22 | #endif |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 23 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 24 | // ----------------------------------------------------------------------------- |
| 25 | // QueuedBufferStore |
| 26 | //This class holds currently and previously queued buffers. |
| 27 | //Provides utilities to store, lock, remove, unlock. |
| 28 | |
| 29 | namespace qhwc{ |
| 30 | static const int MAX_QUEUED_BUFS = 4; |
| 31 | class QueuedBufferStore { |
| 32 | public: |
| 33 | QueuedBufferStore() { |
| 34 | clearCurrent(); |
| 35 | clearPrevious(); |
| 36 | } |
| 37 | ~QueuedBufferStore() {} |
| 38 | void lockAndAdd(private_handle_t*); |
Saurabh Shah | 83523d8 | 2012-08-16 10:19:07 -0700 | [diff] [blame] | 39 | //Unlocks only previous and makes the current as previous |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 40 | void unlockAllPrevious(); |
Saurabh Shah | 83523d8 | 2012-08-16 10:19:07 -0700 | [diff] [blame] | 41 | //Unlocks previous as well as current, useful in suspend case |
| 42 | void unlockAll(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 43 | |
| 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 |
| 61 | inline void QueuedBufferStore::lockAndAdd(private_handle_t *hnd) { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 62 | #ifndef USE_FENCE_SYNC |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 63 | if(lockBuffer(hnd)) |
| 64 | current[curCount++] = hnd; |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 65 | #endif |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | //Unlock all previous drawing round buffers |
| 69 | inline void QueuedBufferStore::unlockAllPrevious() { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 70 | #ifndef USE_FENCE_SYNC |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 71 | //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 Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 80 | #endif |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Saurabh Shah | 83523d8 | 2012-08-16 10:19:07 -0700 | [diff] [blame] | 83 | inline void QueuedBufferStore::unlockAll() { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 84 | #ifndef USE_FENCE_SYNC |
Saurabh Shah | 83523d8 | 2012-08-16 10:19:07 -0700 | [diff] [blame] | 85 | //Unlocks prev and moves current to prev |
| 86 | unlockAllPrevious(); |
| 87 | //Unlocks the newly populated prev if any. |
| 88 | unlockAllPrevious(); |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 89 | #endif |
Saurabh Shah | 83523d8 | 2012-08-16 10:19:07 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 92 | //Clear currentbuf store |
| 93 | inline void QueuedBufferStore::clearCurrent() { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 94 | #ifndef USE_FENCE_SYNC |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 95 | for(int i = 0; i < MAX_QUEUED_BUFS; i++) |
| 96 | current[i] = NULL; |
| 97 | curCount = 0; |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 98 | #endif |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | //Clear previousbuf store |
| 102 | inline void QueuedBufferStore::clearPrevious() { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 103 | #ifndef USE_FENCE_SYNC |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 104 | for(int i = 0; i < MAX_QUEUED_BUFS; i++) |
| 105 | previous[i] = NULL; |
| 106 | prevCount = 0; |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 107 | #endif |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | //Copy from current to previous |
| 111 | inline void QueuedBufferStore::mvCurrToPrev() { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 112 | #ifndef USE_FENCE_SYNC |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 113 | for(int i = 0; i < curCount; i++) |
| 114 | previous[i] = current[i]; |
| 115 | prevCount = curCount; |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 116 | #endif |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | inline bool QueuedBufferStore::lockBuffer(private_handle_t *hnd) { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 120 | #ifndef USE_FENCE_SYNC |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 121 | 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 Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 126 | #endif |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
| 130 | inline void QueuedBufferStore::unlockBuffer(private_handle_t *hnd) { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 131 | #ifndef USE_FENCE_SYNC |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 132 | //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 Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 142 | #endif |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 143 | } |
| 144 | // ----------------------------------------------------------------------------- |
| 145 | };//namespace |
| 146 | |