1 /*
   2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 #pragma once
  26 
  27 #include "D3DPipeline.h"
  28 #include "D3DContext.h"
  29 #include "awt_Toolkit.h"
  30 
  31 typedef class D3DPipelineManager *LPD3DPIPELINEMANAGER;
  32 
  33 typedef struct D3DAdapter
  34 {
  35     D3DContext *pd3dContext;
  36     DWORD state;
  37     HWND fsFocusWindow;
  38 } D3DAdapter;
  39 
  40 class D3DPIPELINE_API D3DPipelineManager
  41 {
  42     friend class D3DInitializer;
  43 private:
  44     // creates and initializes instance of D3DPipelineManager, may return NULL
  45     static D3DPipelineManager* CreateInstance(void);
  46 
  47     // deletes the single instance of the manager
  48     static void DeleteInstance();
  49 
  50 public:
  51     // returns the single instance of the manager, may return NULL
  52     static D3DPipelineManager* GetInstance(void);
  53 
  54     HRESULT GetD3DContext(UINT adapterOrdinal, D3DContext **ppd3dContext);
  55 
  56     HRESULT HandleLostDevices();
  57     // Checks if adapters were added or removed, or if the order had changed
  58     // (which may happen with primary display is changed). If that's the case
  59     // releases current adapters and d3d9 instance, reinitializes the pipeline.
  60     // @param *monHds list of monitor handles retrieved from GDI
  61     // @param monNum number of gdi monitors
  62     static
  63     HRESULT HandleAdaptersChange(HMONITOR *monHds, UINT monNum);
  64     // returns depth stencil buffer format matching adapterFormat and render target
  65     // format for the device specified by adapterOrdinal/devType
  66     D3DFORMAT GetMatchingDepthStencilFormat(UINT adapterOrdinal,
  67                                             D3DFORMAT adapterFormat,
  68                                             D3DFORMAT renderTargetFormat);
  69 
  70     HWND GetCurrentFocusWindow();
  71     // returns previous fs window
  72     HWND SetFSFocusWindow(UINT, HWND);
  73 
  74     LPDIRECT3D9 GetD3DObject() { return pd3d9; }
  75     D3DDEVTYPE GetDeviceType() { return devType; }
  76 
  77     // returns the d3d adapter ordinal given GDI screen number:
  78     // these may differ depending on which display is primary
  79     UINT GetAdapterOrdinalForScreen(jint gdiScreen);
  80 
  81     // notifies adapter event listeners by calling
  82     // AccelDeviceEventNotifier.eventOccured()
  83     static
  84     void NotifyAdapterEventListeners(UINT adapter, jint eventType);
  85 
  86 private:
  87     D3DPipelineManager(void);
  88    ~D3DPipelineManager(void);
  89 
  90     // Creates a Direct3D9 object and initializes adapters.
  91     HRESULT InitD3D(void);
  92     // Releases adapters, Direct3D9 object and the d3d9 library.
  93     HRESULT ReleaseD3D();
  94 
  95     // selects the device type based on user input and available
  96     // device types
  97     D3DDEVTYPE SelectDeviceType();
  98 
  99     // creates array of adapters (releases the old one first)
 100     HRESULT InitAdapters();
 101     // releases each adapter's context, and then releases the array
 102     HRESULT ReleaseAdapters();
 103 
 104     HWND    CreateDefaultFocusWindow();
 105     // returns S_OK if the adapter is capable of running the Direct3D
 106     // pipeline
 107     HRESULT D3DEnabledOnAdapter(UINT Adapter);
 108     // returns adapterOrdinal given a HMONITOR handle
 109     UINT    GetAdapterOrdinalByHmon(HMONITOR hMon);
 110     HRESULT CheckAdaptersInfo();
 111     HRESULT CheckDeviceCaps(UINT Adapter);
 112     // Check the OS, succeeds if the OS is XP or newer client-class OS
 113 static HRESULT CheckOSVersion();
 114     // used to check attached adapters using GDI against known bad hw database
 115     // prior to the instantiation of the pipeline manager
 116 static HRESULT GDICheckForBadHardware();
 117     // given VendorId, DeviceId and driver version, checks against a database
 118     // of known bad hardware/driver combinations.
 119     // If the driver version is not known MAX_VERSION can be used
 120     // which is guaranteed to satisfy the check
 121 static HRESULT CheckForBadHardware(DWORD vId, DWORD dId, LONGLONG version);
 122 
 123 private:
 124 
 125     // current adapter count
 126     UINT adapterCount;
 127     // Pointer to Direct3D9 Object mainained by the pipeline manager
 128     LPDIRECT3D9 pd3d9;
 129     // d3d9.dll lib
 130     HINSTANCE hLibD3D9;
 131 
 132     int currentFSFocusAdapter;
 133     HWND defaultFocusWindow;
 134 
 135     D3DDEVTYPE devType;
 136 
 137     D3DAdapter *pAdapters;
 138     // instance of this object
 139     static LPD3DPIPELINEMANAGER pMgr;
 140 };
 141 
 142 #define OS_UNDEFINED    (0 << 0)
 143 #define OS_VISTA        (1 << 0)
 144 #define OS_WINSERV_2008 (1 << 1)
 145 #define OS_WINXP        (1 << 2)
 146 #define OS_WINXP_64     (1 << 3)
 147 #define OS_WINSERV_2003 (1 << 4)
 148 #define OS_WINDOWS7     (1 << 5)
 149 #define OS_WINSERV_2008R2 (1 << 6)
 150 #define OS_ALL (OS_VISTA|OS_WINSERV_2008|OS_WINXP|OS_WINXP_64|OS_WINSERV_2003|\
 151                 OS_WINDOWS7|OS_WINSERV_2008R2)
 152 #define OS_UNKNOWN      (~OS_ALL)
 153 BOOL D3DPPLM_OsVersionMatches(USHORT osInfo);
 154 
 155 
 156 class D3DInitializer : public AwtToolkit::PreloadAction {
 157 private:
 158     D3DInitializer();
 159     ~D3DInitializer();
 160 
 161 protected:
 162     // PreloadAction overrides
 163     virtual void InitImpl();
 164     virtual void CleanImpl(bool reInit);
 165 
 166 public:
 167     static D3DInitializer& GetInstance() { return theInstance; }
 168 
 169 private:
 170     // single instance
 171     static D3DInitializer theInstance;
 172 
 173     // adapter initializer class
 174     class D3DAdapterInitializer : public AwtToolkit::PreloadAction {
 175     public:
 176         void setAdapter(UINT adapter) { this->adapter = adapter; }
 177     protected:
 178         // PreloadAction overrides
 179         virtual void InitImpl();
 180         virtual void CleanImpl(bool reInit);
 181     private:
 182         UINT adapter;
 183     };
 184 
 185     // the flag indicates success of COM initialization
 186     bool bComInitialized;
 187     D3DAdapterInitializer *pAdapterIniters;
 188 
 189 };
 190