1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. 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
12  * all 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 #ifndef VRLE_H
24 #define VRLE_H
25 
26 #include <vector>
27 #include "vcowptr.h"
28 #include "vglobal.h"
29 #include "vpoint.h"
30 #include "vrect.h"
31 
32 V_BEGIN_NAMESPACE
33 
34 class VRle {
35 public:
36     struct Span {
37         short  x{0};
38         short  y{0};
39         ushort len{0};
40         uchar  coverage{0};
41     };
42     using VRleSpanCb = void (*)(size_t count, const VRle::Span *spans,
43                                 void *userData);
empty()44     bool  empty() const { return d->empty(); }
boundingRect()45     VRect boundingRect() const { return d->bbox(); }
setBoundingRect(const VRect & bbox)46     void  setBoundingRect(const VRect &bbox) { d->setBbox(bbox); }
addSpan(const VRle::Span * span,size_t count)47     void  addSpan(const VRle::Span *span, size_t count)
48     {
49         d.write().addSpan(span, count);
50     }
51 
reset()52     void reset() { d.write().reset(); }
translate(const VPoint & p)53     void translate(const VPoint &p) { d.write().translate(p); }
54 
55     void operator*=(uchar alpha) { d.write() *= alpha; }
56 
57     void intersect(const VRect &r, VRleSpanCb cb, void *userData) const;
58     void intersect(const VRle &rle, VRleSpanCb cb, void *userData) const;
59 
60     void operator&=(const VRle &o);
61     VRle operator&(const VRle &o) const;
62     VRle operator-(const VRle &o) const;
63     VRle operator+(const VRle &o) const { return opGeneric(o, Data::Op::Add); }
64     VRle operator^(const VRle &o) const { return opGeneric(o, Data::Op::Xor); }
65 
66     friend VRle operator-(const VRect &rect, const VRle &o);
67     friend VRle operator&(const VRect &rect, const VRle &o);
68 
unique()69     bool   unique() const { return d.unique(); }
refCount()70     size_t refCount() const { return d.refCount(); }
clone(const VRle & o)71     void   clone(const VRle &o) { d.write().clone(o.d.read()); }
72 
73 public:
74     struct View {
75         Span * _data;
76         size_t _size;
ViewView77         View(const Span *data, size_t sz) : _data((Span *)data), _size(sz) {}
dataView78         Span * data() { return _data; }
sizeView79         size_t size() { return _size; }
80     };
81     struct Data {
82         enum class Op { Add, Xor, Substract };
viewData83         VRle::View view() const
84         {
85             return VRle::View(mSpans.data(), mSpans.size());
86         }
emptyData87         bool  empty() const { return mSpans.empty(); }
88         void  addSpan(const VRle::Span *span, size_t count);
89         void  updateBbox() const;
90         VRect bbox() const;
91         void  setBbox(const VRect &bbox) const;
92         void  reset();
93         void  translate(const VPoint &p);
94         void  operator*=(uchar alpha);
95         void  opGeneric(const VRle::Data &, const VRle::Data &, Op code);
96         void  opSubstract(const VRle::Data &, const VRle::Data &);
97         void  opIntersect(VRle::View a, VRle::View b);
98         void  opIntersect(const VRect &, VRle::VRleSpanCb, void *) const;
99         void  addRect(const VRect &rect);
100         void  clone(const VRle::Data &);
101 
102         std::vector<VRle::Span> mSpans;
103         VPoint                  mOffset;
104         mutable VRect           mBbox;
105         mutable bool            mBboxDirty = true;
106     };
107 
108 private:
109     VRle opGeneric(const VRle &o, Data::Op opcode) const;
110 
111     vcow_ptr<Data> d;
112 };
113 
intersect(const VRect & r,VRleSpanCb cb,void * userData)114 inline void VRle::intersect(const VRect &r, VRleSpanCb cb, void *userData) const
115 {
116     d->opIntersect(r, cb, userData);
117 }
118 
119 V_END_NAMESPACE
120 
121 #endif  // VRLE_H
122