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