1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_ROSEN_RENDER_CONTEXT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_ROSEN_RENDER_CONTEXT_H
18 
19 #ifndef USE_ROSEN_DRAWING
20 #include "include/core/SkCanvas.h"
21 #include "include/core/SkRefCnt.h"
22 #endif
23 
24 #include "base/geometry/rect.h"
25 #ifdef USE_ROSEN_DRAWING
26 #include "core/components_ng/render/drawing_forward.h"
27 #endif
28 #include "core/pipeline/base/render_context.h"
29 #include "core/pipeline/base/render_node.h"
30 
31 #ifndef USE_ROSEN_DRAWING
32 class SkImage;
33 class SkPicture;
34 class SkPictureRecorder;
35 #endif
36 
37 namespace OHOS::Ace {
38 
39 class RosenRenderContext : public RenderContext {
40     DECLARE_ACE_TYPE(RosenRenderContext, RenderContext)
41 
42 public:
43     RosenRenderContext() = default;
44     ~RosenRenderContext() override;
45 
46     void Repaint(const RefPtr<RenderNode>& node) override;
47     void PaintChild(const RefPtr<RenderNode>& child, const Offset& offset) override;
48     bool IsIntersectWith(const RefPtr<RenderNode>& child, Offset& offset) override;
49     void Restore() override;
50 
51     void InitContext(
52         const std::shared_ptr<RSNode>& rsNode, const Rect& rect, const Offset& initialOffset = Offset::Zero());
53 #ifndef USE_ROSEN_DRAWING
54     SkCanvas* GetCanvas();
55 #else
56     RSCanvas* GetCanvas();
57 #endif
58     const std::shared_ptr<RSNode>& GetRSNode();
59 
60     void StartRecording();
61     void StopRecordingIfNeeded();
62 
IsRecording()63     bool IsRecording()
64     {
65         return !!recordingCanvas_;
66     }
67 
68 #ifndef USE_ROSEN_DRAWING
69     sk_sp<SkPicture> FinishRecordingAsPicture();
70     sk_sp<SkImage> FinishRecordingAsImage();
71 #else
72     std::shared_ptr<RSPicture> FinishRecordingAsPicture();
73     std::shared_ptr<RSImage> FinishRecordingAsImage();
74 #endif
75 
76 private:
77     void UpdateChildren(const std::shared_ptr<RSNode>& rsNode);
78 
79     std::shared_ptr<RSNode> rsNode_ = nullptr;
80 #ifndef USE_ROSEN_DRAWING
81     SkPictureRecorder* recorder_ = nullptr;
82     SkCanvas* recordingCanvas_ = nullptr;
83     SkCanvas* rosenCanvas_ = nullptr;
84 #else
85     RSCanvas* recordingCanvas_ = nullptr;
86     RSCanvas* rosenCanvas_ = nullptr;
87 #endif
88     Rect estimatedRect_;
89     std::vector<std::weak_ptr<RSNode>> childNodes_;
90 };
91 
92 } // namespace OHOS::Ace
93 
94 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_ROSEN_RENDER_CONTEXT_H
95