@@ -800,23 +800,45 @@ void copyFromMTLBuffer(void *pDst, id<MTLBuffer> srcBuf, NSUInteger offset, NSUI
800800 (dstBounds.x1 < dstBounds.x2 && dstBounds.y1 < dstBounds.y2 ))
801801 {
802802 @autoreleasepool {
803- id <MTLCommandBuffer > cb = [mtlc createCommandBuffer ];
804- id <MTLBlitCommandEncoder > blitEncoder = [cb blitCommandEncoder ];
805-
806- // Create an intrermediate buffer
807- int totalBuffsize = srcWidth * srcHeight * 4 ;
808- id <MTLBuffer > buff = [[mtlc.device newBufferWithLength: totalBuffsize options: MTLResourceStorageModePrivate ] autorelease ];
809-
810- [blitEncoder copyFromTexture: dstOps->pTexture
811- sourceSlice: 0 sourceLevel: 0 sourceOrigin: MTLOriginMake (srcBounds.x1, srcBounds.y1, 0 ) sourceSize: MTLSizeMake (srcWidth, srcHeight, 1 )
812- toBuffer: buff destinationOffset: 0 destinationBytesPerRow: (srcWidth * 4 ) destinationBytesPerImage: totalBuffsize];
813-
814- [blitEncoder copyFromBuffer: buff
815- sourceOffset: 0 sourceBytesPerRow: srcWidth*4 sourceBytesPerImage: totalBuffsize sourceSize: MTLSizeMake (srcWidth, srcHeight, 1 )
816- toTexture: dstOps->pTexture destinationSlice: 0 destinationLevel: 0 destinationOrigin: MTLOriginMake (dstBounds.x1, dstBounds.y1, 0 )];
817- [blitEncoder endEncoding ];
818-
819- [cb commit ];
803+ struct TxtVertex quadTxVerticesBuffer[6 ];
804+ MTLPooledTextureHandle * interHandle =
805+ [mtlc.texturePool getTexture: texWidth
806+ height: texHeight
807+ format: MTLPixelFormatBGRA8Unorm ];
808+ if (interHandle == nil ) {
809+ J2dTraceLn (J2D_TRACE_ERROR,
810+ " MTLBlitLoops_CopyArea: texture handle is null" );
811+ return ;
812+ }
813+ [[mtlc getCommandBufferWrapper ] registerPooledTexture: interHandle];
814+
815+ id <MTLTexture > interTexture = interHandle.texture ;
816+
817+ /*
818+ * We need to consider common states like clipping while
819+ * performing copyArea, thats why we use drawTex2Tex and
820+ * get encoder with appropriate state from EncoderManager
821+ * and not directly use MTLBlitCommandEncoder for texture copy.
822+ */
823+
824+ // copy content to intermediate texture
825+ drawTex2Tex (mtlc, dstOps->pTexture , interTexture, dstOps->isOpaque ,
826+ JNI_FALSE, INTERPOLATION_NEAREST_NEIGHBOR,
827+ 0 , 0 , texWidth, texHeight, 0 , 0 , texWidth, texHeight);
828+
829+ // copy content with appropriate bounds to destination texture
830+ drawTex2Tex (mtlc, interTexture, dstOps->pTexture , JNI_FALSE,
831+ dstOps->isOpaque , INTERPOLATION_NEAREST_NEIGHBOR,
832+ srcBounds.x1 , srcBounds.y1 , srcBounds.x2 , srcBounds.y2 ,
833+ dstBounds.x1 , dstBounds.y1 , dstBounds.x2 , dstBounds.y2 );
834+ [mtlc.encoderManager endEncoder ];
835+ MTLCommandBufferWrapper * cbwrapper =
836+ [mtlc pullCommandBufferWrapper ];
837+ id <MTLCommandBuffer > commandbuf = [cbwrapper getCommandBuffer ];
838+ [commandbuf addCompletedHandler: ^(id <MTLCommandBuffer > commandbuf) {
839+ [cbwrapper release ];
840+ }];
841+ [commandbuf commit ];
820842 }
821843 }
822844}
0 commit comments