2 * Copyright (c) 2014 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 <dali/internal/event/actor-attachments/image-attachment-impl.h>
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/update/manager/update-manager.h>
25 #include <dali/internal/update/node-attachments/scene-graph-image-attachment.h>
26 #include <dali/internal/event/effects/shader-effect-impl.h>
27 #include <dali/internal/render/shaders/scene-graph-shader.h>
35 ImageAttachmentPtr ImageAttachment::New( EventThreadServices& eventThreadServices, const SceneGraph::Node& parentNode )
37 ImageAttachmentPtr attachment( new ImageAttachment( eventThreadServices ) );
39 // Transfer object ownership of scene-object to message
40 SceneGraph::ImageAttachment* sceneObject = CreateSceneObject();
41 AttachToNodeMessage( eventThreadServices.GetUpdateManager(), parentNode, sceneObject );
43 // Keep raw pointer for message passing
44 attachment->mSceneObject = sceneObject;
49 ImageAttachment::ImageAttachment( EventThreadServices& eventThreadServices )
50 : RenderableAttachment(eventThreadServices),
53 mStyle(Dali::ImageActor::STYLE_QUAD),
54 mBorder(0.45,0.45,0.1,0.1),
55 mIsPixelAreaSet(false),
56 mBorderInPixels(false),
60 mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
61 mSortModifier( 0.0f ),
62 mCullFaceMode( CullNone ),
63 mBlendingMode( BlendingMode::AUTO ),
66 mImageConnectable.Set( NULL, false );
69 ImageAttachment::~ImageAttachment()
73 void ImageAttachment::SetImage( ImagePtr& image )
75 bool onStage = OnStage();
76 // keep a reference to Image object
77 mImageConnectable.Set( image, onStage );
79 // Wait until the scene-graph attachment is connected, before providing resource ID
82 unsigned int resourceId = (image) ? image->GetResourceId() : 0u;
84 // sceneObject is being used in a separate thread; queue a message to set
85 SetTextureIdMessage( GetEventThreadServices(), *mSceneObject, resourceId );
89 ImagePtr ImageAttachment::GetImage()
91 return mImageConnectable.Get();
94 void ImageAttachment::SetPixelArea(const PixelArea& pixelArea)
96 // check to see if pixel area is actually different, using rect::operator==
97 if( pixelArea != mPixelArea )
99 // Cache for public getters
100 mPixelArea = pixelArea;
101 mIsPixelAreaSet = true;
103 // sceneObject is being used in a separate thread; queue a message to set
104 SetPixelAreaMessage( GetEventThreadServices(), *mSceneObject, mPixelArea );
108 void ImageAttachment::ClearPixelArea()
110 // Cache for public getters
111 mIsPixelAreaSet = false;
113 // sceneObject is being used in a separate thread; queue a message to set
114 ClearPixelAreaMessage( GetEventThreadServices(), *mSceneObject );
117 void ImageAttachment::SetStyle(Style style)
119 // Cache for public getters
122 // sceneObject is being used in a separate thread; queue a message to set
123 SetStyleMessage( GetEventThreadServices(), *mSceneObject, style );
126 void ImageAttachment::SetNinePatchBorder(const Vector4& border, bool inPixels)
128 // Cache for public getters
130 mBorderInPixels = inPixels;
132 // sceneObject is being used in a separate thread; queue a message to set
133 SetNinePatchBorderMessage( GetEventThreadServices(), *mSceneObject, border, inPixels );
136 SceneGraph::ImageAttachment* ImageAttachment::CreateSceneObject()
138 return SceneGraph::ImageAttachment::New( 0u );
141 const SceneGraph::ImageAttachment& ImageAttachment::GetSceneObject() const
143 DALI_ASSERT_DEBUG( mSceneObject != NULL );
144 return *mSceneObject;
147 void ImageAttachment::SetSortModifier(float modifier)
149 // Cache for actor-side getters
150 mSortModifier = modifier;
152 // attachment is being used in a separate thread; queue a message to set the value & base value
153 SetSortModifierMessage( GetEventThreadServices(), GetSceneObject(), modifier );
156 float ImageAttachment::GetSortModifier() const
158 // mSortModifier is not animatable; this is the most up-to-date value.
159 return mSortModifier;
162 void ImageAttachment::SetCullFace( CullFaceMode mode )
164 // Cache for actor-side getters
165 mCullFaceMode = mode;
167 // attachment is being used in a separate thread; queue a message to set the value
168 SetCullFaceMessage( GetEventThreadServices(), GetSceneObject(), mode );
171 CullFaceMode ImageAttachment::GetCullFace() const
173 // mCullFaceMode is not animatable; this is the most up-to-date value.
174 return mCullFaceMode;
177 void ImageAttachment::SetBlendMode( BlendingMode::Type mode )
179 mBlendingMode = mode;
181 // attachment is being used in a separate thread; queue a message to set the value
182 SetBlendingModeMessage( GetEventThreadServices(), GetSceneObject(), mode );
185 BlendingMode::Type ImageAttachment::GetBlendMode() const
187 return mBlendingMode;
190 void ImageAttachment::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
191 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
193 // Cache for actor-side getters
194 mBlendingOptions.SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
196 // attachment is being used in a separate thread; queue a message to set the value
197 SetBlendingOptionsMessage( GetEventThreadServices(), GetSceneObject(), mBlendingOptions.GetBitmask() );
200 void ImageAttachment::GetBlendFunc( BlendingFactor::Type& srcFactorRgb, BlendingFactor::Type& destFactorRgb,
201 BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
203 // These are not animatable, the cached values are up-to-date.
204 srcFactorRgb = mBlendingOptions.GetBlendSrcFactorRgb();
205 destFactorRgb = mBlendingOptions.GetBlendDestFactorRgb();
206 srcFactorAlpha = mBlendingOptions.GetBlendSrcFactorAlpha();
207 destFactorAlpha = mBlendingOptions.GetBlendDestFactorAlpha();
210 void ImageAttachment::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
212 mBlendingOptions.SetBlendEquation( equationRgb, equationAlpha );
214 // attachment is being used in a separate thread; queue a message to set the value
215 SetBlendingOptionsMessage( GetEventThreadServices(), GetSceneObject(), mBlendingOptions.GetBitmask() );
218 void ImageAttachment::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
220 // These are not animatable, the cached values are up-to-date.
221 equationRgb = mBlendingOptions.GetBlendEquationRgb();
222 equationAlpha = mBlendingOptions.GetBlendEquationAlpha();
225 void ImageAttachment::SetBlendColor( const Vector4& color )
227 if( mBlendingOptions.SetBlendColor( color ) )
229 // attachment is being used in a separate thread; queue a message to set the value
230 SetBlendColorMessage( GetEventThreadServices(), GetSceneObject(), color );
234 const Vector4& ImageAttachment::GetBlendColor() const
236 const Vector4* optionalColor = mBlendingOptions.GetBlendColor();
239 return *optionalColor;
242 return Vector4::ZERO;
245 void ImageAttachment::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
247 mSamplerBitfield = ImageSampler::PackBitfield( minFilter, magFilter );
249 SetSamplerMessage( GetEventThreadServices(), GetSceneObject(), mSamplerBitfield );
252 void ImageAttachment::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
254 minFilter = ImageSampler::GetMinifyFilterMode( mSamplerBitfield );
255 magFilter = ImageSampler::GetMagnifyFilterMode( mSamplerBitfield );
259 void ImageAttachment::SetShaderEffect(ShaderEffect& effect)
265 mShaderEffect->Disconnect();
268 mShaderEffect.Reset( &effect );
270 const SceneGraph::Shader& shader = dynamic_cast<const SceneGraph::Shader&>( *mShaderEffect->GetSceneObject() );
272 ApplyShaderMessage( GetEventThreadServices(), GetSceneObject(), shader );
274 mShaderEffect->Connect();
278 mShaderEffect = ShaderEffectPtr(&effect);
280 // Effects can only be applied when the Node is connected to scene-graph
283 ShaderEffectPtr ImageAttachment::GetShaderEffect() const
285 return mShaderEffect;
288 void ImageAttachment::RemoveShaderEffect()
292 RemoveShaderMessage( GetEventThreadServices(), GetSceneObject() );
294 // Notify shader effect
297 mShaderEffect->Disconnect();
301 mShaderEffect.Reset();
305 void ImageAttachment::OnStageConnection2()
309 const SceneGraph::Shader& shader = dynamic_cast<const SceneGraph::Shader&>( *mShaderEffect->GetSceneObject() );
311 ApplyShaderMessage( GetEventThreadServices(), GetSceneObject(), shader );
313 // Notify shader effect
314 mShaderEffect->Connect();
317 mImageConnectable.OnStageConnect();
319 // Provide resource ID when scene-graph attachment is connected
320 ImagePtr image = mImageConnectable.Get();
321 unsigned int resourceId = (image) ? image->GetResourceId() : 0u;
322 if ( 0u != resourceId )
324 SetTextureIdMessage( GetEventThreadServices(), *mSceneObject, resourceId );
328 void ImageAttachment::OnStageDisconnection2()
330 // Notify shader effect
333 mShaderEffect->Disconnect();
336 // Remove resource ID when scene-graph attachment is disconnected
337 SetTextureIdMessage( GetEventThreadServices(), *mSceneObject, 0u );
339 mImageConnectable.OnStageDisconnect();
344 } // namespace Internal