1/* 2 * Copyright 2024 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#include "include/core/SkString.h" 8#include "include/gpu/MutableTextureState.h" 9#include "include/gpu/graphite/mtl/MtlGraphiteTypes.h" 10#include "src/gpu/graphite/BackendTexturePriv.h" 11#include "src/gpu/graphite/mtl/MtlGraphiteTypesPriv.h" 12#include "src/gpu/mtl/MtlUtilsPriv.h" 13 14#include <cstdint> 15 16#import <Metal/Metal.h> 17 18namespace skgpu::graphite { 19 20class MtlBackendTextureData final : public BackendTextureData { 21public: 22 MtlBackendTextureData(CFTypeRef tex) : fMtlTexture(tex) {} 23 24#if defined(SK_DEBUG) 25 skgpu::BackendApi type() const override { return skgpu::BackendApi::kMetal; } 26#endif 27 28 CFTypeRef texture() const { return fMtlTexture; } 29 30private: 31 CFTypeRef fMtlTexture; 32 33 void copyTo(AnyBackendTextureData& dstData) const override { 34 // Don't assert that dstData is a metal type because it could be 35 // uninitialized and that assert would fail. 36 dstData.emplace<MtlBackendTextureData>(fMtlTexture); 37 } 38 39 bool equal(const BackendTextureData* that) const override { 40 SkASSERT(!that || that->type() == skgpu::BackendApi::kMetal); 41 if (auto otherMtl = static_cast<const MtlBackendTextureData*>(that)) { 42 return fMtlTexture == otherMtl->fMtlTexture; 43 } 44 return false; 45 } 46}; 47 48static const MtlBackendTextureData* get_and_cast_data(const BackendTexture& tex) { 49 auto data = BackendTexturePriv::GetData(tex); 50 SkASSERT(!data || data->type() == skgpu::BackendApi::kMetal); 51 return static_cast<const MtlBackendTextureData*>(data); 52} 53 54namespace BackendTextures { 55BackendTexture MakeMetal(SkISize dimensions, CFTypeRef mtlTexture) { 56 return BackendTexturePriv::Make( 57 dimensions, TextureInfos::MakeMetal(mtlTexture), MtlBackendTextureData(mtlTexture)); 58} 59 60CFTypeRef GetMtlTexture(const BackendTexture& tex) { 61 if (!tex.isValid() || tex.backend() != skgpu::BackendApi::kMetal) { 62 return nullptr; 63 } 64 const MtlBackendTextureData* mtlData = get_and_cast_data(tex); 65 SkASSERT(mtlData); 66 return mtlData->texture(); 67} 68 69} // namespace BackendTextures 70 71} // namespace skgpu::graphite 72