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 SurfaceError SetDropBufferSwitch(bool isOpen); 86 SurfaceError OnBufferAvailable(); 87 private: 88 void UpdateBasicInfo(const sptr<SurfaceBuffer>& buffer, int64_t timestamp); 89 Rect GetBufferCropRegion(const sptr<OHOS::SurfaceBuffer>& buffer); 90 SurfaceError ValidateEglState(); 91 EGLImageKHR CreateEGLImage(EGLDisplay disp, const sptr<SurfaceBuffer>& buffer); 92 SurfaceError UpdateEGLImageAndTexture(const sptr<SurfaceBuffer>& buffer); 93 void UpdateSurfaceInfo(sptr<SurfaceBuffer> buffer, const sptr<SyncFence> &acquireFence, 94 int64_t timestamp, const Rect& damage); 95 void CheckImageCacheNeedClean(uint32_t seqNum); 96 void DestroyEGLImage(EGLImageKHR &eglImage); 97 void DestroyEGLSync(EGLSyncKHR &eglSync); 98 void NewBufferDestroyEGLImage(bool isNewBuffer, uint32_t seqNum); 99 void DestroyEGLImageBySeq(uint32_t seqNum); 100 101 uint32_t textureId_ = 0; 102 uint32_t textureTarget_ = GL_TEXTURE_EXTERNAL_OES; 103 std::string surfaceImageName_; 104 105 std::mutex opMutex_; 106 std::atomic<bool> updateSurfaceImage_; 107 108 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 109 EGLContext eglContext_ = EGL_NO_CONTEXT; 110 std::map<uint32_t, ImageCacheSeq> imageCacheSeqs_; 111 uint32_t currentSurfaceImage_ = UINT_MAX; 112 sptr<SurfaceBuffer> currentSurfaceBuffer_; 113 int64_t currentTimeStamp_; 114 float currentTransformMatrix_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 115 float currentTransformMatrixV2_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 116 float currentBufferMatrix_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 117 uint64_t uniqueId_ = 0; 118 bool dropFrameMode_ = false; 119 120 /** 121 * @brief Represents the properties of a graphics buffer frame 122 * 123 * This structure encapsulates essential information about a graphics buffer frame, 124 * including its effective region, dimensions, and transformation settings. It is used 125 * to track and compare buffer states for efficient graphics rendering and processing. 126 */ 127 struct BufferProperties { 128 /** 129 * @brief Defines the valid/effective region within the buffer 130 * Represents the actual usable area of the buffer that contains valid content, 131 * which may be smaller than the full buffer dimensions 132 */ 133 Rect crop = {0, 0, 0, 0}; 134 135 /** 136 * @brief Rotation/transformation type applied to the buffer 137 * Specifies how the buffer content should be transformed during rendering 138 */ 139 GraphicTransformType transformType = GraphicTransformType::GRAPHIC_ROTATE_NONE; 140 141 /** 142 * @brief Width of the buffer in pixels 143 * Represents the horizontal dimension of the graphics buffer 144 */ 145 uint32_t bufferWidth = 0; 146 147 /** 148 * @brief Height of the buffer in pixels 149 * Represents the vertical dimension of the graphics buffer 150 */ 151 uint32_t bufferHeight = 0; 152 153 /** 154 * @brief Equality comparison operator 155 * @param other The BufferProperties object to compare with 156 * @return true if all properties match, false otherwise 157 */ 158 bool operator==(const BufferProperties& other) const 159 { 160 return crop == other.crop && 161 transformType == other.transformType && 162 bufferWidth == other.bufferWidth && 163 bufferHeight == other.bufferHeight; 164 } 165 166 /** 167 * @brief Inequality comparison operator 168 * @param other The BufferProperties object to compare with 169 * @return true if any property differs, false if all match 170 */ 171 bool operator!=(const BufferProperties& other) const 172 { 173 return !(*this == other); 174 } 175 }; 176 BufferProperties bufferProperties_; 177 BufferProperties preBufferProperties_; 178 }; 179 180 class SurfaceImageListener : public IBufferConsumerListener { 181 public: SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage)182 explicit SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage) : surfaceImage_(surfaceImage) 183 { 184 BLOGI("SurfaceImageListener"); 185 }; 186 virtual ~SurfaceImageListener(); 187 188 virtual void OnBufferAvailable() override; 189 190 private: 191 wptr<SurfaceImage> surfaceImage_; 192 }; 193 } // namespace OHOS 194 #endif // FRAMEWORKS_SURFACE_IMAGE_H 195