From: Xiangyin Ma Date: Wed, 21 Oct 2015 15:38:09 +0000 (+0100) Subject: [3.0] Added EventThreadCallback class & BitmapLoader::Load() API X-Git-Tag: accepted/tizen/mobile/20151102.112623^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F99%2F50399%2F1;p=platform%2Fcore%2Fuifw%2Fdali-adaptor.git [3.0] Added EventThreadCallback class & BitmapLoader::Load() API Change-Id: Ib17156bd87cf917582e39a17cb3bc6843cb12c24 --- diff --git a/adaptors/common/bitmap-loader-impl.cpp b/adaptors/common/bitmap-loader-impl.cpp index 69505df..1e6c545 100644 --- a/adaptors/common/bitmap-loader-impl.cpp +++ b/adaptors/common/bitmap-loader-impl.cpp @@ -17,9 +17,6 @@ // EXTERNAL INCLUDES #include -#include -#include - // INTERNAL INCLUDES #include "bitmap-loader-impl.h" #include "image-loaders/image-loader.h" @@ -29,15 +26,25 @@ namespace Dali namespace Internal { -IntrusivePtr BitmapLoader::New(const std::string& filename) +IntrusivePtr BitmapLoader::New(const std::string& url, + ImageDimensions size, + FittingMode::Type fittingMode, + SamplingMode::Type samplingMode, + bool orientationCorrection) { - IntrusivePtr internal = new BitmapLoader(); - internal->Initialize(filename); + IntrusivePtr internal = new BitmapLoader( url, size, fittingMode, samplingMode, orientationCorrection ); return internal; } -BitmapLoader::BitmapLoader() -: mBitmap(NULL) +BitmapLoader::BitmapLoader(const std::string& url, + ImageDimensions size, + FittingMode::Type fittingMode, + SamplingMode::Type samplingMode, + bool orientationCorrection) +: mResourceType( size, fittingMode, samplingMode, orientationCorrection ), + mBitmap(NULL), + mUrl(url), + mIsLoaded( false ) { } @@ -45,38 +52,57 @@ BitmapLoader::~BitmapLoader() { } -void BitmapLoader::Initialize(const std::string& filename) +void BitmapLoader::Load() { - // Load with default scaling and orientation correction: - Integration::BitmapResourceType bitmapResourceType; - Integration::ResourcePointer resource = TizenPlatform::ImageLoader::LoadResourceSynchronously( bitmapResourceType, filename ); + IntrusivePtr resource = TizenPlatform::ImageLoader::LoadResourceSynchronously( mResourceType, mUrl ); mBitmap = static_cast(resource.Get()); + mIsLoaded = true; +} + +bool BitmapLoader::IsLoaded() +{ + return mIsLoaded; } unsigned char* BitmapLoader::GetPixelData() const { - return mBitmap->GetBuffer(); + if( mIsLoaded ) + { + return mBitmap->GetBuffer(); + } + + return NULL; } unsigned int BitmapLoader::GetImageHeight() const { - return mBitmap->GetImageHeight(); + if( mIsLoaded ) + { + return mBitmap->GetImageHeight(); + } + + return 0u; } unsigned int BitmapLoader::GetImageWidth() const { - return mBitmap->GetImageWidth(); -} + if( mIsLoaded ) + { + return mBitmap->GetImageWidth(); + } -unsigned int BitmapLoader::GetBufferStride() const -{ - return mBitmap->GetPackedPixelsProfile()->GetBufferStride(); + return 0u; } Pixel::Format BitmapLoader::GetPixelFormat() const { - return mBitmap->GetPixelFormat(); + if( mIsLoaded ) + { + return mBitmap->GetPixelFormat(); + } + + return Pixel::RGBA8888; } } // namespace Internal diff --git a/adaptors/common/bitmap-loader-impl.h b/adaptors/common/bitmap-loader-impl.h index c9f8ff4..143fef1 100644 --- a/adaptors/common/bitmap-loader-impl.h +++ b/adaptors/common/bitmap-loader-impl.h @@ -23,6 +23,7 @@ #include #include #include +#include // INTERNAL INCLUDES #include @@ -35,12 +36,24 @@ namespace Internal class BitmapLoader : public BaseObject { public: - static IntrusivePtr New(const std::string& filename); + + /** + * @copydoc Dali::BitmapLoader::New + */ + static IntrusivePtr New( const std::string& url, + ImageDimensions size, + FittingMode::Type fittingMode, + SamplingMode::Type samplingMode, + bool orientationCorrection); /** * Create the bitmap loader object. */ - BitmapLoader(); + BitmapLoader(const std::string& url, + ImageDimensions size, + FittingMode::Type fittingMode, + SamplingMode::Type samplingMode, + bool orientationCorrection); protected: /** @@ -48,13 +61,17 @@ protected: */ ~BitmapLoader(); +public: + /** - * Second stage initialization - this will load the image synchronously. - * @param[in] filename Filename of the bitmap image to load. + * @copydoc Dali::BitmapLoader::Load */ - void Initialize(const std::string& filename); + void Load(); -public: + /** + * @copydoc Dali::BitmapLoader::IsLoaded + */ + bool IsLoaded(); /** * Get the raw pixel data. @@ -76,18 +93,15 @@ public: unsigned int GetImageWidth() const; /** - * Get the number of bytes in each row of pixels - * @return The buffer stride in bytes. - */ - unsigned int GetBufferStride() const; - - /** * Get the pixel format of the loaded bitmap. */ Pixel::Format GetPixelFormat() const; private: + Integration::BitmapResourceType mResourceType; Integration::BitmapPtr mBitmap; + const std::string mUrl; + bool mIsLoaded; }; } // Internal diff --git a/adaptors/devel-api/adaptor-framework/bitmap-loader.cpp b/adaptors/devel-api/adaptor-framework/bitmap-loader.cpp index 142e600..dabfbd1 100644 --- a/adaptors/devel-api/adaptor-framework/bitmap-loader.cpp +++ b/adaptors/devel-api/adaptor-framework/bitmap-loader.cpp @@ -20,20 +20,19 @@ // EXTERNAL INCLUDES #include -#include -#include -#include - // INTERNAL INCLUDES -#include "image-loaders/image-loader.h" #include namespace Dali { -BitmapLoader BitmapLoader::New(const std::string& filename) +BitmapLoader BitmapLoader::New( const std::string& url, + ImageDimensions size, + FittingMode::Type fittingMode, + SamplingMode::Type samplingMode, + bool orientationCorrection) { - IntrusivePtr internal = Internal::BitmapLoader::New(filename); + IntrusivePtr internal = Internal::BitmapLoader::New(url, size, fittingMode, samplingMode, orientationCorrection); return BitmapLoader( internal.Get() ); } @@ -61,6 +60,16 @@ BitmapLoader& BitmapLoader::operator=(const BitmapLoader& rhs) return *this; } +void BitmapLoader::Load() +{ + GetImplementation(*this).Load(); +} + +bool BitmapLoader::IsLoaded() +{ + return GetImplementation(*this).IsLoaded(); +} + unsigned char* BitmapLoader::GetPixelData() const { return GetImplementation(*this).GetPixelData(); @@ -76,11 +85,6 @@ unsigned int BitmapLoader::GetImageWidth() const return GetImplementation(*this).GetImageWidth(); } -unsigned int BitmapLoader::GetBufferStride() const -{ - return GetImplementation(*this).GetBufferStride(); -} - Pixel::Format BitmapLoader::GetPixelFormat() const { return GetImplementation(*this).GetPixelFormat(); diff --git a/adaptors/devel-api/adaptor-framework/bitmap-loader.h b/adaptors/devel-api/adaptor-framework/bitmap-loader.h index 12971bd..af5ac92 100644 --- a/adaptors/devel-api/adaptor-framework/bitmap-loader.h +++ b/adaptors/devel-api/adaptor-framework/bitmap-loader.h @@ -21,6 +21,7 @@ #include #include #include +#include #include namespace Dali @@ -30,15 +31,33 @@ namespace Internal class BitmapLoader; } +/** + * @brief The BitmapLoader class is used to load bitmap from the URL synchronously. + * + * As the loading is synchronous, it will block the loop whilst executing. + * Therefore, it should be used sparingly in the main event thread, and better to be called in the worker thread. + * The Load() API is thread safe, it can be called from any thread without changing the state of DALI. + */ class DALI_IMPORT_API BitmapLoader : public BaseHandle { public: + /** - * @brief Create an initialized bitmap loader. This will automatically load the image. + * @brief Create an initialized bitmap loader. + * + * By calling Load(), the synchronous loading is started immediately. * - * @param[in] filename Filename of the bitmap image to load. + * @param [in] url The URL of the image file to load. + * @param [in] size The width and height to fit the loaded image to. + * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter. + * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size. + * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header. */ - static BitmapLoader New(const std::string& filename); + static BitmapLoader New( const std::string& url, + ImageDimensions size = ImageDimensions( 0, 0 ), + FittingMode::Type fittingMode = FittingMode::DEFAULT, + SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR, + bool orientationCorrection = true); /** * @brief Create an empty handle. @@ -70,6 +89,18 @@ public: public: /** + * @brief Start the synchronous loading. + */ + void Load(); + + /** + * @brief Query whether the image is loaded. + * + * @reture true if the image is loaded, false otherwise. + */ + bool IsLoaded(); + + /** * Get the raw pixel data. * @return The pixel data. Use the GetHeight(), GetWidth(), GetStride() and GetPixelFormat() methods * to decode the data. @@ -89,12 +120,6 @@ public: unsigned int GetImageWidth() const; /** - * Get the number of bytes in each row of pixels - * @return The buffer stride in bytes. - */ - unsigned int GetBufferStride() const; - - /** * Get the pixel format of the loaded bitmap. */ Pixel::Format GetPixelFormat() const; diff --git a/adaptors/devel-api/adaptor-framework/event-thread-callback.cpp b/adaptors/devel-api/adaptor-framework/event-thread-callback.cpp new file mode 100644 index 0000000..a36775f --- /dev/null +++ b/adaptors/devel-api/adaptor-framework/event-thread-callback.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// CLASS HEADER +#include "event-thread-callback.h" + +// INTERNAL INCLUDES +#include + +namespace Dali +{ + +struct EventThreadCallback::Impl +{ + TriggerEventInterface* eventTrigger; +}; + +EventThreadCallback::EventThreadCallback( CallbackBase* callback ) +: mImpl( new Impl() ) +{ + mImpl->eventTrigger = NULL; + if ( Adaptor::IsAvailable() ) + { + Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ); + mImpl->eventTrigger = adaptorImpl.GetTriggerEventFactoryInterface().CreateTriggerEvent( callback, TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER ); + } +} + +EventThreadCallback::~EventThreadCallback() +{ + if ( Adaptor::IsAvailable() ) + { + Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ); + adaptorImpl.GetTriggerEventFactoryInterface().DestroyTriggerEvent( mImpl->eventTrigger ); + } + delete mImpl; +} + +void EventThreadCallback::Trigger() +{ + if( mImpl->eventTrigger ) + { + mImpl->eventTrigger->Trigger(); + } +} + +} diff --git a/adaptors/devel-api/adaptor-framework/event-thread-callback.h b/adaptors/devel-api/adaptor-framework/event-thread-callback.h new file mode 100644 index 0000000..f15cf07 --- /dev/null +++ b/adaptors/devel-api/adaptor-framework/event-thread-callback.h @@ -0,0 +1,72 @@ +#ifndef __DALI_EVENT_THREAD_CALLBACK_H_ +#define __DALI_EVENT_THREAD_CALLBACK_H_ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + +namespace Dali +{ + +/** + * @brief The EventThreadCallback class provides a mechanism for the worker thread to trigger the execution of a given callback in main event thread . + * + * @note The EventThreadCallback object should only be created in the main thread. + */ +class DALI_IMPORT_API EventThreadCallback +{ +public: + + /** + * @brief Constructor. Create an object that will call the given callback in main event thread. + * + * @param[in] callback The callback to call. + */ + EventThreadCallback( CallbackBase* callback ); + + /** + * @brief Destructor. + */ + ~EventThreadCallback(); + + /** + * @brief Trigger the calling of callback. + * + * The method can be used from worker threads to notify the main thread as main thread is running the event loop and thus cannot be blocked + */ + void Trigger(); + +private: + + // undefined copy constructor. + EventThreadCallback( const EventThreadCallback& ); + + // undefined assignment operator + EventThreadCallback& operator=( const EventThreadCallback& ); + +private: + + struct Impl; + Impl* mImpl; + +}; + +} +#endif /* __DALI_EVENT_THREAD_CALLBACK_H_ */ diff --git a/adaptors/devel-api/file.list b/adaptors/devel-api/file.list index 64d4221..39d7d50 100644 --- a/adaptors/devel-api/file.list +++ b/adaptors/devel-api/file.list @@ -7,6 +7,7 @@ devel_api_src_files = \ $(adaptor_devel_api_dir)/adaptor-framework/color-controller.cpp \ $(adaptor_devel_api_dir)/adaptor-framework/drag-and-drop-detector.cpp \ $(adaptor_devel_api_dir)/adaptor-framework/event-feeder.cpp \ + $(adaptor_devel_api_dir)/adaptor-framework/event-thread-callback.cpp \ $(adaptor_devel_api_dir)/adaptor-framework/feedback-player.cpp \ $(adaptor_devel_api_dir)/adaptor-framework/file-loader.cpp \ $(adaptor_devel_api_dir)/adaptor-framework/imf-manager.cpp \ @@ -32,6 +33,7 @@ devel_api_adaptor_framework_header_files = \ $(adaptor_devel_api_dir)/adaptor-framework/color-controller.h \ $(adaptor_devel_api_dir)/adaptor-framework/drag-and-drop-detector.h \ $(adaptor_devel_api_dir)/adaptor-framework/event-feeder.h \ + $(adaptor_devel_api_dir)/adaptor-framework/event-thread-callback.h \ $(adaptor_devel_api_dir)/adaptor-framework/feedback-plugin.h \ $(adaptor_devel_api_dir)/adaptor-framework/feedback-player.h \ $(adaptor_devel_api_dir)/adaptor-framework/file-loader.h \