2aba159e504bc111506f4dfdb80bb802ae6511c4
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image / batch-image-visual.cpp
1 /*
2  * Copyright (c) 2016 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 "batch-image-visual.h"
20
21 // EXTERNAL HEADER
22 #include <cstring> // for strncasecmp
23 #include <dali/public-api/images/resource-image.h>
24 #include <dali/public-api/images/native-image.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
27 #include <dali/public-api/images/pixel-data.h>
28 #include <dali/public-api/rendering/texture.h>
29 #include <dali/public-api/rendering/texture-set.h>
30 #include <dali/public-api/rendering/texture-set.h>
31
32 // INTERNAL HEADER
33 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
34 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
35 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
37 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
38 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
39 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
40 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
41
42 namespace Dali
43 {
44
45 namespace Toolkit
46 {
47
48 namespace Internal
49 {
50
51 namespace
52 {
53 const char HTTP_URL[] = "http://";
54 const char HTTPS_URL[] = "https://";
55
56 // Properties:
57 const char * const DESIRED_WIDTH( "desiredWidth" );
58 const char * const DESIRED_HEIGHT( "desiredHeight" );
59
60 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
61
62 // The shader used for batched rendering. It uses interleaved data for
63 // attributes. Limitation is that all batched renderers will share same set of uniforms.
64 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
65   attribute mediump vec2 aPosition;\n
66   attribute mediump vec2 aTexCoord;\n
67   uniform mediump mat4 uMvpMatrix;\n
68   varying mediump vec2 vTexCoord;\n
69   \n
70   void main()\n
71   {\n
72     vTexCoord = aTexCoord;\n
73     gl_Position = uMvpMatrix * vec4( aPosition, 0.0, 1.0 );\n
74   }\n
75 );
76
77 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
78   varying mediump vec2 vTexCoord;\n
79   uniform sampler2D sTexture;\n
80   uniform lowp vec4 uColor;\n
81   uniform lowp float uAlphaBlending; // Set to 1.0 for conventional alpha blending; if pre-multiplied alpha blending, set to 0.0
82   \n
83   void main()\n
84   {\n
85     gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4( uColor.rgb*max( uAlphaBlending, uColor.a ), uColor.a );\n
86   }\n
87 );
88
89 } // unnamed namespace
90
91 BatchImageVisualPtr BatchImageVisual::New( VisualFactoryCache& factoryCache )
92 {
93   return new BatchImageVisual( factoryCache );
94 }
95
96 BatchImageVisual::BatchImageVisual( VisualFactoryCache& factoryCache )
97 : Visual::Base( factoryCache ),
98   mDesiredSize()
99 {
100 }
101
102 BatchImageVisual::~BatchImageVisual()
103 {
104 }
105
106 void BatchImageVisual::DoSetProperties( const Property::Map& propertyMap )
107 {
108   std::string oldImageUrl = mImageUrl;
109   Property::Value* imageURLValue = propertyMap.Find( Dali::Toolkit::ImageVisual::Property::URL, Dali::Toolkit::Internal::IMAGE_URL_NAME );
110
111   if( imageURLValue )
112   {
113     imageURLValue->Get( mImageUrl );
114
115     int desiredWidth = 0;
116     Property::Value* desiredWidthValue = propertyMap.Find( Dali::Toolkit::ImageVisual::Property::DESIRED_WIDTH, DESIRED_WIDTH );
117     if( desiredWidthValue )
118     {
119       desiredWidthValue->Get( desiredWidth );
120     }
121
122     int desiredHeight = 0;
123     Property::Value* desiredHeightValue = propertyMap.Find( Dali::Toolkit::ImageVisual::Property::DESIRED_HEIGHT, DESIRED_HEIGHT );
124     if( desiredHeightValue )
125     {
126       desiredHeightValue->Get( desiredHeight );
127     }
128
129     mDesiredSize = ImageDimensions( desiredWidth, desiredHeight );
130   }
131 }
132
133 void BatchImageVisual::SetSize( const Vector2& size )
134 {
135   Visual::Base::SetSize( size );
136 }
137
138 void BatchImageVisual::GetNaturalSize( Vector2& naturalSize ) const
139 {
140   if( mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0 )
141   {
142     naturalSize.x = mDesiredSize.GetWidth();
143     naturalSize.y = mDesiredSize.GetHeight();
144     return;
145   }
146   else if( !mImageUrl.empty() )
147   {
148     ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
149     naturalSize.x = dimentions.GetWidth();
150     naturalSize.y = dimentions.GetHeight();
151     return;
152   }
153
154   naturalSize = Vector2::ZERO;
155 }
156
157 void BatchImageVisual::InitializeRenderer( const std::string& imageUrl )
158 {
159   if( imageUrl.empty() )
160   {
161     return;
162   }
163
164   mImageUrl = imageUrl;
165   mImpl->mRenderer.Reset();
166   mAtlasRect = FULL_TEXTURE_RECT;
167
168   if( !mImpl->mCustomShader &&
169       ( strncasecmp( imageUrl.c_str(),HTTP_URL,  sizeof( HTTP_URL )  -1 ) != 0 ) && // Ignore remote images
170       ( strncasecmp( imageUrl.c_str(), HTTPS_URL, sizeof( HTTPS_URL ) -1 ) != 0 ) )
171   {
172     if( !mImpl->mRenderer )
173     {
174       TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(
175             mAtlasRect,
176             imageUrl,
177             mDesiredSize );
178
179       // If image doesn't fit the atlas, create new texture set with texture that
180       // is used as whole.
181       if( !textureSet )
182       {
183         BitmapLoader loader = BitmapLoader::New( imageUrl, mDesiredSize );
184         loader.Load();
185         Dali::PixelData pixelData = loader.GetPixelData();
186         Texture texture = Texture::New( TextureType::TEXTURE_2D,
187                                         pixelData.GetPixelFormat(),
188                                         pixelData.GetWidth(),
189                                         pixelData.GetHeight() );
190         texture.Upload( pixelData );
191         textureSet = TextureSet::New();
192         textureSet.SetTexture( 0, texture );
193         mAtlasRect = FULL_TEXTURE_RECT;
194       }
195
196       Geometry geometry = mFactoryCache.CreateBatchQuadGeometry( mAtlasRect );
197       Shader shader( GetBatchShader( mFactoryCache ) );
198       mImpl->mRenderer = Renderer::New( geometry, shader );
199       mImpl->mRenderer.SetTextures( textureSet );
200
201       // Turn batching on, to send message it must be on stage.
202       mImpl->mRenderer.SetProperty( Dali::Renderer::Property::BATCHING_ENABLED, true );
203     }
204     mImpl->mFlags |= Impl::IS_FROM_CACHE;
205   }
206 }
207
208 void BatchImageVisual::DoSetOnStage( Actor& actor )
209 {
210   if( !mImageUrl.empty() )
211   {
212     InitializeRenderer( mImageUrl );
213   }
214   // Turn batching on, to send message it must be on stage
215   mImpl->mRenderer.SetProperty( Dali::Renderer::Property::BATCHING_ENABLED, true );
216
217   actor.AddRenderer( mImpl->mRenderer );
218 }
219
220 void BatchImageVisual::DoSetOffStage( Actor& actor )
221 {
222   actor.RemoveRenderer( mImpl->mRenderer );
223
224   // If we own the image then make sure we release it when we go off stage
225   if( !mImageUrl.empty() )
226   {
227     CleanCache( mImageUrl );
228   }
229   else
230   {
231     mImpl->mRenderer.Reset();
232   }
233 }
234
235 void BatchImageVisual::DoCreatePropertyMap( Property::Map& map ) const
236 {
237   map.Clear();
238   map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
239
240   if( !mImageUrl.empty() )
241   {
242     map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
243     map.Insert( Toolkit::ImageVisual::Property::BATCHING_ENABLED, true );
244     map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
245     map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
246   }
247 }
248
249 void BatchImageVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
250 {
251   // TODO
252 }
253
254 Dali::Property::Value BatchImageVisual::DoGetProperty( Dali::Property::Index index )
255 {
256   // TODO
257   return Dali::Property::Value();
258 }
259
260 Shader BatchImageVisual::GetBatchShader( VisualFactoryCache& factoryCache )
261 {
262   Shader shader = factoryCache.GetShader( VisualFactoryCache::BATCH_IMAGE_SHADER );
263   if( !shader )
264   {
265     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
266     factoryCache.SaveShader( VisualFactoryCache::BATCH_IMAGE_SHADER, shader );
267   }
268   return shader;
269 }
270
271 void BatchImageVisual::CleanCache(const std::string& url)
272 {
273   TextureSet textureSet = mImpl->mRenderer.GetTextures();
274   mImpl->mRenderer.Reset();
275   if( mFactoryCache.CleanRendererCache( url ) )
276   {
277     mFactoryCache.GetAtlasManager()->Remove( textureSet, mAtlasRect );
278   }
279 }
280
281
282 } // namespace Internal
283
284 } // namespace Toolkit
285
286 } // namespace Dali