diff options
author | Timothy Liang <timliang@google.com> | 2018-07-31 10:51:17 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-07-31 16:25:55 +0000 |
commit | e30739a1e1d90183bc6bf7414e409e89016881f5 (patch) | |
tree | 4b84aeadee82fde5b45ec75aa1782897e2c9625f /src/gpu/mtl/GrMtlUtil.mm | |
parent | c8ece3d5f877d67114dfda51a336dfdf7838095a (diff) |
implemented copy surface as draw for metal gpu backend
Bug: skia:
Change-Id: Ie61bd6ad9f288b8a025d44f887a0954101a7d710
Reviewed-on: https://skia-review.googlesource.com/142687
Commit-Queue: Timothy Liang <timliang@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
Diffstat (limited to 'src/gpu/mtl/GrMtlUtil.mm')
-rw-r--r-- | src/gpu/mtl/GrMtlUtil.mm | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/gpu/mtl/GrMtlUtil.mm b/src/gpu/mtl/GrMtlUtil.mm index 5a58a259d4..f544421adc 100644 --- a/src/gpu/mtl/GrMtlUtil.mm +++ b/src/gpu/mtl/GrMtlUtil.mm @@ -8,6 +8,15 @@ #include "GrMtlUtil.h" #include "GrTypesPriv.h" +#include "GrSurface.h" +#include "mtl/GrMtlGpu.h" +#include "mtl/GrMtlTexture.h" +#include "mtl/GrMtlRenderTarget.h" +#include "SkSLCompiler.h" + +#import <Metal/Metal.h> + +#define PRINT_MSL 0 // print out the MSL code generated bool GrPixelConfigToMTLFormat(GrPixelConfig config, MTLPixelFormat* format) { MTLPixelFormat dontCare; @@ -143,3 +152,78 @@ MTLTextureDescriptor* GrGetMTLTextureDescriptor(id<MTLTexture> mtlTexture) { texDesc.usage = mtlTexture.usage; return texDesc; } + +#if PRINT_MSL +void print_msl(const char* source) { + SkTArray<SkString> lines; + SkStrSplit(source, "\n", kStrict_SkStrSplitMode, &lines); + for (int i = 0; i < lines.count(); i++) { + SkString& line = lines[i]; + line.prependf("%4i\t", i + 1); + SkDebugf("%s\n", line.c_str()); + } +} +#endif + +id<MTLLibrary> GrCompileMtlShaderLibrary(const GrMtlGpu* gpu, + const char* shaderString, + SkSL::Program::Kind kind, + const SkSL::Program::Settings& settings, + SkSL::Program::Inputs* outInputs) { + std::unique_ptr<SkSL::Program> program = + gpu->shaderCompiler()->convertProgram(kind, + SkSL::String(shaderString), + settings); + + if (!program) { + SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str()); + SkASSERT(false); + } + + *outInputs = program->fInputs; + SkSL::String code; + if (!gpu->shaderCompiler()->toMetal(*program, &code)) { + SkDebugf("%s\n", gpu->shaderCompiler()->errorText().c_str()); + SkASSERT(false); + return nil; + } + NSString* mtlCode = [[NSString alloc] initWithCString: code.c_str() + encoding: NSASCIIStringEncoding]; +#if PRINT_MSL + print_msl([mtlCode cStringUsingEncoding: NSASCIIStringEncoding]); +#endif + + MTLCompileOptions* defaultOptions = [[MTLCompileOptions alloc] init]; + NSError* error = nil; + id<MTLLibrary> compiledLibrary = [gpu->device() newLibraryWithSource: mtlCode + options: defaultOptions + error: &error]; + if (error) { + SkDebugf("Error compiling MSL shader: %s\n", + [[error localizedDescription] cStringUsingEncoding: NSASCIIStringEncoding]); + return nil; + } + return compiledLibrary; +} + +id<MTLTexture> GrGetMTLTextureFromSurface(GrSurface* surface, bool doResolve) { + id<MTLTexture> mtlTexture = nil; + + GrMtlRenderTarget* renderTarget = static_cast<GrMtlRenderTarget*>(surface->asRenderTarget()); + GrMtlTexture* texture; + if (renderTarget) { + if (doResolve) { + // TODO: do resolve and set mtlTexture to resolved texture. As of now, we shouldn't + // have any multisampled render targets. + SkASSERT(false); + } else { + mtlTexture = renderTarget->mtlRenderTexture(); + } + } else { + texture = static_cast<GrMtlTexture*>(surface->asTexture()); + if (texture) { + mtlTexture = texture->mtlTexture(); + } + } + return mtlTexture; +} |