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 #include "native_window.h" 31 32 namespace OHOS { 33 struct ImageCacheSeq { ImageCacheSeqImageCacheSeq34 ImageCacheSeq() : eglImage_(EGL_NO_IMAGE_KHR), eglSync_(EGL_NO_SYNC_KHR) {} 35 EGLImageKHR eglImage_; 36 EGLSyncKHR eglSync_; 37 }; 38 39 static constexpr uint32_t TRANSFORM_MATRIX_ELE_COUNT = 16; 40 typedef void (*OnBufferAvailableListener)(void *context); 41 42 class SurfaceImage : public ConsumerSurface { 43 public: 44 SurfaceImage(uint32_t textureId, uint32_t textureTarget = GL_TEXTURE_EXTERNAL_OES); 45 SurfaceImage(); 46 virtual ~SurfaceImage(); 47 48 void InitSurfaceImage(); 49 GetSurfaceImageName()50 std::string GetSurfaceImageName() const 51 { 52 return surfaceImageName_; 53 } 54 55 SurfaceError UpdateSurfaceImage(); 56 int64_t GetTimeStamp(); 57 58 // update buffer available state, updateSurfaceImage_ and a private mutex OnUpdateBufferAvailableState(bool updated)59 void OnUpdateBufferAvailableState(bool updated) 60 { 61 updateSurfaceImage_ = updated; 62 } 63 GetBufferAvailableState()64 bool GetBufferAvailableState() 65 { 66 return updateSurfaceImage_; 67 } 68 69 SurfaceError AttachContext(uint32_t textureId); 70 SurfaceError DetachContext(); 71 72 SurfaceError GetTransformMatrix(float matrix[16]); 73 SurfaceError GetTransformMatrixV2(float matrix[16]); 74 SurfaceError GetBufferMatrix(float matrix[16]); 75 SurfaceError SetOnBufferAvailableListener(void *context, OnBufferAvailableListener listener); 76 SurfaceError UnsetOnBufferAvailableListener(); 77 OnBufferAvailableListener listener_ = nullptr; 78 void *context_ = nullptr; 79 80 SurfaceError AcquireNativeWindowBuffer(OHNativeWindowBuffer** nativeWindowBuffer, int32_t* fenceFd); 81 SurfaceError ReleaseNativeWindowBuffer(OHNativeWindowBuffer* nativeWindowBuffer, int32_t fenceFd); 82 83 SurfaceError SetDefaultUsage(uint64_t usage); 84 SurfaceError SetDefaultSize(int32_t width, int32_t height); 85 private: 86 void UpdateBasicInfo(const sptr<SurfaceBuffer>& buffer, int64_t timestamp); 87 Rect GetBufferCropRegion(const sptr<OHOS::SurfaceBuffer>& buffer); 88 SurfaceError ValidateEglState(); 89 EGLImageKHR CreateEGLImage(EGLDisplay disp, const sptr<SurfaceBuffer>& buffer); 90 SurfaceError UpdateEGLImageAndTexture(const sptr<SurfaceBuffer>& buffer); 91 void UpdateSurfaceInfo(sptr<SurfaceBuffer> buffer, const sptr<SyncFence> &acquireFence, 92 int64_t timestamp, const Rect& damage); 93 void CheckImageCacheNeedClean(uint32_t seqNum); 94 void DestroyEGLImage(EGLImageKHR &eglImage); 95 void DestroyEGLSync(EGLSyncKHR &eglSync); 96 void NewBufferDestroyEGLImage(bool isNewBuffer, uint32_t seqNum); 97 void DestroyEGLImageBySeq(uint32_t seqNum); 98 99 uint32_t textureId_ = 0; 100 uint32_t textureTarget_ = GL_TEXTURE_EXTERNAL_OES; 101 std::string surfaceImageName_; 102 103 std::mutex opMutex_; 104 std::atomic<bool> updateSurfaceImage_; 105 106 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 107 EGLContext eglContext_ = EGL_NO_CONTEXT; 108 std::map<uint32_t, ImageCacheSeq> imageCacheSeqs_; 109 uint32_t currentSurfaceImage_ = UINT_MAX; 110 sptr<SurfaceBuffer> currentSurfaceBuffer_; 111 int64_t currentTimeStamp_; 112 float currentTransformMatrix_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 113 float currentTransformMatrixV2_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 114 float currentBufferMatrix_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 115 uint64_t uniqueId_ = 0; 116 117 /** 118 * @brief Represents the properties of a graphics buffer frame 119 * 120 * This structure encapsulates essential information about a graphics buffer frame, 121 * including its effective region, dimensions, and transformation settings. It is used 122 * to track and compare buffer states for efficient graphics rendering and processing. 123 */ 124 struct BufferProperties { 125 /** 126 * @brief Defines the valid/effective region within the buffer 127 * Represents the actual usable area of the buffer that contains valid content, 128 * which may be smaller than the full buffer dimensions 129 */ 130 Rect crop = {0, 0, 0, 0}; 131 132 /** 133 * @brief Rotation/transformation type applied to the buffer 134 * Specifies how the buffer content should be transformed during rendering 135 */ 136 GraphicTransformType transformType = GraphicTransformType::GRAPHIC_ROTATE_NONE; 137 138 /** 139 * @brief Width of the buffer in pixels 140 * Represents the horizontal dimension of the graphics buffer 141 */ 142 uint32_t bufferWidth = 0; 143 144 /** 145 * @brief Height of the buffer in pixels 146 * Represents the vertical dimension of the graphics buffer 147 */ 148 uint32_t bufferHeight = 0; 149 150 /** 151 * @brief Equality comparison operator 152 * @param other The BufferProperties object to compare with 153 * @return true if all properties match, false otherwise 154 */ 155 bool operator==(const BufferProperties& other) const 156 { 157 return crop == other.crop && 158 transformType == other.transformType && 159 bufferWidth == other.bufferWidth && 160 bufferHeight == other.bufferHeight; 161 } 162 163 /** 164 * @brief Inequality comparison operator 165 * @param other The BufferProperties object to compare with 166 * @return true if any property differs, false if all match 167 */ 168 bool operator!=(const BufferProperties& other) const 169 { 170 return !(*this == other); 171 } 172 }; 173 BufferProperties bufferProperties_; 174 BufferProperties preBufferProperties_; 175 }; 176 177 class SurfaceImageListener : public IBufferConsumerListener { 178 public: SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage)179 explicit SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage) : surfaceImage_(surfaceImage) 180 { 181 BLOGI("SurfaceImageListener"); 182 }; 183 virtual ~SurfaceImageListener(); 184 185 virtual void OnBufferAvailable() override; 186 187 private: 188 wptr<SurfaceImage> surfaceImage_; 189 }; 190 } // namespace OHOS 191 #endif // FRAMEWORKS_SURFACE_IMAGE_H 192