blob: 69f1aca3d944c1776922c64131c04435b2354de2 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopian67016af2012-02-02 15:14:13 -08002 * Copyright (C) 2012 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <errno.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21
22#include <unistd.h>
23#include <fcntl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
25#include <utils/Log.h>
26
27#include "DisplayHardware/DisplayHardwareBase.h"
28#include "SurfaceFlinger.h"
29
30// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031namespace android {
32
Mathias Agopian3d031502011-04-18 16:25:40 -070033static char const * const kSleepFileName = "/sys/power/wait_for_fb_sleep";
34static char const * const kWakeFileName = "/sys/power/wait_for_fb_wake";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
36// ----------------------------------------------------------------------------
37
Mathias Agopian67016af2012-02-02 15:14:13 -080038DisplayHardwareBase::DisplayEventThread::DisplayEventThread(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039 const sp<SurfaceFlinger>& flinger)
40 : Thread(false), mFlinger(flinger) {
41}
42
Mathias Agopian67016af2012-02-02 15:14:13 -080043DisplayHardwareBase::DisplayEventThread::~DisplayEventThread() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044}
45
Mathias Agopian67016af2012-02-02 15:14:13 -080046status_t DisplayHardwareBase::DisplayEventThread::initCheck() const {
47 return ((access(kSleepFileName, R_OK) == 0 &&
48 access(kWakeFileName, R_OK) == 0)) ? NO_ERROR : NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049}
50
Mathias Agopian67016af2012-02-02 15:14:13 -080051bool DisplayHardwareBase::DisplayEventThread::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
Mathias Agopian67016af2012-02-02 15:14:13 -080053 if (waitForFbSleep() == NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 sp<SurfaceFlinger> flinger = mFlinger.promote();
Steve Block9d453682011-12-20 16:23:08 +000055 ALOGD("About to give-up screen, flinger = %p", flinger.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056 if (flinger != 0) {
57 mBarrier.close();
58 flinger->screenReleased(0);
59 mBarrier.wait();
60 }
Mathias Agopian67016af2012-02-02 15:14:13 -080061 if (waitForFbWake() == NO_ERROR) {
62 sp<SurfaceFlinger> flinger = mFlinger.promote();
63 ALOGD("Screen about to return, flinger = %p", flinger.get());
64 if (flinger != 0) {
65 flinger->screenAcquired(0);
66 }
67 return true;
68 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 }
Mathias Agopian67016af2012-02-02 15:14:13 -080070
71 // error, exit the thread
72 return false;
73}
74
75status_t DisplayHardwareBase::DisplayEventThread::waitForFbSleep() {
76 int err = 0;
77 char buf;
78 int fd = open(kSleepFileName, O_RDONLY, 0);
79 // if the file doesn't exist, the error will be caught in read() below
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 do {
Mathias Agopian67016af2012-02-02 15:14:13 -080081 err = read(fd, &buf, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 } while (err < 0 && errno == EINTR);
83 close(fd);
Mathias Agopian67016af2012-02-02 15:14:13 -080084 ALOGE_IF(err<0, "*** ANDROID_WAIT_FOR_FB_SLEEP failed (%s)", strerror(errno));
85 return err < 0 ? -errno : int(NO_ERROR);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
Mathias Agopian67016af2012-02-02 15:14:13 -080088status_t DisplayHardwareBase::DisplayEventThread::waitForFbWake() {
89 int err = 0;
90 char buf;
91 int fd = open(kWakeFileName, O_RDONLY, 0);
92 // if the file doesn't exist, the error will be caught in read() below
93 do {
94 err = read(fd, &buf, 1);
95 } while (err < 0 && errno == EINTR);
96 close(fd);
97 ALOGE_IF(err<0, "*** ANDROID_WAIT_FOR_FB_WAKE failed (%s)", strerror(errno));
98 return err < 0 ? -errno : int(NO_ERROR);
99}
100
101status_t DisplayHardwareBase::DisplayEventThread::releaseScreen() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 mBarrier.open();
103 return NO_ERROR;
104}
105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106// ----------------------------------------------------------------------------
107
108DisplayHardwareBase::DisplayHardwareBase(const sp<SurfaceFlinger>& flinger,
109 uint32_t displayIndex)
Mathias Agopianf7cdd052011-10-10 22:18:55 -0700110 : mScreenAcquired(true)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111{
112 mDisplayEventThread = new DisplayEventThread(flinger);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113}
114
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800115void DisplayHardwareBase::startSleepManagement() const {
116 if (mDisplayEventThread->initCheck() == NO_ERROR) {
117 mDisplayEventThread->run("DisplayEventThread", PRIORITY_URGENT_DISPLAY);
118 } else {
119 ALOGW("/sys/power/wait_for_fb_{wake|sleep} don't exist");
120 }
121}
122
Mathias Agopian67016af2012-02-02 15:14:13 -0800123DisplayHardwareBase::~DisplayHardwareBase() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 // request exit
125 mDisplayEventThread->requestExitAndWait();
126}
127
Mathias Agopian67016af2012-02-02 15:14:13 -0800128bool DisplayHardwareBase::canDraw() const {
Mathias Agopianf7cdd052011-10-10 22:18:55 -0700129 return mScreenAcquired;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130}
131
Mathias Agopian67016af2012-02-02 15:14:13 -0800132void DisplayHardwareBase::releaseScreen() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 status_t err = mDisplayEventThread->releaseScreen();
134 if (err >= 0) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700135 mScreenAcquired = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 }
137}
138
Mathias Agopian67016af2012-02-02 15:14:13 -0800139void DisplayHardwareBase::acquireScreen() const {
140 mScreenAcquired = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141}
142
Mathias Agopian67016af2012-02-02 15:14:13 -0800143bool DisplayHardwareBase::isScreenAcquired() const {
Mathias Agopian59119e62010-10-11 12:37:43 -0700144 return mScreenAcquired;
145}
146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147}; // namespace android