Merge "Removed redundant resource loading & rendering code" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-set-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 <dali/internal/event/rendering/texture-set-impl.h> // Dali::Internal::TextureSet
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
24 #include <dali/internal/update/manager/update-manager.h>
25 #include <dali/internal/update/rendering/scene-graph-texture-set.h>
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31
32 TextureSetPtr TextureSet::New()
33 {
34   TextureSetPtr textureSet( new TextureSet() );
35   textureSet->Initialize();
36   return textureSet;
37 }
38
39 void TextureSet::SetTexture( size_t index, TexturePtr texture )
40 {
41   size_t textureCount( mTextures.size() );
42   if( index >= textureCount )
43   {
44     mTextures.resize(index + 1);
45
46     bool samplerExist = true;
47     if( mSamplers.size() < index + 1 )
48     {
49       mSamplers.resize( index + 1 );
50       samplerExist = false;
51     }
52
53     for( size_t i(textureCount); i<=index; ++i )
54     {
55       mTextures[i] = NULL;
56
57       if( !samplerExist )
58       {
59         mSamplers[i] = NULL;
60       }
61     }
62   }
63
64   mTextures[index]= texture;
65
66   Render::Texture* renderTexture(0);
67   if( texture )
68   {
69     renderTexture = texture->GetRenderObject();
70   }
71
72   SceneGraph::SetTextureMessage( mEventThreadServices, *mSceneObject, index, renderTexture );
73 }
74
75 Texture* TextureSet::GetTexture( size_t index ) const
76 {
77   Texture* result(0);
78   if( index < mTextures.size() )
79   {
80     result = mTextures[index].Get();
81   }
82   else
83   {
84     DALI_LOG_ERROR( "Error: Invalid index to TextureSet::GetTexture\n");
85   }
86
87   return result;
88 }
89
90 void TextureSet::SetSampler( size_t index, SamplerPtr sampler )
91 {
92   size_t samplerCount( mSamplers.size() );
93   if( samplerCount < index + 1  )
94   {
95     mSamplers.resize( index + 1 );
96     for( size_t i(samplerCount); i<=index; ++i )
97     {
98       mSamplers[i] = NULL;
99     }
100   }
101
102   mSamplers[index] = sampler;
103
104   Render::Sampler* renderSampler(0);
105   if( sampler )
106   {
107     renderSampler = sampler->GetSamplerRenderObject();
108   }
109
110   SceneGraph::SetSamplerMessage( mEventThreadServices, *mSceneObject, index, renderSampler );
111 }
112
113 Sampler* TextureSet::GetSampler( size_t index ) const
114 {
115   Sampler* result(0);
116   if( index < mSamplers.size() )
117   {
118     result = mSamplers[index].Get();
119   }
120   else
121   {
122     DALI_LOG_ERROR( "Error: Invalid index to TextureSet::GetSampler\n");
123   }
124
125   return result;
126 }
127
128 size_t TextureSet::GetTextureCount() const
129 {
130   return mTextures.size();
131 }
132
133 const SceneGraph::TextureSet* TextureSet::GetTextureSetSceneObject() const
134 {
135   return mSceneObject;
136 }
137
138 TextureSet::TextureSet()
139 :mEventThreadServices( *Stage::GetCurrent() ),
140  mSceneObject( NULL )
141 {
142 }
143
144 void TextureSet::Initialize()
145 {
146   SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
147
148   mSceneObject = SceneGraph::TextureSet::New();
149   AddTextureSetMessage( updateManager, *mSceneObject );
150 }
151
152 TextureSet::~TextureSet()
153 {
154   if( EventThreadServices::IsCoreRunning() )
155   {
156     SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
157     RemoveTextureSetMessage( updateManager, *mSceneObject );
158   }
159 }
160
161 } // namespace Internal
162 } // namespace Dali