[3.0] Resource ready signal for Controls (for ImageLoading)
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "visual-base-impl.h"
20
21 // EXTERNAL HEADER
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24
25 //INTERNAL HEARDER
26 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
27 #include <dali-toolkit/public-api/visuals/visual-properties.h>
28 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 namespace Visual
41 {
42
43 Base::Base( VisualFactoryCache& factoryCache )
44 : mImpl( new Impl() ),
45   mFactoryCache( factoryCache )
46 {
47 }
48
49 Base::~Base()
50 {
51   delete mImpl;
52 }
53
54 void Base::SetCustomShader( const Property::Map& shaderMap )
55 {
56   if( mImpl->mCustomShader )
57   {
58     mImpl->mCustomShader->SetPropertyMap( shaderMap );
59   }
60   else
61   {
62    mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
63   }
64 }
65
66 void Base::Initialize( Actor& actor, const Property::Map& propertyMap )
67 {
68
69   Property::Value* customShaderValue = propertyMap.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
70   if( customShaderValue )
71   {
72     Property::Map shaderMap;
73     if( customShaderValue->Get( shaderMap ) )
74     {
75       SetCustomShader( shaderMap );
76     }
77   }
78
79   Property::Value* premultipliedAlphaValue = propertyMap.Find( Toolkit::DevelVisual::Property::PREMULTIPLIED_ALPHA, PREMULTIPLIED_ALPHA );
80   if( premultipliedAlphaValue )
81   {
82     bool premultipliedAlpha( false );
83     if( premultipliedAlphaValue->Get( premultipliedAlpha ) )
84     {
85       EnablePreMultipliedAlpha( premultipliedAlpha );
86     }
87   }
88
89   DoInitialize( actor, propertyMap );
90 }
91
92 void Base::SetSize( const Vector2& size )
93 {
94   mImpl->mSize = size;
95 }
96
97 const Vector2& Base::GetSize() const
98 {
99   return mImpl->mSize;
100 }
101
102 void Base::SetName( const std::string& name )
103 {
104   mImpl->mName = name;
105 }
106
107 const std::string& Base::GetName() const
108 {
109   return mImpl->mName;
110 }
111
112 void Base::GetNaturalSize( Vector2& naturalSize ) const
113 {
114   naturalSize = Vector2::ZERO;
115 }
116
117 void Base::SetClipRect( const Rect<int>& clipRect )
118 {
119 }
120
121 void Base::SetOffset( const Vector2& offset )
122 {
123   mImpl->mOffset = offset;
124 }
125
126 void Base::SetDepthIndex( float index )
127 {
128   mImpl->mDepthIndex = index;
129   if( mImpl->mRenderer )
130   {
131     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
132   }
133 }
134
135 float Base::GetDepthIndex() const
136 {
137   return mImpl->mDepthIndex;
138 }
139
140 void Base::SetOnStage( Actor& actor )
141 {
142   // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
143   // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
144   DoSetOnStage( actor );
145
146   mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
147   mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
148   mImpl->mFlags |= Impl::IS_ON_STAGE;
149 }
150
151 void Base::SetOffStage( Actor& actor )
152 {
153   if( GetIsOnStage() )
154   {
155     DoSetOffStage( actor );
156
157     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
158   }
159 }
160
161 void Base::EnablePreMultipliedAlpha( bool preMultipled )
162 {
163   if( preMultipled )
164   {
165     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
166   }
167   else
168   {
169     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
170   }
171
172   if( mImpl->mRenderer )
173   {
174     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
175   }
176 }
177
178 bool Base::IsPreMultipliedAlphaEnabled() const
179 {
180   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
181 }
182
183 void Base::DoSetOffStage( Actor& actor )
184 {
185   actor.RemoveRenderer( mImpl->mRenderer );
186   mImpl->mRenderer.Reset();
187 }
188
189
190 void Visual::Base::AddResourceObserver( Visual::ResourceObserver& observer)
191 {
192   mImpl->mResourceObserver = &observer;
193 }
194
195 void Visual::Base::RemoveResourceObserver( Visual::ResourceObserver& observer )
196 {
197   mImpl->mResourceObserver = NULL;
198 }
199
200 void Visual::Base::ResourceReady()
201 {
202
203   if( mImpl->mResourceReady )
204   {
205     // only inform the observer the first time the resource is ready
206     return;
207   }
208
209
210   if( mImpl->mResourceObserver )
211   {
212     mImpl->mResourceReady = true;
213
214     // observer is currently a control impl
215     mImpl->mResourceObserver->ResourceReady( *this );
216   }
217 }
218
219 bool Visual::Base::IsResourceReady() const
220 {
221   return mImpl->mResourceReady;
222 }
223
224 void Base::CreatePropertyMap( Property::Map& map ) const
225 {
226   DoCreatePropertyMap( map );
227
228   if( mImpl->mCustomShader )
229   {
230     mImpl->mCustomShader->CreatePropertyMap( map );
231   }
232
233   bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
234   map.Insert( DevelVisual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
235 }
236
237 bool Base::GetIsOnStage() const
238 {
239   return mImpl->mFlags & Impl::IS_ON_STAGE;
240 }
241
242 bool Base::GetIsFromCache() const
243 {
244   return mImpl->mFlags & Impl::IS_FROM_CACHE;
245 }
246
247 } // namespace Visual
248
249 } // namespace Internal
250
251 } // namespace Toolkit
252
253 } // namespace Dali