1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "host-common/GraphicsAgentFactory.h"
15 
16 #include "vm_operations.h"
17 #include "multi_display_agent.h"
18 #include "window_agent.h"
19 #include "record_screen_agent.h"
20 
21 #include <stdio.h>
22 
23 struct QAndroidAutomationAgent;
24 
25 using android::emulation::GraphicsAgentFactory;
26 
27 static GraphicsAgents sGraphicsAgents{0};
28 static bool isInitialized = false;
29 
30 
getGraphicsAgents()31 const GraphicsAgents* getGraphicsAgents() {
32     if (!isInitialized) {
33         // Let's not get involved with undefined behavior, if this happens the
34         // developer is not calling injectGraphicsAgents early enough.
35         fprintf(stderr, "Accessing graphics agents before injecting them.\n");
36         *(uint32_t*)(1234) = 5;
37     }
38     return &sGraphicsAgents;
39 }
40 
41 #define DEFINE_GRAPHICS_AGENT_GETTER_IMPL(typ, name)                   \
42     const typ* const GraphicsAgentFactory::android_get_##typ() const { \
43         return sGraphicsAgents.name;                                   \
44     };
45 
GRAPHICS_AGENTS_LIST(DEFINE_GRAPHICS_AGENT_GETTER_IMPL)46 GRAPHICS_AGENTS_LIST(DEFINE_GRAPHICS_AGENT_GETTER_IMPL)
47 
48 #define GRAPHICS_AGENT_SETTER(typ, name) \
49     sGraphicsAgents.name = factory.android_get_##typ();
50 
51 void android::emulation::injectGraphicsAgents(const GraphicsAgentFactory& factory) {
52     if (isInitialized) {
53         // This is not necessarily an error, as there are cases where this might be acceptable.
54         fprintf(stderr, "Warning: graphics agents were already injected!\n");
55     }
56     GRAPHICS_AGENTS_LIST(GRAPHICS_AGENT_SETTER);
57     isInitialized = true;
58 }
59