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