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