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

Commit

Permalink
8263324: Lanai: use the PtrPixelsRow instead of multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Ushakov committed Mar 10, 2021
1 parent 4639971 commit 084887d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Expand Up @@ -599,11 +599,11 @@ jboolean clipDestCoords(
SurfaceData_InvokeUnlock(env, srcOps, &srcInfo); SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
} }


void copyFromMTLBuffer(void *pDst, id<MTLBuffer> srcBuf, jint offset, jint len, BOOL convertFromArgbPre) { void copyFromMTLBuffer(void *pDst, id<MTLBuffer> srcBuf, NSUInteger offset, NSUInteger len, BOOL convertFromArgbPre) {
char *pSrc = (char*)srcBuf.contents + offset; char *pSrc = (char*)srcBuf.contents + offset;
if (convertFromArgbPre) { if (convertFromArgbPre) {
jint pixelLen = len>>2; NSUInteger pixelLen = len >> 2;
for (int i = 0; i < pixelLen; i++) { for (NSUInteger i = 0; i < pixelLen; i++) {
LoadIntArgbPreTo1IntArgb((jint*)pSrc, 0, i, ((jint*)pDst)[i]); LoadIntArgbPreTo1IntArgb((jint*)pSrc, 0, i, ((jint*)pDst)[i]);
} }
} else { } else {
Expand Down Expand Up @@ -640,6 +640,10 @@ void copyFromMTLBuffer(void *pDst, id<MTLBuffer> srcBuf, jint offset, jint len,
RETURN_IF_NULL(srcOps); RETURN_IF_NULL(srcOps);
RETURN_IF_NULL(dstOps); RETURN_IF_NULL(dstOps);
RETURN_IF_NULL(mtlc); RETURN_IF_NULL(mtlc);
RETURN_IF_TRUE(width < 0);
RETURN_IF_TRUE(height < 0);
NSUInteger w = (NSUInteger)width;
NSUInteger h = (NSUInteger)height;


srcInfo.bounds.x1 = srcx; srcInfo.bounds.x1 = srcx;
srcInfo.bounds.y1 = srcy; srcInfo.bounds.y1 = srcy;
Expand Down Expand Up @@ -675,13 +679,13 @@ void copyFromMTLBuffer(void *pDst, id<MTLBuffer> srcBuf, jint offset, jint len,
width = srcInfo.bounds.x2 - srcInfo.bounds.x1; width = srcInfo.bounds.x2 - srcInfo.bounds.x1;
height = srcInfo.bounds.y2 - srcInfo.bounds.y1; height = srcInfo.bounds.y2 - srcInfo.bounds.y1;


pDst = PtrAddBytes(pDst, dstx * dstInfo.pixelStride); pDst = PtrPixelsRow(pDst, dstx, dstInfo.pixelStride);
pDst = PtrPixelsRow(pDst, dsty, dstInfo.scanStride); pDst = PtrPixelsRow(pDst, dsty, dstInfo.scanStride);


// Metal texture is (0,0) at left-top // Metal texture is (0,0) at left-top
srcx = srcOps->xOffset + srcx; srcx = srcOps->xOffset + srcx;
srcy = srcOps->yOffset + srcy; srcy = srcOps->yOffset + srcy;
const int byteLength = width * height * 4; // NOTE: assume that src format is MTLPixelFormatBGRA8Unorm NSUInteger byteLength = w * h * 4; // NOTE: assume that src format is MTLPixelFormatBGRA8Unorm


// Create MTLBuffer (or use static) // Create MTLBuffer (or use static)
id<MTLBuffer> mtlbuf; id<MTLBuffer> mtlbuf;
Expand All @@ -708,7 +712,7 @@ void copyFromMTLBuffer(void *pDst, id<MTLBuffer> srcBuf, jint offset, jint len,
} }
mtlbuf = mtlIntermediateBuffer; mtlbuf = mtlIntermediateBuffer;
#else // USE_STATIC_BUFFER #else // USE_STATIC_BUFFER
mtlbuf = [mtlc.device newBufferWithLength:width*height*4 options:MTLResourceStorageModeShared]; mtlbuf = [mtlc.device newBufferWithLength:byteLength options:MTLResourceStorageModeShared];
#endif // USE_STATIC_BUFFER #endif // USE_STATIC_BUFFER


// Read from surface into MTLBuffer // Read from surface into MTLBuffer
Expand All @@ -723,10 +727,10 @@ void copyFromMTLBuffer(void *pDst, id<MTLBuffer> srcBuf, jint offset, jint len,
sourceSlice:0 sourceSlice:0
sourceLevel:0 sourceLevel:0
sourceOrigin:MTLOriginMake(srcx, srcy, 0) sourceOrigin:MTLOriginMake(srcx, srcy, 0)
sourceSize:MTLSizeMake(width, height, 1) sourceSize:MTLSizeMake(w, h, 1)
toBuffer:mtlbuf toBuffer:mtlbuf
destinationOffset:0 /*offset already taken in: pDst = PtrAddBytes(pDst, dstx * dstInfo.pixelStride)*/ destinationOffset:0 /*offset already taken in: pDst = PtrPixelsRow(pDst, dstx, dstInfo.pixelStride)*/
destinationBytesPerRow:width*4 destinationBytesPerRow:w*4
destinationBytesPerImage:byteLength]; destinationBytesPerImage:byteLength];
[blitEncoder endEncoding]; [blitEncoder endEncoding];


Expand All @@ -737,7 +741,7 @@ void copyFromMTLBuffer(void *pDst, id<MTLBuffer> srcBuf, jint offset, jint len,
// Perform conversion if necessary // Perform conversion if necessary
BOOL convertFromPre = !RasterFormatInfos[dsttype].isPremult && !srcOps->isOpaque; BOOL convertFromPre = !RasterFormatInfos[dsttype].isPremult && !srcOps->isOpaque;


if ((dstInfo.scanStride == width * dstInfo.pixelStride) && if ((dstInfo.scanStride == w * dstInfo.pixelStride) &&
(height == (dstInfo.bounds.y2 - dstInfo.bounds.y1))) { (height == (dstInfo.bounds.y2 - dstInfo.bounds.y1))) {
// mtlbuf.contents have same dimensions as of pDst // mtlbuf.contents have same dimensions as of pDst
copyFromMTLBuffer(pDst, mtlbuf, 0, byteLength, convertFromPre); copyFromMTLBuffer(pDst, mtlbuf, 0, byteLength, convertFromPre);
Expand All @@ -746,7 +750,7 @@ void copyFromMTLBuffer(void *pDst, id<MTLBuffer> srcBuf, jint offset, jint len,
// copy each row from mtlbuf.contents at appropriate position in pDst // copy each row from mtlbuf.contents at appropriate position in pDst
// Note : pDst is already addjusted for offsets using PtrAddBytes above // Note : pDst is already addjusted for offsets using PtrAddBytes above


int rowSize = width * dstInfo.pixelStride; NSUInteger rowSize = w * dstInfo.pixelStride;
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
copyFromMTLBuffer(pDst, mtlbuf, y * rowSize, rowSize, convertFromPre); copyFromMTLBuffer(pDst, mtlbuf, y * rowSize, rowSize, convertFromPre);
pDst = PtrAddBytes(pDst, dstInfo.scanStride); pDst = PtrAddBytes(pDst, dstInfo.scanStride);
Expand Down
Expand Up @@ -175,13 +175,13 @@ - (id)init:(jboolean)nonPremult shortData:(jboolean)shortData
} else if (numBands == 3) { } else if (numBands == 3) {
// user supplied band for each of R/G/B; alpha band is unused // user supplied band for each of R/G/B; alpha band is unused
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
bands[i] = PtrAddBytes(tableValues, i*bandLength*bytesPerElem); bands[i] = PtrPixelsBand(tableValues, i, bandLength, bytesPerElem);
} }
bands[3] = NULL; bands[3] = NULL;
} else if (numBands == 4) { } else if (numBands == 4) {
// user supplied band for each of R/G/B/A // user supplied band for each of R/G/B/A
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
bands[i] = PtrAddBytes(tableValues, i*bandLength*bytesPerElem); bands[i] = PtrPixelsBand(tableValues, i, bandLength, bytesPerElem);
} }
} }


Expand Down
Expand Up @@ -485,6 +485,9 @@ extern struct _CompositeTypes {
#define PtrPixelsRow(p, y, scanStride) PtrAddBytes(p, \ #define PtrPixelsRow(p, y, scanStride) PtrAddBytes(p, \
((intptr_t) (y)) * (scanStride)) ((intptr_t) (y)) * (scanStride))


#define PtrPixelsBand(p, y, length, elemSize) PtrAddBytes(p, \
((intptr_t) (y)) * (length) * (elemSize))

/* /*
* The function to call with an array of NativePrimitive structures * The function to call with an array of NativePrimitive structures
* to register them with the Java GraphicsPrimitiveMgr. * to register them with the Java GraphicsPrimitiveMgr.
Expand Down

0 comments on commit 084887d

Please sign in to comment.