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