From 28b771a265a88d6fd4a4f8ef9ebdd48aac0b2ed1 Mon Sep 17 00:00:00 2001
From: zhangyanchuan <zhangyanchuan@huawei.com>
Date: Sat, 21 Oct 2023 22:23:45 +0800
Subject: [PATCH] =?UTF-8?q?[TicketNo:]AR000JLVJT=20[Description:]=E5=90=88?=
 =?UTF-8?q?=E5=85=A5OHOS=5FBUGFIX=5FCRASH=20[Binary=20Source:]NA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/cef/libcef/renderer/frame_impl.cc         |   3 +-
 .../external_begin_frame_source_ohos.cc       |  14 +++
 .../viz/service/surfaces/surface.cc           |   6 +-
 src/content/public/common/content_switches.cc |   1 +
 src/content/public/common/content_switches.h  |   1 +
 src/ui/gl/gl_surface_egl_ohos.cc              | 101 +++++++-----------
 src/ui/gl/gl_surface_egl_ohos.h               |  40 +++----
 7 files changed, 80 insertions(+), 86 deletions(-)

diff --git a/src/cef/libcef/renderer/frame_impl.cc b/src/cef/libcef/renderer/frame_impl.cc
index e530b8d97c166..b741bde2a57df 100644
--- a/src/cef/libcef/renderer/frame_impl.cc
+++ b/src/cef/libcef/renderer/frame_impl.cc
@@ -710,7 +710,8 @@ void CefFrameImpl::OnDisconnect(DisconnectReason reason) {
                          ConnectReason::RETRY));
     } else {
       // Trigger a crash in official builds.
-      LOG(FATAL) << GetDebugString() << " connection retry failed";
+      LOG(ERROR) << "Connection retry failure for frame "
+                 << frame_util::GetFrameDebugString(frame_id_);
     }
   }
 }
diff --git a/src/components/viz/service/frame_sinks/external_begin_frame_source_ohos.cc b/src/components/viz/service/frame_sinks/external_begin_frame_source_ohos.cc
index 5f4d4dd0c7985..95300149fdfca 100644
--- a/src/components/viz/service/frame_sinks/external_begin_frame_source_ohos.cc
+++ b/src/components/viz/service/frame_sinks/external_begin_frame_source_ohos.cc
@@ -19,10 +19,22 @@ class ExternalBeginFrameSourceOHOS::VSyncUserData {
  public:
   VSyncUserData(const scoped_refptr<base::SingleThreadTaskRunner>& current,
                 viz::ExternalBeginFrameSourceOHOS* weakPtr)
+#if defined(OHOS_BUGFIX_CRASH)
+      : current_(current), weakPtr_(weakPtr){
+          LOG(INFO) << "VSyncUserData constructor!!!";
+      }
+#else
       : current_(current), weakPtr_(weakPtr) {}
+#endif // defined(OHOS_BUGFIX_CRASH)
   VSyncUserData(const VSyncUserData&) = delete;
   VSyncUserData& operator=(const VSyncUserData&) = delete;
+#if defined(OHOS_BUGFIX_CRASH)
+ ~VSyncUserData() {
+    LOG(INFO) << "VSyncUserData destructor!!!";
+  }
+#else
   ~VSyncUserData() = default;
+#endif // defined(OHOS_BUGFIX_CRASH)
 
   const scoped_refptr<base::SingleThreadTaskRunner>& current_;
   base::WeakPtrFactory<viz::ExternalBeginFrameSourceOHOS> weakPtr_;
@@ -37,9 +49,11 @@ ExternalBeginFrameSourceOHOS::ExternalBeginFrameSourceOHOS(uint32_t restart_id)
       vsync_adapter_(OhosAdapterHelper::GetInstance().GetVSyncAdapter()) {
   TRACE_EVENT0("viz",
                "ExternalBeginFrameSourceOHOS::ExternalBeginFrameSourceOHOS");
+  LOG(INFO) << "ExternalBeginFrameSourceOHOS constructor!!!";
 }
 
 ExternalBeginFrameSourceOHOS::~ExternalBeginFrameSourceOHOS() {
+  LOG(INFO) << "ExternalBeginFrameSourceOHOS destructor!!!";
   SetEnabled(false);
 }
 
diff --git a/src/components/viz/service/surfaces/surface.cc b/src/components/viz/service/surfaces/surface.cc
index 1e477e9618d85..bb8fdf075d016 100644
--- a/src/components/viz/service/surfaces/surface.cc
+++ b/src/components/viz/service/surfaces/surface.cc
@@ -544,7 +544,11 @@ void Surface::ActivateFrame(FrameData frame_data) {
 
   // Save root pass copy requests.
   std::vector<std::unique_ptr<CopyOutputRequest>> old_copy_requests;
-  if (active_frame_data_) {
+#if defined(OHOS_BUGFIX_CRASH)
+  if (active_frame_data_ && !active_frame_data_->frame.render_pass_list.empty()) {
+#else
+  if (active_frame_data_ ) {
+#endif // defined(OHOS_BUGFIX_CRASH)
     std::swap(old_copy_requests,
               active_frame_data_->frame.render_pass_list.back()->copy_requests);
   }
diff --git a/src/content/public/common/content_switches.cc b/src/content/public/common/content_switches.cc
index 3d3a578c108b6..bd4426a97ab9b 100644
--- a/src/content/public/common/content_switches.cc
+++ b/src/content/public/common/content_switches.cc
@@ -1028,6 +1028,7 @@ const char kOhosHapPath[] = "user-hap-path";
 const char kOhosAppApiVersion[] = "user-api-version";
 const char kEnableMultiRendererProcess[] = "enable-multi-renderer-process";
 const char kForBrowser[] = "for-browser";
+const char kOhosHanceSurface[] = "ohos-enhance-surface";
 const char kOhosEnableDrDc[] = "ohos-enable-drdc";
 #ifdef OHOS_NETWORK_LOAD
 const char kOhosCustomScheme[] = "ohos-custom-scheme";
diff --git a/src/content/public/common/content_switches.h b/src/content/public/common/content_switches.h
index ed07de5d93ff5..404dee41f9384 100644
--- a/src/content/public/common/content_switches.h
+++ b/src/content/public/common/content_switches.h
@@ -279,6 +279,7 @@ CONTENT_EXPORT extern const char kOhosAppApiVersion[];
 CONTENT_EXPORT extern const char kEnableMultiRendererProcess[];
 CONTENT_EXPORT extern const char kForBrowser[];
 CONTENT_EXPORT extern const char kOhosEnableDrDc[];
+CONTENT_EXPORT extern const char kOhosHanceSurface[];
 #ifdef OHOS_NETWORK_LOAD
 CONTENT_EXPORT extern const char kOhosCustomScheme[];
 #endif
diff --git a/src/ui/gl/gl_surface_egl_ohos.cc b/src/ui/gl/gl_surface_egl_ohos.cc
index 0ad71ad828e79..2dc70fe8318c0 100644
--- a/src/ui/gl/gl_surface_egl_ohos.cc
+++ b/src/ui/gl/gl_surface_egl_ohos.cc
@@ -3,20 +3,10 @@
 // found in the LICENSE file.
 
 #include "ui/gl/gl_surface_egl_ohos.h"
+#include "content/public/common/content_switches.h"
 
-#include <sys/time.h>
-#include <iomanip>
-#include <thread>
 #include "nweb_native_window_tracker.h"
-
-namespace {
-static int64_t GetNowTime() {
-  struct timeval start = {};
-  gettimeofday(&start, nullptr);
-  constexpr uint32_t kSecToUsec = 1000 * 1000;
-  return static_cast<int64_t>(start.tv_sec) * kSecToUsec + start.tv_usec;
-}
-}  // namespace
+#include "ohos_adapter_helper.h"
 
 namespace gl {
 
@@ -24,67 +14,56 @@ scoped_refptr<gl::NativeViewGLSurfaceEGLOhos>
 NativeViewGLSurfaceEGLOhos::CreateNativeViewGLSurfaceEGLOhos(
     GLDisplay* display,
     gfx::AcceleratedWidget widget) {
-  void* window = NWebNativeWindowTracker::Instance().GetNativeWindow(widget);
-  return scoped_refptr<NativeViewGLSurfaceEGLOhos>(
-      new NativeViewGLSurfaceEGLOhos(display->GetAs<GLDisplayEGL>(),
-                                     EGLNativeWindowType(window)));
+  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
+  if (command_line->HasSwitch(::switches::kOhosHanceSurface)) {
+    LOG(INFO) << "CreateNativeViewGLSurfaceEGLOhos:: enhance surface";
+    WindowsSurfaceInfo* surfaceInfo = static_cast<WindowsSurfaceInfo*>(
+        NWebNativeWindowTracker::Instance().GetNativeWindow(widget));
+    if (surfaceInfo != nullptr) {
+      LOG(INFO) << "clear surface from NWEB";
+      eglDestroySurface(surfaceInfo->display, surfaceInfo->surface);
+      eglDestroyContext(surfaceInfo->display, surfaceInfo->context);
+
+      return scoped_refptr<NativeViewGLSurfaceEGLOhos>(
+          new NativeViewGLSurfaceEGLOhos(display->GetAs<gl::GLDisplayEGL>(),
+              reinterpret_cast<EGLNativeWindowType>(surfaceInfo->window)));
+    }
+  } else {
+    LOG(INFO) << "CreateNativeViewGLSurfaceEGLOhos:: normal surface";
+    void* window = NWebNativeWindowTracker::Instance().GetNativeWindow(widget);
+    return scoped_refptr<NativeViewGLSurfaceEGLOhos>(
+        new NativeViewGLSurfaceEGLOhos(display->GetAs<gl::GLDisplayEGL>(),
+            reinterpret_cast<EGLNativeWindowType>(window)));
+  }
+  return nullptr;
 }
 
 NativeViewGLSurfaceEGLOhos::NativeViewGLSurfaceEGLOhos(
     GLDisplayEGL* display,
     EGLNativeWindowType window)
-    : NativeViewGLSurfaceEGL(display, window, nullptr) {
-  frame_counter_.reset(new FrameCounter);
-  if (frame_counter_) {
-    frame_counter_->Start();
-  }
-}
+    : NativeViewGLSurfaceEGL(display, window, nullptr), window_(window) {}
 
 gfx::SwapResult NativeViewGLSurfaceEGLOhos::SwapBuffers(
     PresentationCallback callback,
     gfx::FrameData data) {
-  auto result = NativeViewGLSurfaceEGL::SwapBuffers(std::move(callback), data);
-  if (frame_counter_) {
-    frame_counter_->Update(result == gfx::SwapResult::SWAP_FAILED ? false
-                                                                  : true);
-  }
+  auto result = NativeViewGLSurfaceEGL::SwapBuffers(std::move(callback),data);
   return result;
 }
 
-void NativeViewGLSurfaceEGLOhos::FrameCounter::Start() {
-  std::weak_ptr<FrameCounter> frame_counter_weak(shared_from_this());
-  std::thread frame_stat_thread([frame_counter_weak]() {
-    while (!frame_counter_weak.expired()) {
-      {
-        auto frame_counter = frame_counter_weak.lock();
-        std::unique_lock<std::mutex> lk(frame_counter->frame_stat_mtx_);
-        int64_t curr_time = GetNowTime();
-        if (frame_counter->last_time_ == 0) {
-          frame_counter->local_render_count_ = 0;
-          frame_counter->last_time_ = curr_time;
-          continue;
-        }
-
-        frame_counter->local_render_count_ = 0;
-        frame_counter->last_time_ = curr_time;
-      }
-      // during sleep(), frame_counter_weak should not be
-      // promote to shared_ptr, to avoid add refcount
-      constexpr int kFrameStatUpdateDur = 1;
-      sleep(kFrameStatUpdateDur);
-    }
-  });
-  frame_stat_thread.detach();
-}
-
-void NativeViewGLSurfaceEGLOhos::FrameCounter::Update(bool flag) {
-  std::unique_lock<std::mutex> lk(frame_stat_mtx_);
-  if (flag) {
-    frame_render_count_++;
-    local_render_count_++;
-  } else {
-    frame_miss_count_++;
+bool NativeViewGLSurfaceEGLOhos::Resize(const gfx::Size& size,
+                                        float scale_factor,
+                                        const gfx::ColorSpace& color_space,
+                                        bool has_alpha) {
+  int32_t ret =
+      OHOS::NWeb::OhosAdapterHelper::GetInstance()
+          .GetWindowAdapterInstance()
+          .NativeWindowHandleOpt(reinterpret_cast<void*>(window_),
+                                 OHOS::NWeb::WindowAdapter::SET_BUFFER_GEOMETRY,
+                                 size.width(), size.height());
+  if (ret != OHOS::NWeb::GSErrorCode::GSERROR_OK) {
+    LOG(ERROR) << "fail to set NativeWindowHandleOpt, ret=" << ret;
+    return false;
   }
+  return true;
 }
-
 }  // namespace gl
diff --git a/src/ui/gl/gl_surface_egl_ohos.h b/src/ui/gl/gl_surface_egl_ohos.h
index 90f59e44cc71c..1294abbb926b7 100644
--- a/src/ui/gl/gl_surface_egl_ohos.h
+++ b/src/ui/gl/gl_surface_egl_ohos.h
@@ -5,43 +5,37 @@
 #ifndef UI_GL_GL_SURFACE_EGL_OHOS_H_
 #define UI_GL_GL_SURFACE_EGL_OHOS_H_
 
-#include <condition_variable>
-#include <memory>
-#include <mutex>
-#include "ui/gl/gl_display.h"
+#include "ui/gl/gl_bindings.h"
 #include "ui/gl/gl_export.h"
 #include "ui/gl/gl_surface_egl.h"
+#include "ui/gl/gl_display.h"
 
 namespace gl {
+typedef struct WindowsSurfaceInfoTag {
+  void* window;
+  EGLDisplay display;
+  EGLContext context;
+  EGLSurface surface;
+} WindowsSurfaceInfo;
 
 class GL_EXPORT NativeViewGLSurfaceEGLOhos : public NativeViewGLSurfaceEGL {
  public:
-  explicit NativeViewGLSurfaceEGLOhos(GLDisplayEGL* display,
-                                      EGLNativeWindowType window);
+  explicit NativeViewGLSurfaceEGLOhos(GLDisplayEGL* display, 
+                                NativeWindowType window);
   NativeViewGLSurfaceEGLOhos(const NativeViewGLSurfaceEGLOhos& other) = delete;
   NativeViewGLSurfaceEGLOhos& operator=(const NativeViewGLSurfaceEGLOhos& rhs) =
       delete;
 
   static scoped_refptr<gl::NativeViewGLSurfaceEGLOhos>
-  CreateNativeViewGLSurfaceEGLOhos(GLDisplay* display,
-                                   gfx::AcceleratedWidget window);
-  gfx::SwapResult SwapBuffers(PresentationCallback callback,
-                              gfx::FrameData data) override;
+  CreateNativeViewGLSurfaceEGLOhos(GLDisplay* display, gfx::AcceleratedWidget window);
+  gfx::SwapResult SwapBuffers(PresentationCallback callback, gfx::FrameData data) override;
+  bool Resize(const gfx::Size& size,
+              float scale_factor,
+              const gfx::ColorSpace& color_space,
+              bool has_alpha) override;
 
  private:
-  class FrameCounter : public std::enable_shared_from_this<FrameCounter> {
-   public:
-    void Start();
-    void Update(bool flag);
-
-   private:
-    std::mutex frame_stat_mtx_;
-    int64_t frame_render_count_ = 0L;
-    int64_t frame_miss_count_ = 0L;
-    int64_t local_render_count_ = 0L;
-    int64_t last_time_ = 0L;
-  };
-  std::shared_ptr<FrameCounter> frame_counter_;
+  EGLNativeWindowType window_;
 };
 
 }  // namespace gl
-- 
2.25.1