| 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_CANVAS_H_ |
| 24 |
#define _TVG_CANVAS_H_ |
| 25 |
|
| 26 |
#include "tvgPaint.h" |
| 27 |
|
| 28 |
|
| 29 |
enum Status : uint8_t {Synced = 0, Updating, Drawing, Damaged}; |
| 30 |
|
| 31 |
struct Canvas::Impl |
| 32 |
{ |
| 33 |
list<Paint*> paints; |
| 34 |
RenderMethod* renderer; |
| 35 |
RenderRegion vport = {0, 0, INT32_MAX, INT32_MAX}; |
| 36 |
Status status = Status::Synced; |
| 37 |
|
| 38 |
Impl(RenderMethod* pRenderer) : renderer(pRenderer) |
| 39 |
{ |
| 40 |
renderer->ref(); |
| 41 |
} |
| 42 |
|
| 43 |
~Impl() |
| 44 |
{ |
| 45 |
|
| 46 |
renderer->sync(); |
| 47 |
renderer->clear(); |
| 48 |
|
| 49 |
clearPaints(); |
| 50 |
|
| 51 |
if (renderer->unref() == 0) delete(renderer); |
| 52 |
} |
| 53 |
|
| 54 |
void clearPaints() |
| 55 |
{ |
| 56 |
for (auto paint : paints) { |
| 57 |
if (P(paint)->unref() == 0) delete(paint); |
| 58 |
} |
| 59 |
paints.clear(); |
| 60 |
} |
| 61 |
|
| 62 |
Result push(unique_ptr<Paint> paint) |
| 63 |
{ |
| 64 |
|
| 65 |
if (status == Status::Drawing) return Result::InsufficientCondition; |
| 66 |
|
| 67 |
auto p = paint.release(); |
| 68 |
if (!p) return Result::MemoryCorruption; |
| 69 |
PP(p)->ref(); |
| 70 |
paints.push_back(p); |
| 71 |
|
| 72 |
return update(p, true); |
| 73 |
} |
| 74 |
|
| 75 |
Result clear(bool free) |
| 76 |
{ |
| 77 |
|
| 78 |
if (!renderer->clear()) return Result::InsufficientCondition; |
| 79 |
|
| 80 |
|
| 81 |
if (free) clearPaints(); |
| 82 |
|
| 83 |
status = Status::Synced; |
| 84 |
|
| 85 |
return Result::Success; |
| 86 |
} |
| 87 |
|
| 88 |
Result update(Paint* paint, bool force) |
| 89 |
{ |
| 90 |
if (paints.empty() || status == Status::Drawing) return Result::InsufficientCondition; |
| 91 |
|
| 92 |
Array<RenderData> clips; |
| 93 |
auto flag = RenderUpdateFlag::None; |
| 94 |
if (status == Status::Damaged || force) flag = RenderUpdateFlag::All; |
| 95 |
|
| 96 |
auto m = Matrix{1, 0, 0, 0, 1, 0, 0, 0, 1}; |
| 97 |
|
| 98 |
if (paint) { |
| 99 |
paint->pImpl->update(renderer, m, clips, 255, flag); |
| 100 |
} else { |
| 101 |
for (auto paint : paints) { |
| 102 |
paint->pImpl->update(renderer, m, clips, 255, flag); |
| 103 |
} |
| 104 |
} |
| 105 |
status = Status::Updating; |
| 106 |
return Result::Success; |
| 107 |
} |
| 108 |
|
| 109 |
Result draw() |
| 110 |
{ |
| 111 |
if (status == Status::Damaged) update(nullptr, false); |
| 112 |
if (status == Status::Drawing || paints.empty() || !renderer->preRender()) return Result::InsufficientCondition; |
| 113 |
|
| 114 |
bool rendered = false; |
| 115 |
for (auto paint : paints) { |
| 116 |
if (paint->pImpl->render(renderer)) rendered = true; |
| 117 |
} |
| 118 |
|
| 119 |
if (!rendered || !renderer->postRender()) return Result::InsufficientCondition; |
| 120 |
|
| 121 |
status = Status::Drawing; |
| 122 |
return Result::Success; |
| 123 |
} |
| 124 |
|
| 125 |
Result sync() |
| 126 |
{ |
| 127 |
if (status == Status::Synced || status == Status::Damaged) return Result::InsufficientCondition; |
| 128 |
|
| 129 |
if (renderer->sync()) { |
| 130 |
status = Status::Synced; |
| 131 |
return Result::Success; |
| 132 |
} |
| 133 |
|
| 134 |
return Result::Unknown; |
| 135 |
} |
| 136 |
|
| 137 |
Result viewport(int32_t x, int32_t y, int32_t w, int32_t h) |
| 138 |
{ |
| 139 |
if (status != Status::Damaged && status != Status::Synced) return Result::InsufficientCondition; |
| 140 |
|
| 141 |
RenderRegion val = {x, y, w, h}; |
| 142 |
|
| 143 |
auto surface = renderer->mainSurface(); |
| 144 |
if (surface && surface->w > 0 && surface->h > 0) { |
| 145 |
val.intersect({0, 0, (int32_t)surface->w, (int32_t)surface->h}); |
| 146 |
} |
| 147 |
if (vport == val) return Result::Success; |
| 148 |
renderer->viewport(val); |
| 149 |
vport = val; |
| 150 |
status = Status::Damaged; |
| 151 |
return Result::Success; |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
#endif |