1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "Direct3DResource9.hpp"
16 
17 #include "Direct3DDevice9.hpp"
18 #include "Debug.hpp"
19 
20 namespace D3D9
21 {
22 	unsigned int Direct3DResource9::memoryUsage = 0;
23 
PrivateData()24 	Direct3DResource9::PrivateData::PrivateData()
25 	{
26 		data = 0;
27 	}
28 
PrivateData(const void * data,int size,bool managed)29 	Direct3DResource9::PrivateData::PrivateData(const void *data, int size, bool managed)
30 	{
31 		this->size = size;
32 		this->managed = managed;
33 
34 		this->data = (void*)new unsigned char[size];
35 		memcpy(this->data, data, size);
36 
37 		if(managed)
38 		{
39 			((IUnknown*)data)->AddRef();
40 		}
41 	}
42 
operator =(const PrivateData & privateData)43 	Direct3DResource9::PrivateData &Direct3DResource9::PrivateData::operator=(const PrivateData &privateData)
44 	{
45 		size = privateData.size;
46 		managed = privateData.managed;
47 
48 		if(data)
49 		{
50 			if(managed)
51 			{
52 				((IUnknown*)data)->Release();
53 			}
54 
55 			delete[] data;
56 		}
57 
58 		data = (void*)new unsigned char[size];
59 		memcpy(data, privateData.data, size);
60 
61 		return *this;
62 	}
63 
~PrivateData()64 	Direct3DResource9::PrivateData::~PrivateData()
65 	{
66 		if(data && managed)
67 		{
68 			((IUnknown*)data)->Release();
69 		}
70 
71 		delete[] data;
72 		data = 0;
73 	}
74 
Direct3DResource9(Direct3DDevice9 * device,D3DRESOURCETYPE type,D3DPOOL pool,unsigned int size)75 	Direct3DResource9::Direct3DResource9(Direct3DDevice9 *device, D3DRESOURCETYPE type, D3DPOOL pool, unsigned int size) : device(device), type(type), pool(pool), size(size)
76 	{
77 		priority = 0;
78 
79 		if(pool == D3DPOOL_DEFAULT)
80 		{
81 			memoryUsage += size;
82 		}
83 	}
84 
~Direct3DResource9()85 	Direct3DResource9::~Direct3DResource9()
86 	{
87 		if(pool == D3DPOOL_DEFAULT)
88 		{
89 			memoryUsage -= size;
90 		}
91 	}
92 
QueryInterface(const IID & iid,void ** object)93 	long Direct3DResource9::QueryInterface(const IID &iid, void **object)
94 	{
95 		CriticalSection cs(device);
96 
97 		TRACE("");
98 
99 		if(iid == IID_IDirect3DResource9 ||
100 		   iid == IID_IUnknown)
101 		{
102 			AddRef();
103 			*object = this;
104 
105 			return S_OK;
106 		}
107 
108 		*object = 0;
109 
110 		return NOINTERFACE(iid);
111 	}
112 
AddRef()113 	unsigned long Direct3DResource9::AddRef()
114 	{
115 		TRACE("");
116 
117 		return Unknown::AddRef();
118 	}
119 
Release()120 	unsigned long Direct3DResource9::Release()
121 	{
122 		TRACE("");
123 
124 		return Unknown::Release();
125 	}
126 
GetDevice(IDirect3DDevice9 ** device)127 	long Direct3DResource9::GetDevice(IDirect3DDevice9 **device)
128 	{
129 		CriticalSection cs(this->device);
130 
131 		TRACE("");
132 
133 		if(!device)
134 		{
135 			return INVALIDCALL();
136 		}
137 
138 		this->device->AddRef();
139 		*device = this->device;
140 
141 		return D3D_OK;
142 	}
143 
SetPrivateData(const GUID & guid,const void * data,unsigned long size,unsigned long flags)144 	long Direct3DResource9::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
145 	{
146 		CriticalSection cs(device);
147 
148 		TRACE("");
149 
150 		privateData[guid] = PrivateData(data, size, flags == D3DSPD_IUNKNOWN);
151 
152 		return D3D_OK;
153 	}
154 
GetPrivateData(const GUID & guid,void * data,unsigned long * size)155 	long Direct3DResource9::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
156 	{
157 		CriticalSection cs(device);
158 
159 		TRACE("");
160 
161 		Iterator result = privateData.find(guid);
162 
163 		if(result == privateData.end())
164 		{
165 			return NOTFOUND();
166 		}
167 
168 		if(result->second.size > *size)
169 		{
170 			return MOREDATA();
171 		}
172 
173 		memcpy(data, result->second.data, result->second.size);
174 
175 		return D3D_OK;
176 	}
177 
FreePrivateData(const GUID & guid)178 	long Direct3DResource9::FreePrivateData(const GUID &guid)
179 	{
180 		CriticalSection cs(device);
181 
182 		TRACE("");
183 
184 		Iterator result = privateData.find(guid);
185 
186 		if(result == privateData.end())
187 		{
188 			return D3DERR_NOTFOUND;
189 		}
190 
191 		privateData.erase(guid);
192 
193 		return D3D_OK;
194 	}
195 
SetPriority(unsigned long newPriority)196 	unsigned long Direct3DResource9::SetPriority(unsigned long newPriority)
197 	{
198 		CriticalSection cs(device);
199 
200 		TRACE("");
201 
202 		unsigned long oldPriority = priority;
203 		priority = newPriority;
204 
205 		return oldPriority;
206 	}
207 
GetPriority()208 	unsigned long Direct3DResource9::GetPriority()
209 	{
210 		CriticalSection cs(device);
211 
212 		TRACE("");
213 
214 		return priority;
215 	}
216 
PreLoad()217 	void Direct3DResource9::PreLoad()
218 	{
219 		CriticalSection cs(device);
220 
221 		TRACE("");
222 	}
223 
GetType()224 	D3DRESOURCETYPE Direct3DResource9::GetType()
225 	{
226 		CriticalSection cs(device);
227 
228 		TRACE("");
229 
230 		return type;
231 	}
232 
getMemoryUsage()233 	unsigned int Direct3DResource9::getMemoryUsage()
234 	{
235 		return memoryUsage;
236 	}
237 
getPool() const238 	D3DPOOL Direct3DResource9::getPool() const
239 	{
240 		return pool;
241 	}
242 }