1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
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 
17  #include "VirtGpu.h"
18  #include <cutils/log.h>
19 
20 namespace {
21 
22 static VirtGpuDevice* sDevice = nullptr;
23 
24 }  // namespace
25 
getInstance(enum VirtGpuCapset capset)26 VirtGpuDevice* VirtGpuDevice::getInstance(enum VirtGpuCapset capset) {
27     // If kCapsetNone is passed, we return a device that was created with any capset.
28     // Otherwise, the created device's capset must match the requested capset.
29     // We could support multiple capsets with a map of devices but that case isn't needed
30     // currently, and with multiple devices it's unclear how to handle kCapsetNone.
31     if (capset != kCapsetNone && sDevice && sDevice->capset() != capset) {
32         ALOGE("Requested VirtGpuDevice capset %u, already created capset %u",
33             capset, sDevice->capset());
34         return nullptr;
35     }
36     if (!sDevice) {
37         sDevice = createPlatformVirtGpuDevice(capset);
38     }
39     return sDevice;
40 }
41 
resetInstance()42 void VirtGpuDevice::resetInstance() {
43     if (sDevice) {
44         delete sDevice;
45         sDevice = nullptr;
46     }
47 }
48 
setInstanceForTesting(VirtGpuDevice * device)49 void VirtGpuDevice::setInstanceForTesting(VirtGpuDevice* device) {
50     sDevice = device;
51 }
52