1 /* 2 * Copyright (c) 2007, 2013, 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 26 #ifndef D3DCONTEXT_H 27 #define D3DCONTEXT_H 28 29 #include "java_awt_Transparency.h" 30 #include "sun_java2d_pipe_BufferedContext.h" 31 #include "sun_java2d_d3d_D3DContext_D3DContextCaps.h" 32 #include "sun_java2d_d3d_D3DSurfaceData.h" 33 #include "sun_java2d_pipe_hw_AccelDeviceEventNotifier.h" 34 35 #include "ShaderList.h" 36 #include "D3DPipeline.h" 37 #include "D3DMaskCache.h" 38 #include "D3DVertexCacher.h" 39 #include "D3DResourceManager.h" 40 41 #include "j2d_md.h" 42 43 typedef enum { 44 TILEFMT_UNKNOWN, 45 TILEFMT_1BYTE_ALPHA, 46 TILEFMT_3BYTE_RGB, 47 TILEFMT_3BYTE_BGR, 48 TILEFMT_4BYTE_ARGB_PRE, 49 } TileFormat; 50 51 typedef enum { 52 CLIP_NONE, 53 CLIP_RECT, 54 CLIP_SHAPE, 55 } ClipType; 56 57 // - State switching optimizations ----------------------------------- 58 59 /** 60 * The goal is to reduce device state switching as much as possible. 61 * This means: don't reset the texture if not needed, don't change 62 * the texture stage states unless necessary. 63 * For this we need to track the current device state. So each operation 64 * supplies its own operation type to BeginScene, which updates the state 65 * as necessary. 66 * 67 * Another optimization is to use a single vertex format for 68 * all primitives. 69 * 70 * See D3DContext::UpdateState() and D3DContext::BeginScene() for 71 * more information. 72 */ 73 #define STATE_CHANGE (0 << 0) 74 #define STATE_RENDEROP (1 << 0) 75 #define STATE_MASKOP (1 << 1) 76 #define STATE_GLYPHOP (1 << 2) 77 #define STATE_TEXTUREOP (1 << 3) 78 #define STATE_AAPGRAMOP (1 << 4) 79 #define STATE_OTHEROP (1 << 5) 80 81 // The max. stage number we currently use (could not be 82 // larger than 7) 83 #define MAX_USED_TEXTURE_SAMPLER 1 84 85 // - Texture pixel format table ------------------------------------- 86 #define TR_OPAQUE java_awt_Transparency_OPAQUE 87 #define TR_BITMASK java_awt_Transparency_BITMASK 88 #define TR_TRANSLUCENT java_awt_Transparency_TRANSLUCENT 89 90 class D3DResource; 91 class D3DResourceManager; 92 class D3DMaskCache; 93 class D3DVertexCacher; 94 class D3DGlyphCache; 95 96 // - D3DContext class ----------------------------------------------- 97 98 /** 99 * This class provides the following functionality: 100 * - holds the state of D3DContext java class (current pixel color, 101 * alpha compositing mode, extra alpha) 102 * - provides access to IDirect3DDevice9 interface (creation, 103 * disposal, exclusive access) 104 * - handles state changes of the direct3d device (transform, 105 * compositing mode, current texture) 106 * - provides means of creating textures, plain surfaces 107 * - holds a glyph cache texture for the associated device 108 * - implements primitives batching mechanism 109 */ 110 class D3DPIPELINE_API D3DContext { 111 public: 112 /** 113 * Releases the old device (if there was one) and all associated 114 * resources, re-creates, initializes and tests the new device. 115 * 116 * If the device doesn't pass the test, it's released. 117 * 118 * Used when the context is first created, and then after a 119 * display change event. 120 * 121 * Note that this method also does the necessary registry checks, 122 * and if the registry shows that we've crashed when attempting 123 * to initialize and test the device last time, it doesn't attempt 124 * to create/init/test the device. 125 */ 126 static 127 HRESULT CreateInstance(IDirect3D9 *pd3d9, UINT adapter, D3DContext **ppCtx); 128 // creates a new D3D windowed device with swap copy effect and default 129 // present interval 130 HRESULT InitContext(); 131 // creates or resets a D3D device given the parameters 132 HRESULT ConfigureContext(D3DPRESENT_PARAMETERS *pNewParams); 133 // resets existing D3D device with the current presentation parameters 134 HRESULT ResetContext(); 135 HRESULT CheckAndResetDevice(); 136 137 // saves the state of the D3D device in a state block, resets 138 // context's state to STATE_CHANGE 139 HRESULT SaveState(); 140 // restores the state of the D3D device from existing state block, 141 // resets context's state to STATE_CHANGE 142 HRESULT RestoreState(); 143 144 void ReleaseContextResources(); 145 void ReleaseDefPoolResources(); 146 virtual ~D3DContext(); 147 148 // methods replicating java-level D3DContext objext 149 HRESULT SetAlphaComposite(jint rule, jfloat extraAlpha, jint flags); 150 HRESULT ResetComposite(); 151 152 /** 153 * Glyph cache-related methods 154 */ 155 HRESULT InitGrayscaleGlyphCache(); 156 HRESULT InitLCDGlyphCache(); 157 D3DGlyphCache* GetGrayscaleGlyphCache() { return pGrayscaleGlyphCache; } 158 D3DGlyphCache* GetLCDGlyphCache() { return pLCDGlyphCache; } 159 160 D3DResourceManager *GetResourceManager() { return pResourceMgr; } 161 D3DMaskCache *GetMaskCache() { return pMaskCache; } 162 163 HRESULT UploadTileToTexture(D3DResource *pTextureRes, void *pixels, 164 jint dstx, jint dsty, 165 jint srcx, jint srcy, 166 jint srcWidth, jint srcHeight, 167 jint srcStride, 168 TileFormat srcFormat, 169 // out: num of pixels in first and last 170 // columns, only counted for LCD glyph uploads 171 jint *pPixelsTouchedL = NULL, 172 jint *pPixelsTouchedR = NULL); 173 174 // returns capabilities of the Direct3D device 175 D3DCAPS9 *GetDeviceCaps() { return &devCaps; } 176 // returns caps in terms of the D3DContext 177 int GetContextCaps() { return contextCaps; } 178 D3DPRESENT_PARAMETERS *GetPresentationParams() { return &curParams; } 179 180 IDirect3DDevice9 *Get3DDevice() { return pd3dDevice; } 181 IDirect3D9 *Get3DObject() { return pd3dObject; } 182 183 /** 184 * This method only sets the texture if it's not already set. 185 */ 186 HRESULT SetTexture(IDirect3DTexture9 *pTexture, DWORD dwSampler = 0); 187 188 /** 189 * This method only updates the texture color state if it hasn't changed. 190 */ 191 HRESULT UpdateTextureColorState(DWORD dwState, DWORD dwSampler = 0); 192 193 HRESULT SetRenderTarget(IDirect3DSurface9 *pSurface); 194 HRESULT SetTransform(jdouble m00, jdouble m10, 195 jdouble m01, jdouble m11, 196 jdouble m02, jdouble m12); 197 HRESULT ResetTransform(); 198 199 // clipping-related methods 200 HRESULT SetRectClip(int x1, int y1, int x2, int y2); 201 HRESULT BeginShapeClip(); 202 HRESULT EndShapeClip(); 203 HRESULT ResetClip(); 204 ClipType GetClipType(); 205 206 /** 207 * Shader-related methods 208 */ 209 HRESULT EnableBasicGradientProgram(jint flags); 210 HRESULT EnableLinearGradientProgram(jint flags); 211 HRESULT EnableRadialGradientProgram(jint flags); 212 HRESULT EnableConvolveProgram(jint flags); 213 HRESULT EnableRescaleProgram(jint flags); 214 HRESULT EnableLookupProgram(jint flags); 215 HRESULT EnableLCDTextProgram(); 216 HRESULT EnableAAParallelogramProgram(); 217 HRESULT DisableAAParallelogramProgram(); 218 219 BOOL IsTextureFilteringSupported(D3DTEXTUREFILTERTYPE fType); 220 BOOL IsStretchRectFilteringSupported(D3DTEXTUREFILTERTYPE fType); 221 BOOL IsPow2TexturesOnly() 222 { return devCaps.TextureCaps & D3DPTEXTURECAPS_POW2; }; 223 BOOL IsSquareTexturesOnly() 224 { return devCaps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY; } 225 BOOL IsHWRasterizer() { return bIsHWRasterizer; } 226 BOOL IsTextureFormatSupported(D3DFORMAT format, DWORD usage = 0); 227 BOOL IsDynamicTextureSupported() 228 { return devCaps.Caps2 & D3DCAPS2_DYNAMICTEXTURES; } 229 // REMIND: for now for performance testing 230 // { return (getenv("J2D_D3D_USE_DYNAMIC_TEX") != NULL); } 231 BOOL IsImmediateIntervalSupported() 232 { return devCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE;} 233 BOOL IsPixelShader20Supported() 234 { return (devCaps.PixelShaderVersion >= D3DPS_VERSION(2,0)); } 235 BOOL IsGradientInstructionExtensionSupported() 236 { return devCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS; } 237 BOOL IsPixelShader30Supported() 238 { return (devCaps.PixelShaderVersion >= D3DPS_VERSION(3,0)); } 239 BOOL IsMultiTexturingSupported() 240 { return (devCaps.MaxSimultaneousTextures > 1); } 241 BOOL IsAlphaRTSurfaceSupported(); 242 BOOL IsAlphaRTTSupported(); 243 BOOL IsOpaqueRTTSupported(); 244 245 jint GetPaintState() { return paintState; } 246 void SetPaintState(jint state) { this->paintState = state; } 247 BOOL IsIdentityTx() { return bIsIdentityTx; } 248 249 HRESULT FlushVertexQueue(); 250 D3DVertexCacher *pVCacher; 251 HRESULT UpdateState(jbyte newState); 252 253 HRESULT Sync(); 254 255 // primitives batching-related methods 256 /** 257 * Calls devices's BeginScene if there weren't one already pending, 258 * sets the pending flag. 259 */ 260 HRESULT BeginScene(jbyte newState); 261 /** 262 * Flushes the vertex queue and does end scene if 263 * a BeginScene is pending 264 */ 265 HRESULT EndScene(); 266 UINT getAdapterOrdinal() { return adapterOrdinal; } 267 268 /** 269 * Fields that track native-specific state. 270 */ 271 jint paintState; 272 jboolean useMask; 273 jfloat extraAlpha; 274 275 /** 276 * Current operation state. 277 * See STATE_* macros above. 278 */ 279 jbyte opState; 280 281 private: 282 283 /** 284 * Glyph cache-related methods/fields... 285 */ 286 D3DGlyphCache *pGrayscaleGlyphCache; 287 D3DGlyphCache *pLCDGlyphCache; 288 289 /** 290 * The handle to the LCD text pixel shader program. 291 */ 292 IDirect3DPixelShader9 *lcdTextProgram; 293 294 /** 295 * The handle to the AA pixel and vertex shader programs. 296 */ 297 IDirect3DPixelShader9 *aaPgramProgram; 298 299 IDirect3DPixelShader9 *CreateFragmentProgram(DWORD **shaders, 300 ShaderList *programs, 301 jint flags); 302 HRESULT EnableFragmentProgram(DWORD **shaders, 303 ShaderList *programList, 304 jint flags); 305 306 // finds appropriate to the target surface depth format, 307 // creates the depth buffer and installs it onto the device 308 HRESULT InitDepthStencilBuffer(D3DSURFACE_DESC *pTargetDesc); 309 // returns true if the current depth buffer is compatible 310 // with the new target, and the dimensions fit, false otherwise 311 BOOL IsDepthStencilBufferOk(D3DSURFACE_DESC *pTargetDesc); 312 313 D3DContext(IDirect3D9 *pd3dObject, UINT adapter); 314 HRESULT InitDevice(IDirect3DDevice9 *d3dDevice); 315 HRESULT InitContextCaps(); 316 // updates the texture transform(s) used for better texel to pixel mapping 317 // for the passed in sampler; 318 // if -1 is passed as the sampler, texture transforms for 319 // samplers [0..MAX_USED_TEXTURE_SAMPLER] are updated 320 // REMIND: see the comment in the method implementation before enabling. 321 #undef UPDATE_TX 322 #ifdef UPDATE_TX 323 HRESULT UpdateTextureTransforms(DWORD dwSamplerToUpdate); 324 #endif // UPDATE_TX 325 IDirect3DDevice9 *pd3dDevice; 326 IDirect3D9 *pd3dObject; 327 328 D3DResourceManager *pResourceMgr; 329 D3DMaskCache *pMaskCache; 330 331 ShaderList convolvePrograms; 332 ShaderList rescalePrograms; 333 ShaderList lookupPrograms; 334 ShaderList basicGradPrograms; 335 ShaderList linearGradPrograms; 336 ShaderList radialGradPrograms; 337 338 // array of the textures currently set to the device 339 IDirect3DTexture9 *lastTexture[MAX_USED_TEXTURE_SAMPLER+1]; 340 341 DWORD lastTextureColorState[MAX_USED_TEXTURE_SAMPLER+1]; 342 343 UINT adapterOrdinal; 344 D3DPRESENT_PARAMETERS curParams; 345 D3DCAPS9 devCaps; 346 int contextCaps; 347 BOOL bIsHWRasterizer; 348 349 BOOL bIsIdentityTx; 350 351 IDirect3DQuery9* pSyncQuery; 352 D3DResource* pSyncRTRes; 353 354 IDirect3DStateBlock9* pStateBlock; 355 356 /** 357 * Used to implement simple primitive batching. 358 * See BeginScene/EndScene/ForceEndScene. 359 */ 360 BOOL bBeginScenePending; 361 }; 362 363 // - Helper Macros --------------------------------------------------- 364 365 #define D3DC_INIT_SHADER_LIST(list, max) \ 366 do { \ 367 (list).head = NULL; \ 368 (list).maxItems = (max); \ 369 (list).dispose = D3DContext_DisposeShader; \ 370 } while (0) 371 372 /** 373 * This constant determines the size of the shared tile texture used 374 * by a number of image rendering methods. For example, the blit tile texture 375 * will have dimensions with width D3DC_BLIT_TILE_SIZE and height 376 * D3DC_BLIT_TILE_SIZE (the tile will always be square). 377 */ 378 #define D3DC_BLIT_TILE_SIZE 256 379 380 /** 381 * See BufferedContext.java for more on these flags... 382 */ 383 #define D3DC_NO_CONTEXT_FLAGS \ 384 sun_java2d_pipe_BufferedContext_NO_CONTEXT_FLAGS 385 #define D3DC_SRC_IS_OPAQUE \ 386 sun_java2d_pipe_BufferedContext_SRC_IS_OPAQUE 387 #define D3DC_USE_MASK \ 388 sun_java2d_pipe_BufferedContext_USE_MASK 389 390 #define CAPS_EMPTY \ 391 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_EMPTY 392 #define CAPS_RT_PLAIN_ALPHA \ 393 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_PLAIN_ALPHA 394 #define CAPS_RT_TEXTURE_ALPHA \ 395 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_TEXTURE_ALPHA 396 #define CAPS_RT_TEXTURE_OPAQUE \ 397 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_TEXTURE_OPAQUE 398 #define CAPS_MULTITEXTURE \ 399 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_MULTITEXTURE 400 #define CAPS_TEXNONPOW2 \ 401 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_TEXNONPOW2 402 #define CAPS_TEXNONSQUARE \ 403 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_TEXNONSQUARE 404 #define CAPS_LCD_SHADER \ 405 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_LCD_SHADER 406 #define CAPS_BIOP_SHADER \ 407 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_BIOP_SHADER 408 #define CAPS_AA_SHADER \ 409 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_AA_SHADER 410 #define CAPS_DEVICE_OK \ 411 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_DEVICE_OK 412 #define CAPS_PS20 \ 413 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_PS20 414 #define CAPS_PS30 \ 415 sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_PS30 416 417 #define DEVICE_RESET \ 418 sun_java2d_pipe_hw_AccelDeviceEventNotifier_DEVICE_RESET 419 #define DEVICE_DISPOSED \ 420 sun_java2d_pipe_hw_AccelDeviceEventNotifier_DEVICE_DISPOSED 421 422 #endif // D3DCONTEXT_H