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/internal/update/node-attachments/scene-graph-image-attachment.h>
23 #include <dali/internal/event/common/stage-impl.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/internal/event/effects/shader-effect-impl.h>
26 #include <dali/internal/render/shaders/scene-graph-shader.h>
34 ImageAttachmentPtr ImageAttachment::New( Stage& stage, const SceneGraph::Node& parentNode )
36 ImageAttachmentPtr attachment( new ImageAttachment( stage ) );
38 // Transfer object ownership of scene-object to message
39 SceneGraph::ImageAttachment* sceneObject = CreateSceneObject();
40 AttachToNodeMessage( stage.GetUpdateManager(), parentNode, sceneObject );
42 // Keep raw pointer for message passing
43 attachment->mSceneObject = sceneObject;
48 ImageAttachment::ImageAttachment( Stage& stage )
49 : RenderableAttachment(stage),
52 mStyle(Dali::ImageActor::STYLE_QUAD),
53 mBorder(0.45,0.45,0.1,0.1),
54 mIsPixelAreaSet(false),
55 mBorderInPixels(false),
59 mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
60 mSortModifier( 0.0f ),
61 mCullFaceMode( CullNone ),
62 mBlendingMode( BlendingMode::AUTO ),
65 mImageConnectable.Set( NULL, false );
68 ImageAttachment::~ImageAttachment()
72 void ImageAttachment::SetImage( ImagePtr& image )
74 bool onStage = OnStage();
75 // keep a reference to Image object
76 mImageConnectable.Set( image, onStage );
78 // Wait until the scene-graph attachment is connected, before providing resource ID
81 unsigned int resourceId = (image) ? image->GetResourceId() : 0u;
83 // sceneObject is being used in a separate thread; queue a message to set
84 SetTextureIdMessage( mStage->GetUpdateInterface(), *mSceneObject, resourceId );
88 ImagePtr ImageAttachment::GetImage()
90 return mImageConnectable.Get();
93 void ImageAttachment::SetPixelArea(const PixelArea& pixelArea)
95 // check to see if pixel area is actually different, using rect::operator==
96 if( pixelArea != mPixelArea )
98 // Cache for public getters
99 mPixelArea = pixelArea;
100 mIsPixelAreaSet = true;
102 // sceneObject is being used in a separate thread; queue a message to set
103 SetPixelAreaMessage( mStage->GetUpdateInterface(), *mSceneObject, mPixelArea );
107 void ImageAttachment::ClearPixelArea()
109 // Cache for public getters
110 mIsPixelAreaSet = false;
112 // sceneObject is being used in a separate thread; queue a message to set
113 ClearPixelAreaMessage( mStage->GetUpdateInterface(), *mSceneObject );
116 void ImageAttachment::SetStyle(Style style)
118 // Cache for public getters
121 // sceneObject is being used in a separate thread; queue a message to set
122 SetStyleMessage( mStage->GetUpdateInterface(), *mSceneObject, style );
125 void ImageAttachment::SetNinePatchBorder(const Vector4& border, bool inPixels)
127 // Cache for public getters
129 mBorderInPixels = inPixels;
131 // sceneObject is being used in a separate thread; queue a message to set
132 SetNinePatchBorderMessage( mStage->GetUpdateInterface(), *mSceneObject, border, inPixels );
135 SceneGraph::ImageAttachment* ImageAttachment::CreateSceneObject()
137 return SceneGraph::ImageAttachment::New( 0u );
140 const SceneGraph::RenderableAttachment& ImageAttachment::GetSceneObject() const
142 DALI_ASSERT_DEBUG( mSceneObject != NULL );
143 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), 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( mStage->GetUpdateInterface(), *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( mStage->GetUpdateInterface(), *mSceneObject, 0u );
339 mImageConnectable.OnStageDisconnect();
344 } // namespace Internal