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 float uAlphaBlending; // Set to 1.0 for conventional alpha blending; if pre-multiplied alpha blending, set to 0.0
86 gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4( uColor.rgb*max( uAlphaBlending, uColor.a ), uColor.a );\n
90 } // unnamed namespace
92 BatchImageVisualPtr BatchImageVisual::New( VisualFactoryCache& factoryCache, const std::string& url, const Property::Map& properties )
94 BatchImageVisualPtr visual = new BatchImageVisual( factoryCache );
95 visual->mImageUrl = url;
96 visual->SetProperties( properties );
101 BatchImageVisual::BatchImageVisual( VisualFactoryCache& factoryCache )
102 : Visual::Base( factoryCache ),
108 BatchImageVisual::~BatchImageVisual()
112 void BatchImageVisual::DoSetProperties( const Property::Map& propertyMap )
114 // url already passed in constructor
116 int desiredWidth = 0;
117 Property::Value* desiredWidthValue = propertyMap.Find( Dali::Toolkit::ImageVisual::Property::DESIRED_WIDTH, DESIRED_WIDTH );
118 if( desiredWidthValue )
120 desiredWidthValue->Get( desiredWidth );
123 int desiredHeight = 0;
124 Property::Value* desiredHeightValue = propertyMap.Find( Dali::Toolkit::ImageVisual::Property::DESIRED_HEIGHT, DESIRED_HEIGHT );
125 if( desiredHeightValue )
127 desiredHeightValue->Get( desiredHeight );
130 mDesiredSize = ImageDimensions( desiredWidth, desiredHeight );
133 void BatchImageVisual::GetNaturalSize( Vector2& naturalSize )
135 if( mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0 )
137 naturalSize.x = mDesiredSize.GetWidth();
138 naturalSize.y = mDesiredSize.GetHeight();
141 else if( !mImageUrl.empty() )
143 ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
144 naturalSize.x = dimentions.GetWidth();
145 naturalSize.y = dimentions.GetHeight();
149 naturalSize = Vector2::ZERO;
152 void BatchImageVisual::InitializeRenderer( const std::string& imageUrl )
154 if( imageUrl.empty() )
159 mImageUrl = imageUrl;
160 mImpl->mRenderer.Reset();
161 mAtlasRect = FULL_TEXTURE_RECT;
163 if( !mImpl->mCustomShader &&
164 ( strncasecmp( imageUrl.c_str(),HTTP_URL, sizeof( HTTP_URL ) -1 ) != 0 ) && // Ignore remote images
165 ( strncasecmp( imageUrl.c_str(), HTTPS_URL, sizeof( HTTPS_URL ) -1 ) != 0 ) )
167 if( !mImpl->mRenderer )
169 TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(
174 // If image doesn't fit the atlas, create new texture set with texture that
178 BitmapLoader loader = BitmapLoader::New( imageUrl, mDesiredSize );
180 Dali::PixelData pixelData = loader.GetPixelData();
181 Texture texture = Texture::New( TextureType::TEXTURE_2D,
182 pixelData.GetPixelFormat(),
183 pixelData.GetWidth(),
184 pixelData.GetHeight() );
185 texture.Upload( pixelData );
186 textureSet = TextureSet::New();
187 textureSet.SetTexture( 0, texture );
188 mAtlasRect = FULL_TEXTURE_RECT;
191 Geometry geometry = mFactoryCache.CreateBatchQuadGeometry( mAtlasRect );
192 Shader shader( GetBatchShader( mFactoryCache ) );
193 mImpl->mRenderer = Renderer::New( geometry, shader );
194 mImpl->mRenderer.SetTextures( textureSet );
196 // Turn batching on, to send message it must be on stage.
197 mImpl->mRenderer.SetProperty( Dali::DevelRenderer::Property::BATCHING_ENABLED, true );
199 mImpl->mFlags |= Impl::IS_FROM_CACHE;
203 void BatchImageVisual::DoSetOnStage( Actor& actor )
205 if( !mImageUrl.empty() )
207 InitializeRenderer( mImageUrl );
209 // Turn batching on, to send message it must be on stage
210 mImpl->mRenderer.SetProperty( Dali::DevelRenderer::Property::BATCHING_ENABLED, true );
212 actor.AddRenderer( mImpl->mRenderer );
215 void BatchImageVisual::DoSetOffStage( Actor& actor )
217 actor.RemoveRenderer( mImpl->mRenderer );
219 // If we own the image then make sure we release it when we go off stage
220 if( !mImageUrl.empty() )
222 CleanCache( mImageUrl );
226 mImpl->mRenderer.Reset();
230 void BatchImageVisual::DoCreatePropertyMap( Property::Map& map ) const
233 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE );
235 if( !mImageUrl.empty() )
237 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
238 map.Insert( Toolkit::ImageVisual::Property::BATCHING_ENABLED, true );
239 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
240 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
244 Shader BatchImageVisual::GetBatchShader( VisualFactoryCache& factoryCache )
246 Shader shader = factoryCache.GetShader( VisualFactoryCache::BATCH_IMAGE_SHADER );
249 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
250 factoryCache.SaveShader( VisualFactoryCache::BATCH_IMAGE_SHADER, shader );
255 void BatchImageVisual::CleanCache(const std::string& url)
257 TextureSet textureSet = mImpl->mRenderer.GetTextures();
258 mImpl->mRenderer.Reset();
259 if( mFactoryCache.CleanRendererCache( url ) )
261 mFactoryCache.GetAtlasManager()->Remove( textureSet, mAtlasRect );
265 void BatchImageVisual::OnSetTransform()
267 if( mImpl->mRenderer )
269 //Register transform properties
270 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
274 } // namespace Internal
276 } // namespace Toolkit