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