2 * Copyright (c) 2015 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 "image-renderer.h"
21 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
22 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
23 #include <dali-toolkit/internal/controls/renderers/control-renderer-impl.h>
24 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
25 #include <dali/public-api/images/resource-image.h>
39 const char * const IMAGE_URL_NAME("image-url");
40 const char * const IMAGE_FITTING_MODE("image-fitting-mode");
41 const char * const IMAGE_SAMPLING_MODE("image-sampling-mode");
42 const char * const IMAGE_DESIRED_WIDTH("image-desired-width");
43 const char * const IMAGE_DESIRED_HEIGHT("image-desired-height");
45 std::string TEXTURE_UNIFORM_NAME = "sTexture";
47 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
48 attribute mediump vec2 aPosition;\n
49 varying mediump vec2 vTexCoord;\n
50 uniform mediump mat4 uMvpMatrix;\n
51 uniform mediump vec3 uSize;\n
55 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
56 vertexPosition.xyz *= uSize;\n
57 vertexPosition = uMvpMatrix * vertexPosition;\n
59 vTexCoord = aPosition + vec2(0.5);\n
60 gl_Position = vertexPosition;\n
64 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
65 varying mediump vec2 vTexCoord;\n
66 uniform sampler2D sTexture;\n
67 uniform lowp vec4 uColor;\n
71 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
77 ImageRenderer::ImageRenderer()
80 mFittingMode( FittingMode::DEFAULT ),
81 mSamplingMode( SamplingMode::DEFAULT )
85 ImageRenderer::~ImageRenderer()
89 void ImageRenderer::Initialize( RendererFactoryCache& factoryCache, const Property::Map& propertyMap )
91 Initialize(factoryCache);
93 Property::Value* imageURLValue = propertyMap.Find( IMAGE_URL_NAME );
96 imageURLValue->Get( mImageUrl );
98 Property::Value* fittingValue = propertyMap.Find( IMAGE_FITTING_MODE );
102 fittingValue->Get( fitting );
104 mFittingMode = FittingMode::DEFAULT;
105 if( fitting == "shrink-to-fit" )
107 mFittingMode = FittingMode::SHRINK_TO_FIT;
109 else if( fitting == "scale-to-fill" )
111 mFittingMode = FittingMode::SCALE_TO_FILL;
113 else if( fitting == "fit-width" )
115 mFittingMode = FittingMode::FIT_WIDTH;
117 else if( fitting == "fit-height" )
119 mFittingMode = FittingMode::FIT_HEIGHT;
121 else if( fitting == "default" )
123 mFittingMode = FittingMode::DEFAULT;
127 DALI_ASSERT_ALWAYS("Unknown fitting mode");
131 Property::Value* samplingValue = propertyMap.Find( IMAGE_SAMPLING_MODE );
134 std::string sampling;
135 samplingValue->Get( sampling );
137 mSamplingMode = SamplingMode::DEFAULT;
138 if( sampling == "box" )
140 mSamplingMode = SamplingMode::BOX;
142 else if( sampling == "nearest" )
144 mSamplingMode = SamplingMode::NEAREST;
146 else if( sampling == "linear" )
148 mSamplingMode = SamplingMode::LINEAR;
150 else if( sampling == "box-then-nearest" )
152 mSamplingMode = SamplingMode::BOX_THEN_NEAREST;
154 else if( sampling == "box-then-linear" )
156 mSamplingMode = SamplingMode::BOX_THEN_LINEAR;
158 else if( sampling == "no-filter" )
160 mSamplingMode = SamplingMode::NO_FILTER;
162 else if( sampling == "dont-care" )
164 mSamplingMode = SamplingMode::DONT_CARE;
166 else if( sampling == "default" )
168 mSamplingMode = SamplingMode::DEFAULT;
172 DALI_ASSERT_ALWAYS("Unknown sampling mode");
176 int desiredWidth = 0;
177 Property::Value* desiredWidthValue = propertyMap.Find( IMAGE_DESIRED_WIDTH );
178 if( desiredWidthValue )
180 desiredWidthValue->Get( desiredWidth );
183 int desiredHeight = 0;
184 Property::Value* desiredHeightValue = propertyMap.Find( IMAGE_DESIRED_HEIGHT );
185 if( desiredHeightValue )
187 desiredHeightValue->Get( desiredHeight );
190 mDesiredSize = ImageDimensions( desiredWidth, desiredHeight );
196 void ImageRenderer::SetSize( const Vector2& size )
198 ControlRenderer::SetSize( size );
199 // ToDo: renderer responds to the size change
202 void ImageRenderer::SetClipRect( const Rect<int>& clipRect )
204 ControlRenderer::SetClipRect( clipRect );
205 //ToDo: renderer responds to the clipRect change
208 void ImageRenderer::SetOffset( const Vector2& offset )
210 //ToDo: renderer applies the offset
213 void ImageRenderer::DoSetOnStage( Actor& actor )
215 if( !mImageUrl.empty() && !mImage )
217 mImage = Dali::ResourceImage::New( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode );
220 ApplyImageToSampler();
223 void ImageRenderer::DoSetOffStage( Actor& actor )
225 //If we own the image then make sure we release it when we go off stage
226 if( !mImageUrl.empty() )
232 void ImageRenderer::Initialize( RendererFactoryCache& factoryCache )
234 mImpl->mGeometry = factoryCache.GetGeometry( RendererFactoryCache::QUAD_GEOMETRY );
235 if( !(mImpl->mGeometry) )
237 mImpl->mGeometry = factoryCache.CreateQuadGeometry();
238 factoryCache.SaveGeometry( RendererFactoryCache::QUAD_GEOMETRY, mImpl->mGeometry );
241 mImpl->mShader = factoryCache.GetShader( RendererFactoryCache::IMAGE_SHADER );
242 if( !mImpl->mShader )
244 mImpl->mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
245 factoryCache.SaveShader( RendererFactoryCache::IMAGE_SHADER, mImpl->mShader );
248 mDesiredSize = ImageDimensions();
249 mFittingMode = FittingMode::DEFAULT;
250 mSamplingMode = SamplingMode::DEFAULT;
254 void ImageRenderer::SetImage( const std::string& imageUrl )
256 SetImage( imageUrl, 0, 0, Dali::FittingMode::DEFAULT, Dali::SamplingMode::DEFAULT );
259 void ImageRenderer::SetImage( const std::string& imageUrl, int desiredWidth, int desiredHeight, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode )
261 if( mImageUrl != imageUrl )
263 mImageUrl = imageUrl;
264 mDesiredSize = ImageDimensions( desiredWidth, desiredHeight );
265 mFittingMode = fittingMode;
266 mSamplingMode = samplingMode;
268 if( !mImageUrl.empty() && mImpl->mIsOnStage )
270 mImage = Dali::ResourceImage::New( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode );
271 ApplyImageToSampler();
280 void ImageRenderer::SetImage( Image image )
282 if( mImage != image )
285 mDesiredSize = ImageDimensions();
286 mFittingMode = FittingMode::DEFAULT;
287 mSamplingMode = SamplingMode::DEFAULT;
290 if( mImage && mImpl->mIsOnStage )
292 ApplyImageToSampler();
297 Image ImageRenderer::GetImage() const
302 void ImageRenderer::ApplyImageToSampler()
306 Material material = mImpl->mRenderer.GetMaterial();
309 for( std::size_t i = 0; i < material.GetNumberOfSamplers(); ++i )
311 Sampler sampler = material.GetSamplerAt( i );
312 if( sampler.GetUniformName() == TEXTURE_UNIFORM_NAME )
314 sampler.SetImage( mImage );
319 Sampler sampler = Sampler::New( mImage, TEXTURE_UNIFORM_NAME );
320 material.AddSampler( sampler );
325 } // namespace Internal
327 } // namespace Toolkit