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 std::unique_ptr< 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)),
54 mCallback( std::unique_ptr< CallbackBase >( callback ) ),
55 mIdNumber(++sNextTimerId)
57 mTimer.TickSignal().Connect( this, &TimerCallback::Tick );
66 mOwner->RunTimerFunction(*this);
71 uint32_t TizenPlatformAbstraction::TimerCallback::sNextTimerId = 0;
73 TizenPlatformAbstraction::TizenPlatformAbstraction()
74 : mDataStoragePath( "" ),
81 TizenPlatformAbstraction::~TizenPlatformAbstraction()
85 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
87 FittingMode::Type fittingMode,
88 SamplingMode::Type samplingMode,
89 bool orientationCorrection )
91 return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
94 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
96 FittingMode::Type fittingMode,
97 SamplingMode::Type samplingMode,
98 bool orientationCorrection )
100 return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
103 Integration::ResourcePointer TizenPlatformAbstraction::LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& resourcePath)
105 return ImageLoader::LoadImageSynchronously( resource, resourcePath );
108 Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer( const Integration::BitmapResourceType& resource, uint8_t * buffer, size_t size )
110 Integration::BitmapPtr resultBitmap;
111 Dali::Devel::PixelBuffer bitmap;
113 Dali::Internal::Platform::FileReader fileReader( buffer, size );
114 FILE * const fp = fileReader.GetFile();
117 bool result = ImageLoader::ConvertStreamToBitmap( resource, "", fp, bitmap );
118 if ( !result || !bitmap )
121 DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
125 Integration::Bitmap::Profile profile{Integration::Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
127 // For backward compatibility the Bitmap must be created
128 auto retval = Integration::Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
130 retval->GetPackedPixelsProfile()->ReserveBuffer(
131 bitmap.GetPixelFormat(),
138 auto& impl = Dali::GetImplementation(bitmap);
140 std::copy( impl.GetBuffer(), impl.GetBuffer()+impl.GetBufferSize(), retval->GetBuffer());
141 resultBitmap.Reset(retval);
148 bool TizenPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
152 #ifdef SHADERBIN_CACHE_ENABLED
155 // First check the system location where shaders are stored at install time:
156 path = DALI_SHADERBIN_DIR;
158 result = LoadFile( path, buffer );
160 // Fallback to the cache of shaders stored after previous runtime compilations:
161 // On desktop this looks in the current working directory that the app was launched from.
162 if( mResourceLoader && result == false )
164 path = mDataStoragePath;
166 result = LoadFile( path, buffer );
173 bool TizenPlatformAbstraction::SaveShaderBinaryFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const
177 #ifdef SHADERBIN_CACHE_ENABLED
179 // Use the cache of shaders stored after previous runtime compilations:
180 // On desktop this looks in the current working directory that the app was launched from.
181 std::string path = mDataStoragePath;
183 result = SaveFile( path, buffer, numBytes );
190 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
192 mDataStoragePath = path;
195 uint32_t TizenPlatformAbstraction::StartTimer( uint32_t milliseconds, CallbackBase* callback )
197 TimerCallback* timerCallbackPtr = new TimerCallback(this, callback, milliseconds);
199 // Stick it in the list
200 mTimerPairsWaiting.push_back( std::unique_ptr< TimerCallback >( timerCallbackPtr ) );
202 return timerCallbackPtr->mIdNumber;
205 void TizenPlatformAbstraction::CancelTimer ( uint32_t timerId )
207 auto iter = std::remove_if(
208 mTimerPairsWaiting.begin(), mTimerPairsWaiting.end(),
209 [&timerId]( std::unique_ptr< TimerCallback >& timerCallbackPtr )
211 if( timerCallbackPtr->mIdNumber == timerId )
213 timerCallbackPtr->mTimer.Stop();
223 mTimerPairsWaiting.erase( iter, mTimerPairsWaiting.end() );
226 void TizenPlatformAbstraction::RunTimerFunction(TimerCallback& timerPtr)
228 CallbackBase::Execute( *timerPtr.mCallback );
230 std::vector< std::unique_ptr< TimerCallback > >::iterator timerIter = std::find_if( mTimerPairsWaiting.begin(), mTimerPairsWaiting.end(),
231 [&]( std::unique_ptr< TimerCallback >& p )
232 { return p.get() == &timerPtr;} );
234 if( timerIter == std::end(mTimerPairsWaiting) )
236 DALI_ASSERT_DEBUG(false);
240 std::move(timerIter, timerIter+1, std::back_inserter(mTimerPairsSpent));
242 mTimerPairsWaiting.erase(timerIter, timerIter+1);
244 Dali::Adaptor::Get().AddIdle( MakeCallback( this, &TizenPlatformAbstraction::CleanupTimers ), false );
248 void TizenPlatformAbstraction::CleanupTimers()
250 mTimerPairsSpent.clear();
254 TizenPlatformAbstraction* CreatePlatformAbstraction()
256 return new TizenPlatformAbstraction();
259 bool SaveFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes )
261 DALI_ASSERT_DEBUG( 0 != filename.length());
266 buf.open(filename.c_str(), std::ios::out | std::ios_base::trunc | std::ios::binary);
269 std::ostream stream(&buf);
271 // determine size of buffer
272 int length = static_cast<int>(numBytes);
274 // write contents of buffer to the file
275 stream.write(reinterpret_cast<const char*>(buffer), length);
286 } // namespace TizenPlatform