• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #ifndef OHOS_RENDER_3D_TEXTURE_LAYER_H
16 #define OHOS_RENDER_3D_TEXTURE_LAYER_H
17 
18 #include <cstdint>
19 #include <future>
20 #include <memory>
21 #include <shared_mutex>
22 
23 #include <EGL/egl.h>
24 #include <EGL/eglext.h>
25 #include <GLES2/gl2.h>
26 #include <GLES2/gl2ext.h>
27 
28 #include <include/core/SkCanvas.h>
29 #include <include/core/SkDrawable.h>
30 #include <include/core/SkImage.h>
31 
32 #include <render_service_client/core/ui/rs_node.h>
33 #include <surface.h>
34 #include <surface_buffer.h>
35 
36 #include "data_type/constants.h"
37 #include "graphics_manager.h"
38 #include "texture_info.h"
39 
40 struct OH_NativeBuffer;
41 struct NativeWindowBuffer;
42 
43 namespace OHOS::Render3D {
44 
45 struct TextureImage {
TextureImageTextureImage46     explicit TextureImage(const TextureInfo& textureInfo) : textureInfo_(textureInfo) {}
47     TextureImage() = default;
48     TextureInfo textureInfo_;
49     std::function<void()> task_ = nullptr;
50     std::shared_future<void> ftr_;
51     std::promise<void> pms_;
52     sk_sp<SkImage> skImage_;
53 };
54 
55 struct WindowChangeInfo {
56     float offsetX = 0.0;
57     float offsetY = 0.0;
58     float width = 0.0;
59     float height = 0.0;
60     float scale = 1.0;
61     float widthScale = 1.0;
62     float heightScale = 1.0;
63     bool recreateWindow = true;
64     SurfaceType surfaceType = SurfaceType::SURFACE_TEXTURE;
65 };
66 
67 class __attribute__((visibility("default"))) TextureLayer : public SkDrawable {
68 public:
69     explicit TextureLayer(int32_t key);
70     virtual ~TextureLayer();
71 
72     void DestroyRenderTarget();
73     virtual SkRect onGetBounds() override;
74     void OnDraw(SkCanvas* canvas);
75     virtual void onDraw(SkCanvas* canvas) override;
76     TextureInfo GetTextureInfo();
77 
78     void SetParent(std::shared_ptr<Rosen::RSNode>& parent);
79     TextureInfo OnWindowChange(float offsetX, float offsetY, float width, float height, float scale,
80         bool recreateWindow, SurfaceType surfaceType = SurfaceType::SURFACE_WINDOW);
81     TextureInfo OnWindowChange(const WindowChangeInfo& windowChangeInfo);
82 
83 private:
84     void DrawTexture(SkCanvas* canvas);
85     void* CreateNativeWindow(uint32_t width, uint32_t height);
86 #if defined(UNIFY_RENDER) && (UNIFY_RENDER == 1)
87     void FreeNativeBuffer();
88     void FreeNativeWindowBuffer();
89     void DrawTextureUnifyRender(SkCanvas* canvas);
90 #endif
91     void AllocGLTexture(uint32_t width, uint32_t height);
92     void AllocEglImage(uint32_t width, uint32_t height);
93     void DestroyProducerSurface();
94     void DestroyNativeWindow();
95     void ConfigWindow(float offsetX, float offsetY, float width, float height, float scale, bool recreateWindow);
96     void ConfigTexture(float width, float height);
97     void RemoveChild();
98     // deprecated
99     void UpdateRenderFinishFuture(std::shared_future<void> &ftr);
100 
101 #if defined(DBG_DRAW_PIXEL) && (DBG_DRAW_PIXEL == 1)
102     auto MakePixelImage();
103     void ReadPixel();
104     void CreateReadFbo();
105 
106     GLuint fbo_ = 0u;
107     uint8_t *data_ = nullptr;
108 #endif
109 
110     TextureImage image_;
111     int32_t offsetX_ = 0u;
112     int32_t offsetY_ = 0u;
113     uint32_t width_ = 0u;
114     uint32_t height_ = 0u;
115     int32_t key_ = INT32_MAX;
116     bool needsRecreateSkImage_ = false;
117     std::mutex ftrMut_;
118     std::mutex skImageMut_;
119 
120     std::shared_ptr<Rosen::RSNode> rsNode_ = nullptr;
121     std::shared_ptr<Rosen::RSNode> parent_ = nullptr;
122     sptr<OHOS::Surface> producerSurface_ = nullptr;
123     RenderBackend backend_ = RenderBackend::UNDEFINE;
124     SurfaceType surface_ = SurfaceType::UNDEFINE;
125 
126 #if defined(UNIFY_RENDER) && (UNIFY_RENDER == 1)
127     // for unify rendering
128     sptr<SurfaceBuffer> surfaceBuffer_;
129     EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR;
130     OH_NativeBuffer *nativeBuffer_ = nullptr;
131     NativeWindowBuffer *nativeWindowBuffer_ = nullptr;
132 #endif
133 };
134 } // namespace OHOS::Render3D
135 #endif // OHOS_RENDER_3D_TEXTURE_LAYER_H
136