0N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#ifndef D3DCONTEXT_H
0N/A#define D3DCONTEXT_H
0N/A
0N/A#include "java_awt_Transparency.h"
430N/A#include "sun_java2d_pipe_BufferedContext.h"
0N/A#include "sun_java2d_d3d_D3DContext.h"
430N/A#include "sun_java2d_d3d_D3DContext_D3DContextCaps.h"
430N/A#include "sun_java2d_d3d_D3DSurfaceData.h"
430N/A#include "sun_java2d_pipe_hw_AccelDeviceEventNotifier.h"
430N/A
430N/A#include "ShaderList.h"
430N/A#include "D3DPipeline.h"
430N/A#include "D3DMaskCache.h"
430N/A#include "D3DVertexCacher.h"
430N/A#include "D3DResourceManager.h"
430N/A
0N/A#include "j2d_md.h"
0N/A
430N/Atypedef enum {
430N/A TILEFMT_UNKNOWN,
430N/A TILEFMT_1BYTE_ALPHA,
430N/A TILEFMT_3BYTE_RGB,
430N/A TILEFMT_3BYTE_BGR,
430N/A TILEFMT_4BYTE_ARGB_PRE,
430N/A} TileFormat;
430N/A
430N/Atypedef enum {
430N/A CLIP_NONE,
430N/A CLIP_RECT,
430N/A CLIP_SHAPE,
430N/A} ClipType;
0N/A
0N/A// - State switching optimizations -----------------------------------
0N/A
0N/A/**
0N/A * The goal is to reduce device state switching as much as possible.
0N/A * This means: don't reset the texture if not needed, don't change
0N/A * the texture stage states unless necessary.
0N/A * For this we need to track the current device state. So each operation
0N/A * supplies its own operation type to BeginScene, which updates the state
0N/A * as necessary.
0N/A *
0N/A * Another optimization is to use a single vertex format for
0N/A * all primitives.
0N/A *
0N/A * See D3DContext::UpdateState() and D3DContext::BeginScene() for
0N/A * more information.
0N/A */
430N/A#define STATE_CHANGE (0 << 0)
430N/A#define STATE_RENDEROP (1 << 0)
430N/A#define STATE_MASKOP (1 << 1)
430N/A#define STATE_GLYPHOP (1 << 2)
430N/A#define STATE_TEXTUREOP (1 << 3)
430N/A#define STATE_AAPGRAMOP (1 << 4)
430N/A#define STATE_OTHEROP (1 << 5)
0N/A
0N/A// The max. stage number we currently use (could not be
0N/A// larger than 7)
430N/A#define MAX_USED_TEXTURE_SAMPLER 1
0N/A
0N/A// - Texture pixel format table -------------------------------------
0N/A#define TR_OPAQUE java_awt_Transparency_OPAQUE
0N/A#define TR_BITMASK java_awt_Transparency_BITMASK
0N/A#define TR_TRANSLUCENT java_awt_Transparency_TRANSLUCENT
0N/A
430N/Aclass D3DResource;
430N/Aclass D3DResourceManager;
430N/Aclass D3DMaskCache;
430N/Aclass D3DVertexCacher;
430N/Aclass D3DGlyphCache;
0N/A
0N/A// - D3DContext class -----------------------------------------------
0N/A
0N/A/**
0N/A * This class provides the following functionality:
0N/A * - holds the state of D3DContext java class (current pixel color,
0N/A * alpha compositing mode, extra alpha)
430N/A * - provides access to IDirect3DDevice9 interface (creation,
0N/A * disposal, exclusive access)
0N/A * - handles state changes of the direct3d device (transform,
0N/A * compositing mode, current texture)
0N/A * - provides means of creating textures, plain surfaces
0N/A * - holds a glyph cache texture for the associated device
0N/A * - implements primitives batching mechanism
0N/A */
430N/Aclass D3DPIPELINE_API D3DContext {
0N/Apublic:
0N/A /**
0N/A * Releases the old device (if there was one) and all associated
0N/A * resources, re-creates, initializes and tests the new device.
0N/A *
0N/A * If the device doesn't pass the test, it's released.
0N/A *
0N/A * Used when the context is first created, and then after a
0N/A * display change event.
0N/A *
0N/A * Note that this method also does the necessary registry checks,
0N/A * and if the registry shows that we've crashed when attempting
0N/A * to initialize and test the device last time, it doesn't attempt
0N/A * to create/init/test the device.
0N/A */
430N/A static
430N/A HRESULT CreateInstance(IDirect3D9 *pd3d9, UINT adapter, D3DContext **ppCtx);
430N/A // creates a new D3D windowed device with swap copy effect and default
430N/A // present interval
430N/A HRESULT InitContext();
430N/A // creates or resets a D3D device given the parameters
430N/A HRESULT ConfigureContext(D3DPRESENT_PARAMETERS *pNewParams);
430N/A // resets existing D3D device with the current presentation parameters
430N/A HRESULT ResetContext();
430N/A HRESULT CheckAndResetDevice();
0N/A
430N/A // saves the state of the D3D device in a state block, resets
430N/A // context's state to STATE_CHANGE
430N/A HRESULT SaveState();
430N/A // restores the state of the D3D device from existing state block,
430N/A // resets context's state to STATE_CHANGE
430N/A HRESULT RestoreState();
0N/A
430N/A void ReleaseContextResources();
430N/A void ReleaseDefPoolResources();
430N/A virtual ~D3DContext();
0N/A
0N/A // methods replicating java-level D3DContext objext
430N/A HRESULT SetAlphaComposite(jint rule, jfloat extraAlpha, jint flags);
430N/A HRESULT ResetComposite();
0N/A
0N/A /**
430N/A * Glyph cache-related methods
0N/A */
430N/A HRESULT InitGrayscaleGlyphCache();
430N/A HRESULT InitLCDGlyphCache();
430N/A D3DGlyphCache* GetGrayscaleGlyphCache() { return pGrayscaleGlyphCache; }
430N/A D3DGlyphCache* GetLCDGlyphCache() { return pLCDGlyphCache; }
430N/A
430N/A D3DResourceManager *GetResourceManager() { return pResourceMgr; }
430N/A D3DMaskCache *GetMaskCache() { return pMaskCache; }
0N/A
430N/A HRESULT UploadTileToTexture(D3DResource *pTextureRes, void *pixels,
430N/A jint dstx, jint dsty,
430N/A jint srcx, jint srcy,
430N/A jint srcWidth, jint srcHeight,
430N/A jint srcStride,
430N/A TileFormat srcFormat,
430N/A // out: num of pixels in first and last
430N/A // columns, only counted for LCD glyph uploads
430N/A jint *pPixelsTouchedL = NULL,
430N/A jint *pPixelsTouchedR = NULL);
0N/A
430N/A // returns capabilities of the Direct3D device
430N/A D3DCAPS9 *GetDeviceCaps() { return &devCaps; }
430N/A // returns caps in terms of the D3DContext
430N/A int GetContextCaps() { return contextCaps; }
430N/A D3DPRESENT_PARAMETERS *GetPresentationParams() { return &curParams; }
0N/A
430N/A IDirect3DDevice9 *Get3DDevice() { return pd3dDevice; }
430N/A IDirect3D9 *Get3DObject() { return pd3dObject; }
0N/A
0N/A /**
0N/A * This method only sets the texture if it's not already set.
0N/A */
430N/A HRESULT SetTexture(IDirect3DTexture9 *pTexture, DWORD dwSampler = 0);
430N/A
430N/A /**
430N/A * This method only updates the texture color state if it hasn't changed.
430N/A */
430N/A HRESULT UpdateTextureColorState(DWORD dwState, DWORD dwSampler = 0);
430N/A
430N/A HRESULT SetRenderTarget(IDirect3DSurface9 *pSurface);
430N/A HRESULT SetTransform(jdouble m00, jdouble m10,
0N/A jdouble m01, jdouble m11,
0N/A jdouble m02, jdouble m12);
430N/A HRESULT ResetTransform();
0N/A
430N/A // clipping-related methods
430N/A HRESULT SetRectClip(int x1, int y1, int x2, int y2);
430N/A HRESULT BeginShapeClip();
430N/A HRESULT EndShapeClip();
430N/A HRESULT ResetClip();
430N/A ClipType GetClipType();
0N/A
0N/A /**
430N/A * Shader-related methods
0N/A */
430N/A HRESULT EnableBasicGradientProgram(jint flags);
430N/A HRESULT EnableLinearGradientProgram(jint flags);
430N/A HRESULT EnableRadialGradientProgram(jint flags);
430N/A HRESULT EnableConvolveProgram(jint flags);
430N/A HRESULT EnableRescaleProgram(jint flags);
430N/A HRESULT EnableLookupProgram(jint flags);
430N/A HRESULT EnableLCDTextProgram();
430N/A HRESULT EnableAAParallelogramProgram();
430N/A HRESULT DisableAAParallelogramProgram();
430N/A
430N/A BOOL IsTextureFilteringSupported(D3DTEXTUREFILTERTYPE fType);
430N/A BOOL IsStretchRectFilteringSupported(D3DTEXTUREFILTERTYPE fType);
430N/A BOOL IsPow2TexturesOnly()
430N/A { return devCaps.TextureCaps & D3DPTEXTURECAPS_POW2; };
430N/A BOOL IsSquareTexturesOnly()
430N/A { return devCaps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY; }
430N/A BOOL IsHWRasterizer() { return bIsHWRasterizer; }
430N/A BOOL IsTextureFormatSupported(D3DFORMAT format, DWORD usage = 0);
430N/A BOOL IsDynamicTextureSupported()
430N/A { return devCaps.Caps2 & D3DCAPS2_DYNAMICTEXTURES; }
430N/A// REMIND: for now for performance testing
430N/A// { return (getenv("J2D_D3D_USE_DYNAMIC_TEX") != NULL); }
430N/A BOOL IsImmediateIntervalSupported()
430N/A { return devCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE;}
430N/A BOOL IsPixelShader20Supported()
430N/A { return (devCaps.PixelShaderVersion >= D3DPS_VERSION(2,0)); }
430N/A BOOL IsGradientInstructionExtensionSupported()
430N/A { return devCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS; }
430N/A BOOL IsPixelShader30Supported()
430N/A { return (devCaps.PixelShaderVersion >= D3DPS_VERSION(3,0)); }
430N/A BOOL IsMultiTexturingSupported()
430N/A { return (devCaps.MaxSimultaneousTextures > 1); }
430N/A BOOL IsAlphaRTSurfaceSupported();
430N/A BOOL IsAlphaRTTSupported();
430N/A BOOL IsOpaqueRTTSupported();
430N/A
430N/A jint GetPaintState() { return paintState; }
430N/A void SetPaintState(jint state) { this->paintState = state; }
430N/A BOOL IsIdentityTx() { return bIsIdentityTx; }
430N/A
430N/A HRESULT FlushVertexQueue();
430N/A D3DVertexCacher *pVCacher;
430N/A HRESULT UpdateState(jbyte newState);
430N/A
430N/A HRESULT Sync();
0N/A
0N/A // primitives batching-related methods
0N/A /**
0N/A * Calls devices's BeginScene if there weren't one already pending,
0N/A * sets the pending flag.
0N/A */
430N/A HRESULT BeginScene(jbyte newState);
0N/A /**
430N/A * Flushes the vertex queue and does end scene if
430N/A * a BeginScene is pending
0N/A */
430N/A HRESULT EndScene();
0N/A
430N/A /**
430N/A * Fields that track native-specific state.
430N/A */
430N/A jint paintState;
430N/A jboolean useMask;
0N/A jfloat extraAlpha;
0N/A
0N/A /**
0N/A * Current operation state.
0N/A * See STATE_* macros above.
0N/A */
0N/A jbyte opState;
0N/A
0N/Aprivate:
0N/A
430N/A /**
430N/A * Glyph cache-related methods/fields...
430N/A */
430N/A D3DGlyphCache *pGrayscaleGlyphCache;
430N/A D3DGlyphCache *pLCDGlyphCache;
430N/A
430N/A /**
430N/A * The handle to the LCD text pixel shader program.
430N/A */
430N/A IDirect3DPixelShader9 *lcdTextProgram;
0N/A
0N/A /**
430N/A * The handle to the AA pixel and vertex shader programs.
0N/A */
430N/A IDirect3DPixelShader9 *aaPgramProgram;
430N/A
430N/A IDirect3DPixelShader9 *CreateFragmentProgram(DWORD **shaders,
430N/A ShaderList *programs,
430N/A jint flags);
430N/A HRESULT EnableFragmentProgram(DWORD **shaders,
430N/A ShaderList *programList,
430N/A jint flags);
430N/A
430N/A // finds appropriate to the target surface depth format,
430N/A // creates the depth buffer and installs it onto the device
430N/A HRESULT InitDepthStencilBuffer(D3DSURFACE_DESC *pTargetDesc);
430N/A // returns true if the current depth buffer is compatible
430N/A // with the new target, and the dimensions fit, false otherwise
430N/A BOOL IsDepthStencilBufferOk(D3DSURFACE_DESC *pTargetDesc);
0N/A
430N/A D3DContext(IDirect3D9 *pd3dObject, UINT adapter);
430N/A HRESULT InitDevice(IDirect3DDevice9 *d3dDevice);
430N/A HRESULT InitContextCaps();
430N/A // updates the texture transform(s) used for better texel to pixel mapping
430N/A // for the passed in sampler;
430N/A // if -1 is passed as the sampler, texture transforms for
430N/A // samplers [0..MAX_USED_TEXTURE_SAMPLER] are updated
430N/A // REMIND: see the comment in the method implementation before enabling.
430N/A#undef UPDATE_TX
430N/A#ifdef UPDATE_TX
430N/A HRESULT UpdateTextureTransforms(DWORD dwSamplerToUpdate);
430N/A#endif // UPDATE_TX
430N/A IDirect3DDevice9 *pd3dDevice;
430N/A IDirect3D9 *pd3dObject;
430N/A
430N/A D3DResourceManager *pResourceMgr;
430N/A D3DMaskCache *pMaskCache;
430N/A
430N/A ShaderList convolvePrograms;
430N/A ShaderList rescalePrograms;
430N/A ShaderList lookupPrograms;
430N/A ShaderList basicGradPrograms;
430N/A ShaderList linearGradPrograms;
430N/A ShaderList radialGradPrograms;
430N/A
430N/A // array of the textures currently set to the device
430N/A IDirect3DTexture9 *lastTexture[MAX_USED_TEXTURE_SAMPLER+1];
430N/A
430N/A DWORD lastTextureColorState[MAX_USED_TEXTURE_SAMPLER+1];
430N/A
430N/A UINT adapterOrdinal;
430N/A D3DPRESENT_PARAMETERS curParams;
430N/A D3DCAPS9 devCaps;
430N/A int contextCaps;
0N/A BOOL bIsHWRasterizer;
0N/A
430N/A BOOL bIsIdentityTx;
430N/A
430N/A IDirect3DQuery9* pSyncQuery;
430N/A D3DResource* pSyncRTRes;
430N/A
430N/A IDirect3DStateBlock9* pStateBlock;
430N/A
0N/A /**
0N/A * Used to implement simple primitive batching.
0N/A * See BeginScene/EndScene/ForceEndScene.
0N/A */
0N/A BOOL bBeginScenePending;
0N/A};
0N/A
0N/A// - Helper Macros ---------------------------------------------------
0N/A
430N/A#define D3DC_INIT_SHADER_LIST(list, max) \
430N/A do { \
430N/A (list).head = NULL; \
430N/A (list).maxItems = (max); \
430N/A (list).dispose = D3DContext_DisposeShader; \
430N/A } while (0)
0N/A
430N/A/**
430N/A * This constant determines the size of the shared tile texture used
430N/A * by a number of image rendering methods. For example, the blit tile texture
430N/A * will have dimensions with width D3DC_BLIT_TILE_SIZE and height
430N/A * D3DC_BLIT_TILE_SIZE (the tile will always be square).
430N/A */
430N/A#define D3DC_BLIT_TILE_SIZE 256
0N/A
430N/A/**
430N/A * See BufferedContext.java for more on these flags...
430N/A */
0N/A#define D3DC_NO_CONTEXT_FLAGS \
430N/A sun_java2d_pipe_BufferedContext_NO_CONTEXT_FLAGS
0N/A#define D3DC_SRC_IS_OPAQUE \
430N/A sun_java2d_pipe_BufferedContext_SRC_IS_OPAQUE
430N/A#define D3DC_USE_MASK \
430N/A sun_java2d_pipe_BufferedContext_USE_MASK
0N/A
430N/A#define CAPS_EMPTY \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_EMPTY
430N/A#define CAPS_RT_PLAIN_ALPHA \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_PLAIN_ALPHA
430N/A#define CAPS_RT_TEXTURE_ALPHA \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_TEXTURE_ALPHA
430N/A#define CAPS_RT_TEXTURE_OPAQUE \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_RT_TEXTURE_OPAQUE
430N/A#define CAPS_MULTITEXTURE \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_MULTITEXTURE
430N/A#define CAPS_TEXNONPOW2 \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_TEXNONPOW2
430N/A#define CAPS_TEXNONSQUARE \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_TEXNONSQUARE
430N/A#define CAPS_LCD_SHADER \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_LCD_SHADER
430N/A#define CAPS_BIOP_SHADER \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_BIOP_SHADER
430N/A#define CAPS_AA_SHADER \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_AA_SHADER
430N/A#define CAPS_DEVICE_OK \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_DEVICE_OK
430N/A#define CAPS_PS20 \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_PS20
430N/A#define CAPS_PS30 \
430N/A sun_java2d_d3d_D3DContext_D3DContextCaps_CAPS_PS30
0N/A
430N/A#define DEVICE_RESET \
430N/A sun_java2d_pipe_hw_AccelDeviceEventNotifier_DEVICE_RESET
430N/A#define DEVICE_DISPOSED \
430N/A sun_java2d_pipe_hw_AccelDeviceEventNotifier_DEVICE_DISPOSED
430N/A
430N/A#endif // D3DCONTEXT_H