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 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 <surface.h> 25 #include "EGL/egl.h" 26 #include "EGL/eglext.h" 27 #include "GLES/gl.h" 28 #include "GLES/glext.h" 29 #include "GLES3/gl32.h" 30 #include "sync_fence.h" 31 #include "pipeline/rs_context.h" 32 33 namespace OHOS { 34 namespace Rosen { 35 class ImageCacheSeq { 36 public: 37 static std::unique_ptr<ImageCacheSeq> Create( 38 EGLDisplay eglDisplay, 39 EGLContext eglContext, 40 const sptr<OHOS::SurfaceBuffer>& buffer); 41 42 ImageCacheSeq( 43 EGLDisplay eglDisplay, 44 EGLImageKHR eglImage, 45 EGLClientBuffer eglClientBuffer); 46 ~ImageCacheSeq() noexcept; 47 TextureId()48 GLuint TextureId() const 49 { 50 return textureId_; 51 } 52 GetThreadIndex()53 pid_t GetThreadIndex() const 54 { 55 return threadIndex_; 56 } 57 SetThreadIndex(const pid_t threadIndex)58 void SetThreadIndex(const pid_t threadIndex) 59 { 60 threadIndex_ = threadIndex; 61 } 62 private: 63 // generate a texture and bind eglImage to it. 64 bool BindToTexture(); 65 66 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 67 EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR; 68 EGLClientBuffer eglClientBuffer_ = nullptr; 69 GLuint textureId_ = 0; 70 pid_t threadIndex_ = 0; 71 }; 72 73 class RSEglImageManager { 74 public: 75 explicit RSEglImageManager(EGLDisplay display); 76 ~RSEglImageManager() noexcept = default; 77 78 GLuint MapEglImageFromSurfaceBuffer(const sptr<OHOS::SurfaceBuffer>& buffer, 79 const sptr<SyncFence>& acquireFence, pid_t threadIndex); 80 void UnMapEglImageFromSurfaceBuffer(int32_t seqNum); 81 void UnMapEglImageFromSurfaceBufferForUniRedraw(int32_t seqNum); 82 void ShrinkCachesIfNeeded(bool isForUniRedraw = false); // only used for divided_render 83 std::unique_ptr<ImageCacheSeq> CreateImageCacheFromBuffer(const sptr<OHOS::SurfaceBuffer>& buffer, 84 const sptr<SyncFence>& acquireFence); 85 private: 86 void WaitAcquireFence(const sptr<SyncFence>& acquireFence); 87 GLuint CreateImageCacheFromBuffer(const sptr<OHOS::SurfaceBuffer>& buffer, 88 const pid_t threadIndex); 89 90 mutable std::mutex opMutex_; 91 static constexpr size_t MAX_CACHE_SIZE = 16; 92 std::queue<int32_t> cacheQueue_; // fifo, size restricted by MAX_CACHE_SIZE 93 std::unordered_map<int32_t, std::unique_ptr<ImageCacheSeq>> imageCacheSeqs_; // guarded by opMutex_. 94 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 95 }; 96 } // namespace Rosen 97 } // namespace OHOS 98 99 #endif // RS_EGL_IMAGE_MANAGER_H 100