aboutsummaryrefslogtreecommitdiff
path: root/src/windows/native/sun/java2d/d3d/D3DResourceManager.h
blob: 87b8d8ccc6b92d2b36128d009c70fefde0a2370a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */


#ifndef _D3DRESOURCEMANAGER_H_
#define _D3DRESOURCEMANAGER_H_

#include "D3DContext.h"
#include "D3DSurfaceData.h"

class D3DResourceManager;
class D3DContext;

/**
 * This interface represents a Direct3D resource which is managed by the
 * D3DResourceManager.
 *
 * Subclasses will need to override Release() and the destructor to release
 * the resources held by the object.
 *
 * The subclasses then can be used like this:
 *   class D3DShaderResource : public IManagedResource {
 *               D3DShaderResource(IDirect3DPixelShader9 *pShader) { // ... }
 *      virtual ~D3DShaderResource() { Release(); }
 *      virtual  Release() { SAFE_RELEASE(pShader); }
 *      virtual  IsDefaultPool() { return FALSE; }
 *   private:
 *      IDirect3DPixelShader9 *pShader;
 *   }
 *
 *   pD3DDevice->CreatePixelShader(..., &pShader);
 *   IManagedResource *pShaderRes = new D3DShaderResource(pShader);
 *   pCtx->GetResourceManager()->AddResource(pShaderRes);
 *
 * D3DResourceManager::ReleaseResource() must be used to dispose of the
 * resource:
 *   pCtx->GetResourceManager()->ReleaseResource(pShaderRes);
 *   // pShaderRes is now invalid, it was deleted
 *   shaderRes = NULL;
 *
 * In certain cases the D3DResourceManager may need to release all its
 * resources (like when resetting the device), so the subclasses must be
 * ready to be released at any time, and be able to notify their users.
 * For an example of how this can be achieved see how D3DSDO's
 * pResource field and D3DResource subclass. d3dsdo->pResource is reset when
 * the D3DResource it was pointing to is disposed.
 */
class IManagedResource {
friend class D3DResourceManager;
public:
    // determines whether the resource should be released by the manager
    // when defaul pool resources are to be released
    virtual BOOL IsDefaultPool() = 0;
protected:
                 IManagedResource() { pPrev = pNext = NULL; };
    virtual     ~IManagedResource() { pPrev = pNext = NULL; };
    virtual void Release() = 0;
private:
    // prevents accidental bad things like copying the object
    IManagedResource& operator=(const IManagedResource&);

    IManagedResource* pPrev;
    IManagedResource* pNext;
};

/**
 * This class handles either IDirect3DResource9 or IDirect3DSwapChain9
 * type of resources and provides access to Texture, Surface or SwapChain,
 * as well as the surface description.
 */
class D3DResource : public IManagedResource {
public:
                         D3DResource(IDirect3DResource9 *pRes)
                             { Init(pRes, NULL); }
                         D3DResource(IDirect3DSwapChain9 *pSC)
                             { Init(NULL, pSC); }
    IDirect3DResource9*  GetResource() { return pResource; }
    IDirect3DTexture9*   GetTexture() { return pTexture; }
    IDirect3DSurface9*   GetSurface() { return pSurface; }
    IDirect3DSwapChain9* GetSwapChain() { return pSwapChain; }
    D3DSDOps*            GetSDOps() { return pOps; }
    void                 SetSDOps(D3DSDOps *pOps);
    D3DSURFACE_DESC*     GetDesc() { return &desc; }
    virtual BOOL         IsDefaultPool();

protected:
    // these are protected because we want D3DResource to be only released via
    // ResourceManager
virtual                 ~D3DResource();
virtual void             Release();
    void                 Init(IDirect3DResource9*, IDirect3DSwapChain9*);

private:
    // prevents accidental bad things like copying the object
                         D3DResource() {}
    D3DResource&         operator=(const D3DResource&);

    IDirect3DResource9*  pResource;
    IDirect3DSwapChain9* pSwapChain;
    IDirect3DSurface9*   pSurface;
    IDirect3DTexture9*   pTexture;
    D3DSDOps*            pOps;
    D3DSURFACE_DESC      desc;
};

/**
 * This class maintains a list of d3d resources created by the pipeline or
 * other clients. It is needed because in some cases all resources have to be
 * released in order to reset the device so we must keep track of them.
 *
 * There is one instance of this class per D3DContext. Clients can either
 * use factory methods for creating resources or create their own encapsulated
 * in an IManagedResource interface subclass and add them to the list
 * using the AddResource() method. Resources added to the list must be released
 * via the ReleaseResource() method so that they can be stopped being managed.
 */
class D3DResourceManager {

public:
            ~D3DResourceManager();
    HRESULT Init(D3DContext *pCtx);
    // Releases and deletes all resources managed by this manager.
    void    ReleaseAll();
    // Releases (and deletes) all resources belonging to the default pool.
    // Note: this method may release other resources as well.
    void    ReleaseDefPoolResources();

    // Adds the resource to the list managed by this class.
    HRESULT AddResource(IManagedResource* pResource);
    // Removes the resource from the list of managed resources, and deletes
    // it. The argument pointer is invalid after this method returns.
    HRESULT ReleaseResource(IManagedResource* pResource);

    HRESULT CreateTexture(UINT width, UINT height,
                          BOOL isRTT, BOOL isOpaque,
                          D3DFORMAT *pFormat/*in/out*/,
                          DWORD dwUsage,
                          D3DResource **ppTextureResource/*out*/);

    HRESULT CreateRTSurface(UINT width, UINT height,
                            BOOL isOpaque, BOOL isLockable,
                            D3DFORMAT *pFormat/*in/out*/,
                            D3DResource ** ppSurfaceResource/*out*/);

    HRESULT CreateSwapChain(HWND hWnd, UINT numBuffers, UINT width, UINT height,
                            D3DSWAPEFFECT swapEffect, UINT presentationInterval,
                            D3DResource ** ppSwapChainResource/*out*/);

    HRESULT GetCachedDestTexture(D3DFORMAT format,
                                 D3DResource **ppTextureResource);
    HRESULT GetBlitTexture(D3DResource **ppTextureResource);
    HRESULT GetBlitRTTexture(UINT width, UINT height, D3DFORMAT format,
                             D3DResource **ppTextureResource);
    HRESULT GetBlitOSPSurface(UINT width, UINT height, D3DFORMAT fmt,
                              D3DResource **ppSurfaceResource);
    HRESULT GetMaskTexture(D3DResource **ppTextureResource);
    HRESULT GetGradientTexture(D3DResource **ppTextureResource);
    HRESULT GetMultiGradientTexture(D3DResource **ppTextureResource);
    HRESULT GetLookupOpLutTexture(D3DResource **ppTextureResource);
    HRESULT GetLockableRTSurface(UINT width, UINT height, D3DFORMAT format,
                                 D3DResource **ppSurfaceResource);

static
    HRESULT CreateInstance(D3DContext *pCtx, D3DResourceManager **ppResMgr);

private:
            D3DResourceManager();
    HRESULT GetStockTextureResource(UINT width, UINT height,
                                    BOOL isRTT, BOOL isOpaque,
                                    D3DFORMAT *pFormat/*in/out*/,
                                    DWORD dwUsage,
                                    D3DResource **ppTextureResource/*out*/);

    HRESULT CreateOSPSurface(UINT width, UINT height,
                             D3DFORMAT fmt,
                             D3DResource ** ppSurfaceResource/*out*/);

    D3DResource*      pCachedDestTexture;
    D3DResource*      pBlitTexture;
    D3DResource*      pBlitRTTexture;
    D3DResource*      pBlitOSPSurface;
    D3DResource*      pGradientTexture;
    D3DResource*      pLookupOpLutTexture;
    D3DResource*      pMaskTexture;
    D3DResource*      pMultiGradientTexture;
    D3DResource*      pLockableRTSurface;

    D3DContext*       pCtx;

    IManagedResource* pHead;
};
#endif _D3DRESOURCEMANAGER_H_