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