blob: d3a8bdea6970766786698e58606fe59e1fd041a7 [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) {
Mathias Agopianb60314a2012-04-10 22:09:54 -070057 flinger->screenReleased();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058 }
Mathias Agopian67016af2012-02-02 15:14:13 -080059 if (waitForFbWake() == NO_ERROR) {
Mathias Agopian67016af2012-02-02 15:14:13 -080060 ALOGD("Screen about to return, flinger = %p", flinger.get());
61 if (flinger != 0) {
Mathias Agopianb60314a2012-04-10 22:09:54 -070062 flinger->screenAcquired();
Mathias Agopian67016af2012-02-02 15:14:13 -080063 }
64 return true;
65 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 }
Mathias Agopian67016af2012-02-02 15:14:13 -080067
68 // error, exit the thread
69 return false;
70}
71
72status_t DisplayHardwareBase::DisplayEventThread::waitForFbSleep() {
73 int err = 0;
74 char buf;
75 int fd = open(kSleepFileName, O_RDONLY, 0);
76 // if the file doesn't exist, the error will be caught in read() below
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 do {
Mathias Agopian67016af2012-02-02 15:14:13 -080078 err = read(fd, &buf, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 } while (err < 0 && errno == EINTR);
80 close(fd);
Mathias Agopian67016af2012-02-02 15:14:13 -080081 ALOGE_IF(err<0, "*** ANDROID_WAIT_FOR_FB_SLEEP failed (%s)", strerror(errno));
82 return err < 0 ? -errno : int(NO_ERROR);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083}
84
Mathias Agopian67016af2012-02-02 15:14:13 -080085status_t DisplayHardwareBase::DisplayEventThread::waitForFbWake() {
86 int err = 0;
87 char buf;
88 int fd = open(kWakeFileName, O_RDONLY, 0);
89 // if the file doesn't exist, the error will be caught in read() below
90 do {
91 err = read(fd, &buf, 1);
92 } while (err < 0 && errno == EINTR);
93 close(fd);
94 ALOGE_IF(err<0, "*** ANDROID_WAIT_FOR_FB_WAKE failed (%s)", strerror(errno));
95 return err < 0 ? -errno : int(NO_ERROR);
96}
97
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098// ----------------------------------------------------------------------------
99
100DisplayHardwareBase::DisplayHardwareBase(const sp<SurfaceFlinger>& flinger,
101 uint32_t displayIndex)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102{
Mathias Agopianb60314a2012-04-10 22:09:54 -0700103 mScreenAcquired = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 mDisplayEventThread = new DisplayEventThread(flinger);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105}
106
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800107void DisplayHardwareBase::startSleepManagement() const {
108 if (mDisplayEventThread->initCheck() == NO_ERROR) {
109 mDisplayEventThread->run("DisplayEventThread", PRIORITY_URGENT_DISPLAY);
110 } else {
111 ALOGW("/sys/power/wait_for_fb_{wake|sleep} don't exist");
112 }
113}
114
Mathias Agopian67016af2012-02-02 15:14:13 -0800115DisplayHardwareBase::~DisplayHardwareBase() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 // request exit
117 mDisplayEventThread->requestExitAndWait();
118}
119
Mathias Agopian67016af2012-02-02 15:14:13 -0800120bool DisplayHardwareBase::canDraw() const {
Mathias Agopianf7cdd052011-10-10 22:18:55 -0700121 return mScreenAcquired;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122}
123
Mathias Agopian67016af2012-02-02 15:14:13 -0800124void DisplayHardwareBase::releaseScreen() const {
Mathias Agopianb60314a2012-04-10 22:09:54 -0700125 mScreenAcquired = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126}
127
Mathias Agopian67016af2012-02-02 15:14:13 -0800128void DisplayHardwareBase::acquireScreen() const {
129 mScreenAcquired = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130}
131
Mathias Agopian67016af2012-02-02 15:14:13 -0800132bool DisplayHardwareBase::isScreenAcquired() const {
Mathias Agopian59119e62010-10-11 12:37:43 -0700133 return mScreenAcquired;
134}
135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136}; // namespace android