• 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 
16 #ifndef RS_UPLOAD_TEXTURE_THREAD_H
17 #define RS_UPLOAD_TEXTURE_THREAD_H
18 
19 #include <condition_variable>
20 #include <vector>
21 #include <mutex>
22 
23 #include "event_handler.h"
24 #include "common/rs_macros.h"
25 #include <include/gpu/GrDirectContext.h>
26 #include "src/gpu/GrSurfaceProxy.h"
27 #ifdef RS_ENABLE_GL
28 #include "EGL/egl.h"
29 #include "EGL/eglext.h"
30 #include "include/core/SkSurface.h"
31 #endif
32 #ifndef USE_ROSEN_DRAWING
33 #include "include/core/SkCanvas.h"
34 #else
35 #include "draw/canvas.h"
36 #endif
37 #include "draw/surface.h"
38 #include "draw/canvas.h"
39 #include "image/gpu_context.h"
40 #include "image/image.h"
41 #include "platform/common/rs_log.h"
42 #include "render_context/render_context.h"
43 
44 namespace OHOS::Rosen {
45 
46 #ifndef USE_ROSEN_DRAWING
47 void UploadTextureWithSkia(bool paraUpload, const sk_sp<SkImage>& imageUp,
48     const std::shared_ptr<Media::PixelMap>& pixelMapUp, uint64_t uniqueId);
49 #else
50 void UploadTextureWithDrawing(bool paraUpload, const std::shared_ptr<Drawing::Image>& imageUp,
51     const std::shared_ptr<Media::PixelMap>& pixelMapUp, uint64_t uniqueId);
52 #endif
53 
54 // Resource Collector
55 class ResourceCollection : public GrDirectContext::ResourceCollector {
56 public:
collectSurfaceProxy(sk_sp<GrSurfaceProxy> & surface)57     void collectSurfaceProxy(sk_sp<GrSurfaceProxy>& surface) override
58     {
59         std::unique_lock<std::mutex> lock(mutex_);
60         grSurfaceProxyVec_.push_back(surface);
61     }
62 
SwapCollection(std::vector<sk_sp<GrSurfaceProxy>> & dst,uint32_t overSize)63     void SwapCollection(std::vector<sk_sp<GrSurfaceProxy>>& dst, uint32_t overSize)
64     {
65         std::unique_lock<std::mutex> lock(mutex_);
66         if (grSurfaceProxyVec_.size() > overSize) {
67             RS_LOGI("RSUploadResourceThread SwapCollection");
68             std::swap(grSurfaceProxyVec_, dst);
69         }
70     }
71 
72 private:
73     std::mutex mutex_;
74     std::vector<sk_sp<GrSurfaceProxy>> grSurfaceProxyVec_;
75 };
76 
77 class RSB_EXPORT RSUploadResourceThread final {
78 public:
79     static RSUploadResourceThread& Instance();
80     // Task Related
81     void PostTask(const std::function<void()>& task);
82     void PostSyncTask(const std::function<void()>& task);
83     void PostTask(const std::function<void()>& task, const std::string& name);
84     void RemoveTask(const std::string& name);
85     void InitRenderContext(RenderContext* context);
86 
87     void OnRenderEnd();
88     void OnProcessBegin();
89     bool TaskIsValid(int64_t count);
GetFrameCount()90     inline int64_t GetFrameCount() const {
91         return frameCount_.load();
92     }
IsEnable()93     inline bool IsEnable() const {
94         return uploadProperity_ && isTargetPlatform_;
95     }
ImageSupportParallelUpload(int w,int h)96     inline bool ImageSupportParallelUpload(int w, int h) {
97         return (w < IMG_WIDTH_MAX) && (h < IMG_HEIGHT_MAX);
98     }
99 
100 #ifndef USE_ROSEN_DRAWING
101     sk_sp<GrDirectContext> GetShareGrContext() const;
102 #else
103     std::shared_ptr<Drawing::GPUContext> GetShareGrContext() const;
104 #endif
105     void ReleaseNotUsedPinnedViews();
106 private:
107     RSUploadResourceThread();
108     ~RSUploadResourceThread() = default;
109     RSUploadResourceThread(const RSUploadResourceThread&) = delete;
110     RSUploadResourceThread(const RSUploadResourceThread&&)= delete;
111     RSUploadResourceThread& operator=(const RSUploadResourceThread&) = delete;
112     RSUploadResourceThread& operator=(const RSUploadResourceThread&&) = delete;
113 
114     void WaitUntilRenderEnd();
115     void CreateShareEglContext();
116 
117     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
118     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
119 
120     std::condition_variable uploadTaskCond_;
121     std::mutex uploadTaskMutex_;
122     bool enableTime_ = false;
123     std::atomic<int64_t> frameCount_{0};
124     bool uploadProperity_ = true;
125     bool isTargetPlatform_ = false;
126     static constexpr int CLEAN_VIEW_COUNT = 10;
127     static constexpr int IMG_WIDTH_MAX = 300;
128     static constexpr int IMG_HEIGHT_MAX = 300;
129     RenderContext* renderContext_ = nullptr;
130 #ifdef RS_ENABLE_GL
131     EGLContext eglShareContext_ = EGL_NO_CONTEXT;
132 #endif
133     std::mutex proxyViewMutex_;
134 #ifndef USE_ROSEN_DRAWING
135     sk_sp<GrDirectContext> CreateShareGrContext();
136     sk_sp<GrDirectContext> grContext_ = nullptr;
137 #else
138     std::shared_ptr<Drawing::GPUContext> CreateShareGrContext();
139     std::shared_ptr<Drawing::GPUContext> grContext_ = nullptr;
140     sk_sp<GrDirectContext> skContext_ = nullptr;
141 #endif
142     ResourceCollection resCollector_;
143 };
144 }
145 #endif // RS_UPLOAD_TEXTURE_THREAD_H
146