Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c95354bc authored by Peiyong Lin's avatar Peiyong Lin Committed by Yiwei Zhang
Browse files

[GPU Service] Implement GPU service.

GPU Service is used to monitor all GPU and graphics driver related features.
This patch implements GPU service into System Server, and implements
functionality to extract the whitelist out of game driver package when the
package is upgraded or removed. This will move the whitelist processing off
critical path when app launches.

BUG: 123290424
Test: Build, flash and boot. Verify by upgrading game driver apk.
Change-Id: I563a138bfe0c4c1bb17ed28dab5d6a8df244021d
Merged-In: I563a138bfe0c4c1bb17ed28dab5d6a8df244021d
parent c4db14e7
Loading
Loading
Loading
Loading
+3 −31
Original line number Original line Diff line number Diff line
@@ -33,11 +33,8 @@ import com.android.framework.protobuf.InvalidProtocolBufferException;


import dalvik.system.VMRuntime;
import dalvik.system.VMRuntime;


import java.io.BufferedReader;
import java.io.File;
import java.io.File;
import java.io.IOException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.List;
import java.util.List;
@@ -57,7 +54,6 @@ public class GraphicsEnvironment {
    private static final boolean DEBUG = false;
    private static final boolean DEBUG = false;
    private static final String TAG = "GraphicsEnvironment";
    private static final String TAG = "GraphicsEnvironment";
    private static final String PROPERTY_GFX_DRIVER = "ro.gfx.driver.0";
    private static final String PROPERTY_GFX_DRIVER = "ro.gfx.driver.0";
    private static final String GAME_DRIVER_WHITELIST_FILENAME = "whitelist.txt";
    private static final String GAME_DRIVER_BLACKLIST_FLAG = "blacklist";
    private static final String GAME_DRIVER_BLACKLIST_FLAG = "blacklist";
    private static final int BASE64_FLAGS = Base64.NO_PADDING | Base64.NO_WRAP;
    private static final int BASE64_FLAGS = Base64.NO_PADDING | Base64.NO_WRAP;


@@ -219,8 +215,9 @@ public class GraphicsEnvironment {
            boolean isOptIn =
            boolean isOptIn =
                    getGlobalSettingsString(coreSettings, Settings.Global.GAME_DRIVER_OPT_IN_APPS)
                    getGlobalSettingsString(coreSettings, Settings.Global.GAME_DRIVER_OPT_IN_APPS)
                            .contains(ai.packageName);
                            .contains(ai.packageName);

            if (!isOptIn
            if (!isOptIn && !onWhitelist(context, driverPackageName, ai.packageName)) {
                    && !getGlobalSettingsString(coreSettings, Settings.Global.GAME_DRIVER_WHITELIST)
                        .contains(ai.packageName)) {
                if (DEBUG) {
                if (DEBUG) {
                    Log.w(TAG, ai.packageName + " is not on the whitelist.");
                    Log.w(TAG, ai.packageName + " is not on the whitelist.");
                }
                }
@@ -313,31 +310,6 @@ public class GraphicsEnvironment {
        return null;
        return null;
    }
    }


    private static boolean onWhitelist(Context context, String driverPackageName,
            String applicationPackageName) {
        try {
            Context driverContext = context.createPackageContext(driverPackageName,
                                                                 Context.CONTEXT_RESTRICTED);
            AssetManager assets = driverContext.getAssets();
            InputStream stream = assets.open(GAME_DRIVER_WHITELIST_FILENAME);
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            for (String packageName; (packageName = reader.readLine()) != null; ) {
                if (packageName.equals(applicationPackageName)) {
                    return true;
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            if (DEBUG) {
                Log.w(TAG, "driver package '" + driverPackageName + "' not installed");
            }
        } catch (IOException e) {
            if (DEBUG) {
                Log.w(TAG, "Failed to load whitelist driver package, abort.");
            }
        }
        return false;
    }

    private static native void setLayerPaths(ClassLoader classLoader, String layerPaths);
    private static native void setLayerPaths(ClassLoader classLoader, String layerPaths);
    private static native void setDebugLayers(String layers);
    private static native void setDebugLayers(String layers);
    private static native void setDriverPath(String path);
    private static native void setDriverPath(String path);
+163 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.gpu;

import static android.content.Intent.ACTION_PACKAGE_ADDED;
import static android.content.Intent.ACTION_PACKAGE_CHANGED;
import static android.content.Intent.ACTION_PACKAGE_REMOVED;

import android.annotation.NonNull;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Slog;

import com.android.server.SystemService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Service to manage GPU related features.
 *
 * <p>GPU service is a core service that monitors, coordinates all GPU related features,
 * as well as collect metrics about the GPU and GPU driver.</p>
 */
public class GpuService extends SystemService {
    public static final String TAG = "GpuService";
    public static final boolean DEBUG = false;

    private static final String PROPERTY_GFX_DRIVER = "ro.gfx.driver.0";
    private static final String WHITELIST_FILENAME = "whitelist.txt";

    private final Context mContext;
    private final String mDriverPackageName;
    private final PackageManager mPackageManager;

    public GpuService(Context context) {
        super(context);

        mContext = context;
        mDriverPackageName = SystemProperties.get(PROPERTY_GFX_DRIVER);
        mPackageManager = context.getPackageManager();
        if (mDriverPackageName != null && !mDriverPackageName.isEmpty()) {
            final IntentFilter packageFilter = new IntentFilter();
            packageFilter.addAction(ACTION_PACKAGE_ADDED);
            packageFilter.addAction(ACTION_PACKAGE_CHANGED);
            packageFilter.addAction(ACTION_PACKAGE_REMOVED);
            packageFilter.addDataScheme("package");
            getContext().registerReceiverAsUser(new PackageReceiver(), UserHandle.ALL,
                                                packageFilter, null, null);
        }
    }

    @Override
    public void onStart() {
    }

    @Override
    public void onBootPhase(int phase) {
        if (phase == PHASE_BOOT_COMPLETED) {
            if (mDriverPackageName == null || mDriverPackageName.isEmpty()) {
                return;
            }
            fetchGameDriverPackageProperties();
        }
    }

    private final class PackageReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(@NonNull final Context context, @NonNull final Intent intent) {
            final Uri data = intent.getData();
            if (data == null && DEBUG) {
                Slog.e(TAG, "Cannot handle package broadcast with null data");
                return;
            }
            final String packageName = data.getSchemeSpecificPart();
            if (!packageName.equals(mDriverPackageName)) {
                return;
            }

            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);

            switch (intent.getAction()) {
                case ACTION_PACKAGE_ADDED:
                case ACTION_PACKAGE_CHANGED:
                case ACTION_PACKAGE_REMOVED:
                    fetchGameDriverPackageProperties();
                    break;
                default:
                    // do nothing
                    break;
            }
        }
    }

    private void fetchGameDriverPackageProperties() {
        final ApplicationInfo driverInfo;
        try {
            driverInfo = mPackageManager.getApplicationInfo(mDriverPackageName,
                                                            PackageManager.MATCH_SYSTEM_ONLY);
        } catch (PackageManager.NameNotFoundException e) {
            if (DEBUG) {
                Slog.e(TAG, "driver package '" + mDriverPackageName + "' not installed");
            }
            return;
        }

        // O drivers are restricted to the sphal linker namespace, so don't try to use
        // packages unless they declare they're compatible with that restriction.
        if (driverInfo.targetSdkVersion < Build.VERSION_CODES.O) {
            if (DEBUG) {
                Slog.w(TAG, "Driver package is not known to be compatible with O");
            }
            return;
        }

        try {
            final Context driverContext = mContext.createPackageContext(mDriverPackageName,
                                                                        Context.CONTEXT_RESTRICTED);
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(driverContext.getAssets().open(WHITELIST_FILENAME)));
            final ArrayList<String> whitelistedPackageNames = new ArrayList<>();
            for (String packageName; (packageName = reader.readLine()) != null; ) {
                whitelistedPackageNames.add(packageName);
            }
            Settings.Global.putString(mContext.getContentResolver(),
                                      Settings.Global.GAME_DRIVER_WHITELIST,
                                      String.join(",", whitelistedPackageNames));
        } catch (PackageManager.NameNotFoundException e) {
            if (DEBUG) {
                Slog.w(TAG, "driver package '" + mDriverPackageName + "' not installed");
            }
        } catch (IOException e) {
            if (DEBUG) {
                Slog.w(TAG, "Failed to load whitelist driver package, abort.");
            }
        }
    }
}
+6 −0
Original line number Original line Diff line number Diff line
@@ -87,6 +87,7 @@ import com.android.server.display.DisplayManagerService;
import com.android.server.dreams.DreamManagerService;
import com.android.server.dreams.DreamManagerService;
import com.android.server.emergency.EmergencyAffordanceService;
import com.android.server.emergency.EmergencyAffordanceService;
import com.android.server.fingerprint.FingerprintService;
import com.android.server.fingerprint.FingerprintService;
import com.android.server.gpu.GpuService;
import com.android.server.hdmi.HdmiControlService;
import com.android.server.hdmi.HdmiControlService;
import com.android.server.input.InputManagerService;
import com.android.server.input.InputManagerService;
import com.android.server.job.JobSchedulerService;
import com.android.server.job.JobSchedulerService;
@@ -747,6 +748,11 @@ public final class SystemServer {
        traceBeginAndSlog("StartBugreportManagerService");
        traceBeginAndSlog("StartBugreportManagerService");
        mSystemServiceManager.startService(BugreportManagerService.class);
        mSystemServiceManager.startService(BugreportManagerService.class);
        traceEnd();
        traceEnd();

        // Serivce for GPU and GPU driver.
        traceBeginAndSlog("GpuService");
        mSystemServiceManager.startService(GpuService.class);
        traceEnd();
    }
    }


    /**
    /**