From 6c1f5f1c77f1d16026f870157cfd90ffe268281b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rulong=20Chen=EF=BC=88=E9=99=88=E6=B1=9D=E9=BE=99=EF=BC=89?= Date: Sun, 16 Jun 2024 13:18:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=9C=BA=E6=99=AF=E4=B8=8BPlatformView=E5=8C=BA=E5=9F=9F?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=BB=91=E5=B1=8F=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原因:当调用MarkNewFrameAvailable函数时,OHOSExternalTextureGL还未初始化。 Change-Id: Id2e29c8297272d9e2bf21fe2b52c39f3e3034dee --- .../ohos/ohos_external_texture_gl.cpp | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/shell/platform/ohos/ohos_external_texture_gl.cpp b/shell/platform/ohos/ohos_external_texture_gl.cpp index ad765b2f63..d7a8f57787 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.cpp +++ b/shell/platform/ohos/ohos_external_texture_gl.cpp @@ -65,25 +65,24 @@ void OHOSExternalTextureGL::Attach() OhosSurfaceGLSkia* ohosSurfaceGLSkia_ = (OhosSurfaceGLSkia*)ohos_surface_ptr; auto result = ohosSurfaceGLSkia_->GLContextMakeCurrent(); if (result->GetResult()) { - FML_DLOG(INFO)<<"ResourceContextMakeCurrent successed"; glGenTextures(1, &texture_name_); - FML_DLOG(INFO) << "OHOSExternalTextureGL::Paint, glGenTextures texture_name_=" << texture_name_; - if (nativeImage_ == nullptr) { + if (!nativeImage_) { nativeImage_ = OH_NativeImage_Create(texture_name_, GL_TEXTURE_EXTERNAL_OES); - if (nativeImage_ == nullptr) { - FML_DLOG(ERROR) << "Error with OH_NativeImage_Create"; + if (!nativeImage_) { + FML_DLOG(ERROR) << "Failed to create OH_NativeImage"; return; } nativeWindow_ = OH_NativeImage_AcquireNativeWindow(nativeImage_); - if (nativeWindow_ == nullptr) { - FML_DLOG(ERROR) << "Error with OH_NativeImage_AcquireNativeWindow"; + if (!nativeWindow_) { + FML_DLOG(ERROR) << "Failed to acquire OHNativeWindow"; return; } } int32_t ret = OH_NativeImage_AttachContext(nativeImage_, texture_name_); if (ret != 0) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_NativeImage_AttachContext err code:"<< ret; + FML_DLOG(FATAL) << "Failed to attach current GL context. ErrorCode:" << ret; + return; } state_ = AttachmentState::attached; } else { @@ -175,6 +174,13 @@ void OHOSExternalTextureGL::OnGrContextDestroyed() void OHOSExternalTextureGL::MarkNewFrameAvailable() { FML_DLOG(INFO)<<" OHOSExternalTextureGL::MarkNewFrameAvailable"; + if (state_ == AttachmentState::detached) { + return; + } + if (state_ == AttachmentState::uninitialized) { + Attach(); + } + new_frame_ready_ = true; first_update_ = true; Update(); @@ -182,16 +188,17 @@ void OHOSExternalTextureGL::MarkNewFrameAvailable() void OHOSExternalTextureGL::OnTextureUnregistered() { - FML_DLOG(INFO)<<" OHOSExternalTextureGL::OnTextureUnregistered"; + FML_DLOG(INFO) << "OHOSExternalTextureGL::OnTextureUnregistered"; OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage_); OH_NativeImage_Destroy(&nativeImage_); } void OHOSExternalTextureGL::Update() { + FML_DLOG(INFO) << "OHOSExternalTextureGL::Update"; int32_t ret = OH_NativeImage_UpdateSurfaceImage(nativeImage_); if (ret != 0) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_NativeImage_UpdateSurfaceImage err code:"<< ret; + FML_DLOG(FATAL) << "Failed to update GL texture. err code:" << ret; return; } UpdateTransform(); @@ -199,16 +206,18 @@ void OHOSExternalTextureGL::Update() void OHOSExternalTextureGL::Detach() { + FML_DLOG(INFO) << "OHOSExternalTextureGL::Detach"; OH_NativeImage_DetachContext(nativeImage_); OH_NativeWindow_DestroyNativeWindow(nativeWindow_); } void OHOSExternalTextureGL::UpdateTransform() { + FML_DLOG(INFO) << "OHOSExternalTextureGL::UpdateTransform"; float m[16] = { 0.0f }; int32_t ret = OH_NativeImage_GetTransformMatrixV2(nativeImage_, m); if (ret != 0) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_NativeImage_GetTransformMatrixV2 err code:"<< ret; + FML_DLOG(FATAL) << "Failed to get the transformation matrix of the texture image. err code:" << ret; } // transform ohos 4x4 matrix to skia 3x3 matrix SkScalar matrix3[] = { @@ -219,22 +228,24 @@ void OHOSExternalTextureGL::UpdateTransform() transform.set9(matrix3); SkMatrix inverted; if (!transform.invert(&inverted)) { - FML_LOG(FATAL) << "OHOSExternalTextureGL Invalid SurfaceTexture transformation matrix"; + FML_LOG(FATAL) << "Invalid SurfaceTexture transformation matrix"; } transform = inverted; } void OHOSExternalTextureGL::DispatchImage(ImageNative* image) { + FML_DLOG(INFO) << "OHOSExternalTextureGL::DispatchImage"; lastImage_ = image; } void OHOSExternalTextureGL::setBackground(int32_t width, int32_t height) { + FML_DLOG(INFO) << "OHOSExternalTextureGL::setBackground"; if (nativeWindow_ == nullptr) { nativeWindow_ = OH_NativeImage_AcquireNativeWindow(nativeImage_); if (nativeWindow_ == nullptr) { - FML_DLOG(ERROR) << "OHOSExternalTextureGL in setBackground Error with OH_NativeImage_AcquireNativeWindow"; + FML_DLOG(ERROR) << "Failed to obtain an **OHNativeWindow** instance"; return; } } @@ -242,20 +253,20 @@ void OHOSExternalTextureGL::setBackground(int32_t width, int32_t height) int code = SET_BUFFER_GEOMETRY; int32_t ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow_, code, width, height); if (ret != 0) { - FML_DLOG(ERROR) << "OHOSExternalTextureGL in setBackground OH_NativeWindow_NativeWindowHandleOpt err:" << ret; + FML_DLOG(ERROR) << "Failed to set the attributes of an **OHNativeWindow** instance. err code:" << ret; return; } ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer_, &fenceFd); if (ret != 0) { - FML_DLOG(ERROR) << "OHOSExternalTextureGL in setBackground OH_NativeWindow_NativeWindowRequestBuffer err:" << ret; + FML_DLOG(ERROR) << " Failed to requests an **OHNativeWindowBuffer**. err code:" << ret; return; } BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer_); void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0); if (mappedAddr == MAP_FAILED) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL in setBackground mmap failed"; + FML_DLOG(FATAL) << "No successful return from mmap()"; return; } @@ -268,36 +279,37 @@ void OHOSExternalTextureGL::setBackground(int32_t width, int32_t height) } } - // munmap after use + // munmap after use ret = munmap(mappedAddr, handle->size); if (ret == -1) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL in setBackground munmap failed"; + FML_DLOG(FATAL) << "Failed to unmap pages of memory"; return; } Region region{nullptr, 0}; ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer_, fenceFd, region); if (ret != 0) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL in setBackground OH_NativeWindow_NativeWindowFlushBuffer err:"<< ret; + FML_DLOG(FATAL) << "Failed to flush the buffer to the consumer. err code:" << ret; } } void OHOSExternalTextureGL::HandlePixelMapBuffer() { + FML_DLOG(INFO) << "OHOSExternalTextureGL::HandlePixelMapBuffer"; BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer_); // get virAddr of bufferHandl by mmap sys interface uint32_t stride = handle->stride; FML_DLOG(INFO) << "OHOSExternalTextureGL stride:" << stride; void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0); if (mappedAddr == MAP_FAILED) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL mmap failed"; + FML_DLOG(FATAL) << "Failed to map pages of memory"; return; } void *pixelAddr = nullptr; int64_t ret = OH_PixelMap_AccessPixels(pixelMap_, &pixelAddr); if (ret != IMAGE_RESULT_SUCCESS) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_PixelMap_AccessPixels err:"<< ret; + FML_DLOG(FATAL) << "Failed to obtain the memory address of the PixelMap object and lock the memory. err code:" << ret; return; } @@ -325,13 +337,14 @@ void OHOSExternalTextureGL::HandlePixelMapBuffer() // munmap after use ret = munmap(mappedAddr, handle->size); if (ret == -1) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL munmap failed"; + FML_DLOG(FATAL) << "Failed to unmap pages of memory"; return; } } void OHOSExternalTextureGL::ProducePixelMapToNativeImage() { + FML_DLOG(INFO) << "OHOSExternalTextureGL::ProducePixelMapToNativeImage"; if (state_ == AttachmentState::detached) { FML_DLOG(ERROR) << "OHOSExternalTextureGL AttachmentState err"; return; @@ -367,6 +380,7 @@ void OHOSExternalTextureGL::ProducePixelMapToNativeImage() EGLDisplay OHOSExternalTextureGL::GetPlatformEglDisplay(EGLenum platform, void *native_display, const EGLint *attrib_list) { + FML_DLOG(INFO) << "OHOSExternalTextureGL::GetPlatformEglDisplay"; GetPlatformDisplayExt eglGetPlatformDisplayExt = NULL; if (!eglGetPlatformDisplayExt) { @@ -387,6 +401,7 @@ EGLDisplay OHOSExternalTextureGL::GetPlatformEglDisplay(EGLenum platform, void * bool OHOSExternalTextureGL::CheckEglExtension(const char *extensions, const char *extension) { + FML_DLOG(INFO) << "OHOSExternalTextureGL::CheckEglExtension"; size_t extlen = strlen(extension); const char *end = extensions + strlen(extensions); while (extensions < end) { @@ -406,6 +421,7 @@ bool OHOSExternalTextureGL::CheckEglExtension(const char *extensions, const char void OHOSExternalTextureGL::DispatchPixelMap(NativePixelMap* pixelMap) { + FML_DLOG(INFO) << "OHOSExternalTextureGL::DispatchPixelMap"; if (pixelMap != nullptr) { pixelMap_ = pixelMap; } -- Gitee From 338acf3668785b2ed0f62d5a90b580db42c50198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rulong=20Chen=EF=BC=88=E9=99=88=E6=B1=9D=E9=BE=99=EF=BC=89?= Date: Sun, 16 Jun 2024 18:28:24 +0800 Subject: [PATCH 2/2] Modify code format. Change-Id: I9ae5f2af1415e0b771e4e3a1195e008e9433f3b8 --- .../ohos/ohos_external_texture_gl.cpp | 427 +++++++++--------- 1 file changed, 214 insertions(+), 213 deletions(-) diff --git a/shell/platform/ohos/ohos_external_texture_gl.cpp b/shell/platform/ohos/ohos_external_texture_gl.cpp index d7a8f57787..d5c0938d8d 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.cpp +++ b/shell/platform/ohos/ohos_external_texture_gl.cpp @@ -61,33 +61,33 @@ OHOSExternalTextureGL::~OHOSExternalTextureGL() void OHOSExternalTextureGL::Attach() { - OHOSSurface* ohos_surface_ptr = ohos_surface_.get(); - OhosSurfaceGLSkia* ohosSurfaceGLSkia_ = (OhosSurfaceGLSkia*)ohos_surface_ptr; - auto result = ohosSurfaceGLSkia_->GLContextMakeCurrent(); - if (result->GetResult()) { - glGenTextures(1, &texture_name_); - if (!nativeImage_) { - nativeImage_ = OH_NativeImage_Create(texture_name_, GL_TEXTURE_EXTERNAL_OES); - if (!nativeImage_) { - FML_DLOG(ERROR) << "Failed to create OH_NativeImage"; - return; - } - nativeWindow_ = OH_NativeImage_AcquireNativeWindow(nativeImage_); - if (!nativeWindow_) { - FML_DLOG(ERROR) << "Failed to acquire OHNativeWindow"; - return; - } - } - - int32_t ret = OH_NativeImage_AttachContext(nativeImage_, texture_name_); - if (ret != 0) { - FML_DLOG(FATAL) << "Failed to attach current GL context. ErrorCode:" << ret; - return; + OHOSSurface* ohos_surface_ptr = ohos_surface_.get(); + OhosSurfaceGLSkia* ohosSurfaceGLSkia_ = (OhosSurfaceGLSkia*)ohos_surface_ptr; + auto result = ohosSurfaceGLSkia_->GLContextMakeCurrent(); + if (result->GetResult()) { + glGenTextures(1, &texture_name_); + if (!nativeImage_) { + nativeImage_ = OH_NativeImage_Create(texture_name_, GL_TEXTURE_EXTERNAL_OES); + if (!nativeImage_) { + FML_DLOG(ERROR) << "Failed to create OH_NativeImage"; + return; + } + nativeWindow_ = OH_NativeImage_AcquireNativeWindow(nativeImage_); + if (!nativeWindow_) { + FML_DLOG(ERROR) << "Failed to acquire OHNativeWindow"; + return; + } + } + + int32_t ret = OH_NativeImage_AttachContext(nativeImage_, texture_name_); + if (ret != 0) { + FML_DLOG(FATAL) << "Failed to attach current GL context. ErrorCode:" << ret; + return; + } + state_ = AttachmentState::attached; + } else { + FML_DLOG(FATAL)<<"ResourceContextMakeCurrent failed"; } - state_ = AttachmentState::attached; - } else { - FML_DLOG(FATAL)<<"ResourceContextMakeCurrent failed"; - } } void OHOSExternalTextureGL::Paint(PaintContext& context, @@ -173,258 +173,259 @@ void OHOSExternalTextureGL::OnGrContextDestroyed() void OHOSExternalTextureGL::MarkNewFrameAvailable() { - FML_DLOG(INFO)<<" OHOSExternalTextureGL::MarkNewFrameAvailable"; - if (state_ == AttachmentState::detached) { - return; - } - if (state_ == AttachmentState::uninitialized) { - Attach(); - } + FML_DLOG(INFO)<<" OHOSExternalTextureGL::MarkNewFrameAvailable"; + if (state_ == AttachmentState::detached) { + return; + } + if (state_ == AttachmentState::uninitialized) { + Attach(); + } - new_frame_ready_ = true; - first_update_ = true; - Update(); + new_frame_ready_ = true; + first_update_ = true; + Update(); } void OHOSExternalTextureGL::OnTextureUnregistered() { - FML_DLOG(INFO) << "OHOSExternalTextureGL::OnTextureUnregistered"; - OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage_); - OH_NativeImage_Destroy(&nativeImage_); + FML_DLOG(INFO) << "OHOSExternalTextureGL::OnTextureUnregistered"; + OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage_); + OH_NativeImage_Destroy(&nativeImage_); } void OHOSExternalTextureGL::Update() { - FML_DLOG(INFO) << "OHOSExternalTextureGL::Update"; - int32_t ret = OH_NativeImage_UpdateSurfaceImage(nativeImage_); - if (ret != 0) { - FML_DLOG(FATAL) << "Failed to update GL texture. err code:" << ret; - return; - } - UpdateTransform(); + FML_DLOG(INFO) << "OHOSExternalTextureGL::Update"; + int32_t ret = OH_NativeImage_UpdateSurfaceImage(nativeImage_); + if (ret != 0) { + FML_DLOG(FATAL) << "Failed to update GL texture. err code:" << ret; + return; + } + UpdateTransform(); } void OHOSExternalTextureGL::Detach() { - FML_DLOG(INFO) << "OHOSExternalTextureGL::Detach"; - OH_NativeImage_DetachContext(nativeImage_); - OH_NativeWindow_DestroyNativeWindow(nativeWindow_); + FML_DLOG(INFO) << "OHOSExternalTextureGL::Detach"; + OH_NativeImage_DetachContext(nativeImage_); + OH_NativeWindow_DestroyNativeWindow(nativeWindow_); } void OHOSExternalTextureGL::UpdateTransform() { - FML_DLOG(INFO) << "OHOSExternalTextureGL::UpdateTransform"; - float m[16] = { 0.0f }; - int32_t ret = OH_NativeImage_GetTransformMatrixV2(nativeImage_, m); - if (ret != 0) { - FML_DLOG(FATAL) << "Failed to get the transformation matrix of the texture image. err code:" << ret; - } - // transform ohos 4x4 matrix to skia 3x3 matrix - SkScalar matrix3[] = { - m[0], m[4], m[12], // - m[1], m[5], m[13], // - m[3], m[7], m[15], // - }; - transform.set9(matrix3); - SkMatrix inverted; - if (!transform.invert(&inverted)) { - FML_LOG(FATAL) << "Invalid SurfaceTexture transformation matrix"; - } - transform = inverted; + FML_DLOG(INFO) << "OHOSExternalTextureGL::UpdateTransform"; + float m[16] = { 0.0f }; + int32_t ret = OH_NativeImage_GetTransformMatrixV2(nativeImage_, m); + if (ret != 0) { + FML_DLOG(FATAL) << "Failed to get the transformation matrix of the texture image. err code:" << ret; + } + // transform ohos 4x4 matrix to skia 3x3 matrix + SkScalar matrix3[] = { + m[0], m[4], m[12], // + m[1], m[5], m[13], // + m[3], m[7], m[15], // + }; + transform.set9(matrix3); + SkMatrix inverted; + if (!transform.invert(&inverted)) { + FML_LOG(FATAL) << "Invalid SurfaceTexture transformation matrix"; + } + transform = inverted; } void OHOSExternalTextureGL::DispatchImage(ImageNative* image) { - FML_DLOG(INFO) << "OHOSExternalTextureGL::DispatchImage"; - lastImage_ = image; + FML_DLOG(INFO) << "OHOSExternalTextureGL::DispatchImage"; + lastImage_ = image; } void OHOSExternalTextureGL::setBackground(int32_t width, int32_t height) { - FML_DLOG(INFO) << "OHOSExternalTextureGL::setBackground"; - if (nativeWindow_ == nullptr) { - nativeWindow_ = OH_NativeImage_AcquireNativeWindow(nativeImage_); + FML_DLOG(INFO) << "OHOSExternalTextureGL::setBackground"; if (nativeWindow_ == nullptr) { - FML_DLOG(ERROR) << "Failed to obtain an **OHNativeWindow** instance"; - return; + nativeWindow_ = OH_NativeImage_AcquireNativeWindow(nativeImage_); + if (nativeWindow_ == nullptr) { + FML_DLOG(ERROR) << "Failed to obtain an **OHNativeWindow** instance"; + return; + } } - } - int code = SET_BUFFER_GEOMETRY; - int32_t ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow_, code, width, height); - if (ret != 0) { - FML_DLOG(ERROR) << "Failed to set the attributes of an **OHNativeWindow** instance. err code:" << ret; - return; - } + int code = SET_BUFFER_GEOMETRY; + int32_t ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow_, code, width, height); + if (ret != 0) { + FML_DLOG(ERROR) << "Failed to set the attributes of an **OHNativeWindow** instance. err code:" << ret; + return; + } - ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer_, &fenceFd); - if (ret != 0) { - FML_DLOG(ERROR) << " Failed to requests an **OHNativeWindowBuffer**. err code:" << ret; - return; - } + ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer_, &fenceFd); + if (ret != 0) { + FML_DLOG(ERROR) << " Failed to requests an **OHNativeWindowBuffer**. err code:" << ret; + return; + } - BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer_); - void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0); - if (mappedAddr == MAP_FAILED) { - FML_DLOG(FATAL) << "No successful return from mmap()"; - return; - } + BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer_); + void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0); + if (mappedAddr == MAP_FAILED) { + FML_DLOG(FATAL) << "No successful return from mmap()"; + return; + } - uint32_t* destAddr = static_cast(mappedAddr); - uint32_t value = 0xFFFFFFFF; + uint32_t* destAddr = static_cast(mappedAddr); + uint32_t value = 0xFFFFFFFF; - for(int32_t x = 0; x < handle->width; x++) { - for (int32_t y = 0; y < handle->height; y++) { - *destAddr++ = value; + for (int32_t x = 0; x < handle->width; x++) { + for (int32_t y = 0; y < handle->height; y++) { + *destAddr++ = value; + } } - } - // munmap after use - ret = munmap(mappedAddr, handle->size); - if (ret == -1) { - FML_DLOG(FATAL) << "Failed to unmap pages of memory"; - return; - } + // munmap after use + ret = munmap(mappedAddr, handle->size); + if (ret == -1) { + FML_DLOG(FATAL) << "Failed to unmap pages of memory"; + return; + } - Region region{nullptr, 0}; - ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer_, fenceFd, region); - if (ret != 0) { - FML_DLOG(FATAL) << "Failed to flush the buffer to the consumer. err code:" << ret; - } + Region region{nullptr, 0}; + ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer_, fenceFd, region); + if (ret != 0) { + FML_DLOG(FATAL) << "Failed to flush the buffer to the consumer. err code:" << ret; + } } void OHOSExternalTextureGL::HandlePixelMapBuffer() { - FML_DLOG(INFO) << "OHOSExternalTextureGL::HandlePixelMapBuffer"; - BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer_); - // get virAddr of bufferHandl by mmap sys interface - uint32_t stride = handle->stride; - FML_DLOG(INFO) << "OHOSExternalTextureGL stride:" << stride; - void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0); - if (mappedAddr == MAP_FAILED) { - FML_DLOG(FATAL) << "Failed to map pages of memory"; - return; - } + FML_DLOG(INFO) << "OHOSExternalTextureGL::HandlePixelMapBuffer"; + BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer_); + // get virAddr of bufferHandl by mmap sys interface + uint32_t stride = handle->stride; + FML_DLOG(INFO) << "OHOSExternalTextureGL stride:" << stride; + void *mappedAddr = mmap(handle->virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0); + if (mappedAddr == MAP_FAILED) { + FML_DLOG(FATAL) << "Failed to map pages of memory"; + return; + } - void *pixelAddr = nullptr; - int64_t ret = OH_PixelMap_AccessPixels(pixelMap_, &pixelAddr); - if (ret != IMAGE_RESULT_SUCCESS) { - FML_DLOG(FATAL) << "Failed to obtain the memory address of the PixelMap object and lock the memory. err code:" << ret; - return; - } + void *pixelAddr = nullptr; + int64_t ret = OH_PixelMap_AccessPixels(pixelMap_, &pixelAddr); + if (ret != IMAGE_RESULT_SUCCESS) { + FML_DLOG(FATAL) << "Failed to obtain the memory address of the PixelMap" + " object and lock the memory. err code:" << ret; + return; + } - uint32_t *pixel = static_cast(pixelAddr); - uint32_t *destAddr = static_cast(mappedAddr); - - FML_DLOG(INFO) << "OHOSExternalTextureGL pixelMapInfo w:" << pixelMapInfo.width - << " h:" << pixelMapInfo.height; - FML_DLOG(INFO) << "OHOSExternalTextureGL pixelMapInfo rowSize:" << pixelMapInfo.rowSize - << " format:" << pixelMapInfo.pixelFormat; - - // 复制图片纹理数据到内存中,需要处理DMA内存补齐相关的逻辑 - if (pixelMapInfo.width * PIXEL_SIZE != pixelMapInfo.rowSize) { - // 直接复制整块内存 - memcpy(destAddr, pixel, pixelMapInfo.height * pixelMapInfo.rowSize); - } else { - // 需要处理DMA内存补齐相关的逻辑 - for (uint32_t i = 0; i < pixelMapInfo.height; i++) { - memcpy(destAddr, pixel, pixelMapInfo.rowSize); - destAddr += stride / PIXEL_SIZE; - pixel += pixelMapInfo.width; + uint32_t *pixel = static_cast(pixelAddr); + uint32_t *destAddr = static_cast(mappedAddr); + + FML_DLOG(INFO) << "OHOSExternalTextureGL pixelMapInfo w:" << pixelMapInfo.width + << " h:" << pixelMapInfo.height; + FML_DLOG(INFO) << "OHOSExternalTextureGL pixelMapInfo rowSize:" << pixelMapInfo.rowSize + << " format:" << pixelMapInfo.pixelFormat; + + // 复制图片纹理数据到内存中,需要处理DMA内存补齐相关的逻辑 + if (pixelMapInfo.width * PIXEL_SIZE != pixelMapInfo.rowSize) { + // 直接复制整块内存 + memcpy(destAddr, pixel, pixelMapInfo.height * pixelMapInfo.rowSize); + } else { + // 需要处理DMA内存补齐相关的逻辑 + for (uint32_t i = 0; i < pixelMapInfo.height; i++) { + memcpy(destAddr, pixel, pixelMapInfo.rowSize); + destAddr += stride / PIXEL_SIZE; + pixel += pixelMapInfo.width; + } + } + OH_PixelMap_UnAccessPixels(pixelMap_); + // munmap after use + ret = munmap(mappedAddr, handle->size); + if (ret == -1) { + FML_DLOG(FATAL) << "Failed to unmap pages of memory"; + return; } - } - OH_PixelMap_UnAccessPixels(pixelMap_); - // munmap after use - ret = munmap(mappedAddr, handle->size); - if (ret == -1) { - FML_DLOG(FATAL) << "Failed to unmap pages of memory"; - return; - } } void OHOSExternalTextureGL::ProducePixelMapToNativeImage() { - FML_DLOG(INFO) << "OHOSExternalTextureGL::ProducePixelMapToNativeImage"; - if (state_ == AttachmentState::detached) { - FML_DLOG(ERROR) << "OHOSExternalTextureGL AttachmentState err"; - return; - } - int32_t ret = -1; - ret = OH_PixelMap_GetImageInfo(pixelMap_, &pixelMapInfo); - if (ret != 0) { - FML_DLOG(ERROR) << "OHOSExternalTextureGL OH_PixelMap_GetImageInfo err:" << ret; - } + FML_DLOG(INFO) << "OHOSExternalTextureGL::ProducePixelMapToNativeImage"; + if (state_ == AttachmentState::detached) { + FML_DLOG(ERROR) << "OHOSExternalTextureGL AttachmentState err"; + return; + } + int32_t ret = -1; + ret = OH_PixelMap_GetImageInfo(pixelMap_, &pixelMapInfo); + if (ret != 0) { + FML_DLOG(ERROR) << "OHOSExternalTextureGL OH_PixelMap_GetImageInfo err:" << ret; + } - int code = SET_BUFFER_GEOMETRY; - ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow_, code, pixelMapInfo.width, pixelMapInfo.height); - if (ret != 0) { - FML_DLOG(ERROR) << "OHOSExternalTextureGL OH_NativeWindow_NativeWindowHandleOpt err:" << ret; - } + int code = SET_BUFFER_GEOMETRY; + ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow_, code, pixelMapInfo.width, pixelMapInfo.height); + if (ret != 0) { + FML_DLOG(ERROR) << "OHOSExternalTextureGL OH_NativeWindow_NativeWindowHandleOpt err:" << ret; + } - if (buffer_ != nullptr) { - OH_NativeWindow_NativeWindowAbortBuffer(nativeWindow_, buffer_); - buffer_ = nullptr; - } - ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer_, &fenceFd); - if (ret != 0) { - FML_DLOG(ERROR) << "OHOSExternalTextureGL OH_NativeWindow_NativeWindowRequestBuffer err:" << ret; - } - HandlePixelMapBuffer(); - Region region{nullptr, 0}; - ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer_, fenceFd, region); - if (ret != 0) { - FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_NativeWindow_NativeWindowFlushBuffer err:"<< ret; - } + if (buffer_ != nullptr) { + OH_NativeWindow_NativeWindowAbortBuffer(nativeWindow_, buffer_); + buffer_ = nullptr; + } + ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer_, &fenceFd); + if (ret != 0) { + FML_DLOG(ERROR) << "OHOSExternalTextureGL OH_NativeWindow_NativeWindowRequestBuffer err:" << ret; + } + HandlePixelMapBuffer(); + Region region{nullptr, 0}; + ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer_, fenceFd, region); + if (ret != 0) { + FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_NativeWindow_NativeWindowFlushBuffer err:"<< ret; + } } EGLDisplay OHOSExternalTextureGL::GetPlatformEglDisplay(EGLenum platform, void *native_display, const EGLint *attrib_list) { - FML_DLOG(INFO) << "OHOSExternalTextureGL::GetPlatformEglDisplay"; - GetPlatformDisplayExt eglGetPlatformDisplayExt = NULL; - - if (!eglGetPlatformDisplayExt) { - const char* extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); - if (extensions && - (CheckEglExtension(extensions, EGL_EXT_PLATFORM_WAYLAND) || - CheckEglExtension(extensions, EGL_KHR_PLATFORM_WAYLAND))) { - eglGetPlatformDisplayExt = (GetPlatformDisplayExt)eglGetProcAddress(EGL_GET_PLATFORM_DISPLAY_EXT); + FML_DLOG(INFO) << "OHOSExternalTextureGL::GetPlatformEglDisplay"; + GetPlatformDisplayExt eglGetPlatformDisplayExt = NULL; + + if (!eglGetPlatformDisplayExt) { + const char* extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); + if (extensions && + (CheckEglExtension(extensions, EGL_EXT_PLATFORM_WAYLAND) || + CheckEglExtension(extensions, EGL_KHR_PLATFORM_WAYLAND))) { + eglGetPlatformDisplayExt = (GetPlatformDisplayExt)eglGetProcAddress(EGL_GET_PLATFORM_DISPLAY_EXT); + } } - } - if (eglGetPlatformDisplayExt) { - return eglGetPlatformDisplayExt(platform, native_display, attrib_list); - } + if (eglGetPlatformDisplayExt) { + return eglGetPlatformDisplayExt(platform, native_display, attrib_list); + } - return eglGetDisplay((EGLNativeDisplayType)native_display); + return eglGetDisplay((EGLNativeDisplayType)native_display); } bool OHOSExternalTextureGL::CheckEglExtension(const char *extensions, const char *extension) { - FML_DLOG(INFO) << "OHOSExternalTextureGL::CheckEglExtension"; - size_t extlen = strlen(extension); - const char *end = extensions + strlen(extensions); - while (extensions < end) { - size_t n = 0; - if (*extensions == CHARACTER_WHITESPACE) { - extensions++; - continue; - } - n = strcspn(extensions, CHARACTER_STRING_WHITESPACE); - if (n == extlen && strncmp(extension, extensions, n) == 0) { - return true; + FML_DLOG(INFO) << "OHOSExternalTextureGL::CheckEglExtension"; + size_t extlen = strlen(extension); + const char *end = extensions + strlen(extensions); + while (extensions < end) { + size_t n = 0; + if (*extensions == CHARACTER_WHITESPACE) { + extensions++; + continue; + } + n = strcspn(extensions, CHARACTER_STRING_WHITESPACE); + if (n == extlen && strncmp(extension, extensions, n) == 0) { + return true; + } + extensions += n; } - extensions += n; - } - return false; + return false; } void OHOSExternalTextureGL::DispatchPixelMap(NativePixelMap* pixelMap) { - FML_DLOG(INFO) << "OHOSExternalTextureGL::DispatchPixelMap"; - if (pixelMap != nullptr) { - pixelMap_ = pixelMap; - } + FML_DLOG(INFO) << "OHOSExternalTextureGL::DispatchPixelMap"; + if (pixelMap != nullptr) { + pixelMap_ = pixelMap; + } } } // namespace flutter \ No newline at end of file -- Gitee