X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fsvg%2Fsvg-rasterize-thread.h;h=c9c305b1da4411ae3dd6edff717a6080e55d03f6;hb=21f841ba0703a6fbf442ab097451560b7b6efe1b;hp=5c8b323e37af613c86bc429694fa1ecfd847f083;hpb=8bb92d7d1170f2ddf59da60bd3588be601ef8cd2;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h index 5c8b323..c9c305b 100644 --- a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h +++ b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h @@ -2,7 +2,7 @@ #define DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 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. @@ -19,37 +19,33 @@ // EXTERNAL INCLUDES #include +#include #include #include #include -#include +#include +#include #include #include +#include #include -#include -#include +#include -#ifdef NO_THORVG -struct NSVGimage; -struct NSVGrasterizer; -#else /* NO_THORVG */ -#include -#include -#endif /* NO_THORVG */ +// INTERNAL INCLUDES +#include +#include namespace Dali { - namespace Toolkit { - namespace Internal { - class SvgVisual; -typedef IntrusivePtr< SvgVisual > SvgVisualPtr; -class RasterizingTask; -typedef IntrusivePtr< RasterizingTask > RasterizingTaskPtr; +typedef IntrusivePtr SvgVisualPtr; +class SvgTask; +typedef IntrusivePtr SvgTaskPtr; +class SvgRasterizeManager; /** * The svg rasterizing tasks to be processed in the worker thread. @@ -60,43 +56,40 @@ typedef IntrusivePtr< RasterizingTask > RasterizingTaskPtr; * 3. If this task gets its turn to do the rasterization, it triggers main thread to apply the rasterized image to material then been deleted in main thread call back * Or if this task is been removed ( new image/size set to the visual or actor off stage) before its turn to be processed, it then been deleted in the worker thread. */ -class RasterizingTask : public RefObject +class SvgTask : public RefObject { public: -#ifdef NO_THORVG /** * Constructor - * - * @param[in] svgRenderer The renderer which the rasterized image to be applied. - * @param[in] parsedSvg The parsed svg for rasterizing. - * Note, after the task is added to the worker thread, the worker thread takes over the ownership. - * When the image is to be deleted, delete it in the worker thread by calling SvgRasterizeThread::DeleteImage( parsedSvg ). - * @param[in] url The URL to svg resource to use. - * @param[in] width The rasterization width. - * @param[in] height The rasterization height. + * @param[in] svgVisual The visual which the rasterized image to be applied. + * @param[in] vectorRenderer The vector rasterizer. */ - RasterizingTask( SvgVisual* svgRenderer, NSVGimage* parsedSvg, const VisualUrl& url, float dpi, unsigned int width, unsigned int height ); -#else /* NO_THORVG */ + SvgTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer); + /** - * Constructor - * @param[in] svgRenderer The renderer which the rasterized image to be applied. - * @param[in] url The URL to svg resource to use. - * @param[in] width The rasterization width. - * @param[in] height The rasterization height. - * @param[in] loaded The svg resource is loaded or not. + * Destructor. */ - RasterizingTask( SvgVisual* svgRenderer, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi, unsigned int width, unsigned int height, bool loaded ); -#endif /* NO_THORVG */ + virtual ~SvgTask() = default; /** - * Destructor. + * Process the task + */ + virtual void Process() = 0; + + /** + * Whether the task is ready to process. + * @return True if the task is ready to process. */ - ~RasterizingTask() override; + virtual bool IsReady() + { + return true; + } /** - * Do the rasterization with the mRasterizer. + * Whether the task has succeeded. + * @return True if the task has succeeded. */ - void Rasterize( ); + bool HasSucceeded() const; /** * Get the svg visual @@ -107,63 +100,100 @@ public: * Get the rasterization result. * @return The pixel data with the rasterized pixels. */ - PixelData GetPixelData() const; + virtual PixelData GetPixelData() const; -#ifdef NO_THORVG +private: + // Undefined + SvgTask(const SvgTask& task) = delete; + + // Undefined + SvgTask& operator=(const SvgTask& task) = delete; + +protected: + SvgVisualPtr mSvgVisual; + VectorImageRenderer mVectorRenderer; + bool mHasSucceeded; +}; + +class SvgLoadingTask : public SvgTask +{ +public: /** - * Get the parsed data. - * @return parsed image data. + * Constructor + * @param[in] svgVisual The visual which the rasterized image to be applied. + * @param[in] vectorRenderer The vector rasterizer. + * @param[in] url The URL to svg resource to use. + * @param[in] dpi The DPI of the screen. */ - NSVGimage* GetParsedImage() const; + SvgLoadingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, const VisualUrl& url, float dpi); + /** - * Get default size of svg - * - * @param[out] width The default width of svg - * @param[out] height The default height of svg + * Destructor. + */ + ~SvgLoadingTask() override; + + /** + * Process the task + */ + void Process() override; + +private: + // Undefined + SvgLoadingTask(const SvgLoadingTask& task) = delete; + + // Undefined + SvgLoadingTask& operator=(const SvgLoadingTask& task) = delete; + +private: + VisualUrl mUrl; + float mDpi; +}; + +class SvgRasterizingTask : public SvgTask +{ +public: + /** + * Constructor + * @param[in] svgVisual The visual which the rasterized image to be applied. + * @param[in] vectorRenderer The vector rasterizer. + * @param[in] width The rasterization width. + * @param[in] height The rasterization height. + */ + SvgRasterizingTask(SvgVisual* svgVisual, VectorImageRenderer vectorRenderer, unsigned int width, unsigned int height); + + /** + * Destructor. */ - void GetDefaultSize( uint32_t& width, uint32_t& height ) const; -#else /* NO_THORVG */ + ~SvgRasterizingTask() override; + /** - * Get the VectorRenderer. - * @return VectorRenderer. + * Process the task accodring to the type */ - VectorImageRenderer GetVectorRenderer() const; + void Process() override; + /** - * Whether the resource is loaded. - * @return True if the resource is loaded. + * Whether the task is ready to process. + * @return True if the task is ready to process. */ - bool IsLoaded() const; -#endif /* NO_THORVG */ + bool IsReady() override; /** - * Load svg file + * Get the rasterization result. + * @return The pixel data with the rasterized pixels. */ - void Load(); + PixelData GetPixelData() const override; private: // Undefined - RasterizingTask( const RasterizingTask& task ); + SvgRasterizingTask(const SvgRasterizingTask& task) = delete; // Undefined - RasterizingTask& operator=( const RasterizingTask& task ); + SvgRasterizingTask& operator=(const SvgRasterizingTask& task) = delete; private: - SvgVisualPtr mSvgVisual; -#ifdef NO_THORVG - NSVGimage* mParsedSvg; -#else /* NO_THORVG */ - VectorImageRenderer mVectorRenderer; -#endif /* NO_THORVG */ - VisualUrl mUrl; - PixelData mPixelData; - float mDpi; - unsigned int mWidth; - unsigned int mHeight; -#ifdef NO_THORVG - NSVGrasterizer* mRasterizer; -#else /* NO_THORVG */ - bool mLoaded; -#endif /* NO_THORVG */ + PixelData mPixelData; + uint32_t mWidth; + uint32_t mHeight; }; /** @@ -172,32 +202,76 @@ private: class SvgRasterizeThread : public Thread { public: + /** + * Constructor. + */ + SvgRasterizeThread(SvgRasterizeManager& svgRasterizeManager); + + /** + * Destructor. + */ + ~SvgRasterizeThread() override; + + /** + * @brief Request the thread to rasterizes the task. + * @return True if the request succeeds, otherwise false. + */ + bool RequestRasterize(); +protected: + /** + * The entry function of the worker thread. + * It rasterizes the image. + */ + void Run() override; + +private: + // Undefined + SvgRasterizeThread(const SvgRasterizeThread& thread) = delete; + + // Undefined + SvgRasterizeThread& operator=(const SvgRasterizeThread& thread) = delete; + +private: + ConditionalWait mConditionalWait; + const Dali::LogFactoryInterface& mLogFactory; + SvgRasterizeManager& mSvgRasterizeManager; + bool mDestroyThread; + bool mIsThreadStarted; + bool mIsThreadIdle; +}; + +/** + * The manager for SVG rasterization. + */ +class SvgRasterizeManager : Integration::Processor +{ +public: /** * Constructor. * * @param[in] trigger The trigger to wake up the main thread. */ - SvgRasterizeThread( EventThreadCallback* trigger ); + SvgRasterizeManager(); /** - * Terminate the svg rasterize thread, join and delete. + * Destructor. */ - static void TerminateThread( SvgRasterizeThread*& thread ); + ~SvgRasterizeManager() override; /** * Add a rasterization task into the waiting queue, called by main thread. * * @param[in] task The task added to the queue. */ - void AddTask( RasterizingTaskPtr task ); + void AddTask(SvgTaskPtr task); /** * Pop the next task out from the completed queue, called by main thread. * * @return The next task in the completed queue. */ - RasterizingTaskPtr NextCompletedTask(); + SvgTaskPtr NextCompletedTask(); /** * Remove the task with the given visual from the waiting queue, called by main thread. @@ -206,81 +280,93 @@ public: * * @param[in] visual The visual pointer. */ - void RemoveTask( SvgVisual* visual ); + void RemoveTask(SvgVisual* visual); -#ifdef NO_THORVG /** - * Delete the parsed SVG image, called by main thread. - * - * The parsed svg should be deleted in worker thread, as the main thread does not know whether a rasterization of this svg is ongoing. - * - * @param[in] parsedImage The image to be deleted + * @copydoc Dali::Integration::Processor::Process() */ - void DeleteImage( NSVGimage* parsedSvg ); -#else /* NO_THORVG */ - /** - * Delete the parsed SVG image, called by main thread. - * - * The parsed svg should be deleted in worker thread, as the main thread does not know whether a rasterization of this svg is ongoing. - * - * @param[in] VectorImage The image to be deleted - */ - void DeleteImage( VectorImageRenderer vectorImage ); -#endif /* NO_THORVG */ - -private: + void Process(bool postProcessor) override; /** * Pop the next task out from the queue. * * @return The next task to be processed. */ - RasterizingTaskPtr NextTaskToProcess(); + SvgTaskPtr NextTaskToProcess(); /** * Add a task in to the queue * * @param[in] task The task added to the queue. */ - void AddCompletedTask( RasterizingTaskPtr task ); - -protected: + void AddCompletedTask(SvgTaskPtr task); +private: /** - * Destructor. + * Applies the rasterized image to material */ - ~SvgRasterizeThread() override; - + void ApplyRasterizedSVGToSampler(); /** - * The entry function of the worker thread. - * It fetches task from the Queue, rasterizes the image and apply to the renderer. + * @brief Unregister a previously registered processor + * */ - void Run() override; + void UnregisterProcessor(); private: + /** + * @brief Helper class to keep the relation between SvgRasterizeThread and corresponding container + */ + class RasterizeHelper + { + public: + /** + * @brief Create an RasterizeHelper. + * + * @param[in] svgRasterizeManager Reference to the SvgRasterizeManager + */ + RasterizeHelper(SvgRasterizeManager& svgRasterizeManager); + + /** + * @brief Request the thread to rasterizes the task. + * @return True if the request succeeds, otherwise false. + */ + bool RequestRasterize(); + + public: + RasterizeHelper(const RasterizeHelper&) = delete; + RasterizeHelper& operator=(const RasterizeHelper&) = delete; + + RasterizeHelper(RasterizeHelper&& rhs); + RasterizeHelper& operator=(RasterizeHelper&& rhs) = delete; + + private: + /** + * @brief Main constructor that used by all other constructors + */ + RasterizeHelper(std::unique_ptr rasterizer, SvgRasterizeManager& svgRasterizeManager); + + private: + std::unique_ptr mRasterizer; + SvgRasterizeManager& mSvgRasterizeManager; + }; +private: // Undefined - SvgRasterizeThread( const SvgRasterizeThread& thread ); + SvgRasterizeManager(const SvgRasterizeManager& thread); // Undefined - SvgRasterizeThread& operator=( const SvgRasterizeThread& thread ); + SvgRasterizeManager& operator=(const SvgRasterizeManager& thread); private: + std::vector mRasterizeTasks; //The queue of the tasks waiting to rasterize the SVG image + std::vector mCompletedTasks; //The queue of the tasks with the SVG rasterization completed - std::vector mRasterizeTasks; //The queue of the tasks waiting to rasterize the SVG image - std::vector mCompletedTasks; //The queue of the tasks with the SVG rasterization completed -#ifdef NO_THORVG - Vector mDeleteSvg; //The images that the event thread requested to delete -#else /* NO_THORVG */ - Vector mDeleteSvg; //The images that the event thread requested to delete -#endif /* NO_THORVG */ - - ConditionalWait mConditionalWait; - Dali::Mutex mMutex; - EventThreadCallback* mTrigger; + RoundRobinContainerView mRasterizers; - bool mIsThreadWaiting; + Dali::Mutex mMutex; + std::unique_ptr mTrigger; + bool mProcessorRegistered; }; } // namespace Internal