2 * Copyright (c) 2021 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>
22 #include <dali/integration-api/bitmap.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/integration-api/resource-types.h>
25 #include <dali/public-api/signals/callback.h>
31 #include <dali/devel-api/adaptor-framework/file-loader.h>
32 #include <dali/internal/adaptor/common/adaptor-impl.h>
33 #include <dali/internal/imaging/common/image-loader.h>
34 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
35 #include <dali/internal/system/common/file-reader.h>
39 namespace TizenPlatform
41 struct TizenPlatformAbstraction::TimerCallback : ConnectionTracker
44 TizenPlatformAbstraction* mOwner;
45 std::unique_ptr<CallbackBase> mCallback;
46 const uint32_t mIdNumber;
48 static uint32_t sNextTimerId;
50 TimerCallback(TizenPlatformAbstraction* owner, CallbackBase* callback, uint32_t ms)
51 : mTimer(Dali::Timer::New(ms)),
53 mCallback(std::unique_ptr<CallbackBase>(callback)),
54 mIdNumber(++sNextTimerId)
56 mTimer.TickSignal().Connect(this, &TimerCallback::Tick);
65 mOwner->RunTimerFunction(*this);
70 uint32_t TizenPlatformAbstraction::TimerCallback::sNextTimerId = 0;
72 TizenPlatformAbstraction::TizenPlatformAbstraction()
73 : mDataStoragePath(""),
80 TizenPlatformAbstraction::~TizenPlatformAbstraction()
84 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize(const std::string& filename,
86 FittingMode::Type fittingMode,
87 SamplingMode::Type samplingMode,
88 bool orientationCorrection)
90 return ImageLoader::GetClosestImageSize(filename, size, fittingMode, samplingMode, orientationCorrection);
93 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize(Integration::ResourcePointer resourceBuffer,
95 FittingMode::Type fittingMode,
96 SamplingMode::Type samplingMode,
97 bool orientationCorrection)
99 return ImageLoader::GetClosestImageSize(resourceBuffer, size, fittingMode, samplingMode, orientationCorrection);
102 Integration::ResourcePointer TizenPlatformAbstraction::LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& resourcePath)
104 return ImageLoader::LoadImageSynchronously(resource, resourcePath);
107 Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer(const Integration::BitmapResourceType& resource, uint8_t* buffer, size_t size)
109 Integration::BitmapPtr resultBitmap;
110 Dali::Devel::PixelBuffer bitmap;
112 Dali::Internal::Platform::FileReader fileReader(buffer, size);
113 FILE* const fp = fileReader.GetFile();
116 bool result = ImageLoader::ConvertStreamToBitmap(resource, "", fp, bitmap);
117 if(!result || !bitmap)
120 DALI_LOG_WARNING("Unable to decode bitmap supplied as in-memory blob.\n");
124 Integration::Bitmap::Profile profile{Integration::Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
126 // For backward compatibility the Bitmap must be created
127 auto retval = Integration::Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
129 retval->GetPackedPixelsProfile()->ReserveBuffer(
130 bitmap.GetPixelFormat(),
136 auto& impl = Dali::GetImplementation(bitmap);
138 std::copy(impl.GetBuffer(), impl.GetBuffer() + impl.GetBufferSize(), retval->GetBuffer());
139 resultBitmap.Reset(retval);
146 bool TizenPlatformAbstraction::LoadShaderBinaryFile(const std::string& filename, Dali::Vector<unsigned char>& buffer) const
150 #ifdef SHADERBIN_CACHE_ENABLED
153 // First check the system location where shaders are stored at install time:
154 path = DALI_SHADERBIN_DIR;
156 result = Dali::FileLoader::ReadFile(path, buffer);
158 // Fallback to the cache of shaders stored after previous runtime compilations:
159 // On desktop this looks in the current working directory that the app was launched from.
162 path = mDataStoragePath;
164 result = Dali::FileLoader::ReadFile(path, buffer);
171 bool TizenPlatformAbstraction::SaveShaderBinaryFile(const std::string& filename, const unsigned char* buffer, unsigned int numBytes) const
175 #ifdef SHADERBIN_CACHE_ENABLED
177 // Use the cache of shaders stored after previous runtime compilations:
178 // On desktop this looks in the current working directory that the app was launched from.
179 std::string path = mDataStoragePath;
181 result = SaveFile(path, buffer, numBytes);
188 void TizenPlatformAbstraction::SetDataStoragePath(const std::string& path)
190 mDataStoragePath = path;
193 uint32_t TizenPlatformAbstraction::StartTimer(uint32_t milliseconds, CallbackBase* callback)
195 TimerCallback* timerCallbackPtr = new TimerCallback(this, callback, milliseconds);
197 // Stick it in the list
198 mTimerPairsWaiting.push_back(std::unique_ptr<TimerCallback>(timerCallbackPtr));
200 return timerCallbackPtr->mIdNumber;
203 void TizenPlatformAbstraction::CancelTimer(uint32_t timerId)
205 auto iter = std::remove_if(
206 mTimerPairsWaiting.begin(), mTimerPairsWaiting.end(), [&timerId](std::unique_ptr<TimerCallback>& timerCallbackPtr) {
207 if(timerCallbackPtr->mIdNumber == timerId)
209 timerCallbackPtr->mTimer.Stop();
218 mTimerPairsWaiting.erase(iter, mTimerPairsWaiting.end());
221 void TizenPlatformAbstraction::RunTimerFunction(TimerCallback& timerPtr)
223 CallbackBase::Execute(*timerPtr.mCallback);
225 std::vector<std::unique_ptr<TimerCallback> >::iterator timerIter = std::find_if(mTimerPairsWaiting.begin(), mTimerPairsWaiting.end(), [&](std::unique_ptr<TimerCallback>& p) { return p.get() == &timerPtr; });
227 if(timerIter == std::end(mTimerPairsWaiting))
229 DALI_ASSERT_DEBUG(false);
233 std::move(timerIter, timerIter + 1, std::back_inserter(mTimerPairsSpent));
235 mTimerPairsWaiting.erase(timerIter, timerIter + 1);
237 Dali::Adaptor::Get().AddIdle(MakeCallback(this, &TizenPlatformAbstraction::CleanupTimers), false);
240 void TizenPlatformAbstraction::CleanupTimers()
242 mTimerPairsSpent.clear();
245 TizenPlatformAbstraction* CreatePlatformAbstraction()
247 return new TizenPlatformAbstraction();
250 bool SaveFile(const std::string& filename, const unsigned char* buffer, unsigned int numBytes)
252 DALI_ASSERT_DEBUG(0 != filename.length());
257 buf.open(filename.c_str(), std::ios::out | std::ios_base::trunc | std::ios::binary);
260 std::ostream stream(&buf);
262 // determine size of buffer
263 int length = static_cast<int>(numBytes);
265 // write contents of buffer to the file
266 stream.write(reinterpret_cast<const char*>(buffer), length);
277 } // namespace TizenPlatform