Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / effects / shader-factory.cpp
1 /*
2  * Copyright (c) 2018 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
18 // CLASS HEADER
19 #include <dali/internal/event/effects/shader-factory.h>
20
21 // EXTERNAL INCLUDES
22 #include <sstream>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/dali-core-version.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/devel-api/common/hash.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/integration-api/platform-abstraction.h>
30 #include <dali/internal/event/common/thread-local-storage.h>
31
32 namespace
33 {
34 const char* VERSION_SEPARATOR = "-";
35 const char* SHADER_SUFFIX = ".dali-bin";
36 }
37
38 namespace Dali
39 {
40
41 namespace Internal
42 {
43
44 namespace
45 {
46
47 /**
48  * @brief Generates a filename for a shader binary based on the hash value passed in.
49  * @param[in] shaderHash A hash over shader sources.
50  * @param[out] filename A string to overwrite with the filename.
51  */
52 void shaderBinaryFilename( size_t shaderHash, std::string& filename )
53 {
54   std::stringstream binaryShaderFilenameBuilder( std::ios_base::out );
55   binaryShaderFilenameBuilder << CORE_MAJOR_VERSION << VERSION_SEPARATOR << CORE_MINOR_VERSION << VERSION_SEPARATOR << CORE_MICRO_VERSION << VERSION_SEPARATOR
56                               << shaderHash
57                               << SHADER_SUFFIX;
58   filename = binaryShaderFilenameBuilder.str();
59 }
60
61 }
62
63 ShaderFactory::ShaderFactory() = default;
64
65 ShaderFactory::~ShaderFactory()
66 {
67   // Let all the cached objects destroy themselves:
68   for( std::size_t i = 0, cacheSize = mShaderBinaryCache.Size(); i < cacheSize; ++i )
69   {
70     if( mShaderBinaryCache[i] )
71     {
72       mShaderBinaryCache[i]->Unreference();
73     }
74   }
75 }
76
77 ShaderDataPtr ShaderFactory::Load( const std::string& vertexSource, const std::string& fragmentSource, const Dali::Shader::Hint::Value hints, size_t& shaderHash )
78 {
79   // Work out the filename for the binary that the glsl source will be compiled and linked to:
80   shaderHash = CalculateHash( vertexSource.c_str(), fragmentSource.c_str() );
81   std::string binaryShaderFilename;
82   shaderBinaryFilename( shaderHash, binaryShaderFilename );
83
84   ShaderDataPtr shaderData;
85
86   /// Check a cache of previously loaded shaders:
87   for( std::size_t i = 0, cacheSize = mShaderBinaryCache.Size(); i < cacheSize; ++i )
88   {
89     if( mShaderBinaryCache[i]->GetHashValue() == shaderHash )
90     {
91       shaderData = mShaderBinaryCache[i];
92
93       DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Mem cache hit on path: \"%s\"\n", binaryShaderFilename.c_str() );
94       break;
95     }
96   }
97
98   // If memory cache failed check the file system for a binary or return a source-only ShaderData:
99   if( shaderData.Get() == nullptr )
100   {
101     // Allocate the structure that returns the loaded shader:
102     shaderData = new ShaderData( vertexSource, fragmentSource, hints );
103     shaderData->SetHashValue( shaderHash );
104     shaderData->GetBuffer().Clear();
105
106     // Try to load the binary (this will fail if the shader source has never been compiled before):
107     ThreadLocalStorage& tls = ThreadLocalStorage::Get();
108     Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
109     const bool loaded = platformAbstraction.LoadShaderBinaryFile( binaryShaderFilename, shaderData->GetBuffer() );
110
111     if( loaded )
112     {
113       MemoryCacheInsert( *shaderData );
114     }
115
116     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, loaded ?
117         "loaded on path: \"%s\"\n" :
118         "failed to load on path: \"%s\"\n",
119         binaryShaderFilename.c_str());
120   }
121
122   return shaderData;
123 }
124
125 void ShaderFactory::SaveBinary( Internal::ShaderDataPtr shaderData )
126 {
127   // Save the binary to the file system:
128   std::string binaryShaderFilename;
129   shaderBinaryFilename( shaderData->GetHashValue(), binaryShaderFilename );
130
131   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
132   Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
133   const bool saved = platformAbstraction.SaveShaderBinaryFile( binaryShaderFilename, &shaderData->GetBuffer()[0], static_cast<unsigned int>( shaderData->GetBufferSize() ) ); // don't expect buffer larger than unsigned int
134
135   // Save the binary into to memory cache:
136   MemoryCacheInsert( *shaderData );
137
138   DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, saved ? "Saved to file: %s\n" : "Save to file failed: %s\n", binaryShaderFilename.c_str() );
139   if( saved ) {} // Avoid unused variable warning in release builds
140 }
141
142 void ShaderFactory::MemoryCacheInsert( ShaderData& shaderData )
143 {
144   DALI_ASSERT_DEBUG( shaderData.GetBufferSize() > 0 );
145
146   // Save the binary into to memory cache:
147   if( shaderData.GetBufferSize() > 0 )
148   {
149     mShaderBinaryCache.Reserve( mShaderBinaryCache.Size() + 1 ); // Make sure the push won't throw after we inc the ref count.
150     shaderData.Reference();
151     mShaderBinaryCache.PushBack( &shaderData );
152     DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "CACHED BINARY FOR HASH: %u\n", shaderData.GetHashValue() );
153   }
154 }
155
156 } // namespace Internal
157
158 } // namespace Dali