Skip to content

Commit 5a4a58c

Browse files
authored
refactor: port more layers to Cobra renderer: HalfTone2 & 3 (synfig#3300)
refactor: port more layers to Cobra renderer: HalfTone2 & 3
2 parents 3eb1fbb + ac871ad commit 5a4a58c

File tree

10 files changed

+319
-178
lines changed

10 files changed

+319
-178
lines changed

synfig-core/src/modules/mod_filter/halftone2.cpp

Lines changed: 79 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
#include <synfig/string.h>
4343
#include <synfig/context.h>
4444
#include <synfig/paramdesc.h>
45-
#include <synfig/renddesc.h>
46-
#include <synfig/surface.h>
47-
#include <synfig/value.h>
45+
#include <synfig/rendering/common/task/taskpixelprocessor.h>
46+
#include <synfig/rendering/software/task/taskpaintpixelsw.h>
47+
#include <synfig/rendering/software/task/tasksw.h>
4848

4949
#endif
5050

@@ -62,6 +62,71 @@ SYNFIG_LAYER_SET_VERSION(Halftone2,"0.0");
6262

6363
/* === P R O C E D U R E S ================================================= */
6464

65+
class TaskHalfTone2: public rendering::TaskPixelProcessorBase, public rendering::TaskInterfaceTransformationGetAndPass
66+
{
67+
public:
68+
typedef etl::handle<TaskHalfTone2> Handle;
69+
SYNFIG_EXPORT static Token token;
70+
Token::Handle get_token() const override { return token.handle(); }
71+
72+
Halftone halftone;
73+
Color color_dark;
74+
Color color_light;
75+
76+
rendering::Holder<rendering::TransformationAffine> transformation;
77+
78+
rendering::Transformation::Handle get_transformation() const override
79+
{
80+
return transformation.handle();
81+
}
82+
};
83+
84+
85+
class TaskHalfTone2SW: public TaskHalfTone2, public rendering::TaskFilterPixelSW
86+
{
87+
public:
88+
typedef etl::handle<TaskHalfTone2SW> Handle;
89+
SYNFIG_EXPORT static Token token;
90+
Token::Handle get_token() const override { return token.handle(); }
91+
92+
void pre_run(const Matrix3& /*matrix*/) const override
93+
{
94+
supersample_size = 1/std::fabs(get_pixels_per_unit()[0]*(halftone.param_size.get(Vector())).mag());
95+
inverted_transformation = transformation->create_inverted();
96+
}
97+
98+
Color get_color(const Vector& p, const Color& c) const override
99+
{
100+
const float amount(halftone(inverted_transformation->transform(p), c.get_y(), supersample_size));
101+
Color halfcolor;
102+
103+
if (amount <= 0.0f)
104+
halfcolor = color_dark;
105+
else if (amount >= 1.0f)
106+
halfcolor = color_light;
107+
else
108+
halfcolor = Color::blend(color_light, color_dark, amount, Color::BLEND_STRAIGHT);
109+
110+
halfcolor.set_a(c.get_a());
111+
112+
return halfcolor;
113+
}
114+
115+
bool run(RunParams&) const override
116+
{
117+
return run_task();
118+
}
119+
120+
protected:
121+
mutable float supersample_size = 1.0f;
122+
mutable rendering::Transformation::Handle inverted_transformation;
123+
};
124+
125+
SYNFIG_EXPORT rendering::Task::Token TaskHalfTone2::token(
126+
DescAbstract<TaskHalfTone2>("HalfTone2") );
127+
SYNFIG_EXPORT rendering::Task::Token TaskHalfTone2SW::token(
128+
DescReal<TaskHalfTone2SW, TaskHalfTone2>("HalfTone2SW") );
129+
65130
/* === M E T H O D S ======================================================= */
66131

67132
Halftone2::Halftone2():
@@ -99,12 +164,6 @@ Halftone2::color_func(const Point &point, float supersample,const Color& color)c
99164
return halfcolor;
100165
}
101166

102-
inline float
103-
Halftone2::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
104-
{
105-
return std::fabs(pw/(halftone.param_size.get(Vector())).mag());
106-
}
107-
108167
synfig::Layer::Handle
109168
Halftone2::hit_check(synfig::Context /*context*/, const synfig::Point &/*point*/)const
110169
{
@@ -195,69 +254,16 @@ Halftone2::get_color(Context context, const Point &point)const
195254
return Color::blend(color,undercolor,get_amount(),get_blend_method());
196255
}
197256

198-
bool
199-
Halftone2::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
257+
rendering::Task::Handle
258+
Halftone2::build_composite_fork_task_vfunc(ContextParams /* context_params */, rendering::Task::Handle sub_task) const
200259
{
201-
RENDER_TRANSFORMED_IF_NEED(__FILE__, __LINE__)
202-
203-
SuperCallback supercb(cb,0,9500,10000);
204-
205-
if(!context.accelerated_render(surface,quality,renddesc,&supercb))
206-
return false;
207-
if(get_amount()==0)
208-
return true;
209-
210-
const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
211-
const Point tl(renddesc.get_tl());
212-
const int w(surface->get_w());
213-
const int h(surface->get_h());
214-
const float supersample_size(std::fabs(pw/(halftone.param_size.get(Vector())).mag()));
215-
216-
Surface::pen pen(surface->begin());
217-
Point pos;
218-
int x,y;
219-
220-
if(is_solid_color())
221-
{
222-
for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
223-
for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
224-
pen.put_value(
225-
color_func(
226-
pos,
227-
supersample_size,
228-
pen.get_value()
229-
)
230-
);
231-
}
232-
else
233-
{
234-
for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
235-
for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
236-
pen.put_value(
237-
Color::blend(
238-
color_func(
239-
pos,
240-
supersample_size,
241-
pen.get_value()
242-
),
243-
pen.get_value(),
244-
get_amount(),
245-
get_blend_method()
246-
)
247-
);
248-
}
249-
250-
// Mark our progress as finished
251-
if(cb && !cb->amount_complete(10000,10000))
252-
return false;
253-
254-
return true;
260+
if (!sub_task)
261+
return sub_task;
262+
263+
TaskHalfTone2::Handle task_halftone2(new TaskHalfTone2());
264+
task_halftone2->color_dark = param_color_dark.get(Color());
265+
task_halftone2->color_light = param_color_light.get(Color());
266+
task_halftone2->halftone = halftone;
267+
task_halftone2->sub_task() = sub_task;//->clone_recursive();
268+
return task_halftone2;
255269
}
256-
257-
///
258-
259-
rendering::Task::Handle
260-
Halftone2::build_rendering_task_vfunc(Context context) const
261-
{ return Layer::build_rendering_task_vfunc(context); }
262-
263-
///

synfig-core/src/modules/mod_filter/halftone2.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,18 @@ class Halftone2 : public Layer_CompositeFork
7272

7373
Color color_func(const Point &x, float supersample,const Color &under_color)const;
7474

75-
float calc_supersample(const Point &x, float pw,float ph)const;
76-
77-
//float halftone_func(Point x)const;
78-
7975
public:
8076
Halftone2();
8177

8278
virtual bool set_param(const String &param, const ValueBase &value);
8379
virtual ValueBase get_param(const String &param)const;
8480
virtual Color get_color(Context context, const Point &pos)const;
85-
virtual bool accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const;
8681
Layer::Handle hit_check(Context context, const Point &point)const;
8782
virtual Vocab get_param_vocab()const;
8883
virtual bool reads_context()const { return true; }
8984

9085
protected:
91-
virtual rendering::Task::Handle build_rendering_task_vfunc(Context context) const;
86+
virtual rendering::Task::Handle build_composite_fork_task_vfunc(ContextParams context_params, rendering::Task::Handle sub_task) const;
9287
}; // END of class Halftone2
9388

9489
/* === E N D =============================================================== */

0 commit comments

Comments
 (0)