changeset 3726:d97f7f568e97

6780496: Javaw process taking up 80-90 percent of CPU time! Summary: The transparency effects get enabled on showing, and disabled on hiding a window Reviewed-by: art, dcherepanov
author anthony
date Thu, 27 Aug 2009 16:42:55 +0400
parents 68832406c9f3
children 43b25dfd1129
files jdk/src/windows/native/sun/windows/awt_Dialog.cpp jdk/src/windows/native/sun/windows/awt_Frame.cpp jdk/src/windows/native/sun/windows/awt_Window.cpp jdk/src/windows/native/sun/windows/awt_Window.h
diffstat 4 files changed, 74 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/windows/native/sun/windows/awt_Dialog.cpp	Thu Aug 20 11:20:21 2009 -0700
+++ b/jdk/src/windows/native/sun/windows/awt_Dialog.cpp	Thu Aug 27 16:42:55 2009 +0400
@@ -364,6 +364,7 @@
     if (locationByPlatform) {
          moveToDefaultLocation();
     }
+    EnableTranslucency(TRUE);
     if (IsFocusableWindow() && (IsAutoRequestFocus() || IsFocusedWindowModalBlocker())) {
         ::ShowWindow(GetHWnd(), SW_SHOW);
     } else {
--- a/jdk/src/windows/native/sun/windows/awt_Frame.cpp	Thu Aug 20 11:20:21 2009 -0700
+++ b/jdk/src/windows/native/sun/windows/awt_Frame.cpp	Thu Aug 27 16:42:55 2009 +0400
@@ -690,6 +690,8 @@
     if (locationByPlatform) {
          moveToDefaultLocation();
     }
+    EnableTranslucency(TRUE);
+
     BOOL autoRequestFocus = IsAutoRequestFocus();
 
     if (m_iconic) {
--- a/jdk/src/windows/native/sun/windows/awt_Window.cpp	Thu Aug 20 11:20:21 2009 -0700
+++ b/jdk/src/windows/native/sun/windows/awt_Window.cpp	Thu Aug 27 16:42:55 2009 +0400
@@ -218,12 +218,7 @@
     if (warningString != NULL) {
         delete [] warningString;
     }
-    ::EnterCriticalSection(&contentBitmapCS);
-    if (hContentBitmap != NULL) {
-        ::DeleteObject(hContentBitmap);
-        hContentBitmap = NULL;
-    }
-    ::LeaveCriticalSection(&contentBitmapCS);
+    DeleteContentBitmap();
     ::DeleteCriticalSection(&contentBitmapCS);
 }
 
@@ -372,6 +367,10 @@
         }
     }
 
+    if (wp->flags & SWP_HIDEWINDOW) {
+        EnableTranslucency(FALSE);
+    }
+
     return mrDoDefault;
 }
 
@@ -1130,6 +1129,8 @@
          moveToDefaultLocation();
     }
 
+    EnableTranslucency(TRUE);
+
     // The following block exists to support Menu/Tooltip animation for
     // Swing programs in a way which avoids introducing any new public api into
     // AWT or Swing.
@@ -2494,27 +2495,73 @@
     }
 }
 
-void AwtWindow::SetTranslucency(BYTE opacity, BOOL opaque)
+// Deletes the hContentBitmap if it is non-null
+void AwtWindow::DeleteContentBitmap()
 {
-    BYTE old_opacity = getOpacity();
-    BOOL old_opaque = isOpaque();
+    ::EnterCriticalSection(&contentBitmapCS);
+    if (hContentBitmap != NULL) {
+        ::DeleteObject(hContentBitmap);
+        hContentBitmap = NULL;
+    }
+    ::LeaveCriticalSection(&contentBitmapCS);
+}
+
+// The effects are enabled only upon showing the window.
+// See 6780496 for details.
+void AwtWindow::EnableTranslucency(BOOL enable)
+{
+    if (enable) {
+        SetTranslucency(getOpacity(), isOpaque(), FALSE, TRUE);
+    } else {
+        SetTranslucency(0xFF, TRUE, FALSE);
+    }
+}
+
+/**
+ * Sets the translucency effects.
+ *
+ * This method is used to:
+ *
+ * 1. Apply the translucency effects upon showing the window
+ *    (setValues == FALSE, useDefaultForOldValues == TRUE);
+ * 2. Turn off the effects upon hiding the window
+ *    (setValues == FALSE, useDefaultForOldValues == FALSE);
+ * 3. Set the effects per user's request
+ *    (setValues == TRUE, useDefaultForOldValues == FALSE);
+ *
+ * In case #3 the effects may or may not be applied immediately depending on
+ * the current visibility status of the window.
+ *
+ * The setValues argument indicates if we need to preserve the passed values
+ * in local fields for further use.
+ * The useDefaultForOldValues argument indicates whether we should consider
+ * the window as if it has not any effects applied at the moment.
+ */
+void AwtWindow::SetTranslucency(BYTE opacity, BOOL opaque, BOOL setValues,
+        BOOL useDefaultForOldValues)
+{
+    BYTE old_opacity = useDefaultForOldValues ? 0xFF : getOpacity();
+    BOOL old_opaque = useDefaultForOldValues ? TRUE : isOpaque();
 
     if (opacity == old_opacity && opaque == old_opaque) {
         return;
     }
 
-    setOpacity(opacity);
-    setOpaque(opaque);
+    if (setValues) {
+       m_opacity = opacity;
+       m_opaque = opaque;
+    }
+
+    // If we're invisible and are storing the values, return
+    // Otherwise, apply the effects immediately
+    if (!IsVisible() && setValues) {
+        return;
+    }
 
     HWND hwnd = GetHWnd();
 
     if (opaque != old_opaque) {
-        ::EnterCriticalSection(&contentBitmapCS);
-        if (hContentBitmap != NULL) {
-            ::DeleteObject(hContentBitmap);
-            hContentBitmap = NULL;
-        }
-        ::LeaveCriticalSection(&contentBitmapCS);
+        DeleteContentBitmap();
     }
 
     if (opaque && opacity == 0xff) {
@@ -2634,9 +2681,7 @@
     }
 
     ::EnterCriticalSection(&contentBitmapCS);
-    if (hContentBitmap != NULL) {
-        ::DeleteObject(hContentBitmap);
-    }
+    DeleteContentBitmap();
     hContentBitmap = hBitmap;
     contentWidth = width;
     contentHeight = height;
--- a/jdk/src/windows/native/sun/windows/awt_Window.h	Thu Aug 20 11:20:21 2009 -0700
+++ b/jdk/src/windows/native/sun/windows/awt_Window.h	Thu Aug 27 16:42:55 2009 +0400
@@ -262,32 +262,29 @@
                                        // from its hierarchy when shown. Currently applied to instances of
                                        // javax/swing/Popup$HeavyWeightWindow class.
 
+    // SetTranslucency() is the setter for the following two fields
     BYTE m_opacity;         // The opacity level. == 0xff by default (when opacity mode is disabled)
     BOOL m_opaque;          // Whether the window uses the perpixel translucency (false), or not (true).
 
     inline BYTE getOpacity() {
         return m_opacity;
     }
-    inline void setOpacity(BYTE opacity) {
-        m_opacity = opacity;
-    }
 
     inline BOOL isOpaque() {
         return m_opaque;
     }
-    inline void setOpaque(BOOL opaque) {
-        m_opaque = opaque;
-    }
 
     CRITICAL_SECTION contentBitmapCS;
     HBITMAP hContentBitmap;
     UINT contentWidth;
     UINT contentHeight;
 
-    void SetTranslucency(BYTE opacity, BOOL opaque);
+    void SetTranslucency(BYTE opacity, BOOL opaque, BOOL setValues = TRUE,
+            BOOL useDefaultForOldValues = FALSE);
     void UpdateWindow(int width, int height, HBITMAP hBitmap);
     void UpdateWindowImpl(int width, int height, HBITMAP hBitmap);
     void RedrawWindow();
+    void DeleteContentBitmap();
 
     static UINT untrustedWindowsCounter;
 
@@ -352,6 +349,8 @@
 
     UINT currentWmSizeState;
 
+    void EnableTranslucency(BOOL enable);
+
 private:
     int m_screenNum;