blob: f3282687d8b69f9c998dae06e69374966e7b8f5c [file] [log] [blame]
Saurabh Shah909c9792015-07-06 15:59:01 -07001/* Copyright (c) 2015, The Linux Foundataion. All rights reserved.
2*
3* Redistribution and use in source and binary forms, with or without
4* modification, are permitted provided that the following conditions are
5* met:
6* * Redistributions of source code must retain the above copyright
7* notice, this list of conditions and the following disclaimer.
8* * Redistributions in binary form must reproduce the above
9* copyright notice, this list of conditions and the following
10* disclaimer in the documentation and/or other materials provided
11* with the distribution.
12* * Neither the name of The Linux Foundation nor the names of its
13* contributors may be used to endorse or promote products derived
14* from this software without specific prior written permission.
15*
16* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*
28*/
29
30#include <cutils/properties.h>
31#include <dlfcn.h>
32
33#include "cpuhint.h"
34#include "hwc_debugger.h"
35
36#define __CLASS__ "CPUHint"
37
38namespace sdm {
39
Saurabh Shah909c9792015-07-06 15:59:01 -070040CPUHint::~CPUHint() {
41 if (lib_handle_) {
42 dlclose(lib_handle_);
43 lib_handle_ = NULL;
44 }
45}
46
47DisplayError CPUHint::Init(HWCDebugHandler *debug_handler) {
48 char path[PROPERTY_VALUE_MAX];
49 if (debug_handler->GetProperty("ro.vendor.extension_library", path) != kErrorNone) {
50 DLOGI("Vendor Extension Library not enabled");
51 return kErrorNotSupported;
52 }
53
54 int pre_enable_window = -1;
55 debug_handler->GetProperty("sdm.perf_hint_window", &pre_enable_window);
56 if (pre_enable_window <= 0) {
57 DLOGI("Invalid CPU Hint Pre-enable Window %d", pre_enable_window);
58 return kErrorNotSupported;
59 }
60
61 DLOGI("CPU Hint Pre-enable Window %d", pre_enable_window);
62 pre_enable_window_ = pre_enable_window;
63 lib_handle_ = dlopen(path, RTLD_NOW);
64
65 if (lib_handle_) {
66 *(reinterpret_cast<void **>(&fn_lock_acquire_)) = dlsym(lib_handle_, "perf_lock_acq");
67 *(reinterpret_cast<void **>(&fn_lock_release_)) = dlsym(lib_handle_, "perf_lock_rel");
68 if (!fn_lock_acquire_ || !fn_lock_release_) {
69 DLOGW("Failed to load symbols for Vendor Extension Library");
70 return kErrorNotSupported;
71 }
72 DLOGI("Successfully Loaded Vendor Extension Library symbols");
73 enabled_ = true;
74 } else {
75 DLOGW("Failed to open %s : %s", path, dlerror());
76 }
77
78 return kErrorNone;
79}
80
81void CPUHint::Set() {
82 if (!enabled_) {
83 return;
84 }
85 if (lock_acquired_) {
86 return;
87 }
88 if (frame_countdown_) {
89 --frame_countdown_;
90 return;
91 }
92
93 int hint = HINT;
94 lock_handle_ = fn_lock_acquire_(0 /*handle*/, 0/*duration*/,
95 &hint, sizeof(hint) / sizeof(int));
96 if (lock_handle_ >= 0) {
97 lock_acquired_ = true;
98 }
99}
100
101void CPUHint::Reset() {
102 if (!enabled_) {
103 return;
104 }
105
106 frame_countdown_ = pre_enable_window_;
107
108 if (!lock_acquired_) {
109 return;
110 }
111
112 fn_lock_release_(lock_handle_);
113 lock_acquired_ = false;
114}
115
116} // namespace sdm