129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed/*
229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * Copyright (C) 2010 The Android Open Source Project
329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * Copyright (C) 2012, Code Aurora Forum. All rights reserved.
429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *
529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * Licensed under the Apache License, Version 2.0 (the "License");
629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * you may not use this file except in compliance with the License.
729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * You may obtain a copy of the License at
829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *
929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *      http://www.apache.org/licenses/LICENSE-2.0
1029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *
1129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * Unless required by applicable law or agreed to in writing, software
1229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * distributed under the License is distributed on an "AS IS" BASIS,
1329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * See the License for the specific language governing permissions and
1529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * limitations under the License.
1629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed */
1729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
1872cf9762f84aab07faab86e35fe830b63ec54d72Naseer Ahmed#include <gralloc_priv.h>
192dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar
202dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#ifndef USE_FENCE_SYNC
2172cf9762f84aab07faab86e35fe830b63ec54d72Naseer Ahmed#include <genlock.h>
222dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#endif
2372cf9762f84aab07faab86e35fe830b63ec54d72Naseer Ahmed
2429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed// -----------------------------------------------------------------------------
2529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed// QueuedBufferStore
2629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed//This class holds currently and previously queued buffers.
2729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed//Provides utilities to store, lock, remove, unlock.
2829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
2929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmednamespace qhwc{
3029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedstatic const int MAX_QUEUED_BUFS = 4;
3129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedclass QueuedBufferStore {
3229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    public:
3329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    QueuedBufferStore() {
3429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        clearCurrent();
3529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        clearPrevious();
3629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    }
3729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    ~QueuedBufferStore() {}
3829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    void lockAndAdd(private_handle_t*);
3983523d815791832a90cd689d1796522589ade7feSaurabh Shah    //Unlocks only previous and makes the current as previous
4029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    void unlockAllPrevious();
4183523d815791832a90cd689d1796522589ade7feSaurabh Shah    //Unlocks previous as well as current, useful in suspend case
4283523d815791832a90cd689d1796522589ade7feSaurabh Shah    void unlockAll();
4329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
4429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    private:
4529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    QueuedBufferStore& operator=(const QueuedBufferStore&);
4629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    QueuedBufferStore(const QueuedBufferStore&);
4729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    bool lockBuffer(private_handle_t *hnd);
4829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    void unlockBuffer(private_handle_t *hnd);
4929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    void clearCurrent();
5029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    void clearPrevious();
5129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    void mvCurrToPrev();
5229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
5329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    //members
5429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    private_handle_t *current[MAX_QUEUED_BUFS]; //holds buf being queued
5529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    private_handle_t *previous[MAX_QUEUED_BUFS]; //holds bufs queued in prev round
5629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    int curCount;
5729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    int prevCount;
5829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed};
5929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
6029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed//Store and lock current drawing round buffers
6129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline void QueuedBufferStore::lockAndAdd(private_handle_t *hnd) {
622dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#ifndef USE_FENCE_SYNC
6329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    if(lockBuffer(hnd))
6429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        current[curCount++] = hnd;
652dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#endif
6629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
6729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
6829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed//Unlock all previous drawing round buffers
6929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline void QueuedBufferStore::unlockAllPrevious() {
702dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#ifndef USE_FENCE_SYNC
7129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    //Unlock
7229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    for(int i = 0; i < prevCount; i++) {
7329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        unlockBuffer(previous[i]);
7429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        previous[i] = NULL;
7529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    }
7629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    //Move current hnd to previous
7729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    mvCurrToPrev();
7829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    //Clear current
7929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    clearCurrent();
802dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#endif
8129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
8229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
8383523d815791832a90cd689d1796522589ade7feSaurabh Shahinline void QueuedBufferStore::unlockAll() {
842dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#ifndef USE_FENCE_SYNC
8583523d815791832a90cd689d1796522589ade7feSaurabh Shah    //Unlocks prev and moves current to prev
8683523d815791832a90cd689d1796522589ade7feSaurabh Shah    unlockAllPrevious();
8783523d815791832a90cd689d1796522589ade7feSaurabh Shah    //Unlocks the newly populated prev if any.
8883523d815791832a90cd689d1796522589ade7feSaurabh Shah    unlockAllPrevious();
892dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#endif
9083523d815791832a90cd689d1796522589ade7feSaurabh Shah}
9183523d815791832a90cd689d1796522589ade7feSaurabh Shah
9229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed//Clear currentbuf store
9329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline void QueuedBufferStore::clearCurrent() {
942dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#ifndef USE_FENCE_SYNC
9529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    for(int i = 0; i < MAX_QUEUED_BUFS; i++)
9629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        current[i] = NULL;
9729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    curCount = 0;
982dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#endif
9929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
10029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
10129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed//Clear previousbuf store
10229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline void QueuedBufferStore::clearPrevious() {
1032dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#ifndef USE_FENCE_SYNC
10429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    for(int i = 0; i < MAX_QUEUED_BUFS; i++)
10529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        previous[i] = NULL;
10629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    prevCount = 0;
1072dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#endif
10829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
10929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
11029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed//Copy from current to previous
11129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline void QueuedBufferStore::mvCurrToPrev() {
1122dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#ifndef USE_FENCE_SYNC
11329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    for(int i = 0; i < curCount; i++)
11429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        previous[i] = current[i];
11529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    prevCount = curCount;
1162dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#endif
11729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
11829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
11929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline bool QueuedBufferStore::lockBuffer(private_handle_t *hnd) {
1202dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#ifndef USE_FENCE_SYNC
12129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    if (GENLOCK_FAILURE == genlock_lock_buffer(hnd, GENLOCK_READ_LOCK,
12229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed                                               GENLOCK_MAX_TIMEOUT)) {
12329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        ALOGE("%s: genlock_lock_buffer(READ) failed", __func__);
12429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        return false;
12529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    }
1262dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#endif
12729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    return true;
12829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
12929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
13029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline void QueuedBufferStore::unlockBuffer(private_handle_t *hnd) {
1312dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#ifndef USE_FENCE_SYNC
13229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    //Check if buffer is still around
13329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    if(private_handle_t::validate(hnd) != 0) {
13429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        ALOGE("%s Invalid Handle", __func__);
13529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        return;
13629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    }
13729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    //Actually try to unlock
13829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    if (GENLOCK_FAILURE == genlock_unlock_buffer(hnd)) {
13929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        ALOGE("%s: genlock_unlock_buffer failed", __func__);
14029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        return;
14129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    }
1422dd04a873bc3d4b3ca50121347a8c702d097a2dfKinjal Bhavsar#endif
14329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
14429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed// -----------------------------------------------------------------------------
14529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed};//namespace
14629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
147