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