61cbe4d3c5c4d16665155900e5e3513656d735fa
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-bitmap-loader.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 // CLASS HEADER
18 #include "toolkit-bitmap-loader.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/object/base-object.h>
22 #include <cstring>
23 #include <semaphore.h>
24
25 using namespace Dali;
26
27 namespace Dali
28 {
29 namespace
30 {
31 Dali::BitmapLoader gBitmapLoader;
32 }
33
34
35 namespace Internal
36 {
37
38 class BitmapLoader : public BaseObject
39 {
40 public:
41   static IntrusivePtr<BitmapLoader> New( const std::string& url,
42                                          ImageDimensions size,
43                                          FittingMode::Type fittingMode,
44                                          SamplingMode::Type samplingMode,
45                                          bool orientationCorrection)
46   {
47     IntrusivePtr<BitmapLoader> internal = new BitmapLoader( url, size, fittingMode, samplingMode, orientationCorrection );
48     return internal;
49   }
50
51   BitmapLoader(const std::string& url,
52                ImageDimensions size,
53                FittingMode::Type fittingMode,
54                SamplingMode::Type samplingMode,
55                bool orientationCorrection)
56   : mSize(size),
57     mPixelData(),
58     mUrl(url)
59   {
60     sem_init( &mySemaphore, 0, 0 );
61   }
62
63   ~BitmapLoader(){}
64
65   void Load()
66   {
67     size_t bufferSize  = mSize.GetWidth() * mSize.GetHeight() * 4;
68     unsigned char* buffer = static_cast<unsigned char*>(malloc(bufferSize));
69     memset(buffer, 0, bufferSize);
70
71     mPixelData = Dali::PixelData::New( buffer, bufferSize, mSize.GetWidth(), mSize.GetHeight(), Pixel::RGBA8888, Dali::PixelData::FREE);
72
73     sem_post( &mySemaphore );
74   }
75
76   Dali::PixelData GetPixelData() const
77   {
78     return mPixelData;
79   }
80
81   const std::string& GetUrl() const
82   {
83     return mUrl;
84   }
85
86   bool IsLoaded()
87   {
88     return mPixelData ? true : false;
89   }
90
91   void WaitForLoading()
92   {
93     if( mPixelData )
94     {
95       return;
96     }
97     sem_wait( &mySemaphore );
98   }
99
100   ImageDimensions mSize;
101   Dali::PixelData mPixelData;
102   const std::string mUrl;
103   sem_t mySemaphore;
104 };
105
106 } // internal
107
108 inline Internal::BitmapLoader& GetImplementation(Dali::BitmapLoader& handle)
109 {
110   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
111   BaseObject& object = handle.GetBaseObject();
112   return static_cast<Internal::BitmapLoader&>(object);
113 }
114
115 inline const Internal::BitmapLoader& GetImplementation(const Dali::BitmapLoader& handle)
116 {
117   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
118   const BaseObject& object = handle.GetBaseObject();
119   return static_cast<const Internal::BitmapLoader&>(object);
120 }
121
122 Dali::BitmapLoader Dali::BitmapLoader::New(std::string const&url, Dali::Uint16Pair size, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, bool orientationCorrection)
123 {
124   IntrusivePtr<Internal::BitmapLoader> internal = Internal::BitmapLoader::New(url, size, fittingMode, samplingMode, orientationCorrection);
125   gBitmapLoader = BitmapLoader(internal.Get());
126   return gBitmapLoader;
127 }
128
129 BitmapLoader::BitmapLoader()
130 {
131 }
132
133 Dali::BitmapLoader::BitmapLoader(Dali::BitmapLoader const& handle)
134 :BaseHandle(handle)
135 {
136 }
137 Dali::BitmapLoader::BitmapLoader(Internal::BitmapLoader* internal)
138 :BaseHandle(internal)
139 {
140 }
141 Dali::BitmapLoader::~BitmapLoader()
142 {
143 }
144 BitmapLoader& BitmapLoader::operator=(const BitmapLoader& rhs)
145 {
146   BaseHandle::operator=(rhs);
147   return *this;
148 }
149 void Dali::BitmapLoader::Load()
150 {
151   GetImplementation(*this).Load();
152 }
153 PixelData Dali::BitmapLoader::GetPixelData() const
154 {
155   return GetImplementation(*this).GetPixelData();
156 }
157 std::string Dali::BitmapLoader::GetUrl() const
158 {
159   return GetImplementation(*this).GetUrl();
160 }
161 bool Dali::BitmapLoader::IsLoaded()
162 {
163   return GetImplementation(*this).IsLoaded();
164 }
165 void Dali::BitmapLoader::WaitForLoading()
166 {
167   GetImplementation(*this).WaitForLoading();
168 }
169
170 BitmapLoader Dali::BitmapLoader::GetLatestCreated()
171 {
172   return gBitmapLoader;
173 }
174
175 void Dali::BitmapLoader::ResetLatestCreated()
176 {
177   gBitmapLoader.Reset();
178 }
179
180 } // Dali