Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrMtlDepthStencil.mm
Go to the documentation of this file.
1/*
2 * Copyright 2018 Google Inc.
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
11
12#if !__has_feature(objc_arc)
13#error This file must be compiled with Arc. Use -fobjc-arc flag
14#endif
15
16GR_NORETAIN_BEGIN
17
18MTLStencilOperation skia_stencil_op_to_mtl(GrStencilOp op) {
19 switch (op) {
21 return MTLStencilOperationKeep;
23 return MTLStencilOperationZero;
25 return MTLStencilOperationReplace;
27 return MTLStencilOperationInvert;
29 return MTLStencilOperationIncrementWrap;
31 return MTLStencilOperationDecrementWrap;
33 return MTLStencilOperationIncrementClamp;
35 return MTLStencilOperationDecrementClamp;
36 }
37}
38
39MTLStencilDescriptor* skia_stencil_to_mtl(GrStencilSettings::Face face) {
40 MTLStencilDescriptor* result = [[MTLStencilDescriptor alloc] init];
41 switch (face.fTest) {
43 result.stencilCompareFunction = MTLCompareFunctionAlways;
44 break;
46 result.stencilCompareFunction = MTLCompareFunctionNever;
47 break;
49 result.stencilCompareFunction = MTLCompareFunctionGreater;
50 break;
52 result.stencilCompareFunction = MTLCompareFunctionGreaterEqual;
53 break;
55 result.stencilCompareFunction = MTLCompareFunctionLess;
56 break;
58 result.stencilCompareFunction = MTLCompareFunctionLessEqual;
59 break;
61 result.stencilCompareFunction = MTLCompareFunctionEqual;
62 break;
64 result.stencilCompareFunction = MTLCompareFunctionNotEqual;
65 break;
66 }
67 result.readMask = face.fTestMask;
68 result.writeMask = face.fWriteMask;
69 result.depthStencilPassOperation = skia_stencil_op_to_mtl(face.fPassOp);
70 result.stencilFailureOperation = skia_stencil_op_to_mtl(face.fFailOp);
71 return result;
72}
73
75 const GrStencilSettings& stencil,
76 GrSurfaceOrigin origin) {
77 MTLDepthStencilDescriptor* desc = [[MTLDepthStencilDescriptor alloc] init];
78 if (!stencil.isDisabled()) {
79 if (stencil.isTwoSided()) {
80 desc.frontFaceStencil = skia_stencil_to_mtl(stencil.postOriginCCWFace(origin));
81 desc.backFaceStencil = skia_stencil_to_mtl(stencil.postOriginCWFace(origin));
82 }
83 else {
84 desc.frontFaceStencil = skia_stencil_to_mtl(stencil.singleSidedFace());
85 desc.backFaceStencil = desc.frontFaceStencil;
86 }
87 }
88
89 return new GrMtlDepthStencil([gpu->device() newDepthStencilStateWithDescriptor: desc],
90 GenerateKey(stencil, origin));
91}
92
94 const int kPassOpShift = 3;
95 const int kFailOpShift = 6;
96
97 faceKey->fReadMask = face.fTestMask;
98 faceKey->fWriteMask = face.fWriteMask;
99
100 SkASSERT(static_cast<int>(face.fTest) <= 7);
101 faceKey->fOps = static_cast<uint32_t>(face.fTest);
102
103 SkASSERT(static_cast<int>(face.fPassOp) <= 7);
104 faceKey->fOps |= (static_cast<uint32_t>(face.fPassOp) << kPassOpShift);
105
106 SkASSERT(static_cast<int>(face.fFailOp) <= 7);
107 faceKey->fOps |= (static_cast<uint32_t>(face.fFailOp) << kFailOpShift);
108}
109
111 GrSurfaceOrigin origin) {
112 Key depthStencilKey;
113
114 if (stencil.isDisabled()) {
115 memset(&depthStencilKey, 0, sizeof(Key));
116 } else {
117 if (stencil.isTwoSided()) {
118 skia_stencil_to_key(stencil.postOriginCCWFace(origin), &depthStencilKey.fFront);
119 skia_stencil_to_key(stencil.postOriginCWFace(origin), &depthStencilKey.fBack);
120 }
121 else {
122 skia_stencil_to_key(stencil.singleSidedFace(), &depthStencilKey.fFront);
123 memcpy(&depthStencilKey.fBack, &depthStencilKey.fFront, sizeof(Key::Face));
124 }
125 }
126
127 return depthStencilKey;
128}
129
130GR_NORETAIN_END
MTLStencilDescriptor * skia_stencil_to_mtl(GrStencilSettings::Face face)
void skia_stencil_to_key(GrStencilSettings::Face face, GrMtlDepthStencil::Key::Face *faceKey)
GR_NORETAIN_BEGIN MTLStencilOperation skia_stencil_op_to_mtl(GrStencilOp op)
GrStencilOp
GrSurfaceOrigin
Definition GrTypes.h:147
#define SkASSERT(cond)
Definition SkAssert.h:116
static Key GenerateKey(const GrStencilSettings &, GrSurfaceOrigin)
static GrMtlDepthStencil * Create(const GrMtlGpu *, const GrStencilSettings &, GrSurfaceOrigin)
id< MTLDevice > device() const
Definition GrMtlGpu.h:49
bool isTwoSided() const
const Face & postOriginCCWFace(GrSurfaceOrigin origin) const
bool isDisabled() const
const Face & singleSidedFace() const
const Face & postOriginCWFace(GrSurfaceOrigin origin) const
GAsyncResult * result