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 #include <iostream>
17 #include <surface.h>
18 #include <cmath>
19 #include <securec.h>
20
21 #include "command/rs_base_node_command.h"
22 #include "command/rs_display_node_command.h"
23 #include "command/rs_surface_node_command.h"
24 #include "common/rs_common_def.h"
25 #include "pipeline/rs_render_result.h"
26 #include "pipeline/rs_render_thread.h"
27 #include "ui/rs_node.h"
28 #include "ui/rs_surface_extractor.h"
29 #include "ui/rs_ui_director.h"
30 #include "core/transaction/rs_interfaces.h"
31 #include "core/ui/rs_display_node.h"
32 #include "core/ui/rs_surface_node.h"
33 // temporary debug
34 #include "foundation/graphic/graphic_2d/rosen/modules/render_service_base/src/platform/ohos/rs_surface_frame_ohos.h"
35 #include "foundation/graphic/graphic_2d/rosen/modules/render_service_base/src/platform/ohos/rs_surface_ohos.h"
36
37 #include "c/drawing_canvas.h"
38 #include "c/drawing_color.h"
39 #include "c/drawing_bitmap.h"
40 #include "c/drawing_path.h"
41 #include "c/drawing_pen.h"
42 #include "c/drawing_brush.h"
43 #include "c/drawing_types.h"
44
45 #include "utils/log.h"
46
47 using namespace OHOS;
48 using namespace Media;
49 using namespace Rosen;
50 using namespace std;
51
52 using TestFunc = std::function<void(OH_Drawing_Canvas*, uint32_t, uint32_t)>;
53 static std::vector<TestFunc> testFuncVec;
54 constexpr static int32_t WIDTH = 720;
55 constexpr static int32_t HEIGHT = 1280;
56
TestDrawPathPro(OH_Drawing_Canvas * cCanvas,uint32_t width,uint32_t height)57 static void TestDrawPathPro(OH_Drawing_Canvas* cCanvas, uint32_t width, uint32_t height)
58 {
59 LOGI("+++++++ TestDrawPathPro");
60 // 300 means length of pentagram border
61 int len = 300;
62
63 // point a position
64 float aX = 500;
65 float aY = 500;
66
67 // point d position, 18 means degree of pentagram
68 float dX = aX - len * std::sin(18.0f);
69 float dY = aY + len * std::cos(18.0f);
70
71 // point c position, 18 means degree of pentagram
72 float cX = aX + len * std::sin(18.0f);
73 float cY = dY;
74
75 // point b position
76 float bX = aX + (len / 2.0);
77 float bY = aY + std::sqrt((cX - dX) * (cX - dX) + (len / 2.0) * (len / 2.0));
78
79 // point e position
80 float eX = aX - (len / 2.0);
81 float eY = bY;
82
83 OH_Drawing_Path* cPath = OH_Drawing_PathCreate();
84 OH_Drawing_PathMoveTo(cPath, aX, aY);
85 OH_Drawing_PathLineTo(cPath, bX, bY);
86 OH_Drawing_PathLineTo(cPath, cX, cY);
87 OH_Drawing_PathLineTo(cPath, dX, dY);
88 OH_Drawing_PathLineTo(cPath, eX, eY);
89 OH_Drawing_PathClose(cPath);
90
91 OH_Drawing_Pen* cPen = OH_Drawing_PenCreate();
92 OH_Drawing_PenSetAntiAlias(cPen, true);
93 OH_Drawing_PenSetColor(cPen, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
94 // 10.0 means pentagram border line width
95 OH_Drawing_PenSetWidth(cPen, 10.0);
96 OH_Drawing_PenSetJoin(cPen, LINE_ROUND_JOIN);
97 OH_Drawing_CanvasAttachPen(cCanvas, cPen);
98
99 OH_Drawing_Brush* cBrush = OH_Drawing_BrushCreate();
100 OH_Drawing_BrushSetColor(cBrush, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0x00));
101 OH_Drawing_CanvasAttachBrush(cCanvas, cBrush);
102
103 OH_Drawing_CanvasDrawPath(cCanvas, cPath);
104
105 // (500, 500) means start position of line
106 float lineStartX = 500;
107 float lineStartY = 500;
108
109 // (500, 1000) means end position of line
110 float lineEndX = 500;
111 float lineEndY = 1000;
112
113 // 20.0 means line width
114 OH_Drawing_PenSetWidth(cPen, 20.0);
115 OH_Drawing_PenSetCap(cPen, LINE_ROUND_CAP);
116 OH_Drawing_CanvasAttachPen(cCanvas, cPen);
117 OH_Drawing_CanvasDetachBrush(cCanvas);
118 OH_Drawing_CanvasDrawLine(cCanvas, lineStartX, lineStartY, lineEndX, lineEndY);
119
120 OH_Drawing_BrushDestroy(cBrush);
121 OH_Drawing_PenDestroy(cPen);
122 OH_Drawing_PathDestroy(cPath);
123 LOGI("+++++++ TestDrawPathPro");
124 }
125
DoDraw(uint8_t * addr,uint32_t width,uint32_t height,size_t index)126 static void DoDraw(uint8_t *addr, uint32_t width, uint32_t height, size_t index)
127 {
128 OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate();
129 OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
130 OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
131
132 OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate();
133 OH_Drawing_CanvasBind(cCanvas, cBitmap);
134
135 OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
136
137 testFuncVec[index](cCanvas, width, height);
138
139 // 4 means stride
140 constexpr uint32_t stride = 4;
141 uint32_t addrSize = width * height * stride;
142 void* bitmapAddr = OH_Drawing_BitmapGetPixels(cBitmap);
143 auto ret = memcpy_s(addr, addrSize, bitmapAddr, addrSize);
144 if (ret != EOK) {
145 LOGI("memcpy_s failed");
146 }
147 OH_Drawing_CanvasDestroy(cCanvas);
148 OH_Drawing_BitmapDestroy(cBitmap);
149 }
150
DrawSurface(std::shared_ptr<RSSurfaceNode> surfaceNode,int32_t width,int32_t height,size_t index)151 static void DrawSurface(std::shared_ptr<RSSurfaceNode> surfaceNode, int32_t width, int32_t height, size_t index)
152 {
153 sptr<Surface> surface = surfaceNode->GetSurface();
154 if (surface == nullptr) {
155 return;
156 }
157
158 sptr<SurfaceBuffer> buffer;
159 int32_t releaseFence;
160 BufferRequestConfig config = {
161 .width = width,
162 .height = height,
163 .strideAlignment = 0x8,
164 .format = PIXEL_FMT_RGBA_8888,
165 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
166 };
167
168 SurfaceError ret = surface->RequestBuffer(buffer, releaseFence, config);
169 LOGI("request buffer ret is: %{public}s", SurfaceErrorStr(ret).c_str());
170
171 if (buffer == nullptr) {
172 LOGE("request buffer failed: buffer is nullptr");
173 return;
174 }
175 if (buffer->GetVirAddr() == nullptr) {
176 LOGE("get virAddr failed: virAddr is nullptr");
177 return;
178 }
179
180 auto addr = static_cast<uint8_t *>(buffer->GetVirAddr());
181 LOGI("buffer width:%{public}d, height:%{public}d", buffer->GetWidth(), buffer->GetHeight());
182 DoDraw(addr, buffer->GetWidth(), buffer->GetHeight(), index);
183
184 BufferFlushConfig flushConfig = {
185 .damage = {
186 .w = buffer->GetWidth(),
187 .h = buffer->GetHeight(),
188 },
189 };
190 ret = surface->FlushBuffer(buffer, -1, flushConfig);
191 LOGI("flushBuffer ret is: %{public}s", SurfaceErrorStr(ret).c_str());
192 }
193
CreateSurface()194 static std::shared_ptr<RSSurfaceNode> CreateSurface()
195 {
196 RSSurfaceNodeConfig config;
197 return RSSurfaceNode::Create(config);
198 }
199
main()200 int main()
201 {
202 testFuncVec.push_back(TestDrawPathPro);
203 auto surfaceNode = CreateSurface();
204 RSDisplayNodeConfig config;
205 RSDisplayNode::SharedPtr displayNode = RSDisplayNode::Create(config);
206 for (size_t i = 0; i < testFuncVec.size(); i++) {
207 auto transactionProxy = RSTransactionProxy::GetInstance();
208 if (transactionProxy == nullptr) {
209 continue;
210 }
211 // sleep 2s
212 sleep(2);
213 displayNode->AddChild(surfaceNode, -1);
214 surfaceNode->SetBounds(0, 0, WIDTH, HEIGHT);
215 transactionProxy->FlushImplicitTransaction();
216 DrawSurface(surfaceNode, WIDTH, HEIGHT, i);
217 // sleep 8s
218 sleep(8);
219 displayNode->RemoveChild(surfaceNode);
220 transactionProxy->FlushImplicitTransaction();
221 }
222 return 0;
223 }
224