sw_engine: concrete shape rendering sequence.
[platform/core/graphics/tizenvg.git] / src / lib / tvgRenderCommon.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17 #ifndef _TVG_RENDER_COMMON_H_
18 #define _TVG_RENDER_COMMON_H_
19
20 namespace tvg
21 {
22
23 struct Surface
24 {
25     //TODO: Union for multiple types
26     uint32_t* buffer;
27     size_t stride;
28     size_t height;
29 };
30
31 class RenderMethod
32 {
33 public:
34     enum UpdateFlag { None = 0, Path = 1, Fill = 2, All = 3 };
35     virtual ~RenderMethod() {}
36     virtual void* prepare(const ShapeNode& shape, void* data, UpdateFlag flags) = 0;
37     virtual bool dispose(const ShapeNode& shape, void *data) = 0;
38     virtual bool render(const ShapeNode& shape, void *data) = 0;
39     virtual size_t ref() = 0;
40     virtual size_t unref() = 0;
41 };
42
43 struct RenderInitializer
44 {
45     RenderMethod* pInst = nullptr;
46     size_t refCnt = 0;
47     bool initialized = false;
48
49     static int init(RenderInitializer& renderInit, RenderMethod* engine)
50     {
51         assert(engine);
52         if (renderInit.pInst || renderInit.refCnt > 0) return -1;
53         renderInit.pInst = engine;
54         renderInit.refCnt = 0;
55         renderInit.initialized = true;
56         return 0;
57     }
58
59     static int term(RenderInitializer& renderInit)
60     {
61         if (!renderInit.pInst || !renderInit.initialized) return -1;
62
63         renderInit.initialized = false;
64
65         //Still it's refered....
66         if (renderInit.refCnt > 0) return  0;
67         delete(renderInit.pInst);
68         renderInit.pInst = nullptr;
69
70         return 0;
71     }
72
73     static size_t unref(RenderInitializer& renderInit)
74     {
75         assert(renderInit.refCnt > 0);
76         --renderInit.refCnt;
77
78         //engine has been requested to termination
79         if (!renderInit.initialized && renderInit.refCnt == 0) {
80             if (renderInit.pInst) {
81                 delete(renderInit.pInst);
82                 renderInit.pInst = nullptr;
83             }
84         }
85         return renderInit.refCnt;
86     }
87
88     static RenderMethod* inst(RenderInitializer& renderInit)
89     {
90         assert(renderInit.pInst);
91         return renderInit.pInst;
92     }
93
94     static size_t ref(RenderInitializer& renderInit)
95     {
96         return ++renderInit.refCnt;
97     }
98
99 };
100
101 }
102
103 #endif //_TVG_RENDER_COMMON_H_