• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "GfxStreamAgents.h"
15 
16 #include <stdint.h>  // for uint32_t
17 #include <stdio.h>   // for fprintf
18 
19 #include <map>      // for map, __ma...
20 #include <utility>  // for pair
21 
22 #include "host-common/HostmemIdMapping.h"             // for android_e...
23 #include "host-common/MultiDisplay.h"                 // for MultiDisp...
24 #include "host-common/multi_display_agent.h"  // for QAndroidM...
25 #include "host-common/vm_operations.h"        // for SnapshotC...
26 #include "host-common/window_agent.h"         // for WindowMes...
27 #include "host-common/misc.h"
28 
29 #ifdef _DEBUG
30 #define DEBUG_LOG(fd, fmt, ...) fprintf(fd, fmt, __VA_ARGS__);
31 #else
32 #define DEBUG_LOG(fd, fmt, ...)
33 #endif
34 
35 std::map<uint32_t, android::MultiDisplayInfo> mMultiDisplay;
36 
37 using namespace android;
38 
39 static const QAndroidMultiDisplayAgent sMultiDisplayAgent = {
40         .setMultiDisplay = [](uint32_t id,
41                               int32_t x,
42                               int32_t y,
43                               uint32_t w,
44                               uint32_t h,
45                               uint32_t dpi,
46                               uint32_t flag,
__anonb35557250102() 47                               bool add) -> int {
48             return 0;
49         },
50         .getMultiDisplay = [](uint32_t id,
51                               int32_t* x,
52                               int32_t* y,
53                               uint32_t* w,
54                               uint32_t* h,
55                               uint32_t* dpi,
56                               uint32_t* flag,
__anonb35557250202() 57                               bool* enabled) -> bool {
58             if (mMultiDisplay.find(id) == mMultiDisplay.end()) {
59                 if (enabled) {
60                     *enabled = false;
61                 }
62                 return false;
63             }
64             if (x) {
65                 *x = mMultiDisplay[id].pos_x;
66             }
67             if (y) {
68                 *y = mMultiDisplay[id].pos_y;
69             }
70             if (w) {
71                 *w = mMultiDisplay[id].width;
72             }
73             if (h) {
74                 *h = mMultiDisplay[id].height;
75             }
76             if (dpi) {
77                 *dpi = mMultiDisplay[id].dpi;
78             }
79             if (flag) {
80                 *flag = mMultiDisplay[id].flag;
81             }
82             if (enabled) {
83                 *enabled = mMultiDisplay[id].enabled;
84             }
85             return true;
86         },
87         .getNextMultiDisplay = [](int32_t start_id,
88                                   uint32_t* id,
89                                   int32_t* x,
90                                   int32_t* y,
91                                   uint32_t* w,
92                                   uint32_t* h,
93                                   uint32_t* dpi,
94                                   uint32_t* flag,
__anonb35557250302() 95                                   uint32_t* cb) -> bool {
96             uint32_t key;
97             std::map<uint32_t, android::MultiDisplayInfo>::iterator i;
98             if (start_id < 0) {
99                 key = 0;
100             } else {
101                 key = start_id + 1;
102             }
103             i = mMultiDisplay.lower_bound(key);
104             if (i == mMultiDisplay.end()) {
105                 return false;
106             } else {
107                 if (id) {
108                     *id = i->first;
109                 }
110                 if (x) {
111                     *x = i->second.pos_x;
112                 }
113                 if (y) {
114                     *y = i->second.pos_y;
115                 }
116                 if (w) {
117                     *w = i->second.width;
118                 }
119                 if (h) {
120                     *h = i->second.height;
121                 }
122                 if (dpi) {
123                     *dpi = i->second.dpi;
124                 }
125                 if (flag) {
126                     *flag = i->second.flag;
127                 }
128                 if (cb) {
129                     *cb = i->second.cb;
130                 }
131                 return true;
132             }
133         },
__anonb35557250402() 134         .isMultiDisplayEnabled = [](void) -> bool {
135             return mMultiDisplay.size() > 1;
136         },
__anonb35557250502() 137         .getCombinedDisplaySize = [](uint32_t* width, uint32_t* height) {},
138         .multiDisplayParamValidate = [](uint32_t id,
139                                         uint32_t w,
140                                         uint32_t h,
141                                         uint32_t dpi,
__anonb35557250602() 142                                         uint32_t flag) -> bool { return true; },
143         .translateCoordination =
__anonb35557250702() 144                 [](uint32_t* x, uint32_t* y, uint32_t* displayId) -> bool {
145             return true;
146         },
__anonb35557250802() 147         .setGpuMode = [](bool isGuestMode, uint32_t w, uint32_t h) {},
__anonb35557250902() 148         .createDisplay = [](uint32_t* displayId) -> int {
149             if (displayId == nullptr) {
150                 fprintf(stderr, "null displayId pointer\n");
151                 return -1;
152             }
153             if (mMultiDisplay.size() >= MultiDisplay::s_maxNumMultiDisplay) {
154                 fprintf(stderr, "cannot create more displays, exceeding limits %d\n",
155                         MultiDisplay::s_maxNumMultiDisplay);
156                 return -1;
157             }
158             if (mMultiDisplay.find(*displayId) != mMultiDisplay.end()) {
159                 return 0;
160             }
161             // displays created by internal rcCommands
162             if (*displayId == MultiDisplay::s_invalidIdMultiDisplay) {
163                 for (int i = MultiDisplay::s_displayIdInternalBegin; i < MultiDisplay::s_maxNumMultiDisplay; i++) {
164                     if (mMultiDisplay.find(i) == mMultiDisplay.end()) {
165                         *displayId = i;
166                         break;
167                     }
168                 }
169             }
170             if (*displayId == MultiDisplay::s_invalidIdMultiDisplay) {
171                 fprintf(stderr, "cannot create more internaldisplays, exceeding limits %d\n",
172                         MultiDisplay::s_maxNumMultiDisplay - MultiDisplay::s_displayIdInternalBegin);
173                 return -1;
174             }
175 
176             mMultiDisplay.emplace(*displayId, android::MultiDisplayInfo());
177             return 0;
178         },
__anonb35557250a02() 179         .destroyDisplay = [](uint32_t displayId) -> int {
180             mMultiDisplay.erase(displayId);
181             return 0;
182         },
183         .setDisplayPose = [](uint32_t displayId,
184                              int32_t x,
185                              int32_t y,
186                              uint32_t w,
187                              uint32_t h,
__anonb35557250b02() 188                              uint32_t dpi) -> int {
189             if (mMultiDisplay.find(displayId) == mMultiDisplay.end()) {
190                 fprintf(stderr, "cannot find display %d\n", displayId);
191                 return -1;
192             }
193             mMultiDisplay[displayId].pos_x = x;
194             mMultiDisplay[displayId].pos_y = y;
195             mMultiDisplay[displayId].width = w;
196             mMultiDisplay[displayId].height = h;
197             mMultiDisplay[displayId].dpi = dpi;
198             return 0;
199         },
200         .getDisplayPose = [](uint32_t displayId,
201                              int32_t* x,
202                              int32_t* y,
203                              uint32_t* w,
__anonb35557250c02() 204                              uint32_t* h) -> int {
205             if (mMultiDisplay.find(displayId) == mMultiDisplay.end()) {
206                 fprintf(stderr, "cannot find display %d\n", displayId);
207                 return -1;
208             }
209             if (x)
210                 *x = mMultiDisplay[displayId].pos_x;
211             if (y)
212                 *y = mMultiDisplay[displayId].pos_y;
213             if (w)
214                 *w = mMultiDisplay[displayId].width;
215             if (h)
216                 *h = mMultiDisplay[displayId].height;
217             return 0;
218         },
219         .getDisplayColorBuffer = [](uint32_t displayId,
__anonb35557250d02() 220                                     uint32_t* colorBuffer) -> int {
221             if (mMultiDisplay.find(displayId) == mMultiDisplay.end()) {
222                 fprintf(stderr, "cannot find display %d\n", displayId);
223                 return -1;
224             }
225             *colorBuffer = mMultiDisplay[displayId].cb;
226             return 0;
227         },
228         .getColorBufferDisplay = [](uint32_t colorBuffer,
__anonb35557250e02() 229                                     uint32_t* displayId) -> int {
230             for (const auto& iter : mMultiDisplay) {
231                 if (iter.second.cb == colorBuffer) {
232                     *displayId = iter.first;
233                     return 0;
234                 }
235             }
236             return -1;
237         },
238         .setDisplayColorBuffer = [](uint32_t displayId,
__anonb35557250f02() 239                                     uint32_t colorBuffer) -> int {
240             if (mMultiDisplay.find(displayId) == mMultiDisplay.end()) {
241                 fprintf(stderr, "cannot find display %d\n", displayId);
242                 return -1;
243             }
244             mMultiDisplay[displayId].cb = colorBuffer;
245             return 0;
246         }};
247 
248 static bool sIsFolded = false;
249 
250 static const QAndroidEmulatorWindowAgent sQAndroidEmulatorWindowAgent = {
251         .getEmulatorWindow =
__anonb35557251002() 252                 [](void) {
253                     DEBUG_LOG(stderr,
254                             "window-agent-GfxStream-impl: "
255                             ".getEmulatorWindow\n");
256                     return (EmulatorWindow*)nullptr;
257                 },
258         .rotate90Clockwise =
__anonb35557251102() 259                 [](void) {
260                     DEBUG_LOG(stderr,
261                             "window-agent-GfxStream-impl: "
262                             ".rotate90Clockwise\n");
263                     return true;
264                 },
265         .rotate =
__anonb35557251202() 266                 [](int rotation) {
267                     DEBUG_LOG(stderr,
268                             "window-agent-GfxStream-impl: "
269                             ".rotate90Clockwise\n");
270                     return true;
271                 },
272         .getRotation =
__anonb35557251302() 273                 [](void) {
274                     DEBUG_LOG(stderr,
275                             "window-agent-GfxStream-impl: .getRotation\n");
276                     return (int)SKIN_ROTATION_0;
277                 },
278         .showMessage =
__anonb35557251402() 279                 [](const char* message, WindowMessageType type, int timeoutMs) {
280                     DEBUG_LOG(stderr,
281                             "window-agent-GfxStream-impl: .showMessage %s\n",
282                             message);
283                 },
284         .showMessageWithDismissCallback =
285                 [](const char* message,
286                    WindowMessageType type,
287                    const char* dismissText,
288                    void* context,
289                    void (*func)(void*),
__anonb35557251502() 290                    int timeoutMs) {
291                     DEBUG_LOG(stderr,
292                             "window-agent-GfxStream-impl: "
293                             ".showMessageWithDismissCallback %s\n",
294                             message);
295                 },
296         .fold =
__anonb35557251602() 297                 [](bool is_fold) -> bool {
298                     DEBUG_LOG(stderr, "window-agent-GfxStream-impl: .fold %d\n",
299                             is_fold);
300                     sIsFolded = is_fold;
301                     return true;
302                 },
__anonb35557251702() 303         .isFolded = [](void) -> bool { return sIsFolded; },
__anonb35557251802() 304         .getFoldedArea = [](int* x, int* y, int* w, int* h) -> bool {
305             DEBUG_LOG(stderr, "window-agent-GfxStream-impl: .getFoldedArea\n");
306             return true;
307         },
__anonb35557251902() 308         .updateFoldablePostureIndicator = [](bool) {
309             DEBUG_LOG(stderr, "window-agent-GfxStream-impl: updateFoldablePostureIndicator\n");
310         },
311         .setUIDisplayRegion =
__anonb35557251a02() 312                 [](int x_offset, int y_offset, int w, int h) {
313                     DEBUG_LOG(stderr,
314                             "window-agent-GfxStream-impl: .setUIDisplayRegion "
315                             "%d %d %dx%d\n",
316                             x_offset, y_offset, w, h);
317                 },
318         .getMultiDisplay = 0,
__anonb35557251b02() 319         .setNoSkin = [](void) {},
__anonb35557251c02() 320         .restoreSkin = [](void) {},
321         .updateUIMultiDisplayPage =
__anonb35557251d02() 322                 [](uint32_t id) {
323                     DEBUG_LOG(stderr, "updateMultiDisplayPage\n");
324                 },
325         .getMonitorRect =
__anonb35557251e02() 326                 [](uint32_t* w, uint32_t* h) -> bool {
327                     if (w)
328                         *w = 2500;
329                     if (h)
330                         *h = 1600;
331                     return true;
332                 },
333 };
334 
335 static const QAndroidVmOperations sQAndroidVmOperations = {
__anonb35557251f02() 336         .vmStop = []() -> bool {
337             DEBUG_LOG(stderr, "goldfish-opengl vm ops: vm stop\n");
338             return true;
339         },
__anonb35557252002() 340         .vmStart = []() -> bool {
341             DEBUG_LOG(stderr, "goldfish-opengl vm ops: vm start\n");
342             return true;
343         },
344         .vmReset =
__anonb35557252102() 345                 []() { DEBUG_LOG(stderr, "goldfish-opengl vm ops: vm reset\n"); },
346         .vmShutdown =
__anonb35557252202() 347                 []() { DEBUG_LOG(stderr, "goldfish-opengl vm ops: vm reset\n"); },
__anonb35557252302() 348         .vmPause = []() -> bool {
349             DEBUG_LOG(stderr, "goldfish-opengl vm ops: vm pause\n");
350             return true;
351         },
__anonb35557252402() 352         .vmResume = []() -> bool {
353             DEBUG_LOG(stderr, "goldfish-opengl vm ops: vm resume\n");
354             return true;
355         },
__anonb35557252502() 356         .vmIsRunning = []() -> bool {
357             DEBUG_LOG(stderr, "goldfish-opengl vm ops: vm is running\n");
358             return true;
359         },
360         .snapshotList =
__anonb35557252602() 361                 [](void*, LineConsumerCallback, LineConsumerCallback) -> bool {
362             DEBUG_LOG(stderr, "goldfish-opengl vm ops: snapshot list\n");
363             return true;
364         },
365         .snapshotSave = [](const char* name,
366                            void* opaque,
__anonb35557252702() 367                            LineConsumerCallback) -> bool {
368             DEBUG_LOG(stderr, "gfxstream vm ops: snapshot save\n");
369             return true;
370         },
371         .snapshotLoad = [](const char* name,
372                            void* opaque,
__anonb35557252802() 373                            LineConsumerCallback) -> bool {
374             DEBUG_LOG(stderr, "gfxstream vm ops: snapshot load\n");
375             return true;
376         },
377         .snapshotDelete = [](const char* name,
378                              void* opaque,
__anonb35557252902() 379                              LineConsumerCallback errConsumer) -> bool {
380             DEBUG_LOG(stderr, "goldfish-opengl vm ops: snapshot delete\n");
381             return true;
382         },
383         .snapshotRemap = [](bool shared,
384                             void* opaque,
__anonb35557252a02() 385                             LineConsumerCallback errConsumer) -> bool {
386             DEBUG_LOG(stderr, "goldfish-opengl vm ops: snapshot remap\n");
387             return true;
388         },
389         .snapshotExport = [](const char* snapshot,
390                              const char* dest,
391                              void* opaque,
__anonb35557252b02() 392                              LineConsumerCallback errConsumer) -> bool {
393             DEBUG_LOG(stderr, "goldfish-opengl vm ops: snapshot export image\n");
394             return true;
395         },
396         .setSnapshotCallbacks =
__anonb35557252c02() 397                 [](void* opaque, const SnapshotCallbacks* callbacks) {
398                     DEBUG_LOG(stderr,
399                             "goldfish-opengl vm ops: set snapshot callbacks\n");
400                 },
401         .mapUserBackedRam =
__anonb35557252d02() 402                 [](uint64_t gpa, void* hva, uint64_t size) {
403                     DEBUG_LOG(stderr, "%s: map user backed ram\n", __func__);
404                 },
405         .unmapUserBackedRam =
__anonb35557252e02() 406                 [](uint64_t gpa, uint64_t size) {
407                     DEBUG_LOG(stderr, "%s: unmap user backed ram\n", __func__);
408                 },
409         .getVmConfiguration =
__anonb35557252f02() 410                 [](VmConfiguration* out) {
411                     DEBUG_LOG(stderr,
412                             "goldfish-opengl vm ops: get vm configuration\n");
413                 },
414         .setFailureReason =
__anonb35557253002() 415                 [](const char* name, int failureReason) {
416                     DEBUG_LOG(stderr,
417                             "goldfish-opengl vm ops: set failure reason\n");
418                 },
419         .setExiting =
__anonb35557253102() 420                 []() {
421                     DEBUG_LOG(stderr, "goldfish-opengl vm ops: set exiting\n");
422                 },
423         .allowRealAudio =
__anonb35557253202() 424                 [](bool allow) {
425                     DEBUG_LOG(stderr,
426                             "goldfish-opengl vm ops: allow real audio\n");
427                 },
428         .physicalMemoryGetAddr =
__anonb35557253302() 429                 [](uint64_t gpa) {
430                     DEBUG_LOG(stderr, "%s: physmemGetAddr\n", __func__);
431                     return (void*)nullptr;
432                 },
433         .isRealAudioAllowed =
__anonb35557253402() 434                 [](void) {
435                     DEBUG_LOG(stderr,
436                             "goldfish-opengl vm ops: is real audiop allowed\n");
437                     return true;
438                 },
439         .setSkipSnapshotSave =
__anonb35557253502() 440                 [](bool used) {
441                     DEBUG_LOG(stderr,
442                             "goldfish-opengl vm ops: set skip snapshot save\n");
443                 },
444         .isSnapshotSaveSkipped =
__anonb35557253602() 445                 []() {
446                     DEBUG_LOG(stderr,
447                             "goldfish-opengl vm ops: is snapshot save "
448                             "skipped\n");
449                     return false;
450                 },
451         .hostmemRegister = android_emulation_hostmem_register,
452         .hostmemUnregister = android_emulation_hostmem_unregister,
453         .hostmemGetInfo = android_emulation_hostmem_get_info,
454 };
455 
456 namespace android {
457 namespace emulation {
458 
459 const QAndroidVmOperations* const
android_get_QAndroidVmOperations() const460 GfxStreamAndroidConsoleFactory::android_get_QAndroidVmOperations() const {
461     return &sQAndroidVmOperations;
462 }
463 
464 const QAndroidMultiDisplayAgent* const
android_get_QAndroidMultiDisplayAgent() const465 GfxStreamAndroidConsoleFactory::android_get_QAndroidMultiDisplayAgent() const {
466     return &sMultiDisplayAgent;
467 }
468 
469 const QAndroidEmulatorWindowAgent* const
android_get_QAndroidEmulatorWindowAgent() const470 GfxStreamAndroidConsoleFactory::android_get_QAndroidEmulatorWindowAgent()
471         const {
472     return &sQAndroidEmulatorWindowAgent;
473 }
474 }  // namespace emulation
475 }  // namespace android
476