2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
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"
30 #define STREAM_BUFFER_SIZE 4 * 1024 * 1024
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) {}
35 RenderThread::~RenderThread() {
39 RenderThread *RenderThread::create(const std::shared_ptr<Renderer> &renderer, IOStream *stream, std::mutex &m) {
40 return new RenderThread(renderer, stream, m);
43 void RenderThread::forceStop() { m_stream->forceStop(); }
45 intptr_t RenderThread::main() {
46 RenderThreadInfo threadInfo;
47 ChecksumCalculatorThreadInfo threadChecksumInfo;
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);
53 ReadBuffer readBuf(STREAM_BUFFER_SIZE);
56 int stat = readBuf.getData(m_stream);
64 std::unique_lock<std::mutex> l(m_lock);
67 threadInfo.m_glDec.decode(readBuf.buf(), readBuf.validData(), m_stream);
70 readBuf.consume(last);
74 threadInfo.m_gl2Dec.decode(readBuf.buf(), readBuf.validData(), m_stream);
77 readBuf.consume(last);
80 last = threadInfo.m_rcDec.decode(readBuf.buf(), readBuf.validData(), m_stream);
82 readBuf.consume(last);
90 threadInfo.m_gl2Dec.freeShader();
91 threadInfo.m_gl2Dec.freeProgram();
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");
98 renderer_->drainWindowSurface();
99 renderer_->drainRenderContext();