1 /* 2 * Copyright (c) 2021 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 FRAMEWORKS_SURFACE_IMAGE_H 17 #define FRAMEWORKS_SURFACE_IMAGE_H 18 19 #include <atomic> 20 #include <mutex> 21 #include <consumer_surface.h> 22 23 #include <EGL/egl.h> 24 #include <EGL/eglext.h> 25 #include <GLES/gl.h> 26 #include <GLES/glext.h> 27 #include <GLES3/gl32.h> 28 #include <hilog/log.h> 29 30 namespace OHOS { 31 namespace { 32 #define SLOGI(fmt, ...) ::OHOS::HiviewDFX::HiLog::Info( \ 33 ::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "SurfaceImage"}, \ 34 "%{public}s: " fmt, __func__, ##__VA_ARGS__) 35 36 #define SLOGE(fmt, ...) ::OHOS::HiviewDFX::HiLog::Error( \ 37 ::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "SurfaceImage"}, \ 38 "%{public}s: " fmt, __func__, ##__VA_ARGS__) 39 } 40 41 struct ImageCacheSeq { ImageCacheSeqImageCacheSeq42 ImageCacheSeq() : eglImage_(EGL_NO_IMAGE_KHR), eglSync_(EGL_NO_SYNC_KHR) {} 43 EGLImageKHR eglImage_; 44 EGLSyncKHR eglSync_; 45 }; 46 47 class SurfaceImage : public ConsumerSurface { 48 public: 49 SurfaceImage(uint32_t textureId, uint32_t textureTarget = GL_TEXTURE_EXTERNAL_OES); 50 virtual ~SurfaceImage(); 51 52 void InitSurfaceImage(); 53 GetSurfaceImageName()54 std::string GetSurfaceImageName() const 55 { 56 return surfaceImageName_; 57 } 58 59 SurfaceError SetDefaultSize(int32_t width, int32_t height); 60 61 SurfaceError UpdateSurfaceImage(); 62 int64_t GetTimeStamp(); 63 64 // update buffer available state, updateSurfaceImage_ and a private mutex OnUpdateBufferAvailableState(bool updated)65 void OnUpdateBufferAvailableState(bool updated) 66 { 67 updateSurfaceImage_ = updated; 68 } 69 GetBufferAvailableState()70 bool GetBufferAvailableState() 71 { 72 return updateSurfaceImage_; 73 } 74 75 SurfaceError AttachContext(); 76 SurfaceError DetachContext(); 77 78 protected: 79 SurfaceError AcquireBuffer(sptr<SurfaceBuffer>& buffer, int32_t &fence, 80 int64_t ×tamp, Rect &damage) override; 81 SurfaceError ReleaseBuffer(sptr<SurfaceBuffer>& buffer, int32_t fence) override; 82 83 private: 84 SurfaceError ValidateEglState(); 85 EGLImageKHR CreateEGLImage(EGLDisplay disp, const sptr<SurfaceBuffer>& buffer); 86 SurfaceError WaitReleaseEGLSync(EGLDisplay disp); 87 SurfaceError WaitOnFence(); 88 89 uint32_t textureId_; 90 uint32_t textureTarget_; 91 std::string surfaceImageName_; 92 93 std::mutex opMutex_; 94 std::atomic<bool> updateSurfaceImage_; 95 bool isAttached = true; 96 97 EGLDisplay eglDisplay_; 98 EGLContext eglContext_; 99 std::map<int32_t, ImageCacheSeq> imageCacheSeqs_; 100 int32_t currentSurfaceImage_; 101 sptr<SurfaceBuffer> currentSurfaceBuffer_; 102 int32_t currentSurfaceBufferFence_; 103 int64_t currentTimeStamp_; 104 }; 105 106 class SurfaceImageListener : public IBufferConsumerListener { 107 public: SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage)108 explicit SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage) : surfaceImage_(surfaceImage) 109 { 110 SLOGI("SurfaceImageListener"); 111 }; 112 virtual ~SurfaceImageListener(); 113 114 virtual void OnBufferAvailable() override; 115 116 private: 117 wptr<SurfaceImage> surfaceImage_; 118 }; 119 } // namespace OHOS 120 121 #endif // FRAMEWORKS_SURFACE_IMAGE_H