1/*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23#include "resource9.h"
24#include "device9.h"
25#include "nine_helpers.h"
26#include "nine_defines.h"
27
28#include "util/u_inlines.h"
29#include "util/u_resource.h"
30
31#include "pipe/p_screen.h"
32
33#define DBG_CHANNEL DBG_RESOURCE
34
35HRESULT
36NineResource9_ctor( struct NineResource9 *This,
37                    struct NineUnknownParams *pParams,
38                    struct pipe_resource *initResource,
39                    BOOL Allocate,
40                    D3DRESOURCETYPE Type,
41                    D3DPOOL Pool,
42                    DWORD Usage)
43{
44    struct pipe_screen *screen;
45    HRESULT hr;
46
47    DBG("This=%p pParams=%p initResource=%p Allocate=%d "
48        "Type=%d Pool=%d Usage=%d\n",
49        This, pParams, initResource, (int) Allocate,
50        Type, Pool, Usage);
51
52    hr = NineUnknown_ctor(&This->base, pParams);
53    if (FAILED(hr))
54        return hr;
55
56    This->info.screen = screen = This->base.device->screen;
57    if (initResource)
58        pipe_resource_reference(&This->resource, initResource);
59
60    if (Allocate) {
61        assert(!initResource);
62
63        /* On Windows it is possible allocation fails when
64         * IDirect3DDevice9::GetAvailableTextureMem() still reports
65         * enough free space.
66         *
67         * Some games allocate surfaces
68         * in a loop until they receive D3DERR_OUTOFVIDEOMEMORY to measure
69         * the available texture memory size.
70         *
71         * We are not using the drivers VRAM statistics because:
72         *  * This would add overhead to each resource allocation.
73         *  * Freeing memory is lazy and takes some time, but applications
74         *    expects the memory counter to change immediately after allocating
75         *    or freeing memory.
76         *
77         * Vertexbuffers and indexbuffers are not accounted !
78         */
79        if (This->info.target != PIPE_BUFFER) {
80            This->size = util_resource_size(&This->info);
81
82            p_atomic_add(&This->base.device->available_texture_mem, -This->size);
83            /* Before failing allocation, evict MANAGED memory */
84            if (This->base.device &&
85                p_atomic_read(&This->base.device->available_texture_mem) <=
86                    This->base.device->available_texture_limit)
87                NineDevice9_EvictManagedResourcesInternal(This->base.device);
88            if (p_atomic_read(&This->base.device->available_texture_mem) <=
89                    This->base.device->available_texture_limit) {
90                DBG("Memory allocation failure: software limit\n");
91                return D3DERR_OUTOFVIDEOMEMORY;
92            }
93        }
94
95        DBG("(%p) Creating pipe_resource.\n", This);
96        This->resource = nine_resource_create_with_retry(This->base.device, screen, &This->info);
97        if (!This->resource)
98            return D3DERR_OUTOFVIDEOMEMORY;
99    }
100
101    DBG("Current texture memory count: (%d/%d)KB\n",
102        (int)(This->base.device->available_texture_mem >> 10),
103        (int)(This->base.device->available_texture_limit >> 10));
104
105    This->type = Type;
106    This->pool = Pool;
107    This->usage = Usage;
108    This->priority = 0;
109
110    return D3D_OK;
111}
112
113void
114NineResource9_dtor( struct NineResource9 *This )
115{
116    DBG("This=%p\n", This);
117
118    /* NOTE: We do have to use refcounting, the driver might
119     * still hold a reference. */
120    pipe_resource_reference(&This->resource, NULL);
121
122    /* NOTE: size is 0, unless something has actually been allocated */
123    if (This->base.device)
124        p_atomic_add(&This->base.device->available_texture_mem, This->size);
125
126    NineUnknown_dtor(&This->base);
127}
128
129struct pipe_resource *
130NineResource9_GetResource( struct NineResource9 *This )
131{
132    return This->resource;
133}
134
135D3DPOOL
136NineResource9_GetPool( struct NineResource9 *This )
137{
138    return This->pool;
139}
140
141DWORD NINE_WINAPI
142NineResource9_SetPriority( struct NineResource9 *This,
143                           DWORD PriorityNew )
144{
145    DWORD prev;
146    DBG("This=%p, PriorityNew=%d\n", This, PriorityNew);
147
148    if (This->pool != D3DPOOL_MANAGED || This->type == D3DRTYPE_SURFACE)
149        return 0;
150
151    prev = This->priority;
152    This->priority = PriorityNew;
153    return prev;
154}
155
156DWORD NINE_WINAPI
157NineResource9_GetPriority( struct NineResource9 *This )
158{
159    if (This->pool != D3DPOOL_MANAGED || This->type == D3DRTYPE_SURFACE)
160        return 0;
161
162    return This->priority;
163}
164
165/* NOTE: Don't forget to adjust locked vtable if you change this ! */
166void NINE_WINAPI
167NineResource9_PreLoad( struct NineResource9 *This )
168{
169    if (This->pool != D3DPOOL_MANAGED)
170        return;
171    /* We don't treat managed vertex or index buffers different from
172     * default ones (are managed vertex buffers even allowed ?), and
173     * the PreLoad for textures is overridden by superclass.
174     */
175}
176
177D3DRESOURCETYPE NINE_WINAPI
178NineResource9_GetType( struct NineResource9 *This )
179{
180    return This->type;
181}
182