Merge "Add borderline property for visual + Integrate some shaders in one" into devel...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image-visual-shader-factory.cpp
1 /*
2  * Copyright (c) 2021 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-toolkit/internal/visuals/image-visual-shader-factory.h>
19
20 // EXTERNAL INCLUDES
21
22 // INTERNAL INCLUDES
23 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
24 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
25 #include <dali/integration-api/debug.h>
26
27 namespace Dali
28 {
29 namespace Toolkit
30 {
31 namespace Internal
32 {
33 namespace
34 {
35 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
36
37 // global string variable to caching complate vertex shader
38 static std::string gVertexShader;
39
40 // global string variable to caching complate fragment shader (no atlas)
41 static std::string gFragmentShaderNoAtlas;
42
43 } // unnamed namespace
44
45 ImageVisualShaderFactory::ImageVisualShaderFactory()
46 {
47 }
48
49 ImageVisualShaderFactory::~ImageVisualShaderFactory()
50 {
51 }
52
53 Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, TextureAtlas atlasing, DefaultTextureWrapMode defaultTextureWrapping, RoundedCorner roundedCorner, Borderline borderline)
54 {
55   Shader shader;
56   VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
57   if(atlasing == TextureAtlas::ENABLED)
58   {
59     if(defaultTextureWrapping == DefaultTextureWrapMode::APPLY)
60     {
61       shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
62     }
63     else
64     {
65       shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
66     }
67   }
68   else
69   {
70     if(roundedCorner == RoundedCorner::ENABLED)
71     {
72       if(borderline == Borderline::ENABLED)
73       {
74         shaderType = VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE;
75       }
76       else
77       {
78         shaderType = VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER;
79       }
80     }
81     else
82     {
83       if(borderline == Borderline::ENABLED)
84       {
85         shaderType = VisualFactoryCache::IMAGE_SHADER_BORDERLINE;
86       }
87     }
88   }
89
90   shader = factoryCache.GetShader(shaderType);
91   if(!shader)
92   {
93     std::string vertexShaderPrefixList;
94     std::string fragmentShaderPrefixList;
95     if(atlasing == TextureAtlas::ENABLED)
96     {
97       if(defaultTextureWrapping == DefaultTextureWrapMode::APPLY)
98       {
99         fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP 1\n";
100       }
101       else
102       {
103         fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP 1\n";
104       }
105     }
106     else
107     {
108       if(roundedCorner == RoundedCorner::ENABLED)
109       {
110         vertexShaderPrefixList   += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
111         fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
112       }
113       if(borderline == Borderline::ENABLED)
114       {
115         vertexShaderPrefixList   += "#define IS_REQUIRED_BORDERLINE 1\n";
116         fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
117       }
118     }
119
120     shader = Shader::New(Dali::Shader::GetVertexShaderPrefix()   + vertexShaderPrefixList   + SHADER_IMAGE_VISUAL_SHADER_VERT.data(),
121                          Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
122     shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
123     factoryCache.SaveShader(shaderType, shader);
124   }
125
126   return shader;
127 }
128
129 std::string_view ImageVisualShaderFactory::GetVertexShaderSource()
130 {
131   if(gVertexShader.empty())
132   {
133     gVertexShader = Dali::Shader::GetVertexShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_VERT.data();
134   }
135
136   return gVertexShader;
137 }
138
139 std::string_view ImageVisualShaderFactory::GetFragmentShaderSource()
140 {
141   if(gFragmentShaderNoAtlas.empty())
142   {
143     gFragmentShaderNoAtlas = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_FRAG.data();
144   }
145   return gFragmentShaderNoAtlas;
146 }
147
148 } // namespace Internal
149
150 } // namespace Toolkit
151
152 } // namespace Dali