1 // Copyright (C) 2016 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 "RenderLibImpl.h"
15 
16 #include "FrameBuffer.h"
17 #include "RendererImpl.h"
18 #include "aemu/base/files/Stream.h"
19 #include "host-common/address_space_device_control_ops.h"
20 #include "host-common/crash_reporter.h"
21 #include "host-common/dma_device.h"
22 #include "host-common/emugl_vm_operations.h"
23 #include "host-common/feature_control.h"
24 #include "host-common/logging.h"
25 #include "host-common/opengl/misc.h"
26 #include "host-common/sync_device.h"
27 
28 #if GFXSTREAM_ENABLE_HOST_GLES
29 #include "OpenGLESDispatch/DispatchTables.h"
30 #include "OpenGLESDispatch/EGLDispatch.h"
31 #endif
32 
33 namespace gfxstream {
34 
setRenderer(SelectedRenderer renderer)35 void RenderLibImpl::setRenderer(SelectedRenderer renderer) {
36     emugl::setRenderer(renderer);
37 }
38 
setAvdInfo(bool phone,int api)39 void RenderLibImpl::setAvdInfo(bool phone, int api) {
40     emugl::setAvdInfo(phone, api);
41 }
42 
getGlesVersion(int * maj,int * min)43 void RenderLibImpl::getGlesVersion(int* maj, int* min) {
44     emugl::getGlesVersion(maj, min);
45 }
46 
setLogger(emugl_logger_struct logger)47 void RenderLibImpl::setLogger(emugl_logger_struct logger) {
48 #ifdef CONFIG_AEMU
49     set_gfxstream_logger(logger.coarse);
50     set_gfxstream_fine_logger(logger.fine);
51 #endif
52 }
53 
setGLObjectCounter(android::base::GLObjectCounter * counter)54 void RenderLibImpl::setGLObjectCounter(
55         android::base::GLObjectCounter* counter) {
56     emugl::setGLObjectCounter(counter);
57 }
58 
setCrashReporter(emugl_crash_reporter_t reporter)59 void RenderLibImpl::setCrashReporter(emugl_crash_reporter_t reporter) {
60     // set_emugl_crash_reporter(reporter);
61 }
62 
setFeatureController(emugl_feature_is_enabled_t featureController)63 void RenderLibImpl::setFeatureController(emugl_feature_is_enabled_t featureController) {
64     android::featurecontrol::setFeatureEnabledCallback(featureController);
65 }
66 
setSyncDevice(emugl_sync_create_timeline_t create_timeline,emugl_sync_create_fence_t create_fence,emugl_sync_timeline_inc_t timeline_inc,emugl_sync_destroy_timeline_t destroy_timeline,emugl_sync_register_trigger_wait_t register_trigger_wait,emugl_sync_device_exists_t device_exists)67 void RenderLibImpl::setSyncDevice
68     (emugl_sync_create_timeline_t create_timeline,
69      emugl_sync_create_fence_t create_fence,
70      emugl_sync_timeline_inc_t timeline_inc,
71      emugl_sync_destroy_timeline_t destroy_timeline,
72      emugl_sync_register_trigger_wait_t register_trigger_wait,
73      emugl_sync_device_exists_t device_exists) {
74     emugl::set_emugl_sync_create_timeline(create_timeline);
75     emugl::set_emugl_sync_create_fence(create_fence);
76     emugl::set_emugl_sync_timeline_inc(timeline_inc);
77     emugl::set_emugl_sync_destroy_timeline(destroy_timeline);
78     emugl::set_emugl_sync_register_trigger_wait(register_trigger_wait);
79     emugl::set_emugl_sync_device_exists(device_exists);
80 }
81 
setDmaOps(emugl_dma_ops ops)82 void RenderLibImpl::setDmaOps(emugl_dma_ops ops) {
83     emugl::set_emugl_dma_get_host_addr(ops.get_host_addr);
84     emugl::set_emugl_dma_unlock(ops.unlock);
85 }
86 
setVmOps(const QAndroidVmOperations & vm_operations)87 void RenderLibImpl::setVmOps(const QAndroidVmOperations &vm_operations) {
88     set_emugl_vm_operations(vm_operations);
89     address_space_set_vm_operations(&get_emugl_vm_operations());
90 }
91 
setAddressSpaceDeviceControlOps(struct address_space_device_control_ops * ops)92 void RenderLibImpl::setAddressSpaceDeviceControlOps(struct address_space_device_control_ops* ops) {
93     set_emugl_address_space_device_control_ops(ops);
94 }
95 
setWindowOps(const QAndroidEmulatorWindowAgent & window_operations,const QAndroidMultiDisplayAgent & multi_display_operations)96 void RenderLibImpl::setWindowOps(const QAndroidEmulatorWindowAgent &window_operations,
97                                  const QAndroidMultiDisplayAgent &multi_display_operations) {
98     emugl::set_emugl_window_operations(window_operations);
99     emugl::set_emugl_multi_display_operations(multi_display_operations);
100 }
101 
setUsageTracker(android::base::CpuUsage * cpuUsage,android::base::MemoryTracker * memUsage)102 void RenderLibImpl::setUsageTracker(android::base::CpuUsage* cpuUsage,
103                                     android::base::MemoryTracker* memUsage) {
104     emugl::setCpuUsage(cpuUsage);
105     emugl::setMemoryTracker(memUsage);
106 }
107 
setGrallocImplementation(GrallocImplementation gralloc)108 void RenderLibImpl::setGrallocImplementation(GrallocImplementation gralloc) {
109     emugl::setGrallocImplementation(gralloc);
110 }
111 
getOpt(RenderOpt * opt)112 bool RenderLibImpl::getOpt(RenderOpt* opt) {
113     FrameBuffer* fb  = FrameBuffer::getFB();
114     if (fb == nullptr) {
115         return false;
116     }
117 
118 #if GFXSTREAM_ENABLE_HOST_GLES
119     opt->display = fb->getDisplay();
120     opt->surface = fb->getWindowSurface();
121     opt->config = fb->getConfig();
122 #endif
123     return (opt->display && opt->surface  && opt->config);
124 }
125 
initRenderer(int width,int height,gfxstream::host::FeatureSet features,bool useSubWindow,bool egl2egl)126 RendererPtr RenderLibImpl::initRenderer(int width, int height,
127                                         gfxstream::host::FeatureSet features,
128                                         bool useSubWindow, bool egl2egl) {
129     if (!mRenderer.expired()) {
130         return nullptr;
131     }
132 
133     const auto res = std::make_shared<RendererImpl>();
134     if (!res->initialize(width, height, features, useSubWindow, egl2egl)) {
135         return nullptr;
136     }
137     mRenderer = res;
138     return res;
139 }
140 
impl_onLastCbRef(uint32_t handle)141 static void impl_onLastCbRef(uint32_t handle) {
142     FrameBuffer* fb = FrameBuffer::getFB();
143     if (fb)
144         fb->onLastColorBufferRef(handle);
145 }
146 
getOnLastColorBufferRef()147 OnLastColorBufferRef RenderLibImpl::getOnLastColorBufferRef() {
148     return (OnLastColorBufferRef)impl_onLastCbRef;
149 }
150 
151 }  // namespace gfxstream
152