| 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_PAINT_H_ |
| 24 |
#define _TVG_PAINT_H_ |
| 25 |
|
| 26 |
#include "tvgRender.h" |
| 27 |
#include "tvgMath.h" |
| 28 |
|
| 29 |
namespace tvg |
| 30 |
{ |
| 31 |
enum ContextFlag : uint8_t {Default = 0, FastTrack = 1}; |
| 32 |
|
| 33 |
struct Iterator |
| 34 |
{ |
| 35 |
virtual ~Iterator() {} |
| 36 |
virtual const Paint* next() = 0; |
| 37 |
virtual uint32_t count() = 0; |
| 38 |
virtual void begin() = 0; |
| 39 |
}; |
| 40 |
|
| 41 |
struct Composite |
| 42 |
{ |
| 43 |
Paint* target; |
| 44 |
Paint* source; |
| 45 |
CompositeMethod method; |
| 46 |
}; |
| 47 |
|
| 48 |
struct Paint::Impl |
| 49 |
{ |
| 50 |
Paint* paint = nullptr; |
| 51 |
Composite* compData = nullptr; |
| 52 |
Paint* clipper = nullptr; |
| 53 |
RenderMethod* renderer = nullptr; |
| 54 |
struct { |
| 55 |
Matrix m; |
| 56 |
Matrix cm; |
| 57 |
float degree; |
| 58 |
float scale; |
| 59 |
bool overriding; |
| 60 |
|
| 61 |
void update() |
| 62 |
{ |
| 63 |
if (overriding) return; |
| 64 |
m.e11 = 1.0f; |
| 65 |
m.e12 = 0.0f; |
| 66 |
m.e21 = 0.0f; |
| 67 |
m.e22 = 1.0f; |
| 68 |
m.e31 = 0.0f; |
| 69 |
m.e32 = 0.0f; |
| 70 |
m.e33 = 1.0f; |
| 71 |
tvg::scale(&m, scale, scale); |
| 72 |
tvg::rotate(&m, degree); |
| 73 |
} |
| 74 |
} tr; |
| 75 |
RenderUpdateFlag renderFlag = RenderUpdateFlag::None; |
| 76 |
BlendMethod blendMethod; |
| 77 |
uint16_t refCnt = 0; |
| 78 |
uint8_t ctxFlag; |
| 79 |
uint8_t opacity; |
| 80 |
|
| 81 |
Impl(Paint* pnt) : paint(pnt) |
| 82 |
{ |
| 83 |
reset(); |
| 84 |
} |
| 85 |
|
| 86 |
~Impl() |
| 87 |
{ |
| 88 |
if (compData) { |
| 89 |
if (P(compData->target)->unref() == 0) delete(compData->target); |
| 90 |
free(compData); |
| 91 |
} |
| 92 |
if (clipper && P(clipper)->unref() == 0) delete(clipper); |
| 93 |
if (renderer && (renderer->unref() == 0)) delete(renderer); |
| 94 |
} |
| 95 |
|
| 96 |
uint8_t ref() |
| 97 |
{ |
| 98 |
return ++refCnt; |
| 99 |
} |
| 100 |
|
| 101 |
uint8_t unref() |
| 102 |
{ |
| 103 |
if (refCnt == 0) TVGERR("RENDERER", "Corrupted reference count!"); |
| 104 |
return --refCnt; |
| 105 |
} |
| 106 |
|
| 107 |
bool transform(const Matrix& m) |
| 108 |
{ |
| 109 |
if (&tr.m != &m) tr.m = m; |
| 110 |
tr.overriding = true; |
| 111 |
renderFlag |= RenderUpdateFlag::Transform; |
| 112 |
|
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
Matrix& transform(bool origin = false) |
| 117 |
{ |
| 118 |
|
| 119 |
if (renderFlag & RenderUpdateFlag::Transform) tr.update(); |
| 120 |
if (origin) return tr.cm; |
| 121 |
return tr.m; |
| 122 |
} |
| 123 |
|
| 124 |
void clip(Paint* clp) |
| 125 |
{ |
| 126 |
if (this->clipper) { |
| 127 |
P(this->clipper)->unref(); |
| 128 |
if (this->clipper != clp && P(this->clipper)->refCnt == 0) { |
| 129 |
delete(this->clipper); |
| 130 |
} |
| 131 |
} |
| 132 |
this->clipper = clp; |
| 133 |
if (!clp) return; |
| 134 |
|
| 135 |
P(clipper)->ref(); |
| 136 |
} |
| 137 |
|
| 138 |
bool composite(Paint* source, Paint* target, CompositeMethod method) |
| 139 |
{ |
| 140 |
|
| 141 |
if ((!target && method != CompositeMethod::None) || (target && method == CompositeMethod::None)) return false; |
| 142 |
|
| 143 |
if (compData) { |
| 144 |
P(compData->target)->unref(); |
| 145 |
if ((compData->target != target) && P(compData->target)->refCnt == 0) { |
| 146 |
delete(compData->target); |
| 147 |
} |
| 148 |
|
| 149 |
if (!target && method == CompositeMethod::None) { |
| 150 |
free(compData); |
| 151 |
compData = nullptr; |
| 152 |
return true; |
| 153 |
} |
| 154 |
} else { |
| 155 |
if (!target && method == CompositeMethod::None) return true; |
| 156 |
compData = static_cast<Composite*>(calloc(1, sizeof(Composite))); |
| 157 |
} |
| 158 |
P(target)->ref(); |
| 159 |
compData->target = target; |
| 160 |
compData->source = source; |
| 161 |
compData->method = method; |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
RenderRegion bounds(RenderMethod* renderer) const; |
| 166 |
Iterator* iterator(); |
| 167 |
bool rotate(float degree); |
| 168 |
bool scale(float factor); |
| 169 |
bool translate(float x, float y); |
| 170 |
bool bounds(float* x, float* y, float* w, float* h, bool transformed, bool stroking, bool origin = false); |
| 171 |
RenderData update(RenderMethod* renderer, const Matrix& pm, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper = false); |
| 172 |
bool render(RenderMethod* renderer); |
| 173 |
Paint* duplicate(Paint* ret = nullptr); |
| 174 |
void reset(); |
| 175 |
}; |
| 176 |
} |
| 177 |
|
| 178 |
#endif |