1 /*
2  * Copyright (c) 2024 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 "drawing_record_cmd.h"
17 
18 #include "drawing_canvas_utils.h"
19 #include "drawing_helper.h"
20 #include "pipeline/rs_record_cmd_utils.h"
21 #include "recording/record_cmd.h"
22 #include "utils/log.h"
23 
24 using namespace OHOS;
25 using namespace Rosen;
26 using namespace Drawing;
27 
CastToCmdUtils(OH_Drawing_RecordCmdUtils * cRecordCmdUtils)28 static RSRecordCmdUtils* CastToCmdUtils(OH_Drawing_RecordCmdUtils* cRecordCmdUtils)
29 {
30     return reinterpret_cast<RSRecordCmdUtils*>(cRecordCmdUtils);
31 }
32 
OH_Drawing_RecordCmdUtilsCreate()33 OH_Drawing_RecordCmdUtils* OH_Drawing_RecordCmdUtilsCreate()
34 {
35     return (OH_Drawing_RecordCmdUtils*)new (std::nothrow) RSRecordCmdUtils;
36 }
37 
OH_Drawing_RecordCmdUtilsDestroy(OH_Drawing_RecordCmdUtils * recordCmdUtils)38 OH_Drawing_ErrorCode OH_Drawing_RecordCmdUtilsDestroy(OH_Drawing_RecordCmdUtils* recordCmdUtils)
39 {
40     if (recordCmdUtils == nullptr) {
41         return OH_DRAWING_ERROR_INVALID_PARAMETER;
42     }
43     delete reinterpret_cast<RSRecordCmdUtils*>(recordCmdUtils);
44     return OH_DRAWING_SUCCESS;
45 }
46 
OH_Drawing_RecordCmdUtilsBeginRecording(OH_Drawing_RecordCmdUtils * cRecordCmdUtils,int32_t width,int32_t height,OH_Drawing_Canvas ** cCanvas)47 OH_Drawing_ErrorCode OH_Drawing_RecordCmdUtilsBeginRecording(OH_Drawing_RecordCmdUtils* cRecordCmdUtils,
48     int32_t width, int32_t height, OH_Drawing_Canvas** cCanvas)
49 {
50     if (cRecordCmdUtils == nullptr || width <= 0 || height <= 0 || cCanvas == nullptr) {
51         return OH_DRAWING_ERROR_INVALID_PARAMETER;
52     }
53     RSRecordCmdUtils* recordCmdUtils = CastToCmdUtils(cRecordCmdUtils);
54     auto bounds = Drawing::Rect(0, 0, width, height);
55     Drawing::Canvas* canvasPtr = recordCmdUtils->BeginRecording(bounds);
56     if (canvasPtr == nullptr) {
57         return OH_DRAWING_ERROR_ALLOCATION_FAILED;
58     }
59     *cCanvas = reinterpret_cast<OH_Drawing_Canvas*>(canvasPtr);
60     return OH_DRAWING_SUCCESS;
61 }
62 
OH_Drawing_RecordCmdUtilsFinishRecording(OH_Drawing_RecordCmdUtils * cRecordCmdUtils,OH_Drawing_RecordCmd ** cRecordCmd)63 OH_Drawing_ErrorCode OH_Drawing_RecordCmdUtilsFinishRecording(OH_Drawing_RecordCmdUtils* cRecordCmdUtils,
64     OH_Drawing_RecordCmd** cRecordCmd)
65 {
66     if (cRecordCmdUtils == nullptr || cRecordCmd == nullptr) {
67         return OH_DRAWING_ERROR_INVALID_PARAMETER;
68     }
69     RSRecordCmdUtils* recordCmdUtils = CastToCmdUtils(cRecordCmdUtils);
70     NativeHandle<RecordCmd>* recordCmdHandle = new(std::nothrow) NativeHandle<RecordCmd>;
71     if (recordCmdHandle == nullptr) {
72         return OH_DRAWING_ERROR_ALLOCATION_FAILED;
73     }
74     recordCmdHandle->value = recordCmdUtils->FinishRecording();
75     if (recordCmdHandle->value == nullptr) {
76         delete recordCmdHandle;
77         return OH_DRAWING_ERROR_ALLOCATION_FAILED;
78     }
79     *cRecordCmd = Helper::CastTo<NativeHandle<RecordCmd>*, OH_Drawing_RecordCmd*>(recordCmdHandle);
80     return OH_DRAWING_SUCCESS;
81 }
82 
OH_Drawing_RecordCmdDestroy(OH_Drawing_RecordCmd * recordCmd)83 OH_Drawing_ErrorCode OH_Drawing_RecordCmdDestroy(OH_Drawing_RecordCmd* recordCmd)
84 {
85     if (recordCmd == nullptr) {
86         return OH_DRAWING_ERROR_INVALID_PARAMETER;
87     }
88     delete reinterpret_cast<RecordCmd*>(recordCmd);
89     return OH_DRAWING_SUCCESS;
90 }