• 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 "c/drawing_canvas.h"
17 #include "c/drawing_color.h"
18 #include "c/drawing_bitmap.h"
19 #include "c/drawing_brush.h"
20 #include "c/drawing_font_collection.h"
21 #include "c/drawing_path.h"
22 #include "c/drawing_pen.h"
23 #include "c/drawing_text_declaration.h"
24 #include "c/drawing_types.h"
25 #include "c/drawing_text_typography.h"
26 
27 #include <cinttypes>
28 #include <cstdio>
29 #include <cstdlib>
30 #include <fstream>
31 #include <iostream>
32 #include <memory>
33 #include <securec.h>
34 #include <sstream>
35 #include <surface.h>
36 #include <unistd.h>
37 
38 #include "command/rs_base_node_command.h"
39 #include "common/rs_common_def.h"
40 #include "command/rs_display_node_command.h"
41 #include "command/rs_surface_node_command.h"
42 #include "core/transaction/rs_interfaces.h"
43 #include "core/ui/rs_display_node.h"
44 #include "core/ui/rs_surface_node.h"
45 #include "display_type.h"
46 #include "render_service_base/src/platform/ohos/rs_surface_frame_ohos.h"
47 #include "render_service_base/src/platform/ohos/rs_surface_ohos.h"
48 #include "pipeline/rs_render_result.h"
49 #include "pipeline/rs_render_thread.h"
50 #include "ui/rs_node.h"
51 #include "ui/rs_surface_extractor.h"
52 #include "ui/rs_ui_director.h"
53 #include "utils/log.h"
54 
55 using namespace OHOS;
56 using namespace Media;
57 using namespace Rosen;
58 using namespace std;
59 
60 constexpr static int32_t WIDTH = 1720;
61 constexpr static int32_t HEIGHT = 2280;
62 
DoDraw(uint8_t * addr,uint32_t width,uint32_t height,size_t index)63 void DoDraw(uint8_t *addr, uint32_t width, uint32_t height, size_t index)
64 {
65     OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate();
66     OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
67     OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
68 
69     OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate();
70     OH_Drawing_CanvasBind(cCanvas, cBitmap);
71     OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
72 
73     OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
74     OH_Drawing_SetTypographyTextDirection(typoStyle, TEXT_DIRECTION_LTR);
75     OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_LEFT);
76 
77     OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle,
78         OH_Drawing_CreateFontCollection());
79 
80     OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
81     OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
82     double fontSize = 30;
83     OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize);
84     OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
85     OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_ALPHABETIC);
86     OH_Drawing_SetTextStyleFontHeight(txtStyle, 1);
87     const char* fontFamilies[] = {"Roboto"};
88     OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies);
89     OH_Drawing_SetTextStyleFontStyle(txtStyle, FONT_STYLE_NORMAL);
90     OH_Drawing_SetTextStyleLocale(txtStyle, "en");
91 
92     OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
93 
94     const char* text = "OpenHarmony\n";
95     OH_Drawing_TypographyHandlerAddText(handler, text);
96     OH_Drawing_TypographyHandlerPopTextStyle(handler);
97 
98     OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler);
99     double maxWidth = 800.0;
100     OH_Drawing_TypographyLayout(typography, maxWidth);
101     double position[2] = {10.0, 15.0};
102     OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]);
103 
104     constexpr uint32_t stride = 4;
105     int32_t addrSize = width * height * stride;
106     void* bitmapAddr = OH_Drawing_BitmapGetPixels(cBitmap);
107     auto ret = memcpy_s(addr, addrSize, bitmapAddr, addrSize);
108     if (ret != EOK) {
109         LOGI("memcpy_s failed");
110     }
111     OH_Drawing_CanvasDestroy(cCanvas);
112     OH_Drawing_BitmapDestroy(cBitmap);
113     OH_Drawing_DestroyTypographyStyle(typoStyle);
114     OH_Drawing_DestroyTextStyle(txtStyle);
115     OH_Drawing_DestroyTypographyHandler(handler);
116     OH_Drawing_DestroyTypography(typography);
117 }
118 
DrawSurface(std::shared_ptr<RSSurfaceNode> surfaceNode,int32_t width,int32_t height,size_t index)119 void DrawSurface(std::shared_ptr<RSSurfaceNode> surfaceNode, int32_t width, int32_t height, size_t index)
120 {
121     sptr<Surface> surface = surfaceNode->GetSurface();
122     if (surface == nullptr) {
123         return;
124     }
125 
126     sptr<SurfaceBuffer> buffer;
127     int32_t releaseFence;
128     BufferRequestConfig config = {
129         .width = width,
130         .height = height,
131         .strideAlignment = 0x8,
132         .format = PIXEL_FMT_RGBA_8888,
133         .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
134     };
135 
136     SurfaceError ret = surface->RequestBuffer(buffer, releaseFence, config);
137     LOGI("request buffer ret is: %{public}s", SurfaceErrorStr(ret).c_str());
138 
139     if (buffer == nullptr) {
140         LOGE("request buffer failed: buffer is nullptr");
141         return;
142     }
143     if (buffer->GetVirAddr() == nullptr) {
144         LOGE("get virAddr failed: virAddr is nullptr");
145         return;
146     }
147 
148     auto addr = static_cast<uint8_t *>(buffer->GetVirAddr());
149     LOGI("buffer width:%{public}d, height:%{public}d", buffer->GetWidth(), buffer->GetHeight());
150     DoDraw(addr, buffer->GetWidth(), buffer->GetHeight(), index);
151 
152     BufferFlushConfig flushConfig = {
153         .damage = {
154             .w = buffer->GetWidth(),
155             .h = buffer->GetHeight(),
156         },
157     };
158     ret = surface->FlushBuffer(buffer, -1, flushConfig);
159     LOGI("flushBuffer ret is: %{public}s", SurfaceErrorStr(ret).c_str());
160 }
161 
CreateSurface()162 std::shared_ptr<RSSurfaceNode> CreateSurface()
163 {
164     RSSurfaceNodeConfig config;
165     return RSSurfaceNode::Create(config);
166 }
167 
main()168 int main()
169 {
170     auto surfaceNode = CreateSurface();
171     RSDisplayNodeConfig config;
172     RSDisplayNode::SharedPtr displayNode = RSDisplayNode::Create(config);
173     int testNumber = 5;
174     for (size_t i = 0; i < testNumber; i++) {
175         int restTime = 4;
176         sleep(restTime);
177         displayNode->AddChild(surfaceNode, -1);
178         surfaceNode->SetBounds(0, 0, WIDTH, HEIGHT);
179         RSTransactionProxy::GetInstance()->FlushImplicitTransaction();
180         DrawSurface(surfaceNode, WIDTH, HEIGHT, i);
181         sleep(restTime);
182         displayNode->RemoveChild(surfaceNode);
183         RSTransactionProxy::GetInstance()->FlushImplicitTransaction();
184     }
185     return 0;
186 }
187