Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
8262115: Crash on graphics card switch when Metal API validation enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
aghaisas committed Feb 22, 2021
1 parent 80418ed commit 9987487
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
Expand Up @@ -65,6 +65,7 @@ enum Clip {
- (const MTLScissorRect *) getRect; - (const MTLScissorRect *) getRect;


- (void)reset; - (void)reset;
- (void)resetStencilState;
- (void)setClipRectX1:(jint)x1 Y1:(jint)y1 X2:(jint)x2 Y2:(jint)y2; - (void)setClipRectX1:(jint)x1 Y1:(jint)y1 X2:(jint)x2 Y2:(jint)y2;
- (void)beginShapeClip:(BMTLSDOps *)dstOps context:(MTLContext *)mtlc; - (void)beginShapeClip:(BMTLSDOps *)dstOps context:(MTLContext *)mtlc;
- (void)endShapeClip:(BMTLSDOps *)dstOps context:(MTLContext *)mtlc; - (void)endShapeClip:(BMTLSDOps *)dstOps context:(MTLContext *)mtlc;
Expand Down
Expand Up @@ -29,6 +29,7 @@
#include "common.h" #include "common.h"


static MTLRenderPipelineDescriptor * templateStencilPipelineDesc = nil; static MTLRenderPipelineDescriptor * templateStencilPipelineDesc = nil;
static id<MTLDepthStencilState> stencilState = nil;


static void initTemplatePipelineDescriptors() { static void initTemplatePipelineDescriptors() {
if (templateStencilPipelineDesc != nil) if (templateStencilPipelineDesc != nil)
Expand All @@ -50,7 +51,6 @@ static void initTemplatePipelineDescriptors() {
} }


static id<MTLDepthStencilState> getStencilState(id<MTLDevice> device) { static id<MTLDepthStencilState> getStencilState(id<MTLDevice> device) {
static id<MTLDepthStencilState> stencilState = nil;
if (stencilState == nil) { if (stencilState == nil) {
MTLDepthStencilDescriptor* stencilDescriptor; MTLDepthStencilDescriptor* stencilDescriptor;
stencilDescriptor = [[MTLDepthStencilDescriptor new] autorelease]; stencilDescriptor = [[MTLDepthStencilDescriptor new] autorelease];
Expand Down Expand Up @@ -138,6 +138,13 @@ - (void)reset {
_stencilMaskGenerationInProgress = JNI_FALSE; _stencilMaskGenerationInProgress = JNI_FALSE;
} }


- (void)resetStencilState {
if (stencilState != nil) {
[stencilState release];
stencilState = nil;
}
}

- (void)setClipRectX1:(jint)x1 Y1:(jint)y1 X2:(jint)x2 Y2:(jint)y2 { - (void)setClipRectX1:(jint)x1 Y1:(jint)y1 X2:(jint)x2 Y2:(jint)y2 {
if (_clipType == SHAPE_CLIP) { if (_clipType == SHAPE_CLIP) {
_dstOps = NULL; _dstOps = NULL;
Expand Down
Expand Up @@ -172,7 +172,7 @@
M01:(jdouble) m01 M11:(jdouble) m11 M01:(jdouble) m01 M11:(jdouble) m11
M02:(jdouble) m02 M12:(jdouble) m12; M02:(jdouble) m02 M12:(jdouble) m12;



- (void)reset;
- (void)resetPaint; - (void)resetPaint;
- (void)setColorPaint:(int)pixel; - (void)setColorPaint:(int)pixel;
- (void)setGradientPaintUseMask:(jboolean)useMask - (void)setGradientPaintUseMask:(jboolean)useMask
Expand Down
Expand Up @@ -173,6 +173,13 @@ - (void)dealloc {
[super dealloc]; [super dealloc];
} }


- (void) reset {
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLContext : reset");

// Add code for context state reset here
[_clip resetStencilState];
}

- (MTLCommandBufferWrapper *) getCommandBufferWrapper { - (MTLCommandBufferWrapper *) getCommandBufferWrapper {
if (_commandBufferWrapper == nil) { if (_commandBufferWrapper == nil) {
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLContext : commandBuffer is NULL"); J2dTraceLn(J2D_TRACE_VERBOSE, "MTLContext : commandBuffer is NULL");
Expand Down
Expand Up @@ -835,6 +835,7 @@ - (NSString *)description {
return @"unknown-paint"; return @"unknown-paint";
} }


static bool samplersInitialized = false;
static id<MTLSamplerState> samplerNearestClamp = nil; static id<MTLSamplerState> samplerNearestClamp = nil;
static id<MTLSamplerState> samplerLinearClamp = nil; static id<MTLSamplerState> samplerLinearClamp = nil;
static id<MTLSamplerState> samplerNearestRepeat = nil; static id<MTLSamplerState> samplerNearestRepeat = nil;
Expand All @@ -843,8 +844,15 @@ - (NSString *)description {
void initSamplers(id<MTLDevice> device) { void initSamplers(id<MTLDevice> device) {
// TODO: move this code into SamplerManager (need implement) // TODO: move this code into SamplerManager (need implement)


if (samplerNearestClamp != nil) if (samplersInitialized) {
return; // Release old samplers if any
[samplerNearestClamp release];
[samplerLinearClamp release];
[samplerNearestRepeat release];
[samplerLinearRepeat release];

samplersInitialized = false;
}


MTLSamplerDescriptor *samplerDescriptor = [[MTLSamplerDescriptor new] autorelease]; MTLSamplerDescriptor *samplerDescriptor = [[MTLSamplerDescriptor new] autorelease];


Expand All @@ -871,6 +879,8 @@ void initSamplers(id<MTLDevice> device) {
samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear; samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear; samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear;
samplerLinearRepeat = [device newSamplerStateWithDescriptor:samplerDescriptor]; samplerLinearRepeat = [device newSamplerStateWithDescriptor:samplerDescriptor];

samplersInitialized = true;
} }


static void setSampler(id<MTLRenderCommandEncoder> encoder, int interpolation, bool repeat) { static void setSampler(id<MTLRenderCommandEncoder> encoder, int interpolation, bool repeat) {
Expand Down
Expand Up @@ -663,10 +663,18 @@ void MTLRenderQueue_CheckPreviousOp(jint op) {
// invalidate the references to the current context and // invalidate the references to the current context and
// destination surface that are maintained at the native level // destination surface that are maintained at the native level
if (mtlc != NULL) { if (mtlc != NULL) {
[mtlc.encoderManager endEncoder]; commitEncodedCommands();
RESET_PREVIOUS_OP();
[mtlc reset];
} }

MTLTR_FreeGlyphCaches();
if (dstOps != NULL) {
MTLSD_Delete(env, dstOps);
}

mtlc = NULL; mtlc = NULL;
// dstOps = NULL; dstOps = NULL;
break; break;
} }
case sun_java2d_pipe_BufferedOpCodes_SYNC: case sun_java2d_pipe_BufferedOpCodes_SYNC:
Expand Down

0 comments on commit 9987487

Please sign in to comment.