xref: /aosp_15_r20/external/skia/tests/graphite/MtlBackendTextureTest.mm (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker/*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2021 Google LLC
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker#include "tests/Test.h"
9*c8dee2aaSAndroid Build Coastguard Worker
10*c8dee2aaSAndroid Build Coastguard Worker#include "include/gpu/graphite/BackendTexture.h"
11*c8dee2aaSAndroid Build Coastguard Worker#include "include/gpu/graphite/Context.h"
12*c8dee2aaSAndroid Build Coastguard Worker#include "include/gpu/graphite/Recorder.h"
13*c8dee2aaSAndroid Build Coastguard Worker#include "include/gpu/graphite/mtl/MtlGraphiteTypes.h"
14*c8dee2aaSAndroid Build Coastguard Worker
15*c8dee2aaSAndroid Build Coastguard Worker#import <Metal/Metal.h>
16*c8dee2aaSAndroid Build Coastguard Worker
17*c8dee2aaSAndroid Build Coastguard Workerusing namespace skgpu::graphite;
18*c8dee2aaSAndroid Build Coastguard Worker
19*c8dee2aaSAndroid Build Coastguard Workernamespace {
20*c8dee2aaSAndroid Build Coastguard Worker    const SkISize kSize = {16, 16};
21*c8dee2aaSAndroid Build Coastguard Worker}
22*c8dee2aaSAndroid Build Coastguard Worker
23*c8dee2aaSAndroid Build Coastguard WorkerDEF_GRAPHITE_TEST_FOR_METAL_CONTEXT(MtlBackendTextureTest, reporter, context, testContext) {
24*c8dee2aaSAndroid Build Coastguard Worker    auto recorder = context->makeRecorder();
25*c8dee2aaSAndroid Build Coastguard Worker
26*c8dee2aaSAndroid Build Coastguard Worker    MtlTextureInfo textureInfo;
27*c8dee2aaSAndroid Build Coastguard Worker    textureInfo.fSampleCount = 1;
28*c8dee2aaSAndroid Build Coastguard Worker    textureInfo.fMipmapped = skgpu::Mipmapped::kNo;
29*c8dee2aaSAndroid Build Coastguard Worker    textureInfo.fFormat = MTLPixelFormatRGBA8Unorm;
30*c8dee2aaSAndroid Build Coastguard Worker    textureInfo.fStorageMode = MTLStorageModePrivate;
31*c8dee2aaSAndroid Build Coastguard Worker    textureInfo.fUsage = MTLTextureUsageShaderRead;
32*c8dee2aaSAndroid Build Coastguard Worker
33*c8dee2aaSAndroid Build Coastguard Worker    // TODO: For now we are just testing the basic case of RGBA single sample because that is what
34*c8dee2aaSAndroid Build Coastguard Worker    // we've added to the backend. However, once we expand the backend support to handle all the
35*c8dee2aaSAndroid Build Coastguard Worker    // formats this test should iterate over a large set of combinations. See the Ganesh
36*c8dee2aaSAndroid Build Coastguard Worker    // MtlBackendAllocationTest for example of doing this.
37*c8dee2aaSAndroid Build Coastguard Worker
38*c8dee2aaSAndroid Build Coastguard Worker    auto beTexture = recorder->createBackendTexture(kSize, TextureInfos::MakeMetal(textureInfo));
39*c8dee2aaSAndroid Build Coastguard Worker    REPORTER_ASSERT(reporter, beTexture.isValid());
40*c8dee2aaSAndroid Build Coastguard Worker    recorder->deleteBackendTexture(beTexture);
41*c8dee2aaSAndroid Build Coastguard Worker
42*c8dee2aaSAndroid Build Coastguard Worker    // It should also pass if we set the usage to be a render target
43*c8dee2aaSAndroid Build Coastguard Worker    textureInfo.fUsage |= MTLTextureUsageRenderTarget;
44*c8dee2aaSAndroid Build Coastguard Worker    beTexture = recorder->createBackendTexture(kSize, TextureInfos::MakeMetal(textureInfo));
45*c8dee2aaSAndroid Build Coastguard Worker    REPORTER_ASSERT(reporter, beTexture.isValid());
46*c8dee2aaSAndroid Build Coastguard Worker    recorder->deleteBackendTexture(beTexture);
47*c8dee2aaSAndroid Build Coastguard Worker
48*c8dee2aaSAndroid Build Coastguard Worker    // It should fail with a format that isn't one of our supported formats
49*c8dee2aaSAndroid Build Coastguard Worker    textureInfo.fFormat = MTLPixelFormatRGB9E5Float;
50*c8dee2aaSAndroid Build Coastguard Worker    beTexture = recorder->createBackendTexture(kSize, TextureInfos::MakeMetal(textureInfo));
51*c8dee2aaSAndroid Build Coastguard Worker    REPORTER_ASSERT(reporter, !beTexture.isValid());
52*c8dee2aaSAndroid Build Coastguard Worker    recorder->deleteBackendTexture(beTexture);
53*c8dee2aaSAndroid Build Coastguard Worker
54*c8dee2aaSAndroid Build Coastguard Worker    // It should fail with a sample count greater than 1
55*c8dee2aaSAndroid Build Coastguard Worker    textureInfo.fFormat = MTLPixelFormatRGBA8Unorm;
56*c8dee2aaSAndroid Build Coastguard Worker    textureInfo.fSampleCount = 4;
57*c8dee2aaSAndroid Build Coastguard Worker    beTexture = recorder->createBackendTexture(kSize, TextureInfos::MakeMetal(textureInfo));
58*c8dee2aaSAndroid Build Coastguard Worker    REPORTER_ASSERT(reporter, !beTexture.isValid());
59*c8dee2aaSAndroid Build Coastguard Worker    recorder->deleteBackendTexture(beTexture);
60*c8dee2aaSAndroid Build Coastguard Worker}
61