TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / graphics / emugl / RenderThread.cpp
1 /*
2 * Copyright (C) 2011 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 "anbox/graphics/emugl/RenderThread.h"
18 #include "anbox/graphics/emugl/ReadBuffer.h"
19 #include "anbox/graphics/emugl/RenderControl.h"
20 #include "anbox/graphics/emugl/RenderThreadInfo.h"
21 #include "anbox/graphics/emugl/Renderer.h"
22 #include "anbox/graphics/emugl/TimeUtils.h"
23 #include "anbox/logger.h"
24
25 #include "external/android-emugl/shared/OpenglCodecCommon/ChecksumCalculatorThreadInfo.h"
26 #include "external/android-emugl/host/include/OpenGLESDispatch/EGLDispatch.h"
27 #include "external/android-emugl/host/include/OpenGLESDispatch/GLESv1Dispatch.h"
28 #include "external/android-emugl/host/include/OpenGLESDispatch/GLESv2Dispatch.h"
29
30 #define STREAM_BUFFER_SIZE 4 * 1024 * 1024
31
32 RenderThread::RenderThread(const std::shared_ptr<Renderer> &renderer, IOStream *stream, std::mutex &m)
33     : emugl::Thread(), renderer_(renderer), m_lock(m), m_stream(stream) {}
34
35 RenderThread::~RenderThread() {
36   forceStop();
37 }
38
39 RenderThread *RenderThread::create(const std::shared_ptr<Renderer> &renderer, IOStream *stream, std::mutex &m) {
40   return new RenderThread(renderer, stream, m);
41 }
42
43 void RenderThread::forceStop() { m_stream->forceStop(); }
44
45 intptr_t RenderThread::main() {
46   RenderThreadInfo threadInfo;
47   ChecksumCalculatorThreadInfo threadChecksumInfo;
48
49   threadInfo.m_glDec.initGL(gles1_dispatch_get_proc_func, NULL);
50   threadInfo.m_gl2Dec.initGL(gles2_dispatch_get_proc_func, NULL);
51   initRenderControlContext(&threadInfo.m_rcDec);
52
53   ReadBuffer readBuf(STREAM_BUFFER_SIZE);
54
55   while (true) {
56     int stat = readBuf.getData(m_stream);
57     if (stat <= 0)
58       break;
59
60     bool progress;
61     do {
62       progress = false;
63
64       std::unique_lock<std::mutex> l(m_lock);
65
66       size_t last =
67           threadInfo.m_glDec.decode(readBuf.buf(), readBuf.validData(), m_stream);
68       if (last > 0) {
69         progress = true;
70         readBuf.consume(last);
71       }
72
73       last =
74           threadInfo.m_gl2Dec.decode(readBuf.buf(), readBuf.validData(), m_stream);
75       if (last > 0) {
76         progress = true;
77         readBuf.consume(last);
78       }
79
80       last = threadInfo.m_rcDec.decode(readBuf.buf(), readBuf.validData(), m_stream);
81       if (last > 0) {
82         readBuf.consume(last);
83         progress = true;
84       }
85
86     } while (progress);
87
88   }
89
90   threadInfo.m_gl2Dec.freeShader();
91   threadInfo.m_gl2Dec.freeProgram();
92
93   // Release references to the current thread's context/surfaces if any
94   renderer_->bindContext(0, 0, 0);
95   if (threadInfo.currContext || threadInfo.currDrawSurf || threadInfo.currReadSurf)
96     ERROR("RenderThread exiting with current context/surfaces");
97
98   renderer_->drainWindowSurface();
99   renderer_->drainRenderContext();
100
101   return 0;
102 }