129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed/*
2a43fb8fce6d9be5577de1e0f49c99bd4fe2f6d44Duy Truong * Copyright (c) 2012, The Linux Foundation. All rights reserved.
329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * Redistribution and use in source and binary forms, with or without
529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * modification, are permitted provided that the following conditions are
629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * met:
729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *   * Redistributions of source code must retain the above copyright
829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *     notice, this list of conditions and the following disclaimer.
929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *   * Redistributions in binary form must reproduce the above
1029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *     copyright notice, this list of conditions and the following
1129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *     disclaimer in the documentation and/or other materials provided
1229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *     with the distribution.
13a43fb8fce6d9be5577de1e0f49c99bd4fe2f6d44Duy Truong *   * Neither the name of The Linux Foundation nor the names of its
1429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *     contributors may be used to endorse or promote products derived
1529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *     from this software without specific prior written permission.
1629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed *
1729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
1829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
2029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
2129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
2429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
2629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed */
2929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
307c958d445fe88a82eedbd1b887cd1e474a7526b1Naseer Ahmed#include "idle_invalidator.h"
3129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#include <unistd.h>
3229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
337c958d445fe88a82eedbd1b887cd1e474a7526b1Naseer Ahmed#define II_DEBUG 0
3429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
3529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedstatic const char *threadName = "Invalidator";
3629a26818d7294055539167b2fbfdaa168bcf725cNaseer AhmedInvalidatorHandler IdleInvalidator::mHandler = NULL;
3729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedandroid::sp<IdleInvalidator> IdleInvalidator::sInstance(0);
3829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
3929a26818d7294055539167b2fbfdaa168bcf725cNaseer AhmedIdleInvalidator::IdleInvalidator(): Thread(false), mHwcContext(0),
4029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    mSleepAgain(false), mSleepTime(0) {
417c958d445fe88a82eedbd1b887cd1e474a7526b1Naseer Ahmed        ALOGD_IF(II_DEBUG, "%s", __func__);
4229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    }
4329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
4429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedint IdleInvalidator::init(InvalidatorHandler reg_handler, void* user_data,
4529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed                          unsigned int idleSleepTime) {
467c958d445fe88a82eedbd1b887cd1e474a7526b1Naseer Ahmed    ALOGD_IF(II_DEBUG, "%s", __func__);
4729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
4829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    /* store registered handler */
4929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    mHandler = reg_handler;
5029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    mHwcContext = user_data;
5129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    mSleepTime = idleSleepTime; //Time in millis
5229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    return 0;
5329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
5429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
5529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedbool IdleInvalidator::threadLoop() {
567c958d445fe88a82eedbd1b887cd1e474a7526b1Naseer Ahmed    ALOGD_IF(II_DEBUG, "%s", __func__);
577c958d445fe88a82eedbd1b887cd1e474a7526b1Naseer Ahmed    usleep(mSleepTime * 500);
5829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    if(mSleepAgain) {
5929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        //We need to sleep again!
6029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        mSleepAgain = false;
6129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        return true;
6229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    }
6329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
6429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    mHandler((void*)mHwcContext);
6529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    return false;
6629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
6729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
6829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedint IdleInvalidator::readyToRun() {
697c958d445fe88a82eedbd1b887cd1e474a7526b1Naseer Ahmed    ALOGD_IF(II_DEBUG, "%s", __func__);
7029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    return 0; /*NO_ERROR*/
7129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
7229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
7329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedvoid IdleInvalidator::onFirstRef() {
747c958d445fe88a82eedbd1b887cd1e474a7526b1Naseer Ahmed    ALOGD_IF(II_DEBUG, "%s", __func__);
7529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
7629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
7729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedvoid IdleInvalidator::markForSleep() {
7829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    mSleepAgain = true;
7929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    //Triggers the threadLoop to run, if not already running.
8029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    run(threadName, android::PRIORITY_AUDIO);
8129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
8229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed
8329a26818d7294055539167b2fbfdaa168bcf725cNaseer AhmedIdleInvalidator *IdleInvalidator::getInstance() {
847c958d445fe88a82eedbd1b887cd1e474a7526b1Naseer Ahmed    ALOGD_IF(II_DEBUG, "%s", __func__);
8529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    if(sInstance.get() == NULL)
8629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed        sInstance = new IdleInvalidator();
8729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed    return sInstance.get();
8829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}
89