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 53 private: 54 // generate a texture and bind eglImage to it. 55 bool BindToTexture(); 56 57 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 58 EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR; 59 EGLClientBuffer eglClientBuffer_ = nullptr; 60 GLuint textureId_ = 0; 61 }; 62 63 class RSEglImageManager { 64 public: 65 explicit RSEglImageManager(EGLDisplay display); 66 ~RSEglImageManager() noexcept = default; 67 68 GLuint MapEglImageFromSurfaceBuffer(const sptr<OHOS::SurfaceBuffer>& buffer, 69 const sptr<SyncFence>& acquireFence); 70 void UnMapEglImageFromSurfaceBuffer(int32_t seqNum); 71 void ShrinkCachesIfNeeded(); 72 private: 73 void WaitAcquireFence(const sptr<SyncFence>& acquireFence); 74 GLuint CreateImageCacheFromBuffer(const sptr<OHOS::SurfaceBuffer>& buffer); 75 76 mutable std::mutex opMutex_; 77 static constexpr size_t MAX_CACHE_SIZE = 64; 78 std::queue<int32_t> cacheQueue_; // fifo, size restricted by MAX_CACHE_SIZE 79 std::unordered_map<int32_t, std::unique_ptr<ImageCacheSeq>> imageCacheSeqs_; // guarded by opMutex_. 80 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 81 }; 82 } // namespace Rosen 83 } // namespace OHOS 84 85 #endif // RS_EGL_IMAGE_MANAGER_H 86