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 <array> 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 "buffer_log.h" 29 #include "consumer_surface.h" 30 31 namespace OHOS { 32 struct ImageCacheSeq { ImageCacheSeqImageCacheSeq33 ImageCacheSeq() : eglImage_(EGL_NO_IMAGE_KHR), eglSync_(EGL_NO_SYNC_KHR) {} 34 EGLImageKHR eglImage_; 35 EGLSyncKHR eglSync_; 36 }; 37 38 static constexpr int64_t TRANSFORM_MATRIX_ELE_COUNT = 16; 39 40 class SurfaceImage : public ConsumerSurface { 41 public: 42 SurfaceImage(uint32_t textureId, uint32_t textureTarget = GL_TEXTURE_EXTERNAL_OES); 43 virtual ~SurfaceImage(); 44 45 void InitSurfaceImage(); 46 GetSurfaceImageName()47 std::string GetSurfaceImageName() const 48 { 49 return surfaceImageName_; 50 } 51 52 SurfaceError SetDefaultSize(int32_t width, int32_t height); 53 54 SurfaceError UpdateSurfaceImage(); 55 int64_t GetTimeStamp(); 56 57 // update buffer available state, updateSurfaceImage_ and a private mutex OnUpdateBufferAvailableState(bool updated)58 void OnUpdateBufferAvailableState(bool updated) 59 { 60 updateSurfaceImage_ = updated; 61 } 62 GetBufferAvailableState()63 bool GetBufferAvailableState() 64 { 65 return updateSurfaceImage_; 66 } 67 68 SurfaceError AttachContext(uint32_t textureId); 69 SurfaceError DetachContext(); 70 71 SurfaceError GetTransformMatrix(float matrix[16]); 72 73 protected: 74 SurfaceError AcquireBuffer(sptr<SurfaceBuffer>& buffer, int32_t &fence, 75 int64_t ×tamp, Rect &damage) override; 76 SurfaceError ReleaseBuffer(sptr<SurfaceBuffer>& buffer, int32_t fence) override; 77 78 void ComputeTransformMatrix(); 79 80 private: 81 SurfaceError ValidateEglState(); 82 EGLImageKHR CreateEGLImage(EGLDisplay disp, const sptr<SurfaceBuffer>& buffer); 83 SurfaceError WaitReleaseEGLSync(EGLDisplay disp); 84 SurfaceError WaitOnFence(); 85 std::array<float, 16> MatrixProduct(const std::array<float, 16>& lMat, const std::array<float, 16>& rMat); 86 87 uint32_t textureId_; 88 uint32_t textureTarget_; 89 std::string surfaceImageName_; 90 91 std::mutex opMutex_; 92 std::atomic<bool> updateSurfaceImage_; 93 bool isAttached = true; 94 95 EGLDisplay eglDisplay_; 96 EGLContext eglContext_; 97 std::map<uint32_t, ImageCacheSeq> imageCacheSeqs_; 98 uint32_t currentSurfaceImage_; 99 sptr<SurfaceBuffer> currentSurfaceBuffer_; 100 int32_t currentSurfaceBufferFence_; 101 int64_t currentTimeStamp_; 102 Rect currentCrop_ = {}; 103 GraphicTransformType currentTransformType_ = GraphicTransformType::GRAPHIC_ROTATE_NONE; 104 std::array<float, TRANSFORM_MATRIX_ELE_COUNT> currentTransformMatrix_ {}; 105 }; 106 107 class SurfaceImageListener : public IBufferConsumerListener { 108 public: SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage)109 explicit SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage) : surfaceImage_(surfaceImage) 110 { 111 BLOGI("SurfaceImageListener"); 112 }; 113 virtual ~SurfaceImageListener(); 114 115 virtual void OnBufferAvailable() override; 116 117 private: 118 wptr<SurfaceImage> surfaceImage_; 119 }; 120 } // namespace OHOS 121 122 #endif // FRAMEWORKS_SURFACE_IMAGE_H