Debian Codegraph @

thirdparty/thorvg/src/renderer/sw_engine/tvgSwRenderer.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #ifdef THORVG_SW_OPENMP_SUPPORT
24 #include <omp.h>
25 #endif
26 #include <algorithm>
27 #include "tvgMath.h"
28 #include "tvgSwCommon.h"
29 #include "tvgTaskScheduler.h"
30 #include "tvgSwRenderer.h"
31
32 /************************************************************************/
33 /* Internal Class Implementation */
34 /************************************************************************/
35 static int32_t initEngineCnt = false;
36 static int32_t rendererCnt = 0;
37 static SwMpool* globalMpool = nullptr;
38 static uint32_t threadsCnt = 0;
39
40 struct SwTask : Task
41 {
42 SwSurface* surface = nullptr;
43 SwMpool* mpool = nullptr;
44 SwBBox bbox; //Rendering Region
45 Matrix transform;
46 Array<RenderData> clips;
47 RenderUpdateFlag flags = RenderUpdateFlag::None;
48 uint8_t opacity;
49 bool pushed = false; //Pushed into task list?
50 bool disposed = false; //Disposed task?
51
52 RenderRegion bounds()
53 {
54 //Can we skip the synchronization?
55 done();
56
57 RenderRegion region;
58
59 //Range over?
60 region.x = bbox.min.x > 0 ? bbox.min.x : 0;
61 region.y = bbox.min.y > 0 ? bbox.min.y : 0;
62 region.w = bbox.max.x - region.x;
63 region.h = bbox.max.y - region.y;
64 if (region.w < 0) region.w = 0;
65 if (region.h < 0) region.h = 0;
66
67 return region;
68 }
69
70 virtual void dispose() = 0;
71 virtual bool clip(SwRle* target) = 0;
72 virtual ~SwTask() {}
73 };
74
75
76 struct SwShapeTask : SwTask
77 {
78 SwShape shape;
79 const RenderShape* rshape = nullptr;
80 bool clipper = false;
81
82 /* We assume that if the stroke width is greater than 2,
83 the shape's outline beneath the stroke could be adequately covered by the stroke drawing.
84 Therefore, antialiasing is disabled under this condition.
85 Additionally, the stroke style should not be dashed. */
86 bool antialiasing(float strokeWidth)
87 {
88 return strokeWidth < 2.0f || rshape->stroke->dashCnt > 0 || rshape->stroke->strokeFirst || rshape->strokeTrim() || rshape->stroke->color[3] < 255;;
89 }
90
91 float validStrokeWidth()
92 {
93 if (!rshape->stroke) return 0.0f;
94
95 auto width = rshape->stroke->width;
96 if (tvg::zero(width)) return 0.0f;
97
98 if (!rshape->stroke->fill && (MULTIPLY(rshape->stroke->color[3], opacity) == 0)) return 0.0f;
99 if (tvg::zero(rshape->stroke->trim.begin - rshape->stroke->trim.end)) return 0.0f;
100
101 return (width * sqrt(transform.e11 * transform.e11 + transform.e12 * transform.e12));
102 }
103
104 bool clip(SwRle* target) override
105 {
106 if (shape.fastTrack) return rleClip(target, &bbox);
107 else if (shape.rle) return rleClip(target, shape.rle);
108 return false;
109 }
110
111 void run(unsigned tid) override
112 {
113 //Invisible
114 if (opacity == 0 && !clipper) {
115 bbox.reset();
116 return;
117 }
118
119 auto strokeWidth = validStrokeWidth();
120 SwBBox renderRegion{};
121 auto updateShape = flags & (RenderUpdateFlag::Path | RenderUpdateFlag::Transform | RenderUpdateFlag::Clip);
122 auto updateFill = false;
123
124 //Shape
125 if (updateShape || flags & (RenderUpdateFlag::Color | RenderUpdateFlag::Gradient)) {
126 uint8_t alpha = 0;
127 rshape->fillColor(nullptr, nullptr, nullptr, &alpha);
128 updateFill = (MULTIPLY(alpha, opacity) || rshape->fill);
129 if (updateShape) shapeReset(&shape);
130 if (updateFill || clipper) {
131 if (shapePrepare(&shape, rshape, transform, bbox, renderRegion, mpool, tid, clips.count > 0 ? true : false)) {
132 if (!shapeGenRle(&shape, rshape, mpool, tid, antialiasing(strokeWidth))) goto err;
133 } else {
134 updateFill = false;
135 renderRegion.reset();
136 }
137 }
138 }
139 //Fill
140 if (updateFill) {
141 if (auto fill = rshape->fill) {
142 auto ctable = (flags & RenderUpdateFlag::Gradient) ? true : false;
143 if (ctable) shapeResetFill(&shape);
144 if (!shapeGenFillColors(&shape, fill, transform, surface, opacity, ctable)) goto err;
145 }
146 }
147 //Stroke
148 if (updateShape || flags & RenderUpdateFlag::Stroke) {
149 if (strokeWidth > 0.0f) {
150 shapeResetStroke(&shape, rshape, transform);
151 if (!shapeGenStrokeRle(&shape, rshape, transform, bbox, renderRegion, mpool, tid)) goto err;
152 if (auto fill = rshape->strokeFill()) {
153 auto ctable = (flags & RenderUpdateFlag::GradientStroke) ? true : false;
154 if (ctable) shapeResetStrokeFill(&shape);
155 if (!shapeGenStrokeFillColors(&shape, fill, transform, surface, opacity, ctable)) goto err;
156 }
157 } else {
158 shapeDelStroke(&shape);
159 }
160 }
161
162 //Clear current task memorypool here if the clippers would use the same memory pool
163 shapeDelOutline(&shape, mpool, tid);
164
165 //Clip Path
166 for (auto clip = clips.begin(); clip < clips.end(); ++clip) {
167 auto clipper = static_cast<SwTask*>(*clip);
168 auto clipShapeRle = shape.rle ? clipper->clip(shape.rle) : true;
169 auto clipStrokeRle = shape.strokeRle ? clipper->clip(shape.strokeRle) : true;
170 if (!clipShapeRle && !clipStrokeRle) goto err;
171 }
172
173 bbox = renderRegion; //sync
174
175 return;
176
177 err:
178 bbox.reset();
179 shapeReset(&shape);
180 rleReset(shape.strokeRle);
181 shapeDelOutline(&shape, mpool, tid);
182 }
183
184 void dispose() override
185 {
186 shapeFree(&shape);
187 }
188 };
189
190
191 struct SwImageTask : SwTask
192 {
193 SwImage image;
194 RenderSurface* source; //Image source
195
196 bool clip(SwRle* target) override
197 {
198 TVGERR("SW_ENGINE", "Image is used as ClipPath?");
199 return true;
200 }
201
202 void run(unsigned tid) override
203 {
204 auto clipRegion = bbox;
205
206 //Convert colorspace if it's not aligned.
207 rasterConvertCS(source, surface->cs);
208 rasterPremultiply(source);
209
210 image.data = source->data;
211 image.w = source->w;
212 image.h = source->h;
213 image.stride = source->stride;
214 image.channelSize = source->channelSize;
215
216 //Invisible shape turned to visible by alpha.
217 if ((flags & (RenderUpdateFlag::Image | RenderUpdateFlag::Transform | RenderUpdateFlag::Color)) && (opacity > 0)) {
218 imageReset(&image);
219 if (!image.data || image.w == 0 || image.h == 0) goto end;
220
221 if (!imagePrepare(&image, transform, clipRegion, bbox, mpool, tid)) goto end;
222
223 if (clips.count > 0) {
224 if (!imageGenRle(&image, bbox, mpool, tid, false)) goto end;
225 if (image.rle) {
226 //Clear current task memorypool here if the clippers would use the same memory pool
227 imageDelOutline(&image, mpool, tid);
228 for (auto clip = clips.begin(); clip < clips.end(); ++clip) {
229 auto clipper = static_cast<SwTask*>(*clip);
230 if (!clipper->clip(image.rle)) goto err;
231 }
232 return;
233 }
234 }
235 }
236 goto end;
237 err:
238 rleReset(image.rle);
239 end:
240 imageDelOutline(&image, mpool, tid);
241 }
242
243 void dispose() override
244 {
245 imageFree(&image);
246 }
247 };
248
249
250 static void _termEngine()
251 {
252 if (rendererCnt > 0) return;
253
254 mpoolTerm(globalMpool);
255 globalMpool = nullptr;
256 }
257
258
259 static void _renderFill(SwShapeTask* task, SwSurface* surface, uint8_t opacity)
260 {
261 uint8_t r, g, b, a;
262 if (auto fill = task->rshape->fill) {
263 rasterGradientShape(surface, &task->shape, fill, opacity);
264 } else {
265 task->rshape->fillColor(&r, &g, &b, &a);
266 a = MULTIPLY(opacity, a);
267 if (a > 0) rasterShape(surface, &task->shape, r, g, b, a);
268 }
269 }
270
271 static void _renderStroke(SwShapeTask* task, SwSurface* surface, uint8_t opacity)
272 {
273 uint8_t r, g, b, a;
274 if (auto strokeFill = task->rshape->strokeFill()) {
275 rasterGradientStroke(surface, &task->shape, strokeFill, opacity);
276 } else {
277 if (task->rshape->strokeColor(&r, &g, &b, &a)) {
278 a = MULTIPLY(opacity, a);
279 if (a > 0) rasterStroke(surface, &task->shape, r, g, b, a);
280 }
281 }
282 }
283
284 /************************************************************************/
285 /* External Class Implementation */
286 /************************************************************************/
287
288 SwRenderer::~SwRenderer()
289 {
290 clearCompositors();
291
292 delete(surface);
293
294 if (!sharedMpool) mpoolTerm(mpool);
295
296 --rendererCnt;
297
298 if (rendererCnt == 0 && initEngineCnt == 0) _termEngine();
299 }
300
301
302 bool SwRenderer::clear()
303 {
304 for (auto task = tasks.begin(); task < tasks.end(); ++task) {
305 if ((*task)->disposed) {
306 delete(*task);
307 } else {
308 (*task)->done();
309 (*task)->pushed = false;
310 }
311 }
312 tasks.clear();
313
314 if (!sharedMpool) mpoolClear(mpool);
315
316 if (surface) {
317 vport.x = vport.y = 0;
318 vport.w = surface->w;
319 vport.h = surface->h;
320 }
321
322 return true;
323 }
324
325
326 bool SwRenderer::sync()
327 {
328 return true;
329 }
330
331
332 RenderRegion SwRenderer::viewport()
333 {
334 return vport;
335 }
336
337
338 bool SwRenderer::viewport(const RenderRegion& vp)
339 {
340 vport = vp;
341 return true;
342 }
343
344
345 bool SwRenderer::target(pixel_t* data, uint32_t stride, uint32_t w, uint32_t h, ColorSpace cs)
346 {
347 if (!data || stride == 0 || w == 0 || h == 0 || w > stride) return false;
348
349 clearCompositors();
350
351 if (!surface) surface = new SwSurface;
352
353 surface->data = data;
354 surface->stride = stride;
355 surface->w = w;
356 surface->h = h;
357 surface->cs = cs;
358 surface->channelSize = CHANNEL_SIZE(cs);
359 surface->premultiplied = true;
360
361 return rasterCompositor(surface);
362 }
363
364
365 bool SwRenderer::preRender()
366 {
367 return rasterClear(surface, 0, 0, surface->w, surface->h);
368 }
369
370
371 void SwRenderer::clearCompositors()
372 {
373 //Free Composite Caches
374 for (auto comp = compositors.begin(); comp < compositors.end(); ++comp) {
375 free((*comp)->compositor->image.data);
376 delete((*comp)->compositor);
377 delete(*comp);
378 }
379 compositors.reset();
380 }
381
382
383 bool SwRenderer::postRender()
384 {
385 //Unmultiply alpha if needed
386 if (surface->cs == ColorSpace::ABGR8888S || surface->cs == ColorSpace::ARGB8888S) {
387 rasterUnpremultiply(surface);
388 }
389
390 for (auto task = tasks.begin(); task < tasks.end(); ++task) {
391 if ((*task)->disposed) delete(*task);
392 else (*task)->pushed = false;
393 }
394 tasks.clear();
395
396 return true;
397 }
398
399
400 bool SwRenderer::renderImage(RenderData data)
401 {
402 auto task = static_cast<SwImageTask*>(data);
403 task->done();
404
405 if (task->opacity == 0) return true;
406
407 //Outside of the viewport, skip the rendering
408 auto& bbox = task->bbox;
409 if (bbox.max.x <= bbox.min.x || bbox.max.y <= bbox.min.y || bbox.min.x >= int32_t(surface->w) || bbox.min.y >= int32_t(surface->h)) return true;
410
411 auto& image = task->image;
412
413 //RLE Image
414 if (image.rle) {
415 if (image.direct) return rasterDirectRleImage(surface, image, task->opacity);
416 else if (image.scaled) return rasterScaledRleImage(surface, image, task->transform, bbox, task->opacity);
417 else {
418 //create a intermediate buffer for rle clipping
419 auto cmp = request(sizeof(pixel_t), false);
420 cmp->compositor->method = CompositeMethod::None;
421 cmp->compositor->valid = true;
422 cmp->compositor->image.rle = image.rle;
423 rasterClear(cmp, (uint32_t)bbox.min.x, (uint32_t)bbox.min.y, (uint32_t)(bbox.max.x - bbox.min.x), (uint32_t)(bbox.max.y - bbox.min.y), 0);
424 rasterTexmapPolygon(cmp, image, task->transform, bbox, 255);
425 return rasterDirectRleImage(surface, cmp->compositor->image, task->opacity);
426 }
427 //Whole Image
428 } else {
429 if (image.direct) return rasterDirectImage(surface, image, bbox, task->opacity);
430 else if (image.scaled) return rasterScaledImage(surface, image, task->transform, bbox, task->opacity);
431 else return rasterTexmapPolygon(surface, image, task->transform, bbox, task->opacity);
432 }
433 }
434
435
436 bool SwRenderer::renderShape(RenderData data)
437 {
438 auto task = static_cast<SwShapeTask*>(data);
439 if (!task) return false;
440
441 task->done();
442
443 if (task->opacity == 0) return true;
444
445 //Main raster stage
446 if (task->rshape->stroke && task->rshape->stroke->strokeFirst) {
447 _renderStroke(task, surface, task->opacity);
448 _renderFill(task, surface, task->opacity);
449 } else {
450 _renderFill(task, surface, task->opacity);
451 _renderStroke(task, surface, task->opacity);
452 }
453
454 return true;
455 }
456
457
458 bool SwRenderer::blend(BlendMethod method)
459 {
460 if (surface->blendMethod == method) return true;
461 surface->blendMethod = method;
462
463 switch (method) {
464 case BlendMethod::Normal:
465 surface->blender = nullptr;
466 break;
467 case BlendMethod::Multiply:
468 surface->blender = opBlendMultiply;
469 break;
470 case BlendMethod::Screen:
471 surface->blender = opBlendScreen;
472 break;
473 case BlendMethod::Overlay:
474 surface->blender = opBlendOverlay;
475 break;
476 case BlendMethod::Darken:
477 surface->blender = opBlendDarken;
478 break;
479 case BlendMethod::Lighten:
480 surface->blender = opBlendLighten;
481 break;
482 case BlendMethod::ColorDodge:
483 surface->blender = opBlendColorDodge;
484 break;
485 case BlendMethod::ColorBurn:
486 surface->blender = opBlendColorBurn;
487 break;
488 case BlendMethod::HardLight:
489 surface->blender = opBlendHardLight;
490 break;
491 case BlendMethod::SoftLight:
492 surface->blender = opBlendSoftLight;
493 break;
494 case BlendMethod::Difference:
495 surface->blender = opBlendDifference;
496 break;
497 case BlendMethod::Exclusion:
498 surface->blender = opBlendExclusion;
499 break;
500 case BlendMethod::Add:
501 surface->blender = opBlendAdd;
502 break;
503 default:
504 TVGLOG("SW_ENGINE", "Non supported blending option = %d", (int) method);
505 surface->blender = nullptr;
506 break;
507 }
508 return false;
509 }
510
511
512 RenderRegion SwRenderer::region(RenderData data)
513 {
514 return static_cast<SwTask*>(data)->bounds();
515 }
516
517
518 bool SwRenderer::beginComposite(RenderCompositor* cmp, CompositeMethod method, uint8_t opacity)
519 {
520 if (!cmp) return false;
521 auto p = static_cast<SwCompositor*>(cmp);
522
523 p->method = method;
524 p->opacity = opacity;
525
526 //Current Context?
527 if (p->method != CompositeMethod::None) {
528 surface = p->recoverSfc;
529 surface->compositor = p;
530 }
531
532 return true;
533 }
534
535
536 bool SwRenderer::mempool(bool shared)
537 {
538 if (shared == sharedMpool) return true;
539
540 if (shared) {
541 if (!sharedMpool) {
542 if (!mpoolTerm(mpool)) return false;
543 mpool = globalMpool;
544 }
545 } else {
546 if (sharedMpool) mpool = mpoolInit(threadsCnt);
547 }
548
549 sharedMpool = shared;
550
551 if (mpool) return true;
552 return false;
553 }
554
555
556 const RenderSurface* SwRenderer::mainSurface()
557 {
558 return surface;
559 }
560
561
562 SwSurface* SwRenderer::request(int channelSize, bool square)
563 {
564 SwSurface* cmp = nullptr;
565 uint32_t w, h;
566
567 if (square) {
568 //Same Dimensional Size is demanded for the Post Processing Fast Flipping
569 w = h = std::max(surface->w, surface->h);
570 } else {
571 w = surface->w;
572 h = surface->h;
573 }
574
575 //Use cached data
576 for (auto p = compositors.begin(); p < compositors.end(); ++p) {
577 auto cur = *p;
578 if (cur->compositor->valid && cur->compositor->image.channelSize == channelSize) {
579 if (w == cur->w && h == cur->h) {
580 cmp = *p;
581 break;
582 }
583 }
584 }
585
586 //New Composition
587 if (!cmp) {
588 //Inherits attributes from main surface
589 cmp = new SwSurface(surface);
590 cmp->compositor = new SwCompositor;
591 cmp->compositor->image.data = (pixel_t*)malloc(channelSize * w * h);
592 cmp->w = cmp->compositor->image.w = w;
593 cmp->h = cmp->compositor->image.h = h;
594 cmp->stride = cmp->compositor->image.stride = w;
595 cmp->compositor->image.direct = true;
596 cmp->compositor->valid = true;
597 cmp->channelSize = cmp->compositor->image.channelSize = channelSize;
598
599 compositors.push(cmp);
600 }
601
602 //Sync. This may have been modified by post-processing.
603 cmp->data = cmp->compositor->image.data;
604
605 return cmp;
606 }
607
608
609 RenderCompositor* SwRenderer::target(const RenderRegion& region, ColorSpace cs, CompositionFlag flags)
610 {
611 auto x = region.x;
612 auto y = region.y;
613 auto w = region.w;
614 auto h = region.h;
615 auto sw = static_cast<int32_t>(surface->w);
616 auto sh = static_cast<int32_t>(surface->h);
617
618 //Out of boundary
619 if (x >= sw || y >= sh || x + w < 0 || y + h < 0) return nullptr;
620
621 auto cmp = request(CHANNEL_SIZE(cs), (flags & CompositionFlag::PostProcessing));
622
623 //Boundary Check
624 if (x < 0) x = 0;
625 if (y < 0) y = 0;
626 if (x + w > sw) w = (sw - x);
627 if (y + h > sh) h = (sh - y);
628
629 if (w == 0 || h == 0) return nullptr;
630
631 cmp->compositor->recoverSfc = surface;
632 cmp->compositor->recoverCmp = surface->compositor;
633 cmp->compositor->valid = false;
634 cmp->compositor->bbox.min.x = x;
635 cmp->compositor->bbox.min.y = y;
636 cmp->compositor->bbox.max.x = x + w;
637 cmp->compositor->bbox.max.y = y + h;
638
639 /* TODO: Currently, only blending might work.
640 Blending and composition must be handled together. */
641 auto color = (surface->blender && !surface->compositor) ? 0x00ffffff : 0x00000000;
642 rasterClear(cmp, x, y, w, h, color);
643
644 //Switch render target
645 surface = cmp;
646
647 return cmp->compositor;
648 }
649
650
651 bool SwRenderer::endComposite(RenderCompositor* cmp)
652 {
653 if (!cmp) return false;
654
655 auto p = static_cast<SwCompositor*>(cmp);
656
657 //Recover Context
658 surface = p->recoverSfc;
659 surface->compositor = p->recoverCmp;
660
661 //only invalid (currently used) surface can be composited
662 if (p->valid) return true;
663 p->valid = true;
664
665 //Default is alpha blending
666 if (p->method == CompositeMethod::None) {
667 return rasterDirectImage(surface, p->image, p->bbox, p->opacity);
668 }
669
670 return true;
671 }
672
673
674 void SwRenderer::prepare(RenderEffect* effect, const Matrix& transform)
675 {
676 switch (effect->type) {
677 case SceneEffect::GaussianBlur: effectGaussianBlurUpdate(static_cast<RenderEffectGaussianBlur*>(effect), transform); break;
678 case SceneEffect::DropShadow: effectDropShadowUpdate(static_cast<RenderEffectDropShadow*>(effect), transform); break;
679 case SceneEffect::Fill: effectFillUpdate(static_cast<RenderEffectFill*>(effect)); break;
680 case SceneEffect::Tint: effectTintUpdate(static_cast<RenderEffectTint*>(effect)); break;
681 case SceneEffect::Tritone: effectTritoneUpdate(static_cast<RenderEffectTritone*>(effect)); break;
682 default: break;
683 }
684 }
685
686
687 bool SwRenderer::region(RenderEffect* effect)
688 {
689 switch (effect->type) {
690 case SceneEffect::GaussianBlur: return effectGaussianBlurRegion(static_cast<RenderEffectGaussianBlur*>(effect));
691 case SceneEffect::DropShadow: return effectDropShadowRegion(static_cast<RenderEffectDropShadow*>(effect));
692 default: return false;
693 }
694 }
695
696
697 bool SwRenderer::render(RenderCompositor* cmp, const RenderEffect* effect, bool direct)
698 {
699 auto p = static_cast<SwCompositor*>(cmp);
700
701 if (p->image.channelSize != sizeof(uint32_t)) {
702 TVGERR("SW_ENGINE", "Not supported grayscale Gaussian Blur!");
703 return false;
704 }
705
706 //TODO: Support grayscale effects.
707 if (p->recoverSfc->channelSize != sizeof(uint32_t)) direct = false;
708
709 switch (effect->type) {
710 case SceneEffect::GaussianBlur: {
711 return effectGaussianBlur(p, request(surface->channelSize, true), static_cast<const RenderEffectGaussianBlur*>(effect));
712 }
713 case SceneEffect::DropShadow: {
714 auto cmp1 = request(surface->channelSize, true);
715 cmp1->compositor->valid = false; //prevent a conflict with cmp2 request.
716 auto cmp2 = request(surface->channelSize, true);
717 SwSurface* surfaces[] = {cmp1, cmp2};
718 auto ret = effectDropShadow(p, surfaces, static_cast<const RenderEffectDropShadow*>(effect), direct);
719 cmp1->compositor->valid = true;
720 return ret;
721 }
722 case SceneEffect::Fill: {
723 return effectFill(p, static_cast<const RenderEffectFill*>(effect), direct);
724 }
725 case SceneEffect::Tint: {
726 return effectTint(p, static_cast<const RenderEffectTint*>(effect), direct);
727 }
728 case SceneEffect::Tritone: {
729 return effectTritone(p, static_cast<const RenderEffectTritone*>(effect), direct);
730 }
731 default: return false;
732 }
733 }
734
735
736 ColorSpace SwRenderer::colorSpace()
737 {
738 if (surface) return surface->cs;
739 else return ColorSpace::Unsupported;
740 }
741
742
743 void SwRenderer::dispose(RenderData data)
744 {
745 auto task = static_cast<SwTask*>(data);
746 if (!task) return;
747 task->done();
748 task->dispose();
749
750 if (task->pushed) task->disposed = true;
751 else delete(task);
752 }
753
754
755 void* SwRenderer::prepareCommon(SwTask* task, const Matrix& transform, const Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags)
756 {
757 if (!surface || (transform.e11 == 0.0f && transform.e12 == 0.0f) || (transform.e21 == 0.0f && transform.e22 == 0.0f)) return task; //invalid
758
759 //TODO: Failed threading them. It would be better if it's possible.
760 //See: https://github.com/thorvg/thorvg/issues/1409
761 //Guarantee composition targets get ready.
762 for (auto clip = clips.begin(); clip < clips.end(); ++clip) {
763 static_cast<SwTask*>(*clip)->done();
764 }
765
766 task->clips = clips;
767 task->transform = transform;
768
769 //zero size?
770 if (task->transform.e11 == 0.0f && task->transform.e12 == 0.0f) return task; //zero width
771 if (task->transform.e21 == 0.0f && task->transform.e22 == 0.0f) return task; //zero height
772
773 task->opacity = opacity;
774 task->surface = surface;
775 task->mpool = mpool;
776 task->flags = flags;
777 task->bbox.min.x = std::max(static_cast<SwCoord>(0), static_cast<SwCoord>(vport.x));
778 task->bbox.min.y = std::max(static_cast<SwCoord>(0), static_cast<SwCoord>(vport.y));
779 task->bbox.max.x = std::min(static_cast<SwCoord>(surface->w), static_cast<SwCoord>(vport.x + vport.w));
780 task->bbox.max.y = std::min(static_cast<SwCoord>(surface->h), static_cast<SwCoord>(vport.y + vport.h));
781
782 if (!task->pushed) {
783 task->pushed = true;
784 tasks.push(task);
785 }
786
787 //TODO: Failed threading them. It would be better if it's possible.
788 //See: https://github.com/thorvg/thorvg/issues/1409
789 //Guarantee composition targets get ready.
790 if (flags & RenderUpdateFlag::Clip) {
791 for (uint32_t i = 0; i < clips.count; ++i) {
792 static_cast<SwTask*>(clips[i])->done();
793 }
794 }
795
796 if (flags) TaskScheduler::request(task);
797
798 return task;
799 }
800
801
802 RenderData SwRenderer::prepare(RenderSurface* surface, RenderData data, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags)
803 {
804 //prepare task
805 auto task = static_cast<SwImageTask*>(data);
806 if (task) task->done();
807 else {
808 task = new SwImageTask;
809 task->source = surface;
810 }
811
812 return prepareCommon(task, transform, clips, opacity, flags);
813 }
814
815
816 RenderData SwRenderer::prepare(const RenderShape& rshape, RenderData data, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags, bool clipper)
817 {
818 //prepare task
819 auto task = static_cast<SwShapeTask*>(data);
820 if (task) task->done();
821 else {
822 task = new SwShapeTask;
823 task->rshape = &rshape;
824 }
825
826 task->clipper = clipper;
827
828 return prepareCommon(task, transform, clips, opacity, flags);
829 }
830
831
832 SwRenderer::SwRenderer():mpool(globalMpool)
833 {
834 }
835
836
837 bool SwRenderer::init(uint32_t threads)
838 {
839 if ((initEngineCnt++) > 0) return true;
840
841 threadsCnt = threads;
842
843 //Share the memory pool among the renderer
844 globalMpool = mpoolInit(threads);
845 if (!globalMpool) {
846 --initEngineCnt;
847 return false;
848 }
849
850 return true;
851 }
852
853
854 int32_t SwRenderer::init()
855 {
856 #ifdef THORVG_SW_OPENMP_SUPPORT
857 omp_set_num_threads(TaskScheduler::threads());
858 #endif
859
860 return initEngineCnt;
861 }
862
863
864 bool SwRenderer::term()
865 {
866 if ((--initEngineCnt) > 0) return true;
867
868 initEngineCnt = 0;
869
870 _termEngine();
871
872 return true;
873 }
874
875 SwRenderer* SwRenderer::gen()
876 {
877 ++rendererCnt;
878 return new SwRenderer();
879 }