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/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>
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>
53 const char HTTP_URL[] = "http://";
54 const char HTTPS_URL[] = "https://";
57 const char * const DESIRED_WIDTH( "desiredWidth" );
58 const char * const DESIRED_HEIGHT( "desiredHeight" );
60 const Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
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
72 vTexCoord = aTexCoord;\n
73 gl_Position = uMvpMatrix * vec4( aPosition, 0.0, 1.0 );\n
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
85 gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4( uColor.rgb*max( uAlphaBlending, uColor.a ), uColor.a );\n
89 } // unnamed namespace
91 BatchImageVisualPtr BatchImageVisual::New( VisualFactoryCache& factoryCache, const std::string& url )
93 BatchImageVisualPtr visual = new BatchImageVisual( factoryCache );
94 visual->mImageUrl = url;
98 BatchImageVisual::BatchImageVisual( VisualFactoryCache& factoryCache )
99 : Visual::Base( factoryCache ),
105 BatchImageVisual::~BatchImageVisual()
109 void BatchImageVisual::DoSetProperties( const Property::Map& propertyMap )
111 // url already passed in constructor
113 int desiredWidth = 0;
114 Property::Value* desiredWidthValue = propertyMap.Find( Dali::Toolkit::ImageVisual::Property::DESIRED_WIDTH, DESIRED_WIDTH );
115 if( desiredWidthValue )
117 desiredWidthValue->Get( desiredWidth );
120 int desiredHeight = 0;
121 Property::Value* desiredHeightValue = propertyMap.Find( Dali::Toolkit::ImageVisual::Property::DESIRED_HEIGHT, DESIRED_HEIGHT );
122 if( desiredHeightValue )
124 desiredHeightValue->Get( desiredHeight );
127 mDesiredSize = ImageDimensions( desiredWidth, desiredHeight );
130 void BatchImageVisual::GetNaturalSize( Vector2& naturalSize )
132 if( mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0 )
134 naturalSize.x = mDesiredSize.GetWidth();
135 naturalSize.y = mDesiredSize.GetHeight();
138 else if( !mImageUrl.empty() )
140 ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
141 naturalSize.x = dimentions.GetWidth();
142 naturalSize.y = dimentions.GetHeight();
146 naturalSize = Vector2::ZERO;
149 void BatchImageVisual::InitializeRenderer( const std::string& imageUrl )
151 if( imageUrl.empty() )
156 mImageUrl = imageUrl;
157 mImpl->mRenderer.Reset();
158 mAtlasRect = FULL_TEXTURE_RECT;
160 if( !mImpl->mCustomShader &&
161 ( strncasecmp( imageUrl.c_str(),HTTP_URL, sizeof( HTTP_URL ) -1 ) != 0 ) && // Ignore remote images
162 ( strncasecmp( imageUrl.c_str(), HTTPS_URL, sizeof( HTTPS_URL ) -1 ) != 0 ) )
164 if( !mImpl->mRenderer )
166 TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(
171 // If image doesn't fit the atlas, create new texture set with texture that
175 BitmapLoader loader = BitmapLoader::New( imageUrl, mDesiredSize );
177 Dali::PixelData pixelData = loader.GetPixelData();
178 Texture texture = Texture::New( TextureType::TEXTURE_2D,
179 pixelData.GetPixelFormat(),
180 pixelData.GetWidth(),
181 pixelData.GetHeight() );
182 texture.Upload( pixelData );
183 textureSet = TextureSet::New();
184 textureSet.SetTexture( 0, texture );
185 mAtlasRect = FULL_TEXTURE_RECT;
188 Geometry geometry = mFactoryCache.CreateBatchQuadGeometry( mAtlasRect );
189 Shader shader( GetBatchShader( mFactoryCache ) );
190 mImpl->mRenderer = Renderer::New( geometry, shader );
191 mImpl->mRenderer.SetTextures( textureSet );
193 // Turn batching on, to send message it must be on stage.
194 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::BATCHING_ENABLED, true );
196 mImpl->mFlags |= Impl::IS_FROM_CACHE;
200 void BatchImageVisual::DoSetOnStage( Actor& actor )
202 if( !mImageUrl.empty() )
204 InitializeRenderer( mImageUrl );
206 // Turn batching on, to send message it must be on stage
207 mImpl->mRenderer.SetProperty( Dali::Renderer::Property::BATCHING_ENABLED, true );
209 actor.AddRenderer( mImpl->mRenderer );
212 void BatchImageVisual::DoSetOffStage( Actor& actor )
214 actor.RemoveRenderer( mImpl->mRenderer );
216 // If we own the image then make sure we release it when we go off stage
217 if( !mImageUrl.empty() )
219 CleanCache( mImageUrl );
223 mImpl->mRenderer.Reset();
227 void BatchImageVisual::DoCreatePropertyMap( Property::Map& map ) const
230 map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
232 if( !mImageUrl.empty() )
234 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
235 map.Insert( Toolkit::ImageVisual::Property::BATCHING_ENABLED, true );
236 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
237 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
241 Shader BatchImageVisual::GetBatchShader( VisualFactoryCache& factoryCache )
243 Shader shader = factoryCache.GetShader( VisualFactoryCache::BATCH_IMAGE_SHADER );
246 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
247 factoryCache.SaveShader( VisualFactoryCache::BATCH_IMAGE_SHADER, shader );
252 void BatchImageVisual::CleanCache(const std::string& url)
254 TextureSet textureSet = mImpl->mRenderer.GetTextures();
255 mImpl->mRenderer.Reset();
256 if( mFactoryCache.CleanRendererCache( url ) )
258 mFactoryCache.GetAtlasManager()->Remove( textureSet, mAtlasRect );
262 void BatchImageVisual::OnSetTransform()
264 if( mImpl->mRenderer )
266 //Register transform properties
267 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
271 } // namespace Internal
273 } // namespace Toolkit