1 /*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tests/Test.h"
9
10 #include "experimental/graphite/include/Context.h"
11 #include "experimental/graphite/include/Recorder.h"
12 #include "experimental/graphite/src/Device.h"
13
14 using namespace skgpu;
15
16 // Tests to make sure the managing of back pointers between Recorder and Device all work properly.
DEF_GRAPHITE_TEST_FOR_CONTEXTS(RecorderDevicePtrTest,reporter,context)17 DEF_GRAPHITE_TEST_FOR_CONTEXTS(RecorderDevicePtrTest, reporter, context) {
18 std::unique_ptr<Recorder> recorder = context->makeRecorder();
19
20 SkImageInfo info = SkImageInfo::Make({16, 16}, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
21
22 sk_sp<Device> device1 = Device::Make(recorder.get(), info);
23
24 REPORTER_ASSERT(reporter, device1->recorder() == recorder.get());
25 REPORTER_ASSERT(reporter, recorder->deviceIsRegistered(device1.get()));
26
27 Device* devPtr = device1.get();
28 device1.reset();
29 REPORTER_ASSERT(reporter, !recorder->deviceIsRegistered(devPtr));
30
31 // Test adding multiple devices
32 device1 = Device::Make(recorder.get(), info);
33 sk_sp<Device> device2 = Device::Make(recorder.get(), info);
34 sk_sp<Device> device3 = Device::Make(recorder.get(), info);
35 REPORTER_ASSERT(reporter, device1->recorder() == recorder.get());
36 REPORTER_ASSERT(reporter, device2->recorder() == recorder.get());
37 REPORTER_ASSERT(reporter, device3->recorder() == recorder.get());
38 REPORTER_ASSERT(reporter, recorder->deviceIsRegistered(device1.get()));
39 REPORTER_ASSERT(reporter, recorder->deviceIsRegistered(device2.get()));
40 REPORTER_ASSERT(reporter, recorder->deviceIsRegistered(device3.get()));
41
42 // Test freeing a device in the middle.
43 devPtr = device2.get();
44 device2.reset();
45 REPORTER_ASSERT(reporter, recorder->deviceIsRegistered(device1.get()));
46 REPORTER_ASSERT(reporter, !recorder->deviceIsRegistered(devPtr));
47 REPORTER_ASSERT(reporter, recorder->deviceIsRegistered(device3.get()));
48
49 // Delete the recorder and make sure remaining devices not longer have a valid recorder.
50 recorder.reset();
51 REPORTER_ASSERT(reporter, device1->recorder() == nullptr);
52 REPORTER_ASSERT(reporter, device3->recorder() == nullptr);
53
54 // Make sure freeing Devices after recorder doesn't cause any crash. This would get checked
55 // naturually when these devices go out of scope, but manually reseting will give us a better
56 // stack trace if something does go wrong.
57 device1.reset();
58 device3.reset();
59 }
60