Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / dali / internal / event / actor-attachments / image-attachment-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/actor-attachments/image-attachment-impl.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/update/node-attachments/scene-graph-image-attachment.h>
22 #include <dali/internal/event/common/stage-impl.h>
23 #include <dali/public-api/common/dali-common.h>
24
25 using namespace std;
26
27 namespace
28 {
29 static Dali::Internal::SceneGraph::ImageAttachment::PixelArea EMPTY_PIXEL_AREA(0,0,0,0);
30 }
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 ImageAttachmentPtr ImageAttachment::New( const SceneGraph::Node& parentNode, Image* image )
39 {
40   StagePtr stage = Stage::GetCurrent();
41
42   ImageAttachmentPtr attachment( new ImageAttachment( *stage, image ) );
43
44   // Transfer object ownership of scene-object to message
45   SceneGraph::ImageAttachment* sceneObject = CreateSceneObject( image );
46   AttachToNodeMessage( stage->GetUpdateManager(), parentNode, sceneObject );
47
48   // Keep raw pointer for message passing
49   attachment->mSceneObject = sceneObject;
50
51   return attachment;
52 }
53
54 ImageAttachment::ImageAttachment(Stage& stage, Image* image)
55 : RenderableAttachment(stage),
56   mSceneObject(NULL),
57   mPixelArea(EMPTY_PIXEL_AREA),
58   mStyle(Dali::ImageActor::STYLE_QUAD),
59   mBorder(0.45,0.45,0.1,0.1),
60   mIsPixelAreaSet(false),
61   mBorderInPixels(false)
62 {
63   mImageConnectable.Set( image, false );
64 }
65
66 ImageAttachment::~ImageAttachment()
67 {
68 }
69
70 void ImageAttachment::SetImage(Image* image)
71 {
72   bool onStage = OnStage();
73   // keep a reference to Image object
74   mImageConnectable.Set( image, onStage );
75
76   // Wait until the scene-graph attachment is connected, before providing resource ID
77   if ( OnStage() )
78   {
79     unsigned int resourceId = (NULL != image) ? image->GetResourceId() : 0u;
80
81     // sceneObject is being used in a separate thread; queue a message to set
82     SetTextureIdMessage( mStage->GetUpdateInterface(), *mSceneObject, resourceId );
83   }
84 }
85
86 Dali::Image ImageAttachment::GetImage()
87 {
88   Dali::Image image;
89   Image* current = mImageConnectable.Get();
90   if ( current != NULL)
91   {
92     image = Dali::Image( current );
93   }
94   else
95   {
96     // returning uninitialized image (empty image)
97   }
98   return image;
99 }
100
101 void ImageAttachment::SetPixelArea(const PixelArea& pixelArea)
102 {
103   // check to see if pixel area is actually different, using rect::operator==
104   if( pixelArea != mPixelArea )
105   {
106     // Cache for public getters
107     mPixelArea = pixelArea;
108     mIsPixelAreaSet = true;
109
110     // sceneObject is being used in a separate thread; queue a message to set
111     SetPixelAreaMessage( mStage->GetUpdateInterface(), *mSceneObject, mPixelArea );
112   }
113 }
114
115 void ImageAttachment::ClearPixelArea()
116 {
117   // Cache for public getters
118   mIsPixelAreaSet = false;
119
120   // sceneObject is being used in a separate thread; queue a message to set
121   ClearPixelAreaMessage( mStage->GetUpdateInterface(), *mSceneObject );
122 }
123
124 void ImageAttachment::SetStyle(Style style)
125 {
126   // Cache for public getters
127   mStyle = style;
128
129   // sceneObject is being used in a separate thread; queue a message to set
130   SetStyleMessage( mStage->GetUpdateInterface(), *mSceneObject, style );
131 }
132
133 void ImageAttachment::SetNinePatchBorder(const Vector4& border, bool inPixels)
134 {
135   // Cache for public getters
136   mBorder = border;
137   mBorderInPixels = inPixels;
138
139   // sceneObject is being used in a separate thread; queue a message to set
140   SetNinePatchBorderMessage( mStage->GetUpdateInterface(), *mSceneObject, border, inPixels );
141 }
142
143 SceneGraph::ImageAttachment* ImageAttachment::CreateSceneObject( const Image* current )
144 {
145   if ( current )
146   {
147     return SceneGraph::ImageAttachment::New( current->GetResourceId() );
148   }
149
150   return SceneGraph::ImageAttachment::New( 0u );
151 }
152
153 void ImageAttachment::OnStageConnection2()
154 {
155   mImageConnectable.OnStageConnect();
156
157   // Provide resource ID when scene-graph attachment is connected
158   Image* image = mImageConnectable.Get();
159   unsigned int resourceId = (NULL != image) ? image->GetResourceId() : 0u;
160   if ( 0u != resourceId )
161   {
162     SetTextureIdMessage( mStage->GetUpdateInterface(), *mSceneObject, resourceId );
163   }
164 }
165
166 void ImageAttachment::OnStageDisconnection2()
167 {
168   // Remove resource ID when scene-graph attachment is disconnected
169   SetTextureIdMessage( mStage->GetUpdateInterface(), *mSceneObject, 0u );
170
171   mImageConnectable.OnStageDisconnect();
172 }
173
174 const SceneGraph::RenderableAttachment& ImageAttachment::GetSceneObject() const
175 {
176   DALI_ASSERT_DEBUG( mSceneObject != NULL );
177   return *mSceneObject;
178 }
179
180 } // namespace Internal
181
182 } // namespace Dali