2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/legacy/common/tizen-platform-abstraction.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/bitmap.h>
27 #include <dali/integration-api/resource-types.h>
28 #include <dali/public-api/signals/callback.h>
31 #include <dali/internal/adaptor/common/adaptor-impl.h>
32 #include <dali/internal/imaging/common/image-loader.h>
33 #include <dali/internal/system/common/file-reader.h>
34 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
39 namespace TizenPlatform
42 struct TizenPlatformAbstraction::TimerCallback : ConnectionTracker
45 TizenPlatformAbstraction* mOwner;
46 CallbackBase* mCallback;
47 const uint32_t mIdNumber;
49 static uint32_t sNextTimerId;
51 TimerCallback(TizenPlatformAbstraction* owner, CallbackBase* callback, uint32_t ms)
52 : mTimer(Dali::Timer::New(ms)),
55 mIdNumber(sNextTimerId++)
57 mTimer.TickSignal().Connect( this, &TimerCallback::Tick );
63 mOwner->RunTimerFunction(*this);
68 uint32_t TizenPlatformAbstraction::TimerCallback::sNextTimerId = 0;
70 TizenPlatformAbstraction::TizenPlatformAbstraction()
71 : mDataStoragePath( "" ),
78 TizenPlatformAbstraction::~TizenPlatformAbstraction()
82 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
84 FittingMode::Type fittingMode,
85 SamplingMode::Type samplingMode,
86 bool orientationCorrection )
88 return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
91 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
93 FittingMode::Type fittingMode,
94 SamplingMode::Type samplingMode,
95 bool orientationCorrection )
97 return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
100 Integration::ResourcePointer TizenPlatformAbstraction::LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& resourcePath)
102 return ImageLoader::LoadImageSynchronously( resource, resourcePath );
105 Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer( const Integration::BitmapResourceType& resource, uint8_t * buffer, size_t size )
107 Integration::BitmapPtr resultBitmap;
108 Dali::Devel::PixelBuffer bitmap;
110 Dali::Internal::Platform::FileReader fileReader( buffer, size );
111 FILE * const fp = fileReader.GetFile();
114 bool result = ImageLoader::ConvertStreamToBitmap( resource, "", fp, bitmap );
115 if ( !result || !bitmap )
118 DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
122 Integration::Bitmap::Profile profile{Integration::Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
124 // For backward compatibility the Bitmap must be created
125 auto retval = Integration::Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
127 retval->GetPackedPixelsProfile()->ReserveBuffer(
128 bitmap.GetPixelFormat(),
135 auto& impl = Dali::GetImplementation(bitmap);
137 std::copy( impl.GetBuffer(), impl.GetBuffer()+impl.GetBufferSize(), retval->GetBuffer());
138 resultBitmap.Reset(retval);
145 bool TizenPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
149 #ifdef SHADERBIN_CACHE_ENABLED
152 // First check the system location where shaders are stored at install time:
153 path = DALI_SHADERBIN_DIR;
155 result = LoadFile( path, buffer );
157 // Fallback to the cache of shaders stored after previous runtime compilations:
158 // On desktop this looks in the current working directory that the app was launched from.
159 if( mResourceLoader && result == false )
161 path = mDataStoragePath;
163 result = LoadFile( path, buffer );
170 bool TizenPlatformAbstraction::SaveShaderBinaryFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const
174 #ifdef SHADERBIN_CACHE_ENABLED
176 // Use the cache of shaders stored after previous runtime compilations:
177 // On desktop this looks in the current working directory that the app was launched from.
178 std::string path = mDataStoragePath;
180 result = SaveFile( path, buffer, numBytes );
187 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
189 mDataStoragePath = path;
192 uint32_t TizenPlatformAbstraction::StartTimer( uint32_t milliseconds, CallbackBase* callback )
194 TimerCallback* timerCallbackPtr = new TimerCallback(this, callback, milliseconds);
196 // Stick it in the list
197 mTimerPairsWaiting.push_back(timerCallbackPtr);
199 return timerCallbackPtr->mIdNumber;
202 void TizenPlatformAbstraction::CancelTimer ( uint32_t timerId )
204 auto iter = std::remove_if(
205 mTimerPairsWaiting.begin(), mTimerPairsWaiting.end(),
206 [&timerId]( TimerCallback* timerCallbackPtr )
208 if( timerCallbackPtr->mIdNumber == timerId )
210 timerCallbackPtr->mTimer.Stop();
220 mTimerPairsWaiting.erase( iter, mTimerPairsWaiting.end() );
223 void TizenPlatformAbstraction::RunTimerFunction(TimerCallback& timerPtr)
225 CallbackBase::Execute( *timerPtr.mCallback );
227 std::vector<TimerCallback*>::iterator timerIter = std::find( mTimerPairsWaiting.begin(), mTimerPairsWaiting.end(), &timerPtr );
229 if( timerIter == std::end(mTimerPairsWaiting) )
231 DALI_ASSERT_DEBUG(false);
235 std::move(timerIter, timerIter+1, std::back_inserter(mTimerPairsSpent));
237 mTimerPairsWaiting.erase(timerIter, timerIter+1);
239 Dali::Adaptor::Get().AddIdle( MakeCallback( this, &TizenPlatformAbstraction::CleanupTimers ), false );
243 void TizenPlatformAbstraction::CleanupTimers()
245 mTimerPairsSpent.clear();
249 TizenPlatformAbstraction* CreatePlatformAbstraction()
251 return new TizenPlatformAbstraction();
254 bool SaveFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes )
256 DALI_ASSERT_DEBUG( 0 != filename.length());
261 buf.open(filename.c_str(), std::ios::out | std::ios_base::trunc | std::ios::binary);
264 std::ostream stream(&buf);
266 // determine size of buffer
267 int length = static_cast<int>(numBytes);
269 // write contents of buffer to the file
270 stream.write(reinterpret_cast<const char*>(buffer), length);
281 } // namespace TizenPlatform