1 /*
2  * Copyright (c) 2022 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 #ifndef SURFACE_DRAW_H
17 #define SURFACE_DRAW_H
18 
19 #include <ui/rs_surface_node.h>
20 #include "pixel_map.h"
21 
22 #include "refbase.h"
23 #include "wm_common.h"
24 #include "draw/canvas.h"
25 #include "nocopyable.h"
26 #include "pixel_map.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 class SurfaceDraw {
31 public:
32     SurfaceDraw() = default;
33     ~SurfaceDraw() = default;
34     static bool DrawColor(std::shared_ptr<RSSurfaceNode> surfaceNode, int32_t bufferWidth,
35         int32_t bufferHeight, uint32_t color);
36     static bool DrawImage(std::shared_ptr<RSSurfaceNode> surfaceNode, int32_t bufferWidth,
37         int32_t bufferHeight, const std::string& imagePath);
38     static bool DrawImage(std::shared_ptr<RSSurfaceNode> surfaceNode, int32_t bufferWidth,
39         int32_t bufferHeight, std::shared_ptr<Media::PixelMap> pixelMap);
40     static bool DrawImageRect(std::shared_ptr<RSSurfaceNode> surfaceNode, Rect winRect,
41         std::shared_ptr<Media::PixelMap> pixelMap, uint32_t bkgColor, bool fillWindow = false);
42     static bool GetSurfaceSnapshot(const std::shared_ptr<RSSurfaceNode> surfaceNode,
43         std::shared_ptr<Media::PixelMap>& pixelMap, int32_t timeoutMs, float scaleW = 0.5, float scaleH = 0.5);
44     static bool DrawMasking(std::shared_ptr<RSSurfaceNode> surfaceNode, Rect screenRect,
45         Rect transparentRect);
46 
47 private:
48     static bool DoDraw(uint8_t* addr, uint32_t width, uint32_t height, const std::string& imagePath);
49     static bool DoDraw(uint8_t* addr, uint32_t width, uint32_t height, uint32_t color);
50     static bool DoDraw(uint8_t* addr, uint32_t width, uint32_t height, std::shared_ptr<Media::PixelMap> pixelMap);
51     static sptr<OHOS::Surface> GetLayer(std::shared_ptr<RSSurfaceNode> surfaceNode);
52     static sptr<OHOS::SurfaceBuffer> GetSurfaceBuffer(sptr<OHOS::Surface> layer, int32_t bufferWidth,
53         int32_t bufferHeight);
54     static void DrawPixelmap(Drawing::Canvas& canvas, const std::string& imagePath);
55     static std::unique_ptr<OHOS::Media::PixelMap> DecodeImageToPixelMap(const std::string& imagePath);
56     static bool DoDrawImageRect(sptr<OHOS::SurfaceBuffer> buffer, const Rect& rect,
57         std::shared_ptr<Media::PixelMap> pixelMap, uint32_t color, bool fillWindow = false);
58 };
59 } // Rosen
60 } // OHOS
61 #endif  // SURFACE_DRAW_H