1 #ifndef __DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H__
2 #define __DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H__
5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <dali/devel-api/threading/conditional-wait.h>
22 #include <dali/devel-api/threading/mutex.h>
23 #include <dali/devel-api/threading/thread.h>
24 #include <dali/devel-api/rendering/texture-set.h>
25 #include <dali/devel-api/images/pixel-data.h>
26 #include <dali/public-api/images/buffer-image.h>
27 #include <dali/public-api/common/intrusive-ptr.h>
28 #include <dali/public-api/common/vector-wrapper.h>
29 #include <dali/public-api/object/ref-object.h>
30 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
33 struct NSVGrasterizer;
45 typedef IntrusivePtr< SvgRenderer > SvgRendererPtr;
46 class RasterizingTask;
47 typedef IntrusivePtr< RasterizingTask > RasterizingTaskPtr;
50 * The svg rasterizing tasks to be processed in the worker thread.
52 * Life cycle of a rasterizing task is as follows:
53 * 1. Created by SvgRenderer in the main thread
54 * 2. Queued in the worked thread waiting to be processed.
55 * 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
56 * Or if this task is been removed ( new image/size set to the renderer or actor off stage) before its turn to be processed, it then been deleted in the worker thread.
58 class RasterizingTask : public RefObject
64 * @param[in] svgRenderer The renderer which the rasterized image to be applied.
65 * @param[in] parsedSvg The parsed svg for rasterizing.
66 * Note, after the task is added to the worker thread, the worker thread takes over the ownership.
67 * When the image is to be deleted, delete it in the worker thread by calling SvgRasterizeThread::DeleteImage( parsedSvg ).
68 * @param[in] width The rasterization width.
69 * @param[in] height The rasterization height.
71 RasterizingTask( SvgRenderer* svgRenderer, NSVGimage* parsedSvg, unsigned int width, unsigned int height );
74 * Do the rasterization with the given rasterizer.
75 *@param[in] rasterizer The rasterizer that rasterize the SVG to a buffer image
77 void Rasterize( NSVGrasterizer* rasterizer );
80 * Get the svg renderer
82 SvgRenderer* GetSvgRenderer() const;
85 * Get the rasterization result.
86 * @return The pixel data with the rasterized pixels.
88 PixelDataPtr GetPixelData() const;
93 RasterizingTask( const RasterizingTask& task );
96 RasterizingTask& operator=( const RasterizingTask& task );
99 SvgRendererPtr mSvgRenderer;
100 PixelDataPtr mPixelData;
101 NSVGimage* mParsedSvg;
103 unsigned int mHeight;
108 * The worker thread for SVG rasterization.
110 class SvgRasterizeThread : public Thread
117 * @param[in] trigger The trigger to wake up the main thread.
119 SvgRasterizeThread( EventThreadCallback* trigger );
122 * Terminate the svg rasterize thread, join and delete.
124 static void TerminateThread( SvgRasterizeThread*& thread );
127 * Add a rasterization task into the waiting queue, called by main thread.
129 * @param[in] task The task added to the queue.
131 void AddTask( RasterizingTaskPtr task );
134 * Pop the next task out from the completed queue, called by main thread.
136 * @return The next task in the completed queue.
138 RasterizingTaskPtr NextCompletedTask();
141 * Remove the task with the given renderer from the waiting queue, called by main thread.
143 * Typically called when the actor is put off stage, so the renderer is not needed anymore.
145 * @param[in] renderer The renderer pointer.
147 void RemoveTask( SvgRenderer* renderer );
150 * Delete the parsed SVG image, called by main thread.
152 * The parsed svg should be delelted in worker thread, as the main thread does not know whether a rasterization of this svg is ongoing.
154 * @param[in] parsedImage The image to be deleted
156 void DeleteImage( NSVGimage* parsedSvg );
161 * Pop the next task out from the queue.
163 * @return The next task to be processed.
165 RasterizingTaskPtr NextTaskToProcess();
168 * Add a task in to the queue
170 * @param[in] task The task added to the queue.
172 void AddCompletedTask( RasterizingTaskPtr task );
179 virtual ~SvgRasterizeThread();
183 * The entry function of the worker thread.
184 * It fetches task from the Queue, rasterizes the image and apply to the renderer.
191 SvgRasterizeThread( const SvgRasterizeThread& thread );
194 SvgRasterizeThread& operator=( const SvgRasterizeThread& thread );
198 std::vector<RasterizingTaskPtr> mRasterizeTasks; //The queue of the tasks waiting to rasterize the SVG image
199 std::vector <RasterizingTaskPtr> mCompletedTasks; //The queue of the tasks with the SVG rasterization completed
200 Vector<NSVGimage*> mDeleteSvg; //The images that the event thread requested to delete
202 ConditionalWait mConditionalWait;
204 EventThreadCallback* mTrigger;
206 NSVGrasterizer* mRasterizer;
207 bool mIsThreadWaiting;
210 } // namespace Internal
212 } // namespace Toolkit
216 #endif /* __DALI_TOOLKIT_SVG_RASTERIZE_THREAD_H__ */