summaryrefslogtreecommitdiffstats
path: root/display/renderer/device-files/graphics.sh
blob: 1dd44621dfce4ed8633f9d7fd4c466bc6417067e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/system/xbin/bash
#
# Copyright (C) 2017 Jeremy Rand <jeremy@veclabs.net>
# Partially based on code by Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de> and Filippo Fil Bergamo
#
# This file is part of "Replicant Renderer Switcher", a set of shell scripts to switch between
# the Android Software Renderer and the llvmpipe renderer in Replicant.
#
# Replicant Renderer Switcher is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Replicant Renderer Switcher is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -euf -o pipefail

usage()
{
	echo "Usage: graphics.sh [faster|compatible]"
	echo "Examples:"
	echo "graphics.sh faster"
	echo "|-> Uses Android's libagl"
	echo "    which is faster"
	echo "    but some applications"
	echo "    like icecat won't work"
	echo "graphics.sh compatible"
	echo "|-> Uses llvmpipe"
	echo "    which is slower"
	echo "    but more compatible"
	exit 1
}

check_permissions()
{
	new_renderer="$1"

	root_access="$(getprop persist.sys.root_access)"
	uid="$(id -u)"

	# 0: Root access: Disabled, 2: Root access: ADB only
	#    For both the terminal application and ADB we have something
	#    like that:
	#        shell@i9100:/ $ su
	#        255|shell@i9100:/ $
	# 1: Root access: Apps only
	#    From ADB:
	#        shell@i9100:/ $ su
	#        Permission denied
	#        1|shell@i9100:/ $
	#    From the terminal application
	#        u0_a51@i9100:/ $ su
	#        root@i9100:/ #
	if [ "${uid}" != "0" ] ; then
	    echo ""
	    echo "This script requires root."
	    echo "To give it root:"
	    case "${root_access}" in
		"0"|"1"|"2")
		    echo "- Go in Settings"
		    echo "- Go in Developer options"
		    echo "- Select Root access"
		    echo "- Set it to \"Apps and ADB\""
		    ;;
	    esac
	    echo "- Run these 2 commands:"
	    echo "  su"
	    echo "  graphics.sh ${new_renderer}"
	    exit 1
	fi
}

graphics()
{
    new_renderer="$1" # libagl || llvmpipe

    check_permissions "${new_renderer}"

    grep -q "ro.libagl=0" /system/build.prop && \
	old_renderer="llvmpipe" || old_renderer="libagl"

    if [ "${old_renderer}" == "${new_renderer}" ] ; then
	if [ "${old_renderer}" = "libagl" ] ; then
	    echo "Android software renderer is already enabled!"
	else
	    echo "${old_renderer} renderer is already enabled!"
	fi
	exit 1
    fi

    echo "Remounting system partition as writable..."
    mount -o rw,remount /system

    if [ "${new_renderer}" == "libagl" ] ; then
	echo "Enabling Android software renderer..."
	sed "s/ro.libagl=0/ro.libagl=1/" -i /system/build.prop
    else
	echo "Enabling ${new_renderer}..."
	sed "s/ro.libagl=1/ro.libagl=0/" -i /system/build.prop
    fi

    echo "Syncing filesystem..."
    sync
    sync

    echo "Remounting system partition as read-only..."
    mount -o ro,remount /system

    echo "Syncing filesystem..."
    sync
    sync

    if [ "${new_renderer}" == "libagl" ] ; then
	echo "Android software renderer will be enabled after we reboot now..."
    else
	echo "${new_renderer} will be enabled after we reboot now..."
    fi

    # https://android.stackexchange.com/a/139139
    am start -a android.intent.action.REBOOT
}

if [ $# -ne 1 ] ; then
	usage
fi

if [ "$1" = "faster" ] ; then
    graphics libagl
elif [ "$1" = "compatible" ] ;then
    graphics llvmpipe
else
	usage
fi