• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "command/rs_base_node_command.h"
21 #include "command/rs_display_node_command.h"
22 #include "command/rs_surface_node_command.h"
23 #include "common/rs_common_def.h"
24 #include "display_type.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 "draw/color.h"
38 
39 #include "image/bitmap.h"
40 #include "image/image.h"
41 #include "image/picture.h"
42 
43 #include "draw/brush.h"
44 #include "draw/canvas.h"
45 #include "draw/pen.h"
46 #include "draw/path.h"
47 #include "draw/clip.h"
48 #include "effect/path_effect.h"
49 #include "effect/shader_effect.h"
50 #include "utils/rect.h"
51 
52 #include "utils/matrix.h"
53 #include "utils/camera3d.h"
54 
55 #include "image_type.h"
56 #include "pixel_map.h"
57 
58 using namespace OHOS;
59 using namespace Media;
60 using namespace Rosen;
61 using namespace Drawing;
62 using namespace std;
63 
64 using TestFunc = std::function<void(Canvas&, uint32_t, uint32_t)>;
65 namespace {
66 #define LOG(fmt, ...) ::OHOS::HiviewDFX::HiLog::Info(             \
67     ::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "DrawingSampleRS"}, \
68     "%{public}s: " fmt, __func__, ##__VA_ARGS__)
69 }
70 
71 std::vector<TestFunc> testFuncVec;
72 constexpr static int32_t WIDTH = 720;
73 constexpr static int32_t HEIGHT = 1280;
74 
TestDrawPath(Canvas & canvas,uint32_t width,uint32_t height)75 void TestDrawPath(Canvas &canvas, uint32_t width, uint32_t height)
76 {
77     LOGI("+++++++ TestDrawPath");
78     Path path1;
79     // Add beginning of contour at point {200, 200}.
80     path1.MoveTo(200, 200);
81     // Add quad from last point towards (600, 200), to (600, 600).
82     path1.QuadTo(600, 200, 600, 600);
83     path1.Close();
84 
85     Pen pen;
86     pen.SetAntiAlias(true);
87     pen.SetColor(Drawing::Color::COLOR_GRAY);
88     pen.SetWidth(10); // The thickness of the pen is 10
89     canvas.AttachPen(pen);
90 
91     Brush brush;
92     brush.SetColor(Drawing::Color::COLOR_BLUE);
93     canvas.AttachBrush(brush);
94 
95     Path path2;
96     // Add oval to path, bounds of ellipse added is {200, 200, 600, 1000}.
97     path2.AddOval({200, 200, 600, 1000});
98 
99     Path dest;
100     dest.Op(path1, path2, PathOp::UNION);
101     canvas.DrawPath(dest);
102     LOGI("+++++++ TestDrawPath");
103 }
104 
TestDrawPathPro(Canvas & canvas,uint32_t width,uint32_t height)105 void TestDrawPathPro(Canvas &canvas, uint32_t width, uint32_t height)
106 {
107     LOGI("+++++++ TestDrawPathPro");
108     int len = 300;
109     Point a(500, 500); // point at {500, 500}
110 
111     Point c;
112     Point d;
113 
114     d.SetX(a.GetX() - len * std::sin(18.0f));
115     d.SetY(a.GetY() + len * std::cos(18.0f));
116 
117     c.SetX(a.GetX() + len * std::sin(18.0f));
118     c.SetY(d.GetY());
119 
120     Point b;
121     b.SetX(a.GetX() + (len / 2.0));
122     b.SetY(a.GetY() + std::sqrt((c.GetX() - d.GetX()) * (c.GetX() - d.GetX()) + (len / 2.0) * (len / 2.0)));
123 
124     Point e;
125     e.SetX(a.GetX() - (len / 2.0));
126     e.SetY(b.GetY());
127 
128     Path path;
129     path.MoveTo(a.GetX(), a.GetY());
130     path.LineTo(b.GetX(), b.GetY());
131     path.LineTo(c.GetX(), c.GetY());
132     path.LineTo(d.GetX(), d.GetY());
133     path.LineTo(e.GetX(), e.GetY());
134     path.Close();
135 
136     Pen pen;
137     pen.SetAntiAlias(true);
138     pen.SetColor(Drawing::Color::COLOR_RED);
139     pen.SetWidth(10); // The thickness of the pen is 10
140     canvas.AttachPen(pen);
141 
142     Brush brush;
143     brush.SetColor(Drawing::Color::COLOR_BLUE);
144     canvas.AttachBrush(brush);
145 
146     canvas.AttachPen(pen).AttachBrush(brush).DrawPath(path);
147     LOGI("+++++++ TestDrawPathPro");
148 }
149 
TestDrawBase(Canvas & canvas,uint32_t width,uint32_t height)150 void TestDrawBase(Canvas &canvas, uint32_t width, uint32_t height)
151 {
152     LOGI("+++++++ TestDrawBase");
153 
154     Pen pen;
155     pen.SetAntiAlias(true);
156     pen.SetColor(Drawing::Color::COLOR_RED);
157     canvas.AttachPen(pen);
158 
159     // Draw line segment from Point(0, 0) to Point(width, height)
160     canvas.DrawLine(Point(0, 0), Point(width, height));
161 
162     pen.Reset();
163     pen.SetColor(Drawing::Color::COLOR_RED);
164     pen.SetWidth(100); // The thickness of the pen is 100
165     Point point;
166     point.SetX(width / 2.0);
167     point.SetY(height / 2.0);
168     canvas.AttachPen(pen);
169     canvas.DrawPoint(point);
170 
171     Brush brush;
172     brush.SetColor(Drawing::Color::COLOR_BLUE);
173     brush.Reset();
174     canvas.AttachBrush(brush);
175     canvas.DrawRect({1200, 100, 1500, 700}); // rect is set to (fLeft, fTop, fRight, fBottom)
176     LOGI("------- TestDrawBase");
177 }
178 
TestDrawPathEffect(Canvas & canvas,uint32_t width,uint32_t height)179 void TestDrawPathEffect(Canvas &canvas, uint32_t width, uint32_t height)
180 {
181     LOGI("+++++++ TestDrawPathEffect");
182 
183     Pen pen;
184     pen.SetAntiAlias(true);
185     pen.SetColor(Drawing::Color::COLOR_RED);
186     pen.SetWidth(10); // The thickness of the pen is 10
187     scalar vals[] = {10.0, 20.0};
188     // Number of elements in the intervals array is 2; offset into the intervals array is 25.
189     pen.SetPathEffect(PathEffect::CreateDashPathEffect(vals, 2, 25));
190     canvas.AttachPen(pen);
191 
192     Brush brush;
193     brush.SetColor(Drawing::Color::COLOR_BLUE);
194     canvas.AttachBrush(brush);
195     canvas.DrawRect({1200, 300, 1500, 600}); // rect is set to (fLeft, fTop, fRight, fBottom)
196 
197     canvas.DetachPen();
198     canvas.DrawRect({1200, 700, 1500, 1000}); // rect is set to (fLeft, fTop, fRight, fBottom)
199     LOGI("------- TestDrawPathEffect");
200 }
201 
TestDrawFilter(Canvas & canvas,uint32_t width,uint32_t height)202 void TestDrawFilter(Canvas &canvas, uint32_t width, uint32_t height)
203 {
204     LOGI("+++++++ TestDrawFilter");
205 
206     Pen pen;
207     pen.SetAntiAlias(true);
208     pen.SetColor(Drawing::Color::COLOR_BLUE);
209     pen.SetColor(pen.GetColor4f(), Drawing::ColorSpace::CreateSRGBLinear());
210     pen.SetAlpha(0x44); // alpha value is 0x44
211     pen.SetWidth(10); // The thickness of the pen is 10
212     Filter filter;
213     filter.SetColorFilter(ColorFilter::CreateBlendModeColorFilter(Drawing::Color::COLOR_RED, BlendMode::SRC_ATOP));
214     // Sigma value of the Gaussian blur to apply is 10.
215     filter.SetMaskFilter(MaskFilter::CreateBlurMaskFilter(BlurType::NORMAL, 10));
216     pen.SetFilter(filter);
217     canvas.AttachPen(pen);
218 
219     Brush brush;
220     brush.SetColor(Drawing::Color::COLOR_BLUE);
221     brush.SetColor(brush.GetColor4f(), Drawing::ColorSpace::CreateSRGBLinear());
222     brush.SetAlpha(0x44); // alpha value is 0x44
223     brush.SetBlendMode(BlendMode::SRC_ATOP);
224     brush.SetFilter(filter);
225     canvas.AttachBrush(brush);
226     canvas.DrawRect({1200, 300, 1500, 600}); // rect is set to (fLeft, fTop, fRight, fBottom)
227 
228     canvas.DetachPen();
229     canvas.DrawRect({1200, 700, 1500, 1000}); // rect is set to (fLeft, fTop, fRight, fBottom)
230     LOGI("------- TestDrawFilter");
231 }
232 
TestDrawBitmap(Canvas & canvas,uint32_t width,uint32_t height)233 void TestDrawBitmap(Canvas &canvas, uint32_t width, uint32_t height)
234 {
235     LOGI("+++++++ TestDrawBitmap");
236     Bitmap bmp;
237     BitmapFormat format {COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE};
238     bmp.Build(200, 200, format); // bitmap width and height
239     bmp.ClearWithColor(Drawing::Color::COLOR_BLUE);
240 
241     Pen pen;
242     pen.SetAntiAlias(true);
243     pen.SetColor(Drawing::Color::COLOR_BLUE);
244     pen.SetWidth(10); // The thickness of the pen is 10
245     canvas.AttachPen(pen);
246     canvas.DrawBitmap(bmp, 500, 500);
247 
248     LOGI("------- TestDrawBitmap");
249 }
250 
TestDrawImage(Canvas & canvas,uint32_t width,uint32_t height)251 void TestDrawImage(Canvas& canvas, uint32_t width, uint32_t height)
252 {
253     LOGI("+++++++ TestDrawImage");
254     Bitmap bmp;
255     BitmapFormat format {COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE};
256     bmp.Build(300, 300, format); // bitmap width and height
257     bmp.ClearWithColor(Drawing::Color::COLOR_BLUE);
258 
259     Image image;
260     image.BuildFromBitmap(bmp);
261     int imageWidth = image.GetWidth();
262     int imageHeight = image.GetHeight();
263     LOGI("image width = %{public}d, image height = %{public}d", imageWidth, imageHeight);
264     Matrix matrix;
265     // Set matrix to rotate by degrees 45 about a pivot point at (0, 0).
266     matrix.Rotate(45, 0, 0);
267     SamplingOptions sampling = SamplingOptions(Drawing::FilterMode::NEAREST, Drawing::MipmapMode::NEAREST);
268     auto e = ShaderEffect::CreateImageShader(image, TileMode::REPEAT, TileMode::MIRROR, sampling, matrix);
269     LOGI("sampling useCubic = %{public}d, filter = %{public}d, mipmap = %{public}d",
270             sampling.GetUseCubic(), sampling.GetFilterMode(), sampling.GetMipmapMode());
271     auto c = Drawing::ColorSpace::CreateRefImage(image);
272 
273     Pen pen;
274     pen.SetAntiAlias(true);
275     pen.SetColor(Drawing::Color::COLOR_BLUE);
276     pen.SetColor(pen.GetColor4f(), c);
277     pen.SetWidth(10); // The thickness of the pen is 10
278     pen.SetShaderEffect(e);
279     canvas.AttachPen(pen);
280     canvas.DrawImage(image, 500, 500, sampling); // Draw image at (500,500)
281 
282     LOGI("------- TestDrawImage");
283 }
284 
TestDrawImageRect(Canvas & canvas,uint32_t width,uint32_t height)285 void TestDrawImageRect(Canvas& canvas, uint32_t width, uint32_t height)
286 {
287     LOGI("+++++++ TestDrawImageRect");
288     Bitmap bmp;
289     BitmapFormat format {COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE};
290     bmp.Build(300, 300, format); // bitmap width and height
291     bmp.ClearWithColor(Drawing::Color::COLOR_BLUE);
292 
293     Image image;
294     image.BuildFromBitmap(bmp);
295     Drawing::Rect r1(100, 100, 200, 200); // rect is set to (fLeft, fTop, fRight, fBottom)
296     Drawing::Rect r2(300, 300, 500, 500);
297     SamplingOptions sampling = SamplingOptions(Drawing::FilterMode::NEAREST, Drawing::MipmapMode::NEAREST);
298 
299     Brush brush;
300     brush.SetColor(Drawing::Color::COLOR_RED);
301     canvas.AttachBrush(brush);
302     canvas.DrawImageRect(image, r1, r2, sampling, SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT);
303 
304     LOGI("------- TestDrawImageRect");
305 }
306 
TestPicture(Canvas & canvas,uint32_t width,uint32_t height)307 void TestPicture(Canvas& canvas, uint32_t width, uint32_t height)
308 {
309     LOGI("+++++++ TestPicture");
310     Image image;
311     Picture picture;
312     Matrix matrix;
313     auto srgbColorSpace = Drawing::ColorSpace::CreateSRGB();
314 
315     Brush brush;
316     brush.SetColor(Drawing::Color::COLOR_BLUE);
317     // Created image width and height are set by {50, 50}
318     image.BuildFromPicture(picture, {50, 50}, matrix, brush, BitDepth::KU8, srgbColorSpace);
319 
320     Drawing::Rect rect(1000, 0, 1300, 300); // The tile rectangle size in picture coordinates.
321     auto e = ShaderEffect::CreatePictureShader(picture, TileMode::REPEAT, TileMode::MIRROR, FilterMode::NEAREST, matrix, rect);
322     Pen pen;
323     pen.SetAntiAlias(true);
324     pen.SetColor(Drawing::Color::COLOR_BLUE);
325     pen.SetWidth(10); // The thickness of the pen is 10
326     pen.SetShaderEffect(e);
327     canvas.AttachPen(pen);
328     canvas.DrawPicture(picture);
329 
330     LOGI("------- TestPicture");
331 }
332 
TestMatrix(Canvas & canvas,uint32_t width,uint32_t height)333 void TestMatrix(Canvas &canvas, uint32_t width, uint32_t height)
334 {
335     LOGI("+++++++ TestMatrix");
336     Brush brush;
337     brush.SetColor(Drawing::Color::COLOR_BLUE);
338     canvas.AttachBrush(brush);
339     canvas.DrawRect({1200, 100, 1500, 400}); // rect is set to (fLeft, fTop, fRight, fBottom)
340 
341     brush.SetColor(Drawing::Color::COLOR_RED);
342     canvas.AttachBrush(brush);
343     Matrix m;
344     // Set matrix to scale by 0.5 and 1.5, about a pivot point at (0, 0).
345     m.Scale(0.5, 1.5, 0, 0);
346 
347     canvas.SetMatrix(m);
348     canvas.DrawRect({1000, 0, 1300, 300}); // rect is set to (fLeft, fTop, fRight, fBottom)
349 
350     Matrix n;
351     /* Set matrix to:
352         | 1  0  100 |
353         | 0  1  300 |
354         | 0  0   1  |
355     */
356     n.SetMatrix(1, 0, 100,
357          0, 1, 300,
358          0, 0, 1);
359     brush.SetColor(Drawing::Color::COLOR_GREEN);
360     canvas.AttachBrush(brush);
361     canvas.SetMatrix(n);
362     canvas.DrawRect({1200, 100, 1500, 400}); // rect is set to (fLeft, fTop, fRight, fBottom)
363 
364     Matrix m1;
365     Matrix m2;
366     m1.Translate(100, 300); // Set matrix to translate by (100, 300).
367     // Set matrix to rotate by degrees 45 about a pivot point at (0, 0).
368     m2.Rotate(45, 0, 0);
369     m = m1 * m2;
370 
371     brush.SetColor(Drawing::Color::COLOR_BLACK);
372     canvas.AttachBrush(brush);
373     canvas.SetMatrix(m);
374     canvas.DrawRect({1200, 100, 1500, 400}); // rect is set to (fLeft, fTop, fRight, fBottom)
375 
376     Matrix matrix;
377     matrix.Translate(100, 100); // 100 means offset size
378     std::vector<Point> point = {{0, 0}}; // {0, 0} means point position
379     matrix.MapPoints(point, point, point.size());
380     for (size_t i = 0; i < point.size(); i++) {
381         LOGI("mapped point fx = %{public}f, fy = %{public}f", point[i].GetX(), point[i].GetY());
382     }
383 
384     LOGI("------- TestMatrix");
385 }
386 
TestCamera(Canvas & canvas,uint32_t width,uint32_t height)387 void TestCamera(Canvas &canvas, uint32_t width, uint32_t height)
388 {
389     LOGI("+++++++ TestCamera");
390 
391     Brush brush;
392     Matrix m0;
393     m0.Translate(width / 2.0, height / 2.0); // Set matrix m0 to translate by (width / 2.0, height / 2.0).
394     canvas.SetMatrix(m0);
395 
396     Camera3D camera;
397     camera.RotateXDegrees(-25); // Set camera to rotate by degrees -25 at x-aixs.
398     camera.RotateYDegrees(45); // Set camera to rotate by degrees 45 at y-aixs.
399     camera.Translate(-50, 50, 50); // Set camera to translate by (-50, 50, 50).
400     Drawing::Rect r(0, 0, 500, 500); // rect is set to (fLeft, fTop, fRight, fBottom)
401 
402     canvas.Save();
403     camera.Save();
404     camera.RotateYDegrees(90); // Set camera to rotate by degrees 90 at y-aixs.
405     Matrix m1;
406     camera.ApplyToMatrix(m1);
407     camera.Restore();
408     canvas.ConcatMatrix(m1);
409     brush.SetColor(Drawing::Color::COLOR_LTGRAY);
410     canvas.AttachBrush(brush);
411     canvas.DrawRect(r);
412     canvas.Restore();
413     LOGI("------- TestCamera");
414 }
415 
TestDrawShader(Canvas & canvas,uint32_t width,uint32_t height)416 void TestDrawShader(Canvas &canvas, uint32_t width, uint32_t height)
417 {
418     LOGI("+++++++ TestDrawShader");
419 
420     Brush brush;
421     brush.SetColor(Drawing::Color::COLOR_BLUE);
422     std::vector<ColorQuad> colors{Drawing::Color::COLOR_GREEN, Drawing::Color::COLOR_BLUE, Drawing::Color::COLOR_RED};
423     std::vector<scalar> pos{0.0, 0.5, 1.0};
424     // The start and end points for the gradient: {1200, 700}, {1300, 800}.
425     auto e = ShaderEffect::CreateLinearGradient({1200, 700}, {1300, 800}, colors, pos, TileMode::MIRROR);
426     brush.SetShaderEffect(e);
427 
428     canvas.AttachBrush(brush);
429     canvas.DrawRect({1200, 700, 1500, 1000}); // rect is set to (fLeft, fTop, fRight, fBottom)
430     LOGI("------- TestDrawShader");
431 }
432 
TestDrawShadow(Canvas & canvas,uint32_t width,uint32_t height)433 void TestDrawShadow(Canvas &canvas, uint32_t width, uint32_t height)
434 {
435     LOGI("+++++++ TestDrawShadow");
436 
437     Path path;
438     // Add oval to path, bounds of ellipse added is {200, 200, 600, 1000}.
439     path.AddOval({200, 200, 600, 1000});
440     Point3 planeParams = { 540.0, 0.0, 600.0 };
441     Point3 devLightPos = {0, 0, 0};
442     scalar lightRadius = 0.5;
443     Drawing::Color ambientColor = Drawing::Color::ColorQuadSetARGB(0, 0, 0, 0);
444     Drawing::Color spotColor = Drawing::Color::COLOR_RED;
445     ShadowFlags flag = ShadowFlags::TRANSPARENT_OCCLUDER;
446     canvas.DrawShadow(path, planeParams, devLightPos, lightRadius, ambientColor, spotColor, flag);
447     LOGI("------- TestDrawShadow");
448 }
449 
ConstructPixmap()450 std::unique_ptr<PixelMap> ConstructPixmap()
451 {
452     uint32_t pixelMapWidth = 50;
453     uint32_t pixelMapHeight = 50;
454     std::unique_ptr<PixelMap> pixelMap = std::make_unique<PixelMap>();
455     ImageInfo info;
456     info.size.width = pixelMapWidth;
457     info.size.height = pixelMapHeight;
458     info.pixelFormat = Media::PixelFormat::RGBA_8888;
459     info.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
460     info.colorSpace = Media::ColorSpace::SRGB;
461     pixelMap->SetImageInfo(info);
462     LOGI("Constructed pixelMap info: width = %{public}d, height = %{public}d, pixelformat = %{public}d, alphatype = %{public}d, colorspace = %{public}d",
463         info.size.width, info.size.height, info.pixelFormat, info.alphaType, info.colorSpace);
464 
465     uint32_t rowDataSize = pixelMapWidth;
466     uint32_t bufferSize = rowDataSize * pixelMapHeight;
467     void *buffer = malloc(bufferSize);
468     if (buffer == nullptr) {
469         LOGE("alloc memory size:[%{public}d] error.", bufferSize);
470         return nullptr;
471     }
472     char *ch = static_cast<char *>(buffer);
473     for (unsigned int i = 0; i < bufferSize; i++) {
474         *(ch++) = static_cast<char>(i);
475     }
476 
477     pixelMap->SetPixelsAddr(buffer, nullptr, bufferSize, AllocatorType::HEAP_ALLOC, nullptr);
478 
479     return pixelMap;
480 }
481 
TestDrawPixelmap(Canvas & canvas,uint32_t width,uint32_t height)482 void TestDrawPixelmap(Canvas &canvas, uint32_t width, uint32_t height)
483 {
484     LOGI("+++++++ TestDrawPixelmap");
485     std::unique_ptr<PixelMap> pixelmap = ConstructPixmap();
486 
487     Pen pen;
488     pen.SetAntiAlias(true);
489     pen.SetColor(Drawing::Color::COLOR_BLUE);
490     pen.SetWidth(10); // The thickness of the pen is 10
491     canvas.AttachPen(pen);
492 
493     Brush brush;
494     brush.SetColor(Drawing::Color::COLOR_RED);
495     canvas.AttachBrush(brush);
496 
497     canvas.DrawBitmap(*pixelmap.get(), 500, 500);
498     LOGI("------- TestDrawPixelmap");
499 }
500 
DoDraw(uint8_t * addr,uint32_t width,uint32_t height,size_t index)501 void DoDraw(uint8_t *addr, uint32_t width, uint32_t height, size_t index)
502 {
503     Bitmap bitmap;
504     BitmapFormat format {COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE};
505     bitmap.Build(width, height, format);
506 
507     Canvas canvas;
508     canvas.Bind(bitmap);
509     canvas.Clear(Drawing::Color::COLOR_WHITE);
510 
511     testFuncVec[index](canvas, width, height);
512 
513     constexpr uint32_t stride = 4;
514     uint32_t addrSize = width * height * stride;
515     auto ret = memcpy_s(addr, addrSize, bitmap.GetPixels(), addrSize);
516     if (ret != EOK) {
517         LOGI("memcpy_s failed");
518     }
519 }
520 
DrawSurface(std::shared_ptr<RSSurfaceNode> surfaceNode,int32_t width,int32_t height,size_t index)521 void DrawSurface(std::shared_ptr<RSSurfaceNode> surfaceNode, int32_t width, int32_t height, size_t index)
522 {
523     sptr<Surface> surface = surfaceNode->GetSurface();
524     if (surface == nullptr) {
525         return;
526     }
527 
528     sptr<SurfaceBuffer> buffer;
529     int32_t releaseFence;
530     BufferRequestConfig config = {
531         .width = width,
532         .height = height,
533         .strideAlignment = 0x8,
534         .format = PIXEL_FMT_RGBA_8888,
535         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
536     };
537 
538     SurfaceError ret = surface->RequestBuffer(buffer, releaseFence, config);
539     LOGI("request buffer ret is: %{public}s", SurfaceErrorStr(ret).c_str());
540 
541     if (buffer == nullptr) {
542         LOGE("request buffer failed: buffer is nullptr");
543         return;
544     }
545     if (buffer->GetVirAddr() == nullptr) {
546         LOGE("get virAddr failed: virAddr is nullptr");
547         return;
548     }
549 
550     auto addr = static_cast<uint8_t *>(buffer->GetVirAddr());
551     LOGI("buffer width:%{public}d, height:%{public}d", buffer->GetWidth(), buffer->GetHeight());
552     DoDraw(addr, buffer->GetWidth(), buffer->GetHeight(), index);
553 
554     BufferFlushConfig flushConfig = {
555         .damage = {
556             .w = buffer->GetWidth(),
557             .h = buffer->GetHeight(),
558         },
559     };
560     ret = surface->FlushBuffer(buffer, -1, flushConfig);
561     LOGI("flushBuffer ret is: %{public}s", SurfaceErrorStr(ret).c_str());
562 }
563 
CreateSurface()564 std::shared_ptr<RSSurfaceNode> CreateSurface()
565 {
566     RSSurfaceNodeConfig config;
567     return RSSurfaceNode::Create(config);
568 }
569 
main()570 int main()
571 {
572     testFuncVec.push_back(TestDrawPathPro);
573     testFuncVec.push_back(TestCamera);
574     testFuncVec.push_back(TestDrawBase);
575     testFuncVec.push_back(TestDrawPath);
576     testFuncVec.push_back(TestDrawPathEffect);
577     testFuncVec.push_back(TestDrawBitmap);
578     testFuncVec.push_back(TestDrawImage);
579     testFuncVec.push_back(TestDrawImageRect);
580     testFuncVec.push_back(TestPicture);
581     testFuncVec.push_back(TestDrawFilter);
582     testFuncVec.push_back(TestDrawShader);
583     testFuncVec.push_back(TestDrawShadow);
584     testFuncVec.push_back(TestDrawPixelmap);
585     testFuncVec.push_back(TestMatrix);
586 
587     auto surfaceNode = CreateSurface();
588     RSDisplayNodeConfig config;
589     RSDisplayNode::SharedPtr displayNode = RSDisplayNode::Create(config);
590     for (size_t i = 0; i < testFuncVec.size(); i++) {
591         auto transactionProxy = RSTransactionProxy::GetInstance();
592         if (transactionProxy == nullptr) {
593             continue;
594         }
595         sleep(2); // wait 2s
596         displayNode->AddChild(surfaceNode, -1);
597         surfaceNode->SetBounds(0, 0, WIDTH, HEIGHT);
598         transactionProxy->FlushImplicitTransaction();
599         DrawSurface(surfaceNode, WIDTH, HEIGHT, i);
600         sleep(4); // wait 4s
601         displayNode->RemoveChild(surfaceNode);
602         transactionProxy->FlushImplicitTransaction();
603     }
604     return 0;
605 }
606