• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "modifier_ng/custom/rs_custom_modifier.h"
17 
18 #include "pipeline/rs_node_map.h"
19 #include "pipeline/rs_recording_canvas.h"
20 #include "platform/common/rs_log.h"
21 #include "ui/rs_canvas_node.h"
22 
23 namespace OHOS::Rosen::ModifierNG {
CreateDrawingContext(std::shared_ptr<RSNode> node)24 RSDrawingContext RSCustomModifierHelper::CreateDrawingContext(std::shared_ptr<RSNode> node)
25 {
26     if (!node) {
27         RS_LOGE("RSCustomModifierHelper::FinishDrawing: node is nullptr");
28         return { nullptr };
29     }
30     if (!node->IsInstanceOf<RSCanvasNode>()) {
31         RS_LOGW("RSCustomModifierHelper::FinishDrawing: node is not a RSCanvasNode, type=%{public}u", node->GetType());
32         return { nullptr };
33     }
34     auto canvasNode = std::static_pointer_cast<RSCanvasNode>(node);
35     auto recordingCanvas = new ExtendRecordingCanvas(canvasNode->GetPaintWidth(), canvasNode->GetPaintHeight());
36     recordingCanvas->SetIsCustomTextType(canvasNode->GetIsCustomTextType());
37     recordingCanvas->SetIsCustomTypeface(canvasNode->GetIsCustomTypeface());
38     return { recordingCanvas, canvasNode->GetPaintWidth(), canvasNode->GetPaintHeight() };
39 }
40 
FinishDrawing(RSDrawingContext & ctx)41 std::shared_ptr<Drawing::DrawCmdList> RSCustomModifierHelper::FinishDrawing(RSDrawingContext& ctx)
42 {
43     auto recordingCanvas = static_cast<ExtendRecordingCanvas*>(ctx.canvas);
44     if (!recordingCanvas) {
45         RS_LOGW("RSCustomModifierHelper::FinishDrawing: recordingCanvas is nullptr");
46         return nullptr;
47     }
48     auto recording = recordingCanvas->GetDrawCmdList();
49     if (!recording) {
50         RS_LOGW("RSCustomModifierHelper::FinishDrawing: recording is nullptr");
51         delete ctx.canvas;
52         ctx.canvas = nullptr;
53         return nullptr;
54     }
55     if (RSSystemProperties::GetDrawTextAsBitmap()) {
56         // replace drawOpItem with cached one (generated by CPU)
57         recording->GenerateCache();
58     }
59     delete ctx.canvas;
60     ctx.canvas = nullptr;
61     return recording;
62 }
63 } // namespace OHOS::Rosen::ModifierNG
64