Remove incorrect empty Actor handle parameter from Visuals DoInitialize
[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::DoSetProperties( 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
132 void BatchImageVisual::SetSize( const Vector2& size )
133 {
134   Visual::Base::SetSize( size );
135 }
136
137 void BatchImageVisual::GetNaturalSize( Vector2& naturalSize ) const
138 {
139   if( mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0 )
140   {
141     naturalSize.x = mDesiredSize.GetWidth();
142     naturalSize.y = mDesiredSize.GetHeight();
143     return;
144   }
145   else if( !mImageUrl.empty() )
146   {
147     ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
148     naturalSize.x = dimentions.GetWidth();
149     naturalSize.y = dimentions.GetHeight();
150     return;
151   }
152
153   naturalSize = Vector2::ZERO;
154 }
155
156 void BatchImageVisual::InitializeRenderer( const std::string& imageUrl )
157 {
158   if( imageUrl.empty() )
159   {
160     return;
161   }
162
163   mImageUrl = imageUrl;
164   mImpl->mRenderer.Reset();
165   mAtlasRect = FULL_TEXTURE_RECT;
166
167   if( !mImpl->mCustomShader &&
168       ( strncasecmp( imageUrl.c_str(),HTTP_URL,  sizeof( HTTP_URL )  -1 ) != 0 ) && // Ignore remote images
169       ( strncasecmp( imageUrl.c_str(), HTTPS_URL, sizeof( HTTPS_URL ) -1 ) != 0 ) )
170   {
171     if( !mImpl->mRenderer )
172     {
173       TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(
174             mAtlasRect,
175             imageUrl,
176             mDesiredSize );
177
178       // If image doesn't fit the atlas, create new texture set with texture that
179       // is used as whole.
180       if( !textureSet )
181       {
182         BitmapLoader loader = BitmapLoader::New( imageUrl, mDesiredSize );
183         loader.Load();
184         Dali::PixelData pixelData = loader.GetPixelData();
185         Texture texture = Texture::New( TextureType::TEXTURE_2D,
186                                         pixelData.GetPixelFormat(),
187                                         pixelData.GetWidth(),
188                                         pixelData.GetHeight() );
189         texture.Upload( pixelData );
190         textureSet = TextureSet::New();
191         textureSet.SetTexture( 0, texture );
192         mAtlasRect = FULL_TEXTURE_RECT;
193       }
194
195       Geometry geometry = mFactoryCache.CreateBatchQuadGeometry( mAtlasRect );
196       Shader shader( GetBatchShader( mFactoryCache ) );
197       mImpl->mRenderer = Renderer::New( geometry, shader );
198       mImpl->mRenderer.SetTextures( textureSet );
199
200       // Turn batching on, to send message it must be on stage.
201       mImpl->mRenderer.SetProperty( Dali::Renderer::Property::BATCHING_ENABLED, true );
202     }
203     mImpl->mFlags |= Impl::IS_FROM_CACHE;
204   }
205 }
206
207 void BatchImageVisual::DoSetOnStage( Actor& actor )
208 {
209   if( !mImageUrl.empty() )
210   {
211     InitializeRenderer( mImageUrl );
212   }
213   // Turn batching on, to send message it must be on stage
214   mImpl->mRenderer.SetProperty( Dali::Renderer::Property::BATCHING_ENABLED, true );
215
216   actor.AddRenderer( mImpl->mRenderer );
217 }
218
219 void BatchImageVisual::DoSetOffStage( Actor& actor )
220 {
221   actor.RemoveRenderer( mImpl->mRenderer );
222
223   // If we own the image then make sure we release it when we go off stage
224   if( !mImageUrl.empty() )
225   {
226     CleanCache( mImageUrl );
227   }
228   else
229   {
230     mImpl->mRenderer.Reset();
231   }
232 }
233
234 void BatchImageVisual::DoCreatePropertyMap( Property::Map& map ) const
235 {
236   map.Clear();
237   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
238
239   if( !mImageUrl.empty() )
240   {
241     map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
242     map.Insert( Toolkit::ImageVisual::Property::BATCHING_ENABLED, true );
243     map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
244     map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
245   }
246 }
247
248 void BatchImageVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
249 {
250   // TODO
251 }
252
253 Dali::Property::Value BatchImageVisual::DoGetProperty( Dali::Property::Index index )
254 {
255   // TODO
256   return Dali::Property::Value();
257 }
258
259 Shader BatchImageVisual::GetBatchShader( VisualFactoryCache& factoryCache )
260 {
261   Shader shader = factoryCache.GetShader( VisualFactoryCache::BATCH_IMAGE_SHADER );
262   if( !shader )
263   {
264     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
265     factoryCache.SaveShader( VisualFactoryCache::BATCH_IMAGE_SHADER, shader );
266   }
267   return shader;
268 }
269
270 void BatchImageVisual::CleanCache(const std::string& url)
271 {
272   TextureSet textureSet = mImpl->mRenderer.GetTextures();
273   mImpl->mRenderer.Reset();
274   if( mFactoryCache.CleanRendererCache( url ) )
275   {
276     mFactoryCache.GetAtlasManager()->Remove( textureSet, mAtlasRect );
277   }
278 }
279
280
281 } // namespace Internal
282
283 } // namespace Toolkit
284
285 } // namespace Dali