Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/java2d/d3d/D3DContext.h
32288 views
/*1* Copyright (c) 2007, 2013, 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"32#include "sun_java2d_pipe_hw_AccelDeviceEventNotifier.h"3334#include "ShaderList.h"35#include "D3DPipeline.h"36#include "D3DMaskCache.h"37#include "D3DVertexCacher.h"38#include "D3DResourceManager.h"3940#include "j2d_md.h"4142typedef enum {43TILEFMT_UNKNOWN,44TILEFMT_1BYTE_ALPHA,45TILEFMT_3BYTE_RGB,46TILEFMT_3BYTE_BGR,47TILEFMT_4BYTE_ARGB_PRE,48} TileFormat;4950typedef enum {51CLIP_NONE,52CLIP_RECT,53CLIP_SHAPE,54} ClipType;5556// - State switching optimizations -----------------------------------5758/**59* The goal is to reduce device state switching as much as possible.60* This means: don't reset the texture if not needed, don't change61* the texture stage states unless necessary.62* For this we need to track the current device state. So each operation63* supplies its own operation type to BeginScene, which updates the state64* as necessary.65*66* Another optimization is to use a single vertex format for67* all primitives.68*69* See D3DContext::UpdateState() and D3DContext::BeginScene() for70* more information.71*/72#define STATE_CHANGE (0 << 0)73#define STATE_RENDEROP (1 << 0)74#define STATE_MASKOP (1 << 1)75#define STATE_GLYPHOP (1 << 2)76#define STATE_TEXTUREOP (1 << 3)77#define STATE_AAPGRAMOP (1 << 4)78#define STATE_OTHEROP (1 << 5)7980// The max. stage number we currently use (could not be81// larger than 7)82#define MAX_USED_TEXTURE_SAMPLER 18384// - Texture pixel format table -------------------------------------85#define TR_OPAQUE java_awt_Transparency_OPAQUE86#define TR_BITMASK java_awt_Transparency_BITMASK87#define TR_TRANSLUCENT java_awt_Transparency_TRANSLUCENT8889class D3DResource;90class D3DResourceManager;91class D3DMaskCache;92class D3DVertexCacher;93class D3DGlyphCache;9495// - D3DContext class -----------------------------------------------9697/**98* This class provides the following functionality:99* - holds the state of D3DContext java class (current pixel color,100* alpha compositing mode, extra alpha)101* - provides access to IDirect3DDevice9 interface (creation,102* disposal, exclusive access)103* - handles state changes of the direct3d device (transform,104* compositing mode, current texture)105* - provides means of creating textures, plain surfaces106* - holds a glyph cache texture for the associated device107* - implements primitives batching mechanism108*/109class D3DPIPELINE_API D3DContext {110public:111/**112* Releases the old device (if there was one) and all associated113* resources, re-creates, initializes and tests the new device.114*115* If the device doesn't pass the test, it's released.116*117* Used when the context is first created, and then after a118* display change event.119*120* Note that this method also does the necessary registry checks,121* and if the registry shows that we've crashed when attempting122* to initialize and test the device last time, it doesn't attempt123* to create/init/test the device.124*/125static126HRESULT CreateInstance(IDirect3D9 *pd3d9, UINT adapter, D3DContext **ppCtx);127// creates a new D3D windowed device with swap copy effect and default128// present interval129HRESULT InitContext();130// creates or resets a D3D device given the parameters131HRESULT ConfigureContext(D3DPRESENT_PARAMETERS *pNewParams);132// resets existing D3D device with the current presentation parameters133HRESULT ResetContext();134HRESULT CheckAndResetDevice();135136// saves the state of the D3D device in a state block, resets137// context's state to STATE_CHANGE138HRESULT SaveState();139// restores the state of the D3D device from existing state block,140// resets context's state to STATE_CHANGE141HRESULT RestoreState();142143void ReleaseContextResources();144void ReleaseDefPoolResources();145virtual ~D3DContext();146147// methods replicating java-level D3DContext objext148HRESULT SetAlphaComposite(jint rule, jfloat extraAlpha, jint flags);149HRESULT ResetComposite();150151/**152* Glyph cache-related methods153*/154HRESULT InitGrayscaleGlyphCache();155HRESULT InitLCDGlyphCache();156D3DGlyphCache* GetGrayscaleGlyphCache() { return pGrayscaleGlyphCache; }157D3DGlyphCache* GetLCDGlyphCache() { return pLCDGlyphCache; }158159D3DResourceManager *GetResourceManager() { return pResourceMgr; }160D3DMaskCache *GetMaskCache() { return pMaskCache; }161162HRESULT UploadTileToTexture(D3DResource *pTextureRes, void *pixels,163jint dstx, jint dsty,164jint srcx, jint srcy,165jint srcWidth, jint srcHeight,166jint srcStride,167TileFormat srcFormat,168// out: num of pixels in first and last169// columns, only counted for LCD glyph uploads170jint *pPixelsTouchedL = NULL,171jint *pPixelsTouchedR = NULL);172173// returns capabilities of the Direct3D device174D3DCAPS9 *GetDeviceCaps() { return &devCaps; }175// returns caps in terms of the D3DContext176int GetContextCaps() { return contextCaps; }177D3DPRESENT_PARAMETERS *GetPresentationParams() { return &curParams; }178179IDirect3DDevice9 *Get3DDevice() { return pd3dDevice; }180IDirect3D9 *Get3DObject() { return pd3dObject; }181182/**183* This method only sets the texture if it's not already set.184*/185HRESULT SetTexture(IDirect3DTexture9 *pTexture, DWORD dwSampler = 0);186187/**188* This method only updates the texture color state if it hasn't changed.189*/190HRESULT UpdateTextureColorState(DWORD dwState, DWORD dwSampler = 0);191192HRESULT SetRenderTarget(IDirect3DSurface9 *pSurface);193HRESULT SetTransform(jdouble m00, jdouble m10,194jdouble m01, jdouble m11,195jdouble m02, jdouble m12);196HRESULT ResetTransform();197198// clipping-related methods199HRESULT SetRectClip(int x1, int y1, int x2, int y2);200HRESULT BeginShapeClip();201HRESULT EndShapeClip();202HRESULT ResetClip();203ClipType GetClipType();204205/**206* Shader-related methods207*/208HRESULT EnableBasicGradientProgram(jint flags);209HRESULT EnableLinearGradientProgram(jint flags);210HRESULT EnableRadialGradientProgram(jint flags);211HRESULT EnableConvolveProgram(jint flags);212HRESULT EnableRescaleProgram(jint flags);213HRESULT EnableLookupProgram(jint flags);214HRESULT EnableLCDTextProgram();215HRESULT EnableAAParallelogramProgram();216HRESULT DisableAAParallelogramProgram();217218BOOL IsTextureFilteringSupported(D3DTEXTUREFILTERTYPE fType);219BOOL IsStretchRectFilteringSupported(D3DTEXTUREFILTERTYPE fType);220BOOL IsPow2TexturesOnly()221{ return devCaps.TextureCaps & D3DPTEXTURECAPS_POW2; };222BOOL IsSquareTexturesOnly()223{ return devCaps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY; }224BOOL IsHWRasterizer() { return bIsHWRasterizer; }225BOOL IsTextureFormatSupported(D3DFORMAT format, DWORD usage = 0);226BOOL IsDynamicTextureSupported()227{ return devCaps.Caps2 & D3DCAPS2_DYNAMICTEXTURES; }228// REMIND: for now for performance testing229// { return (getenv("J2D_D3D_USE_DYNAMIC_TEX") != NULL); }230BOOL IsImmediateIntervalSupported()231{ return devCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE;}232BOOL IsPixelShader20Supported()233{ return (devCaps.PixelShaderVersion >= D3DPS_VERSION(2,0)); }234BOOL IsGradientInstructionExtensionSupported()235{ return devCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS; }236BOOL IsPixelShader30Supported()237{ return (devCaps.PixelShaderVersion >= D3DPS_VERSION(3,0)); }238BOOL IsMultiTexturingSupported()239{ return (devCaps.MaxSimultaneousTextures > 1); }240BOOL IsAlphaRTSurfaceSupported();241BOOL IsAlphaRTTSupported();242BOOL IsOpaqueRTTSupported();243244jint GetPaintState() { return paintState; }245void SetPaintState(jint state) { this->paintState = state; }246BOOL IsIdentityTx() { return bIsIdentityTx; }247248HRESULT FlushVertexQueue();249D3DVertexCacher *pVCacher;250HRESULT UpdateState(jbyte newState);251252HRESULT Sync();253254// primitives batching-related methods255/**256* Calls devices's BeginScene if there weren't one already pending,257* sets the pending flag.258*/259HRESULT BeginScene(jbyte newState);260/**261* Flushes the vertex queue and does end scene if262* a BeginScene is pending263*/264HRESULT EndScene();265266/**267* Fields that track native-specific state.268*/269jint paintState;270jboolean useMask;271jfloat extraAlpha;272273/**274* Current operation state.275* See STATE_* macros above.276*/277jbyte opState;278279private:280281/**282* Glyph cache-related methods/fields...283*/284D3DGlyphCache *pGrayscaleGlyphCache;285D3DGlyphCache *pLCDGlyphCache;286287/**288* The handle to the LCD text pixel shader program.289*/290IDirect3DPixelShader9 *lcdTextProgram;291292/**293* The handle to the AA pixel and vertex shader programs.294*/295IDirect3DPixelShader9 *aaPgramProgram;296297IDirect3DPixelShader9 *CreateFragmentProgram(DWORD **shaders,298ShaderList *programs,299jint flags);300HRESULT EnableFragmentProgram(DWORD **shaders,301ShaderList *programList,302jint flags);303304// finds appropriate to the target surface depth format,305// creates the depth buffer and installs it onto the device306HRESULT InitDepthStencilBuffer(D3DSURFACE_DESC *pTargetDesc);307// returns true if the current depth buffer is compatible308// with the new target, and the dimensions fit, false otherwise309BOOL IsDepthStencilBufferOk(D3DSURFACE_DESC *pTargetDesc);310311D3DContext(IDirect3D9 *pd3dObject, UINT adapter);312HRESULT InitDevice(IDirect3DDevice9 *d3dDevice);313HRESULT InitContextCaps();314// updates the texture transform(s) used for better texel to pixel mapping315// for the passed in sampler;316// if -1 is passed as the sampler, texture transforms for317// samplers [0..MAX_USED_TEXTURE_SAMPLER] are updated318// REMIND: see the comment in the method implementation before enabling.319#undef UPDATE_TX320#ifdef UPDATE_TX321HRESULT UpdateTextureTransforms(DWORD dwSamplerToUpdate);322#endif // UPDATE_TX323IDirect3DDevice9 *pd3dDevice;324IDirect3D9 *pd3dObject;325326D3DResourceManager *pResourceMgr;327D3DMaskCache *pMaskCache;328329ShaderList convolvePrograms;330ShaderList rescalePrograms;331ShaderList lookupPrograms;332ShaderList basicGradPrograms;333ShaderList linearGradPrograms;334ShaderList radialGradPrograms;335336// array of the textures currently set to the device337IDirect3DTexture9 *lastTexture[MAX_USED_TEXTURE_SAMPLER+1];338339DWORD lastTextureColorState[MAX_USED_TEXTURE_SAMPLER+1];340341UINT adapterOrdinal;342D3DPRESENT_PARAMETERS curParams;343D3DCAPS9 devCaps;344int contextCaps;345BOOL bIsHWRasterizer;346347BOOL bIsIdentityTx;348349IDirect3DQuery9* pSyncQuery;350D3DResource* pSyncRTRes;351352IDirect3DStateBlock9* pStateBlock;353354/**355* Used to implement simple primitive batching.356* See BeginScene/EndScene/ForceEndScene.357*/358BOOL bBeginScenePending;359};360361// - Helper Macros ---------------------------------------------------362363#define D3DC_INIT_SHADER_LIST(list, max) \364do { \365(list).head = NULL; \366(list).maxItems = (max); \367(list).dispose = D3DContext_DisposeShader; \368} while (0)369370/**371* This constant determines the size of the shared tile texture used372* by a number of image rendering methods. For example, the blit tile texture373* will have dimensions with width D3DC_BLIT_TILE_SIZE and height374* D3DC_BLIT_TILE_SIZE (the tile will always be square).375*/376#define D3DC_BLIT_TILE_SIZE 256377378/**379* See BufferedContext.java for more on these flags...380*/381#define D3DC_NO_CONTEXT_FLAGS \382sun_java2d_pipe_BufferedContext_NO_CONTEXT_FLAGS383#define D3DC_SRC_IS_OPAQUE \384sun_java2d_pipe_BufferedContext_SRC_IS_OPAQUE385#define D3DC_USE_MASK \386sun_java2d_pipe_BufferedContext_USE_MASK387388#define CAPS_EMPTY \389sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_EMPTY390#define CAPS_RT_PLAIN_ALPHA \391sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_PLAIN_ALPHA392#define CAPS_RT_TEXTURE_ALPHA \393sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_TEXTURE_ALPHA394#define CAPS_RT_TEXTURE_OPAQUE \395sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_TEXTURE_OPAQUE396#define CAPS_MULTITEXTURE \397sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_MULTITEXTURE398#define CAPS_TEXNONPOW2 \399sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_TEXNONPOW2400#define CAPS_TEXNONSQUARE \401sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_TEXNONSQUARE402#define CAPS_LCD_SHADER \403sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_LCD_SHADER404#define CAPS_BIOP_SHADER \405sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_BIOP_SHADER406#define CAPS_AA_SHADER \407sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_AA_SHADER408#define CAPS_DEVICE_OK \409sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_DEVICE_OK410#define CAPS_PS20 \411sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_PS20412#define CAPS_PS30 \413sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_PS30414415#define DEVICE_RESET \416sun_java2d_pipe_hw_AccelDeviceEventNotifier_DEVICE_RESET417#define DEVICE_DISPOSED \418sun_java2d_pipe_hw_AccelDeviceEventNotifier_DEVICE_DISPOSED419420#endif // D3DCONTEXT_H421422423