ResourceImage/Image split
[platform/core/uifw/dali-core.git] / dali / internal / event / images / image-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/images/image-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/object/type-registry.h>
24
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/event/resources/resource-ticket.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/event/resources/resource-client.h>
29 #include <dali/internal/event/common/stage-impl.h>
30
31 using namespace Dali::Integration;
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41
42 TypeRegistration mType( typeid(Dali::Image), typeid(Dali::BaseHandle), NULL );
43
44 Dali::SignalConnectorType signalConnector2(mType, Dali::Image::SIGNAL_IMAGE_UPLOADED, &Image::DoConnectSignal);
45
46 }
47
48 ImagePtr Image::New( NativeImage& nativeImg )
49 {
50   ImagePtr image = new Image;
51   image->Initialize();
52
53   ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
54
55   image->mWidth  = nativeImg.GetWidth();
56   image->mHeight = nativeImg.GetHeight();
57
58   const ResourceTicketPtr& ticket = resourceClient.AddNativeImage( nativeImg );
59   DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
60   image->mTicket = static_cast<ImageTicket*>(ticket.Get());
61   image->mTicket->AddObserver( *image );
62
63   return image;
64 }
65
66 bool Image::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
67 {
68   bool connected( true );
69   DALI_ASSERT_DEBUG( dynamic_cast<Image*>( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
70   Image* image = static_cast<Image*>(object);
71
72   if(Dali::Image::SIGNAL_IMAGE_UPLOADED == signalName)
73   {
74     image->UploadedSignal().Connect( tracker, functor );
75   }
76   else
77   {
78     // signalName does not match any signal
79     connected = false;
80   }
81
82   return connected;
83 }
84
85 ResourceId Image::GetResourceId() const
86 {
87   ResourceId ret = mTicket ? mTicket->GetId() : 0;
88
89   return ret;
90 }
91
92 void Image::ResourceLoadingFailed(const ResourceTicket& ticket)
93 {
94   // do nothing
95 }
96
97 void Image::ResourceLoadingSucceeded(const ResourceTicket& ticket)
98 {
99   // do nothing
100 }
101
102 void Image::ResourceUploaded(const ResourceTicket& ticket)
103 {
104   mUploaded.Emit( Dali::Image( this ) );
105 }
106
107 void Image::ResourceSavingSucceeded( const ResourceTicket& ticket )
108 {
109   // do nothing
110 }
111
112 void Image::ResourceSavingFailed( const ResourceTicket& ticket )
113 {
114   // do nothing
115 }
116
117 unsigned int Image::GetWidth() const
118 {
119   return mWidth;
120 }
121
122 unsigned int Image::GetHeight() const
123 {
124   return mHeight;
125 }
126
127 Vector2 Image::GetNaturalSize() const
128 {
129   return Vector2( mWidth, mHeight );
130 }
131
132 Image::Image( ReleasePolicy releasePol )
133 : mWidth( 0 ),
134   mHeight( 0 ),
135   mConnectionCount( 0 ),
136   mReleasePolicy( releasePol )
137 {
138 }
139
140 Image::~Image()
141 {
142   if( mTicket )
143   {
144     mTicket->RemoveObserver( *this );
145     mTicket.Reset();
146   }
147
148   if( Stage::IsInstalled() )
149   {
150     UnregisterObject();
151   }
152 }
153
154 void Image::Initialize()
155 {
156   RegisterObject();
157 }
158
159 } // namespace Internal
160
161 } // namespace Dali