Path: blob/master/src/java.desktop/windows/native/libawt/java2d/d3d/D3DContext.h
41159 views
/*1* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#ifndef D3DCONTEXT_H26#define D3DCONTEXT_H2728#include "java_awt_Transparency.h"29#include "sun_java2d_pipe_BufferedContext.h"30#include "sun_java2d_d3d_D3DContext_D3DContextCaps.h"31#include "sun_java2d_d3d_D3DSurfaceData.h"3233#include "ShaderList.h"34#include "D3DPipeline.h"35#include "D3DMaskCache.h"36#include "D3DVertexCacher.h"37#include "D3DResourceManager.h"3839#include "j2d_md.h"4041typedef enum {42TILEFMT_UNKNOWN,43TILEFMT_1BYTE_ALPHA,44TILEFMT_3BYTE_RGB,45TILEFMT_3BYTE_BGR,46TILEFMT_4BYTE_ARGB_PRE,47} TileFormat;4849typedef enum {50CLIP_NONE,51CLIP_RECT,52CLIP_SHAPE,53} ClipType;5455// - State switching optimizations -----------------------------------5657/**58* The goal is to reduce device state switching as much as possible.59* This means: don't reset the texture if not needed, don't change60* the texture stage states unless necessary.61* For this we need to track the current device state. So each operation62* supplies its own operation type to BeginScene, which updates the state63* as necessary.64*65* Another optimization is to use a single vertex format for66* all primitives.67*68* See D3DContext::UpdateState() and D3DContext::BeginScene() for69* more information.70*/71#define STATE_CHANGE (0 << 0)72#define STATE_RENDEROP (1 << 0)73#define STATE_MASKOP (1 << 1)74#define STATE_GLYPHOP (1 << 2)75#define STATE_TEXTUREOP (1 << 3)76#define STATE_AAPGRAMOP (1 << 4)77#define STATE_OTHEROP (1 << 5)7879// The max. stage number we currently use (could not be80// larger than 7)81#define MAX_USED_TEXTURE_SAMPLER 18283// - Texture pixel format table -------------------------------------84#define TR_OPAQUE java_awt_Transparency_OPAQUE85#define TR_BITMASK java_awt_Transparency_BITMASK86#define TR_TRANSLUCENT java_awt_Transparency_TRANSLUCENT8788class D3DResource;89class D3DResourceManager;90class D3DMaskCache;91class D3DVertexCacher;92class D3DGlyphCache;9394// - D3DContext class -----------------------------------------------9596/**97* This class provides the following functionality:98* - holds the state of D3DContext java class (current pixel color,99* alpha compositing mode, extra alpha)100* - provides access to IDirect3DDevice9 interface (creation,101* disposal, exclusive access)102* - handles state changes of the direct3d device (transform,103* compositing mode, current texture)104* - provides means of creating textures, plain surfaces105* - holds a glyph cache texture for the associated device106* - implements primitives batching mechanism107*/108class D3DPIPELINE_API D3DContext {109public:110/**111* Releases the old device (if there was one) and all associated112* resources, re-creates, initializes and tests the new device.113*114* If the device doesn't pass the test, it's released.115*116* Used when the context is first created, and then after a117* display change event.118*119* Note that this method also does the necessary registry checks,120* and if the registry shows that we've crashed when attempting121* to initialize and test the device last time, it doesn't attempt122* to create/init/test the device.123*/124static125HRESULT CreateInstance(IDirect3D9 *pd3d9, UINT adapter, D3DContext **ppCtx);126// creates a new D3D windowed device with swap copy effect and default127// present interval128HRESULT InitContext();129// creates or resets a D3D device given the parameters130HRESULT ConfigureContext(D3DPRESENT_PARAMETERS *pNewParams);131// resets existing D3D device with the current presentation parameters132HRESULT ResetContext();133HRESULT CheckAndResetDevice();134135void ReleaseContextResources();136void ReleaseDefPoolResources();137virtual ~D3DContext();138139// methods replicating java-level D3DContext objext140HRESULT SetAlphaComposite(jint rule, jfloat extraAlpha, jint flags);141HRESULT ResetComposite();142143/**144* Glyph cache-related methods145*/146HRESULT InitGrayscaleGlyphCache();147HRESULT InitLCDGlyphCache();148D3DGlyphCache* GetGrayscaleGlyphCache() { return pGrayscaleGlyphCache; }149D3DGlyphCache* GetLCDGlyphCache() { return pLCDGlyphCache; }150151D3DResourceManager *GetResourceManager() { return pResourceMgr; }152D3DMaskCache *GetMaskCache() { return pMaskCache; }153154HRESULT UploadTileToTexture(D3DResource *pTextureRes, void *pixels,155jint dstx, jint dsty,156jint srcx, jint srcy,157jint srcWidth, jint srcHeight,158jint srcStride,159TileFormat srcFormat,160// out: num of pixels in first and last161// columns, only counted for LCD glyph uploads162jint *pPixelsTouchedL = NULL,163jint *pPixelsTouchedR = NULL);164165// returns capabilities of the Direct3D device166D3DCAPS9 *GetDeviceCaps() { return &devCaps; }167// returns caps in terms of the D3DContext168int GetContextCaps() { return contextCaps; }169D3DPRESENT_PARAMETERS *GetPresentationParams() { return &curParams; }170171IDirect3DDevice9 *Get3DDevice() { return pd3dDevice; }172IDirect3D9 *Get3DObject() { return pd3dObject; }173174/**175* This method only sets the texture if it's not already set.176*/177HRESULT SetTexture(IDirect3DTexture9 *pTexture, DWORD dwSampler = 0);178179/**180* This method only updates the texture color state if it hasn't changed.181*/182HRESULT UpdateTextureColorState(DWORD dwState, DWORD dwSampler = 0);183184HRESULT SetRenderTarget(IDirect3DSurface9 *pSurface);185HRESULT SetTransform(jdouble m00, jdouble m10,186jdouble m01, jdouble m11,187jdouble m02, jdouble m12);188HRESULT ResetTransform();189190// clipping-related methods191HRESULT SetRectClip(int x1, int y1, int x2, int y2);192HRESULT BeginShapeClip();193HRESULT EndShapeClip();194HRESULT ResetClip();195ClipType GetClipType();196197/**198* Shader-related methods199*/200HRESULT EnableBasicGradientProgram(jint flags);201HRESULT EnableLinearGradientProgram(jint flags);202HRESULT EnableRadialGradientProgram(jint flags);203HRESULT EnableConvolveProgram(jint flags);204HRESULT EnableRescaleProgram(jint flags);205HRESULT EnableLookupProgram(jint flags);206HRESULT EnableLCDTextProgram();207HRESULT EnableAAParallelogramProgram();208HRESULT DisableAAParallelogramProgram();209210BOOL IsTextureFilteringSupported(D3DTEXTUREFILTERTYPE fType);211BOOL IsStretchRectFilteringSupported(D3DTEXTUREFILTERTYPE fType);212BOOL IsPow2TexturesOnly()213{ return devCaps.TextureCaps & D3DPTEXTURECAPS_POW2; };214BOOL IsSquareTexturesOnly()215{ return devCaps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY; }216BOOL IsHWRasterizer() { return bIsHWRasterizer; }217BOOL IsTextureFormatSupported(D3DFORMAT format, DWORD usage = 0);218BOOL IsDynamicTextureSupported()219{ return devCaps.Caps2 & D3DCAPS2_DYNAMICTEXTURES; }220// REMIND: for now for performance testing221// { return (getenv("J2D_D3D_USE_DYNAMIC_TEX") != NULL); }222BOOL IsImmediateIntervalSupported()223{ return devCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE;}224BOOL IsPixelShader20Supported()225{ return (devCaps.PixelShaderVersion >= D3DPS_VERSION(2,0)); }226BOOL IsGradientInstructionExtensionSupported()227{ return devCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS; }228BOOL IsPixelShader30Supported()229{ return (devCaps.PixelShaderVersion >= D3DPS_VERSION(3,0)); }230BOOL IsMultiTexturingSupported()231{ return (devCaps.MaxSimultaneousTextures > 1); }232BOOL IsAlphaRTSurfaceSupported();233BOOL IsAlphaRTTSupported();234BOOL IsOpaqueRTTSupported();235236jint GetPaintState() { return paintState; }237void SetPaintState(jint state) { this->paintState = state; }238BOOL IsIdentityTx() { return bIsIdentityTx; }239240HRESULT FlushVertexQueue();241D3DVertexCacher *pVCacher;242HRESULT UpdateState(jbyte newState);243244HRESULT Sync();245246// primitives batching-related methods247/**248* Calls devices's BeginScene if there weren't one already pending,249* sets the pending flag.250*/251HRESULT BeginScene(jbyte newState);252/**253* Flushes the vertex queue and does end scene if254* a BeginScene is pending255*/256HRESULT EndScene();257258/**259* Fields that track native-specific state.260*/261jint paintState;262jboolean useMask;263jfloat extraAlpha;264265/**266* Current operation state.267* See STATE_* macros above.268*/269jbyte opState;270271private:272273/**274* Glyph cache-related methods/fields...275*/276D3DGlyphCache *pGrayscaleGlyphCache;277D3DGlyphCache *pLCDGlyphCache;278279/**280* The handle to the LCD text pixel shader program.281*/282IDirect3DPixelShader9 *lcdTextProgram;283284/**285* The handle to the AA pixel and vertex shader programs.286*/287IDirect3DPixelShader9 *aaPgramProgram;288289IDirect3DPixelShader9 *CreateFragmentProgram(DWORD **shaders,290ShaderList *programs,291jint flags);292HRESULT EnableFragmentProgram(DWORD **shaders,293ShaderList *programList,294jint flags);295296// finds appropriate to the target surface depth format,297// creates the depth buffer and installs it onto the device298HRESULT InitDepthStencilBuffer(D3DSURFACE_DESC *pTargetDesc);299// returns true if the current depth buffer is compatible300// with the new target, and the dimensions fit, false otherwise301BOOL IsDepthStencilBufferOk(D3DSURFACE_DESC *pTargetDesc);302303D3DContext(IDirect3D9 *pd3dObject, UINT adapter);304HRESULT InitDevice(IDirect3DDevice9 *d3dDevice);305HRESULT InitContextCaps();306// updates the texture transform(s) used for better texel to pixel mapping307// for the passed in sampler;308// if -1 is passed as the sampler, texture transforms for309// samplers [0..MAX_USED_TEXTURE_SAMPLER] are updated310// REMIND: see the comment in the method implementation before enabling.311#undef UPDATE_TX312#ifdef UPDATE_TX313HRESULT UpdateTextureTransforms(DWORD dwSamplerToUpdate);314#endif // UPDATE_TX315IDirect3DDevice9 *pd3dDevice;316IDirect3D9 *pd3dObject;317318D3DResourceManager *pResourceMgr;319D3DMaskCache *pMaskCache;320321ShaderList convolvePrograms;322ShaderList rescalePrograms;323ShaderList lookupPrograms;324ShaderList basicGradPrograms;325ShaderList linearGradPrograms;326ShaderList radialGradPrograms;327328// array of the textures currently set to the device329IDirect3DTexture9 *lastTexture[MAX_USED_TEXTURE_SAMPLER+1];330331DWORD lastTextureColorState[MAX_USED_TEXTURE_SAMPLER+1];332333UINT adapterOrdinal;334D3DPRESENT_PARAMETERS curParams;335D3DCAPS9 devCaps;336int contextCaps;337BOOL bIsHWRasterizer;338339BOOL bIsIdentityTx;340341IDirect3DQuery9* pSyncQuery;342D3DResource* pSyncRTRes;343344IDirect3DStateBlock9* pStateBlock;345346/**347* Used to implement simple primitive batching.348* See BeginScene/EndScene/ForceEndScene.349*/350BOOL bBeginScenePending;351};352353// - Helper Macros ---------------------------------------------------354355#define D3DC_INIT_SHADER_LIST(list, max) \356do { \357(list).head = NULL; \358(list).maxItems = (max); \359(list).dispose = D3DContext_DisposeShader; \360} while (0)361362/**363* This constant determines the size of the shared tile texture used364* by a number of image rendering methods. For example, the blit tile texture365* will have dimensions with width D3DC_BLIT_TILE_SIZE and height366* D3DC_BLIT_TILE_SIZE (the tile will always be square).367*/368#define D3DC_BLIT_TILE_SIZE 256369370/**371* See BufferedContext.java for more on these flags...372*/373#define D3DC_NO_CONTEXT_FLAGS \374sun_java2d_pipe_BufferedContext_NO_CONTEXT_FLAGS375#define D3DC_SRC_IS_OPAQUE \376sun_java2d_pipe_BufferedContext_SRC_IS_OPAQUE377#define D3DC_USE_MASK \378sun_java2d_pipe_BufferedContext_USE_MASK379380#define CAPS_EMPTY \381sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_EMPTY382#define CAPS_RT_PLAIN_ALPHA \383sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_PLAIN_ALPHA384#define CAPS_RT_TEXTURE_ALPHA \385sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_TEXTURE_ALPHA386#define CAPS_RT_TEXTURE_OPAQUE \387sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_TEXTURE_OPAQUE388#define CAPS_MULTITEXTURE \389sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_MULTITEXTURE390#define CAPS_TEXNONPOW2 \391sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_TEXNONPOW2392#define CAPS_TEXNONSQUARE \393sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_TEXNONSQUARE394#define CAPS_LCD_SHADER \395sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_LCD_SHADER396#define CAPS_BIOP_SHADER \397sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_BIOP_SHADER398#define CAPS_AA_SHADER \399sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_AA_SHADER400#define CAPS_DEVICE_OK \401sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_DEVICE_OK402#define CAPS_PS20 \403sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_PS20404#define CAPS_PS30 \405sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_PS30406407#endif // D3DCONTEXT_H408409410