dawn-cmake/src/dawn/wire/client/Instance.cpp
Corentin Wallez 0f97df8c53 dawn::wire::client: Track the object generation on the objects
Previously the ObjectAllocator was tracking the generation on the side
of the object. This was done to avoid the need to check that the objects
aren't null before accessing the generation in ClientHandlers. This is
only a very minor optimization for return commands so it is removed in
favor of simplifying the code.

The code is simplified in a bunch of place by getting the ObjectHandle
for an object directly (since it knows the generation now) instead of
walking the object graph returned by the allocator.

The ObjectBase class is also changed to store an ObjectHandle
interrnally that's only accessible via getters. Encapsulating the other
memebers will be done in follow-up CLs.

Also adds the generation to the ObjectBaseParams since all ObjectBases
now require it.

Bug: dawn:1451
Change-Id: Ic6c850fc989f715f7c80952ff447b7c29378cd27
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/93146
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
2022-06-14 14:55:46 +00:00

102 lines
4.0 KiB
C++

// Copyright 2021 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "dawn/wire/client/Instance.h"
#include "dawn/wire/client/Client.h"
namespace dawn::wire::client {
Instance::~Instance() {
mRequestAdapterRequests.CloseAll([](RequestAdapterData* request) {
request->callback(WGPURequestAdapterStatus_Unknown, nullptr,
"Instance destroyed before callback", request->userdata);
});
}
void Instance::CancelCallbacksForDisconnect() {
mRequestAdapterRequests.CloseAll([](RequestAdapterData* request) {
request->callback(WGPURequestAdapterStatus_Unknown, nullptr, "GPU connection lost",
request->userdata);
});
}
void Instance::RequestAdapter(const WGPURequestAdapterOptions* options,
WGPURequestAdapterCallback callback,
void* userdata) {
if (client->IsDisconnected()) {
callback(WGPURequestAdapterStatus_Error, nullptr, "GPU connection lost", userdata);
return;
}
Adapter* adapter = client->AdapterAllocator().New(client);
uint64_t serial = mRequestAdapterRequests.Add({callback, adapter->GetWireId(), userdata});
InstanceRequestAdapterCmd cmd;
cmd.instanceId = GetWireId();
cmd.requestSerial = serial;
cmd.adapterObjectHandle = adapter->GetWireHandle();
cmd.options = options;
client->SerializeCommand(cmd);
}
bool Client::DoInstanceRequestAdapterCallback(Instance* instance,
uint64_t requestSerial,
WGPURequestAdapterStatus status,
const char* message,
const WGPUAdapterProperties* properties,
const WGPUSupportedLimits* limits,
uint32_t featuresCount,
const WGPUFeatureName* features) {
// May have been deleted or recreated so this isn't an error.
if (instance == nullptr) {
return true;
}
return instance->OnRequestAdapterCallback(requestSerial, status, message, properties, limits,
featuresCount, features);
}
bool Instance::OnRequestAdapterCallback(uint64_t requestSerial,
WGPURequestAdapterStatus status,
const char* message,
const WGPUAdapterProperties* properties,
const WGPUSupportedLimits* limits,
uint32_t featuresCount,
const WGPUFeatureName* features) {
RequestAdapterData request;
if (!mRequestAdapterRequests.Acquire(requestSerial, &request)) {
return false;
}
Adapter* adapter = client->AdapterAllocator().GetObject(request.adapterObjectId);
// If the return status is a failure we should give a null adapter to the callback and
// free the allocation.
if (status != WGPURequestAdapterStatus_Success) {
client->AdapterAllocator().Free(adapter);
request.callback(status, nullptr, message, request.userdata);
return true;
}
adapter->SetProperties(properties);
adapter->SetLimits(limits);
adapter->SetFeatures(features, featuresCount);
request.callback(status, ToAPI(adapter), message, request.userdata);
return true;
}
} // namespace dawn::wire::client