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> |
| 19 | #include <genlock.h> |
| 20 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 21 | // ----------------------------------------------------------------------------- |
| 22 | // QueuedBufferStore |
| 23 | //This class holds currently and previously queued buffers. |
| 24 | //Provides utilities to store, lock, remove, unlock. |
| 25 | |
| 26 | namespace qhwc{ |
| 27 | static const int MAX_QUEUED_BUFS = 4; |
| 28 | class QueuedBufferStore { |
| 29 | public: |
| 30 | QueuedBufferStore() { |
| 31 | clearCurrent(); |
| 32 | clearPrevious(); |
| 33 | } |
| 34 | ~QueuedBufferStore() {} |
| 35 | void lockAndAdd(private_handle_t*); |
Saurabh Shah | 83523d8 | 2012-08-16 10:19:07 -0700 | [diff] [blame] | 36 | //Unlocks only previous and makes the current as previous |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 37 | void unlockAllPrevious(); |
Saurabh Shah | 83523d8 | 2012-08-16 10:19:07 -0700 | [diff] [blame] | 38 | //Unlocks previous as well as current, useful in suspend case |
| 39 | void unlockAll(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 40 | |
| 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 |
| 58 | inline void QueuedBufferStore::lockAndAdd(private_handle_t *hnd) { |
| 59 | if(lockBuffer(hnd)) |
| 60 | current[curCount++] = hnd; |
| 61 | } |
| 62 | |
| 63 | //Unlock all previous drawing round buffers |
| 64 | inline 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 Shah | 83523d8 | 2012-08-16 10:19:07 -0700 | [diff] [blame] | 76 | inline 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 Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 83 | //Clear currentbuf store |
| 84 | inline 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 |
| 91 | inline 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 |
| 98 | inline void QueuedBufferStore::mvCurrToPrev() { |
| 99 | for(int i = 0; i < curCount; i++) |
| 100 | previous[i] = current[i]; |
| 101 | prevCount = curCount; |
| 102 | } |
| 103 | |
| 104 | inline 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 | |
| 113 | inline 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 | |