| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifndef _TVG_RENDER_H_ |
| 24 |
#define _TVG_RENDER_H_ |
| 25 |
|
| 26 |
#include <math.h> |
| 27 |
#include <cstdarg> |
| 28 |
#include "tvgCommon.h" |
| 29 |
#include "tvgArray.h" |
| 30 |
#include "tvgLock.h" |
| 31 |
|
| 32 |
namespace tvg |
| 33 |
{ |
| 34 |
|
| 35 |
using RenderData = void*; |
| 36 |
using pixel_t = uint32_t; |
| 37 |
|
| 38 |
#define DASH_PATTERN_THRESHOLD 0.001f |
| 39 |
|
| 40 |
enum RenderUpdateFlag : uint16_t {None = 0, Path = 1, Color = 2, Gradient = 4, Stroke = 8, Transform = 16, Image = 32, GradientStroke = 64, Blend = 128, Clip = 256, All = 0xffff}; |
| 41 |
enum CompositionFlag : uint8_t {Invalid = 0, Opacity = 1, Blending = 2, Masking = 4, PostProcessing = 8}; |
| 42 |
|
| 43 |
static inline void operator|=(RenderUpdateFlag& a, const RenderUpdateFlag b) |
| 44 |
{ |
| 45 |
a = RenderUpdateFlag(uint16_t(a) | uint16_t(b)); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
enum ColorSpace : uint8_t |
| 50 |
{ |
| 51 |
ABGR8888 = 0, |
| 52 |
ARGB8888, |
| 53 |
ABGR8888S, |
| 54 |
ARGB8888S, |
| 55 |
Grayscale8, |
| 56 |
Unsupported |
| 57 |
}; |
| 58 |
|
| 59 |
struct RenderSurface |
| 60 |
{ |
| 61 |
union { |
| 62 |
pixel_t* data = nullptr; |
| 63 |
uint32_t* buf32; |
| 64 |
uint8_t* buf8; |
| 65 |
}; |
| 66 |
Key key; |
| 67 |
uint32_t stride = 0; |
| 68 |
uint32_t w = 0, h = 0; |
| 69 |
ColorSpace cs = ColorSpace::Unsupported; |
| 70 |
uint8_t channelSize = 0; |
| 71 |
bool premultiplied = false; |
| 72 |
|
| 73 |
RenderSurface() |
| 74 |
{ |
| 75 |
} |
| 76 |
|
| 77 |
RenderSurface(const RenderSurface* rhs) |
| 78 |
{ |
| 79 |
data = rhs->data; |
| 80 |
stride = rhs->stride; |
| 81 |
w = rhs->w; |
| 82 |
h = rhs->h; |
| 83 |
cs = rhs->cs; |
| 84 |
channelSize = rhs->channelSize; |
| 85 |
premultiplied = rhs->premultiplied; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
}; |
| 90 |
|
| 91 |
struct RenderCompositor |
| 92 |
{ |
| 93 |
CompositeMethod method; |
| 94 |
uint8_t opacity; |
| 95 |
}; |
| 96 |
|
| 97 |
struct RenderRegion |
| 98 |
{ |
| 99 |
int32_t x, y, w, h; |
| 100 |
|
| 101 |
void intersect(const RenderRegion& rhs); |
| 102 |
void add(const RenderRegion& rhs); |
| 103 |
|
| 104 |
bool operator==(const RenderRegion& rhs) const |
| 105 |
{ |
| 106 |
if (x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h) return true; |
| 107 |
return false; |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
struct RenderStroke |
| 112 |
{ |
| 113 |
float width = 0.0f; |
| 114 |
uint8_t color[4] = {0, 0, 0, 0}; |
| 115 |
Fill *fill = nullptr; |
| 116 |
float* dashPattern = nullptr; |
| 117 |
uint32_t dashCnt = 0; |
| 118 |
float dashOffset = 0.0f; |
| 119 |
StrokeCap cap = StrokeCap::Square; |
| 120 |
StrokeJoin join = StrokeJoin::Bevel; |
| 121 |
float miterlimit = 4.0f; |
| 122 |
bool strokeFirst = false; |
| 123 |
|
| 124 |
struct { |
| 125 |
float begin = 0.0f; |
| 126 |
float end = 1.0f; |
| 127 |
bool simultaneous = true; |
| 128 |
} trim; |
| 129 |
|
| 130 |
void operator=(const RenderStroke& rhs) |
| 131 |
{ |
| 132 |
width = rhs.width; |
| 133 |
|
| 134 |
memcpy(color, rhs.color, sizeof(color)); |
| 135 |
|
| 136 |
delete(fill); |
| 137 |
if (rhs.fill) fill = rhs.fill->duplicate(); |
| 138 |
else fill = nullptr; |
| 139 |
|
| 140 |
free(dashPattern); |
| 141 |
if (rhs.dashCnt > 0) { |
| 142 |
dashPattern = static_cast<float*>(malloc(sizeof(float) * rhs.dashCnt)); |
| 143 |
memcpy(dashPattern, rhs.dashPattern, sizeof(float) * rhs.dashCnt); |
| 144 |
} else { |
| 145 |
dashPattern = nullptr; |
| 146 |
} |
| 147 |
dashCnt = rhs.dashCnt; |
| 148 |
dashOffset = rhs.dashOffset; |
| 149 |
miterlimit = rhs.miterlimit; |
| 150 |
cap = rhs.cap; |
| 151 |
join = rhs.join; |
| 152 |
strokeFirst = rhs.strokeFirst; |
| 153 |
trim = rhs.trim; |
| 154 |
} |
| 155 |
|
| 156 |
bool strokeTrim(float& begin, float& end) const |
| 157 |
{ |
| 158 |
begin = trim.begin; |
| 159 |
end = trim.end; |
| 160 |
|
| 161 |
if (fabsf(end - begin) >= 1.0f) { |
| 162 |
begin = 0.0f; |
| 163 |
end = 1.0f; |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
auto loop = true; |
| 168 |
|
| 169 |
if (begin > 1.0f && end > 1.0f) loop = false; |
| 170 |
if (begin < 0.0f && end < 0.0f) loop = false; |
| 171 |
if (begin >= 0.0f && begin <= 1.0f && end >= 0.0f && end <= 1.0f) loop = false; |
| 172 |
|
| 173 |
if (begin > 1.0f) begin -= 1.0f; |
| 174 |
if (begin < 0.0f) begin += 1.0f; |
| 175 |
if (end > 1.0f) end -= 1.0f; |
| 176 |
if (end < 0.0f) end += 1.0f; |
| 177 |
|
| 178 |
if ((loop && begin < end) || (!loop && begin > end)) std::swap(begin, end); |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
~RenderStroke() |
| 183 |
{ |
| 184 |
free(dashPattern); |
| 185 |
delete(fill); |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
struct RenderShape |
| 190 |
{ |
| 191 |
struct |
| 192 |
{ |
| 193 |
Array<PathCommand> cmds; |
| 194 |
Array<Point> pts; |
| 195 |
} path; |
| 196 |
|
| 197 |
Fill *fill = nullptr; |
| 198 |
RenderStroke *stroke = nullptr; |
| 199 |
uint8_t color[4] = {0, 0, 0, 0}; |
| 200 |
FillRule rule = FillRule::Winding; |
| 201 |
|
| 202 |
~RenderShape() |
| 203 |
{ |
| 204 |
delete(fill); |
| 205 |
delete(stroke); |
| 206 |
} |
| 207 |
|
| 208 |
void fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const |
| 209 |
{ |
| 210 |
if (r) *r = color[0]; |
| 211 |
if (g) *g = color[1]; |
| 212 |
if (b) *b = color[2]; |
| 213 |
if (a) *a = color[3]; |
| 214 |
} |
| 215 |
|
| 216 |
float strokeWidth() const |
| 217 |
{ |
| 218 |
if (!stroke) return 0; |
| 219 |
return stroke->width; |
| 220 |
} |
| 221 |
|
| 222 |
bool strokeTrim() const |
| 223 |
{ |
| 224 |
if (!stroke) return false; |
| 225 |
if (stroke->trim.begin == 0.0f && stroke->trim.end == 1.0f) return false; |
| 226 |
if (fabsf(stroke->trim.end - stroke->trim.begin) >= 1.0f) return false; |
| 227 |
return true; |
| 228 |
} |
| 229 |
|
| 230 |
bool strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const |
| 231 |
{ |
| 232 |
if (!stroke) return false; |
| 233 |
|
| 234 |
if (r) *r = stroke->color[0]; |
| 235 |
if (g) *g = stroke->color[1]; |
| 236 |
if (b) *b = stroke->color[2]; |
| 237 |
if (a) *a = stroke->color[3]; |
| 238 |
|
| 239 |
return true; |
| 240 |
} |
| 241 |
|
| 242 |
const Fill* strokeFill() const |
| 243 |
{ |
| 244 |
if (!stroke) return nullptr; |
| 245 |
return stroke->fill; |
| 246 |
} |
| 247 |
|
| 248 |
uint32_t strokeDash(const float** dashPattern, float* offset) const |
| 249 |
{ |
| 250 |
if (!stroke) return 0; |
| 251 |
if (dashPattern) *dashPattern = stroke->dashPattern; |
| 252 |
if (offset) *offset = stroke->dashOffset; |
| 253 |
return stroke->dashCnt; |
| 254 |
} |
| 255 |
|
| 256 |
StrokeCap strokeCap() const |
| 257 |
{ |
| 258 |
if (!stroke) return StrokeCap::Square; |
| 259 |
return stroke->cap; |
| 260 |
} |
| 261 |
|
| 262 |
StrokeJoin strokeJoin() const |
| 263 |
{ |
| 264 |
if (!stroke) return StrokeJoin::Bevel; |
| 265 |
return stroke->join; |
| 266 |
} |
| 267 |
|
| 268 |
float strokeMiterlimit() const |
| 269 |
{ |
| 270 |
if (!stroke) return 4.0f; |
| 271 |
return stroke->miterlimit;; |
| 272 |
} |
| 273 |
}; |
| 274 |
|
| 275 |
struct RenderEffect |
| 276 |
{ |
| 277 |
RenderData rd = nullptr; |
| 278 |
RenderRegion extend = {0, 0, 0, 0}; |
| 279 |
SceneEffect type; |
| 280 |
bool valid = false; |
| 281 |
|
| 282 |
virtual ~RenderEffect() |
| 283 |
{ |
| 284 |
free(rd); |
| 285 |
} |
| 286 |
}; |
| 287 |
|
| 288 |
struct RenderEffectGaussianBlur : RenderEffect |
| 289 |
{ |
| 290 |
float sigma; |
| 291 |
uint8_t direction; |
| 292 |
uint8_t border; |
| 293 |
uint8_t quality; |
| 294 |
|
| 295 |
static RenderEffectGaussianBlur* gen(va_list& args) |
| 296 |
{ |
| 297 |
auto inst = new RenderEffectGaussianBlur; |
| 298 |
inst->sigma = std::max((float) va_arg(args, double), 0.0f); |
| 299 |
inst->direction = std::min(va_arg(args, int), 2); |
| 300 |
inst->border = std::min(va_arg(args, int), 1); |
| 301 |
inst->quality = std::min(va_arg(args, int), 100); |
| 302 |
inst->type = SceneEffect::GaussianBlur; |
| 303 |
return inst; |
| 304 |
} |
| 305 |
}; |
| 306 |
|
| 307 |
struct RenderEffectDropShadow : RenderEffect |
| 308 |
{ |
| 309 |
uint8_t color[4]; |
| 310 |
float angle; |
| 311 |
float distance; |
| 312 |
float sigma; |
| 313 |
uint8_t quality; |
| 314 |
|
| 315 |
static RenderEffectDropShadow* gen(va_list& args) |
| 316 |
{ |
| 317 |
auto inst = new RenderEffectDropShadow; |
| 318 |
inst->color[0] = va_arg(args, int); |
| 319 |
inst->color[1] = va_arg(args, int); |
| 320 |
inst->color[2] = va_arg(args, int); |
| 321 |
inst->color[3] = va_arg(args, int); |
| 322 |
inst->angle = (float) va_arg(args, double); |
| 323 |
inst->distance = (float) va_arg(args, double); |
| 324 |
inst->sigma = std::max((float) va_arg(args, double), 0.0f); |
| 325 |
inst->quality = std::min(va_arg(args, int), 100); |
| 326 |
inst->type = SceneEffect::DropShadow; |
| 327 |
return inst; |
| 328 |
} |
| 329 |
}; |
| 330 |
|
| 331 |
struct RenderEffectFill : RenderEffect |
| 332 |
{ |
| 333 |
uint8_t color[4]; |
| 334 |
|
| 335 |
static RenderEffectFill* gen(va_list& args) |
| 336 |
{ |
| 337 |
auto inst = new RenderEffectFill; |
| 338 |
inst->color[0] = va_arg(args, int); |
| 339 |
inst->color[1] = va_arg(args, int); |
| 340 |
inst->color[2] = va_arg(args, int); |
| 341 |
inst->color[3] = va_arg(args, int); |
| 342 |
inst->type = SceneEffect::Fill; |
| 343 |
return inst; |
| 344 |
} |
| 345 |
}; |
| 346 |
|
| 347 |
struct RenderEffectTint : RenderEffect |
| 348 |
{ |
| 349 |
uint8_t black[3]; |
| 350 |
uint8_t white[3]; |
| 351 |
uint8_t intensity; |
| 352 |
|
| 353 |
static RenderEffectTint* gen(va_list& args) |
| 354 |
{ |
| 355 |
auto inst = new RenderEffectTint; |
| 356 |
inst->black[0] = va_arg(args, int); |
| 357 |
inst->black[1] = va_arg(args, int); |
| 358 |
inst->black[2] = va_arg(args, int); |
| 359 |
inst->white[0] = va_arg(args, int); |
| 360 |
inst->white[1] = va_arg(args, int); |
| 361 |
inst->white[2] = va_arg(args, int); |
| 362 |
inst->intensity = (uint8_t)(va_arg(args, double) * 2.55); |
| 363 |
inst->type = SceneEffect::Tint; |
| 364 |
return inst; |
| 365 |
} |
| 366 |
}; |
| 367 |
|
| 368 |
struct RenderEffectTritone : RenderEffect |
| 369 |
{ |
| 370 |
uint8_t shadow[3]; |
| 371 |
uint8_t midtone[3]; |
| 372 |
uint8_t highlight[3]; |
| 373 |
|
| 374 |
static RenderEffectTritone* gen(va_list& args) |
| 375 |
{ |
| 376 |
auto inst = new RenderEffectTritone; |
| 377 |
inst->shadow[0] = va_arg(args, int); |
| 378 |
inst->shadow[1] = va_arg(args, int); |
| 379 |
inst->shadow[2] = va_arg(args, int); |
| 380 |
inst->midtone[0] = va_arg(args, int); |
| 381 |
inst->midtone[1] = va_arg(args, int); |
| 382 |
inst->midtone[2] = va_arg(args, int); |
| 383 |
inst->highlight[0] = va_arg(args, int); |
| 384 |
inst->highlight[1] = va_arg(args, int); |
| 385 |
inst->highlight[2] = va_arg(args, int); |
| 386 |
inst->type = SceneEffect::Tritone; |
| 387 |
return inst; |
| 388 |
} |
| 389 |
}; |
| 390 |
|
| 391 |
class RenderMethod |
| 392 |
{ |
| 393 |
private: |
| 394 |
uint32_t refCnt = 0; |
| 395 |
Key key; |
| 396 |
|
| 397 |
public: |
| 398 |
uint32_t ref(); |
| 399 |
uint32_t unref(); |
| 400 |
|
| 401 |
virtual ~RenderMethod() {} |
| 402 |
virtual RenderData prepare(const RenderShape& rshape, RenderData data, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags, bool clipper) = 0; |
| 403 |
virtual RenderData prepare(RenderSurface* surface, RenderData data, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags) = 0; |
| 404 |
virtual bool preRender() = 0; |
| 405 |
virtual bool renderShape(RenderData data) = 0; |
| 406 |
virtual bool renderImage(RenderData data) = 0; |
| 407 |
virtual bool postRender() = 0; |
| 408 |
virtual void dispose(RenderData data) = 0; |
| 409 |
virtual RenderRegion region(RenderData data) = 0; |
| 410 |
virtual RenderRegion viewport() = 0; |
| 411 |
virtual bool viewport(const RenderRegion& vp) = 0; |
| 412 |
virtual bool blend(BlendMethod method) = 0; |
| 413 |
virtual ColorSpace colorSpace() = 0; |
| 414 |
virtual const RenderSurface* mainSurface() = 0; |
| 415 |
|
| 416 |
virtual bool clear() = 0; |
| 417 |
virtual bool sync() = 0; |
| 418 |
|
| 419 |
virtual RenderCompositor* target(const RenderRegion& region, ColorSpace cs, CompositionFlag flags) = 0; |
| 420 |
virtual bool beginComposite(RenderCompositor* cmp, CompositeMethod method, uint8_t opacity) = 0; |
| 421 |
virtual bool endComposite(RenderCompositor* cmp) = 0; |
| 422 |
|
| 423 |
virtual void prepare(RenderEffect* effect, const Matrix& transform) = 0; |
| 424 |
virtual bool region(RenderEffect* effect) = 0; |
| 425 |
virtual bool render(RenderCompositor* cmp, const RenderEffect* effect, bool direct) = 0; |
| 426 |
}; |
| 427 |
|
| 428 |
static inline bool MASK_REGION_MERGING(CompositeMethod method) |
| 429 |
{ |
| 430 |
switch(method) { |
| 431 |
case CompositeMethod::AlphaMask: |
| 432 |
case CompositeMethod::InvAlphaMask: |
| 433 |
case CompositeMethod::LumaMask: |
| 434 |
case CompositeMethod::InvLumaMask: |
| 435 |
case CompositeMethod::SubtractMask: |
| 436 |
case CompositeMethod::IntersectMask: |
| 437 |
return false; |
| 438 |
|
| 439 |
case CompositeMethod::AddMask: |
| 440 |
case CompositeMethod::DifferenceMask: |
| 441 |
case CompositeMethod::LightenMask: |
| 442 |
case CompositeMethod::DarkenMask: |
| 443 |
return true; |
| 444 |
default: |
| 445 |
TVGERR("RENDERER", "Unsupported Composite Method! = %d", (int)method); |
| 446 |
return false; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
static inline uint8_t CHANNEL_SIZE(ColorSpace cs) |
| 451 |
{ |
| 452 |
switch(cs) { |
| 453 |
case ColorSpace::ABGR8888: |
| 454 |
case ColorSpace::ABGR8888S: |
| 455 |
case ColorSpace::ARGB8888: |
| 456 |
case ColorSpace::ARGB8888S: |
| 457 |
return sizeof(uint32_t); |
| 458 |
case ColorSpace::Grayscale8: |
| 459 |
return sizeof(uint8_t); |
| 460 |
case ColorSpace::Unsupported: |
| 461 |
default: |
| 462 |
TVGERR("RENDERER", "Unsupported Channel Size! = %d", (int)cs); |
| 463 |
return 0; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
static inline ColorSpace COMPOSITE_TO_COLORSPACE(RenderMethod* renderer, CompositeMethod method) |
| 468 |
{ |
| 469 |
switch(method) { |
| 470 |
case CompositeMethod::AlphaMask: |
| 471 |
case CompositeMethod::InvAlphaMask: |
| 472 |
case CompositeMethod::AddMask: |
| 473 |
case CompositeMethod::DifferenceMask: |
| 474 |
case CompositeMethod::SubtractMask: |
| 475 |
case CompositeMethod::IntersectMask: |
| 476 |
case CompositeMethod::LightenMask: |
| 477 |
case CompositeMethod::DarkenMask: |
| 478 |
return ColorSpace::Grayscale8; |
| 479 |
|
| 480 |
case CompositeMethod::LumaMask: |
| 481 |
case CompositeMethod::InvLumaMask: |
| 482 |
return renderer->colorSpace(); |
| 483 |
default: |
| 484 |
TVGERR("RENDERER", "Unsupported Composite Size! = %d", (int)method); |
| 485 |
return ColorSpace::Unsupported; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
static inline uint8_t MULTIPLY(uint8_t c, uint8_t a) |
| 490 |
{ |
| 491 |
return (((c) * (a) + 0xff) >> 8); |
| 492 |
} |
| 493 |
|
| 494 |
} |
| 495 |
|
| 496 |
#endif |