[dali_2.3.42] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / adaptor-framework / shader-precompiler.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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 // CLASS HEADER
18 #include <dali/integration-api/adaptor-framework/shader-precompiler.h>
19
20 // EXTERNAL INCLUDES
21 #include <dali/integration-api/debug.h>
22
23 namespace Dali
24 {
25 std::unique_ptr<ShaderPreCompiler> ShaderPreCompiler::mInstance = nullptr;
26 std::once_flag ShaderPreCompiler::mOnceFlag;
27
28 ShaderPreCompiler::ShaderPreCompiler()
29 : mRawShaderList(),
30   mPrecompiled(false),
31   mEnabled(false)
32 {
33 }
34
35 ShaderPreCompiler& ShaderPreCompiler::Get()
36 {
37   std::call_once(mOnceFlag, []()
38                  { mInstance.reset(new ShaderPreCompiler); });
39
40   return *(mInstance.get());
41 }
42
43 void ShaderPreCompiler::AddPreCompiledProgram(Graphics::UniquePtr<Dali::Graphics::Program> program)
44 {
45   mProgram.push_back(move(program));
46 }
47
48 void ShaderPreCompiler::GetPreCompileShaderList(std::vector<RawShaderData>& shaderList)
49 {
50   // move shader list
51   shaderList = mRawShaderList;
52 }
53
54 void ShaderPreCompiler::SavePreCompileShaderList(std::vector<RawShaderData>& shaderList)
55 {
56   mRawShaderList = shaderList;
57   mPrecompiled = true;
58   Awake();
59 }
60
61 bool ShaderPreCompiler::IsReady() const
62 {
63   return mPrecompiled;
64 }
65
66 void ShaderPreCompiler::Enable()
67 {
68   mEnabled = true;
69 }
70
71 bool ShaderPreCompiler::IsEnable()
72 {
73   return mEnabled;
74 }
75
76 void ShaderPreCompiler::Wait()
77 {
78   ConditionalWait::ScopedLock lock(mConditionalWait);
79   {
80     Dali::Mutex::ScopedLock mutexLock(mMutex);
81     if(!mNeedsSleep)
82     {
83       return;
84     }
85   }
86
87   mConditionalWait.Wait(lock);
88 }
89
90 void ShaderPreCompiler::Awake()
91 {
92   ConditionalWait::ScopedLock lock(mConditionalWait);
93   Dali::Mutex::ScopedLock mutexLock(mMutex);
94   mNeedsSleep = false;
95   mConditionalWait.Notify(lock);
96 }
97 } // namespace Dali