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