8eca47e3308885aa9756292ad639f6274657dca0
[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/shader-effects/shader-effect.h>
31 #include <dali/public-api/rendering/texture-set.h>
32
33 // INTERNAL HEADER
34 #include <dali-toolkit/public-api/visuals/batch-image-visual-properties.h>
35 #include <dali-toolkit/public-api/visuals/image-visual-properties.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 float uAlphaBlending; // Set to 1.0 for conventional alpha blending; if pre-multiplied alpha blending, set to 0.0
83   \n
84   void main()\n
85   {\n
86     gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4( uColor.rgb*max( uAlphaBlending, uColor.a ), uColor.a );\n
87   }\n
88 );
89
90 } //unnamed namespace
91
92 BatchImageVisual::BatchImageVisual( VisualFactoryCache& factoryCache, ImageAtlasManager& atlasManager )
93   : Visual::Base( factoryCache ),
94     mAtlasManager( atlasManager ),
95     mDesiredSize()
96 {
97 }
98
99 BatchImageVisual::~BatchImageVisual()
100 {
101 }
102
103 void BatchImageVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
104 {
105   std::string oldImageUrl = mImageUrl;
106   Property::Value* imageURLValue = propertyMap.Find( Dali::Toolkit::BatchImageVisual::Property::URL, Dali::Toolkit::Internal::IMAGE_URL_NAME );
107
108   if( imageURLValue )
109   {
110     imageURLValue->Get( mImageUrl );
111
112     int desiredWidth = 0;
113     Property::Value* desiredWidthValue = propertyMap.Find( Dali::Toolkit::BatchImageVisual::Property::DESIRED_WIDTH, DESIRED_WIDTH );
114     if( desiredWidthValue )
115     {
116       desiredWidthValue->Get( desiredWidth );
117     }
118
119     int desiredHeight = 0;
120     Property::Value* desiredHeightValue = propertyMap.Find( Dali::Toolkit::BatchImageVisual::Property::DESIRED_HEIGHT, DESIRED_HEIGHT );
121     if( desiredHeightValue )
122     {
123       desiredHeightValue->Get( desiredHeight );
124     }
125
126     mDesiredSize = ImageDimensions( desiredWidth, desiredHeight );
127   }
128
129   // Remove old renderer if exit.
130   if( mImpl->mRenderer )
131   {
132     if( actor ) // Remove old renderer from actor.
133     {
134       actor.RemoveRenderer( mImpl->mRenderer );
135     }
136     if( !oldImageUrl.empty() ) // Clean old renderer from cache.
137     {
138       CleanCache( oldImageUrl );
139     }
140   }
141
142   // If actor is on stage, create new renderer and apply to actor.
143   if( actor && actor.OnStage() )
144   {
145     SetOnStage( actor );
146   }
147 }
148
149 void BatchImageVisual::SetSize( const Vector2& size )
150 {
151   Visual::Base::SetSize( size );
152 }
153
154 void BatchImageVisual::GetNaturalSize( Vector2& naturalSize ) const
155 {
156   if( mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0 )
157   {
158     naturalSize.x = mDesiredSize.GetWidth();
159     naturalSize.y = mDesiredSize.GetHeight();
160     return;
161   }
162   else if( !mImageUrl.empty() )
163   {
164     ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
165     naturalSize.x = dimentions.GetWidth();
166     naturalSize.y = dimentions.GetHeight();
167     return;
168   }
169
170   naturalSize = Vector2::ZERO;
171 }
172
173 void BatchImageVisual::SetClipRect( const Rect<int>& clipRect )
174 {
175   Visual::Base::SetClipRect( clipRect );
176 }
177
178 void BatchImageVisual::InitializeRenderer( const std::string& imageUrl )
179 {
180   if( imageUrl.empty() )
181   {
182     return;
183   }
184
185   mImageUrl = imageUrl;
186   mImpl->mRenderer.Reset();
187   mAtlasRect = FULL_TEXTURE_RECT;
188
189   if( !mImpl->mCustomShader &&
190       ( strncasecmp( imageUrl.c_str(),HTTP_URL,  sizeof( HTTP_URL )  -1 ) != 0 ) && // Ignore remote images
191       ( strncasecmp( imageUrl.c_str(), HTTPS_URL, sizeof( HTTPS_URL ) -1 ) != 0 ) )
192   {
193     if( !mImpl->mRenderer )
194     {
195       TextureSet textureSet = mAtlasManager.Add(
196             mAtlasRect,
197             imageUrl,
198             mDesiredSize );
199
200       // If image doesn't fit the atlas, create new texture set with texture that
201       // is used as whole.
202       if( !textureSet )
203       {
204         BitmapLoader loader = BitmapLoader::New( imageUrl, mDesiredSize );
205         loader.Load();
206         Dali::PixelData pixelData = loader.GetPixelData();
207         Texture texture = Texture::New( TextureType::TEXTURE_2D,
208                                         pixelData.GetPixelFormat(),
209                                         pixelData.GetWidth(),
210                                         pixelData.GetHeight() );
211         texture.Upload( pixelData );
212         textureSet = TextureSet::New();
213         textureSet.SetTexture( 0, texture );
214         mAtlasRect = FULL_TEXTURE_RECT;
215       }
216
217       Geometry geometry = mFactoryCache.CreateBatchQuadGeometry( mAtlasRect );
218       Shader shader( GetBatchShader( mFactoryCache ) );
219       mImpl->mRenderer = Renderer::New( geometry, shader );
220       mImpl->mRenderer.SetTextures( textureSet );
221
222       // Turn batching on, to send message it must be on stage.
223       mImpl->mRenderer.SetProperty( Dali::Renderer::Property::BATCHING_ENABLED, true );
224     }
225     mImpl->mFlags |= Impl::IS_FROM_CACHE;
226   }
227 }
228
229 void BatchImageVisual::DoSetOnStage( Actor& actor )
230 {
231   if( !mImageUrl.empty() )
232   {
233     InitializeRenderer( mImageUrl );
234   }
235   // Turn batching on, to send message it must be on stage
236   mImpl->mRenderer.SetProperty( Dali::Renderer::Property::BATCHING_ENABLED, true );
237 }
238
239 void BatchImageVisual::DoSetOffStage( Actor& actor )
240 {
241   actor.RemoveRenderer( mImpl->mRenderer );
242
243   // If we own the image then make sure we release it when we go off stage
244   if( !mImageUrl.empty() )
245   {
246     CleanCache( mImageUrl );
247   }
248   else
249   {
250     mImpl->mRenderer.Reset();
251   }
252 }
253
254 void BatchImageVisual::DoCreatePropertyMap( Property::Map& map ) const
255 {
256   map.Clear();
257   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::BATCH_IMAGE );
258
259   if( !mImageUrl.empty() )
260   {
261     map.Insert( Toolkit::BatchImageVisual::Property::URL, mImageUrl );
262     map.Insert( Toolkit::BatchImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
263     map.Insert( Toolkit::BatchImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
264   }
265 }
266
267 Shader BatchImageVisual::GetBatchShader( VisualFactoryCache& factoryCache )
268 {
269   Shader shader = factoryCache.GetShader( VisualFactoryCache::BATCH_IMAGE_SHADER );
270   if( !shader )
271   {
272     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
273     factoryCache.SaveShader( VisualFactoryCache::BATCH_IMAGE_SHADER, shader );
274   }
275   return shader;
276 }
277
278 void BatchImageVisual::CleanCache(const std::string& url)
279 {
280   TextureSet textureSet = mImpl->mRenderer.GetTextures();
281   mImpl->mRenderer.Reset();
282   if( mFactoryCache.CleanRendererCache( url ) )
283   {
284     mAtlasManager.Remove( textureSet, mAtlasRect );
285   }
286 }
287
288
289 } // namespace Internal
290
291 } // namespace Toolkit
292
293 } // namespace Dali