1 //* Copyright 2019 The Dawn Authors 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 "common/Assert.h" 16 #include "dawn_wire/server/Server.h" 17 18 namespace dawn_wire { namespace server { 19 //* Implementation of the command doers 20 {% for command in cmd_records["command"] %} 21 {% set type = command.derived_object %} 22 {% set method = command.derived_method %} 23 {% set is_method = method is not none %} 24 25 {% set Suffix = command.name.CamelCase() %} 26 {% if Suffix not in client_side_commands %} 27 {% if is_method and Suffix not in server_handwritten_commands %} 28 bool Server::Do{{Suffix}}( 29 {%- for member in command.members -%} 30 {%- if member.is_return_value -%} 31 {%- if member.handle_type -%} 32 {{as_cType(member.handle_type.name)}}* {{as_varName(member.name)}} 33 {%- else -%} 34 {{as_cType(member.type.name)}}* {{as_varName(member.name)}} 35 {%- endif -%} 36 {%- else -%} 37 {{as_annotated_cType(member)}} 38 {%- endif -%} 39 {%- if not loop.last -%}, {% endif %} 40 {%- endfor -%} 41 ) { 42 {% set ret = command.members|selectattr("is_return_value")|list %} 43 //* If there is a return value, assign it. 44 {% if ret|length == 1 %} 45 *{{as_varName(ret[0].name)}} = 46 {% else %} 47 //* Only one member should be a return value. 48 {{ assert(ret|length == 0) }} 49 {% endif %} 50 mProcs.{{as_varName(type.name, method.name)}}( 51 {%- for member in command.members if not member.is_return_value -%} 52 {{as_varName(member.name)}} 53 {%- if not loop.last -%}, {% endif %} 54 {%- endfor -%} 55 ); 56 {% if ret|length == 1 %} 57 //* WebGPU error handling guarantees that no null object can be returned by 58 //* object creation functions. 59 ASSERT(*{{as_varName(ret[0].name)}} != nullptr); 60 {% endif %} 61 return true; 62 } 63 {% endif %} 64 {% endif %} 65 {% endfor %} 66 67 bool Server::DoDestroyObject(ObjectType objectType, ObjectId objectId) { 68 //* ID 0 are reserved for nullptr and cannot be destroyed. 69 if (objectId == 0) { 70 return false; 71 } 72 73 switch(objectType) { 74 {% for type in by_category["object"] %} 75 case ObjectType::{{type.name.CamelCase()}}: { 76 auto* data = {{type.name.CamelCase()}}Objects().Get(objectId); 77 if (data == nullptr) { 78 return false; 79 } 80 if (data->deviceInfo != nullptr) { 81 if (!UntrackDeviceChild(data->deviceInfo, objectType, objectId)) { 82 return false; 83 } 84 } 85 if (data->state == AllocationState::Allocated) { 86 ASSERT(data->handle != nullptr); 87 {% if type.name.CamelCase() in server_reverse_lookup_objects %} 88 {{type.name.CamelCase()}}ObjectIdTable().Remove(data->handle); 89 {% endif %} 90 91 {% if type.name.get() == "device" %} 92 //* TODO(crbug.com/dawn/384): This is a hack to make sure that all child objects 93 //* are destroyed before their device. We should have a solution in 94 //* Dawn native that makes all child objects internally null if their 95 //* Device is destroyed. 96 while (data->info->childObjectTypesAndIds.size() > 0) { 97 ObjectType childObjectType; 98 ObjectId childObjectId; 99 std::tie(childObjectType, childObjectId) = UnpackObjectTypeAndId( 100 *data->info->childObjectTypesAndIds.begin()); 101 if (!DoDestroyObject(childObjectType, childObjectId)) { 102 return false; 103 } 104 } 105 if (data->handle != nullptr) { 106 //* Deregisters uncaptured error and device lost callbacks since 107 //* they should not be forwarded if the device no longer exists on the wire. 108 ClearDeviceCallbacks(data->handle); 109 } 110 {% endif %} 111 112 mProcs.{{as_varName(type.name, Name("release"))}}(data->handle); 113 } 114 {{type.name.CamelCase()}}Objects().Free(objectId); 115 return true; 116 } 117 {% endfor %} 118 default: 119 return false; 120 } 121 } 122 123 }} // namespace dawn_wire::server 124