2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "batch-image-visual.h"
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>
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>
54 const char HTTP_URL[] = "http://";
55 const char HTTPS_URL[] = "https://";
58 const char * const DESIRED_WIDTH( "desiredWidth" );
59 const char * const DESIRED_HEIGHT( "desiredHeight" );
61 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
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
73 vTexCoord = aTexCoord;\n
74 gl_Position = uMvpMatrix * vec4( aPosition, 0.0, 1.0 );\n
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
86 return vec4( mixColor.rgb * mix( 1.0, mixColor.a, preMultipliedAlpha ), mixColor.a );\n
91 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * visualMixColor();
95 } // unnamed namespace
97 BatchImageVisualPtr BatchImageVisual::New( VisualFactoryCache& factoryCache, const std::string& url, const Property::Map& properties )
99 BatchImageVisualPtr visual = new BatchImageVisual( factoryCache );
100 visual->mImageUrl = url;
101 visual->SetProperties( properties );
106 BatchImageVisual::BatchImageVisual( VisualFactoryCache& factoryCache )
107 : Visual::Base( factoryCache ),
113 BatchImageVisual::~BatchImageVisual()
117 void BatchImageVisual::DoSetProperties( const Property::Map& propertyMap )
119 // url already passed in constructor
121 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
123 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
124 if( keyValue.first.type == Property::Key::INDEX )
126 DoSetProperty( keyValue.first.indexKey, keyValue.second );
130 if( keyValue.first == DESIRED_WIDTH )
132 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
134 else if( keyValue.first == DESIRED_HEIGHT )
136 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
142 void BatchImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
146 case Dali::Toolkit::ImageVisual::Property::DESIRED_WIDTH:
149 if( value.Get( width ) )
151 mDesiredSize.SetWidth( width );
155 DALI_LOG_ERROR("BatchImageVisual: width property has incorrect type\n");
159 case Dali::Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
162 if( value.Get( height ) )
164 mDesiredSize.SetHeight( height );
168 DALI_LOG_ERROR("BatchImageVisual: height property has incorrect type\n");
175 void BatchImageVisual::GetNaturalSize( Vector2& naturalSize )
177 if( mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0 )
179 naturalSize.x = mDesiredSize.GetWidth();
180 naturalSize.y = mDesiredSize.GetHeight();
183 else if( !mImageUrl.empty() )
185 ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
186 naturalSize.x = dimentions.GetWidth();
187 naturalSize.y = dimentions.GetHeight();
191 naturalSize = Vector2::ZERO;
194 void BatchImageVisual::InitializeRenderer( const std::string& imageUrl )
196 if( imageUrl.empty() )
201 mImageUrl = imageUrl;
202 mImpl->mRenderer.Reset();
203 mAtlasRect = FULL_TEXTURE_RECT;
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 ) )
209 if( !mImpl->mRenderer )
211 TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(
216 // If image doesn't fit the atlas, create new texture set with texture that
220 BitmapLoader loader = BitmapLoader::New( imageUrl, mDesiredSize );
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;
233 Geometry geometry = mFactoryCache.CreateBatchQuadGeometry( mAtlasRect );
234 Shader shader( GetBatchShader( mFactoryCache ) );
235 mImpl->mRenderer = Renderer::New( geometry, shader );
236 mImpl->mRenderer.SetTextures( textureSet );
238 // Turn batching on, to send message it must be on stage.
239 mImpl->mRenderer.SetProperty( Dali::DevelRenderer::Property::BATCHING_ENABLED, true );
241 mImpl->mFlags |= Impl::IS_FROM_CACHE;
245 void BatchImageVisual::DoSetOnStage( Actor& actor )
247 if( !mImageUrl.empty() )
249 InitializeRenderer( mImageUrl );
251 // Turn batching on, to send message it must be on stage
252 mImpl->mRenderer.SetProperty( Dali::DevelRenderer::Property::BATCHING_ENABLED, true );
254 actor.AddRenderer( mImpl->mRenderer );
257 void BatchImageVisual::DoSetOffStage( Actor& actor )
259 actor.RemoveRenderer( mImpl->mRenderer );
261 // If we own the image then make sure we release it when we go off stage
262 if( !mImageUrl.empty() )
264 CleanCache( mImageUrl );
268 mImpl->mRenderer.Reset();
272 void BatchImageVisual::DoCreatePropertyMap( Property::Map& map ) const
275 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE );
277 if( !mImageUrl.empty() )
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() );
286 Shader BatchImageVisual::GetBatchShader( VisualFactoryCache& factoryCache )
288 Shader shader = factoryCache.GetShader( VisualFactoryCache::BATCH_IMAGE_SHADER );
291 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
292 factoryCache.SaveShader( VisualFactoryCache::BATCH_IMAGE_SHADER, shader );
297 void BatchImageVisual::CleanCache(const std::string& url)
299 TextureSet textureSet = mImpl->mRenderer.GetTextures();
300 mImpl->mRenderer.Reset();
301 if( mFactoryCache.CleanRendererCache( url ) )
303 mFactoryCache.GetAtlasManager()->Remove( textureSet, mAtlasRect );
307 void BatchImageVisual::OnSetTransform()
309 if( mImpl->mRenderer )
311 //Register transform properties
312 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
316 } // namespace Internal
318 } // namespace Toolkit