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