1 /* 2 * Copyright (c) 2022-2025 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_EGL_IMAGE_MANAGER_H 17 #define RS_EGL_IMAGE_MANAGER_H 18 19 #include <memory> 20 #include <mutex> 21 #include <queue> 22 #include <unordered_map> 23 24 #include "rs_image_manager.h" 25 26 #include "EGL/egl.h" 27 #include "EGL/eglext.h" 28 #include "GLES/gl.h" 29 #include "GLES/glext.h" 30 #include "GLES3/gl32.h" 31 #include "pipeline/render_thread/rs_base_render_util.h" 32 #include "sync_fence.h" 33 34 namespace OHOS { 35 namespace Rosen { 36 class EglImageResource { 37 public: 38 static std::unique_ptr<EglImageResource> Create(EGLDisplay eglDisplay, EGLContext eglContext, 39 const sptr<OHOS::SurfaceBuffer>& buffer); 40 EglImageResource(EGLDisplay eglDisplay,EGLImageKHR eglImage,EGLClientBuffer eglClientBuffer)41 EglImageResource(EGLDisplay eglDisplay, EGLImageKHR eglImage, EGLClientBuffer eglClientBuffer) 42 : eglDisplay_(eglDisplay), eglImage_(eglImage), eglClientBuffer_(eglClientBuffer) {} 43 ~EglImageResource() noexcept; 44 GetTextureId()45 GLuint GetTextureId() const 46 { 47 return textureId_; 48 } 49 GetThreadIndex()50 pid_t GetThreadIndex() const 51 { 52 return threadIndex_; 53 } 54 SetThreadIndex(const pid_t threadIndex)55 void SetThreadIndex(const pid_t threadIndex) 56 { 57 threadIndex_ = threadIndex; 58 } 59 private: 60 // generate a texture and bind eglImage to it. 61 bool BindToTexture(); 62 63 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 64 EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR; 65 EGLClientBuffer eglClientBuffer_ = nullptr; 66 GLuint textureId_ = 0; 67 pid_t threadIndex_ = 0; 68 }; 69 70 class RSEglImageManager : public RSImageManager { 71 public: RSEglImageManager(EGLDisplay display)72 explicit RSEglImageManager(EGLDisplay display) : eglDisplay_(display) {}; 73 ~RSEglImageManager() noexcept override = default; 74 75 void UnMapImageFromSurfaceBuffer(int32_t seqNum) override; 76 std::shared_ptr<Drawing::Image> CreateImageFromBuffer( 77 RSPaintFilterCanvas& canvas, const BufferDrawParam& params, 78 const std::shared_ptr<Drawing::ColorSpace>& drawingColorSpace) override; 79 std::shared_ptr<Drawing::Image> GetIntersectImage(Drawing::RectI& imgCutRect, 80 const std::shared_ptr<Drawing::GPUContext>& context, const sptr<OHOS::SurfaceBuffer>& buffer, 81 const sptr<SyncFence>& acquireFence, pid_t threadIndex = 0) override; 82 83 void ShrinkCachesIfNeeded(bool isForUniRedraw = false) override; // only used for divided_render 84 85 private: 86 void WaitAcquireFence(const sptr<SyncFence>& acquireFence); 87 GLuint CreateEglImageCacheFromBuffer(const sptr<OHOS::SurfaceBuffer>& buffer, 88 const pid_t threadIndex); 89 GLuint MapEglImageFromSurfaceBuffer(const sptr<OHOS::SurfaceBuffer>& buffer, 90 const sptr<SyncFence>& acquireFence, pid_t threadIndex); 91 void UnMapEglImageFromSurfaceBufferForUniRedraw(int32_t seqNum); 92 93 static constexpr size_t MAX_CACHE_SIZE = 16; 94 std::queue<int32_t> cacheQueue_; // fifo, size restricted by MAX_CACHE_SIZE 95 std::unordered_map<int32_t, std::unique_ptr<EglImageResource>> imageCacheSeqs_; // guarded by opMutex_ 96 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 97 }; 98 } // namespace Rosen 99 } // namespace OHOS 100 101 #endif // RS_EGL_IMAGE_MANAGER_H 102