Nicolas Capens | 17b29fd | 2016-09-15 09:32:16 -0400 | [diff] [blame] | 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 "Direct3DResource8.hpp" |
| 16 | |
| 17 | #include "Direct3DDevice8.hpp" |
| 18 | #include "Debug.hpp" |
| 19 | |
| 20 | namespace D3D8 |
| 21 | { |
| 22 | unsigned int Direct3DResource8::memoryUsage = 0; |
| 23 | |
| 24 | Direct3DResource8::PrivateData::PrivateData() |
| 25 | { |
| 26 | data = 0; |
| 27 | } |
| 28 | |
| 29 | Direct3DResource8::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 | |
| 43 | Direct3DResource8::PrivateData &Direct3DResource8::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 | |
| 64 | Direct3DResource8::PrivateData::~PrivateData() |
| 65 | { |
| 66 | if(data && managed) |
| 67 | { |
| 68 | ((IUnknown*)data)->Release(); |
| 69 | } |
| 70 | |
| 71 | delete[] data; |
| 72 | data = 0; |
| 73 | } |
| 74 | |
| 75 | Direct3DResource8::Direct3DResource8(Direct3DDevice8 *device, D3DRESOURCETYPE type, unsigned int size) : device(device), type(type), size(size) |
| 76 | { |
| 77 | priority = 0; |
| 78 | |
| 79 | memoryUsage += size; |
| 80 | } |
| 81 | |
| 82 | Direct3DResource8::~Direct3DResource8() |
| 83 | { |
| 84 | memoryUsage -= size; |
| 85 | } |
| 86 | |
| 87 | long Direct3DResource8::QueryInterface(const IID &iid, void **object) |
| 88 | { |
| 89 | TRACE(""); |
| 90 | |
| 91 | if(iid == IID_IDirect3DResource8 || |
| 92 | iid == IID_IUnknown) |
| 93 | { |
| 94 | AddRef(); |
| 95 | *object = this; |
| 96 | |
| 97 | return S_OK; |
| 98 | } |
| 99 | |
| 100 | *object = 0; |
| 101 | |
| 102 | return NOINTERFACE(iid); |
| 103 | } |
| 104 | |
| 105 | unsigned long Direct3DResource8::AddRef() |
| 106 | { |
| 107 | TRACE(""); |
| 108 | |
| 109 | return Unknown::AddRef(); |
| 110 | } |
| 111 | |
| 112 | unsigned long Direct3DResource8::Release() |
| 113 | { |
| 114 | TRACE(""); |
| 115 | |
| 116 | return Unknown::Release(); |
| 117 | } |
| 118 | |
| 119 | long Direct3DResource8::GetDevice(IDirect3DDevice8 **device) |
| 120 | { |
| 121 | TRACE(""); |
| 122 | |
| 123 | if(!device) |
| 124 | { |
| 125 | return INVALIDCALL(); |
| 126 | } |
| 127 | |
| 128 | this->device->AddRef(); |
| 129 | *device = this->device; |
| 130 | |
| 131 | return D3D_OK; |
| 132 | } |
| 133 | |
| 134 | long Direct3DResource8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags) |
| 135 | { |
| 136 | TRACE(""); |
| 137 | |
| 138 | privateData[guid] = PrivateData(data, size, flags == D3DSPD_IUNKNOWN); |
| 139 | |
| 140 | return D3D_OK; |
| 141 | } |
| 142 | |
| 143 | long Direct3DResource8::GetPrivateData(const GUID &guid, void *data, unsigned long *size) |
| 144 | { |
| 145 | TRACE(""); |
| 146 | |
| 147 | Iterator result = privateData.find(guid); |
| 148 | |
| 149 | if(result == privateData.end()) |
| 150 | { |
| 151 | return NOTFOUND(); |
| 152 | } |
| 153 | |
| 154 | if(result->second.size > *size) |
| 155 | { |
| 156 | return MOREDATA(); |
| 157 | } |
| 158 | |
| 159 | memcpy(data, result->second.data, result->second.size); |
| 160 | |
| 161 | return D3D_OK; |
| 162 | } |
| 163 | |
| 164 | long Direct3DResource8::FreePrivateData(const GUID &guid) |
| 165 | { |
| 166 | TRACE(""); |
| 167 | |
| 168 | Iterator result = privateData.find(guid); |
| 169 | |
| 170 | if(result == privateData.end()) |
| 171 | { |
| 172 | return D3DERR_NOTFOUND; |
| 173 | } |
| 174 | |
| 175 | privateData.erase(guid); |
| 176 | |
| 177 | return D3D_OK; |
| 178 | } |
| 179 | |
| 180 | unsigned long Direct3DResource8::SetPriority(unsigned long newPriority) |
| 181 | { |
| 182 | TRACE(""); |
| 183 | |
| 184 | unsigned long oldPriority = priority; |
| 185 | priority = newPriority; |
| 186 | |
| 187 | return oldPriority; |
| 188 | } |
| 189 | |
| 190 | unsigned long Direct3DResource8::GetPriority() |
| 191 | { |
| 192 | TRACE(""); |
| 193 | |
| 194 | return priority; |
| 195 | } |
| 196 | |
| 197 | void Direct3DResource8::PreLoad() |
| 198 | { |
| 199 | TRACE(""); |
| 200 | |
| 201 | return; // FIXME: Anything to do? |
| 202 | } |
| 203 | |
| 204 | D3DRESOURCETYPE Direct3DResource8::GetType() |
| 205 | { |
| 206 | TRACE(""); |
| 207 | |
| 208 | return type; |
| 209 | } |
| 210 | |
| 211 | unsigned int Direct3DResource8::getMemoryUsage() |
| 212 | { |
| 213 | return memoryUsage; |
| 214 | } |
| 215 | } |