Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLBufImgOps.m
41159 views
/*1* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <jlong.h>2627#include "MTLBufImgOps.h"28#include "MTLContext.h"29#include "MTLRenderQueue.h"30#include "MTLSurfaceDataBase.h"31#include "GraphicsPrimitiveMgr.h"3233@implementation MTLRescaleOp {34jboolean _isNonPremult;35jfloat _normScaleFactors[4];36jfloat _normOffsets[4];37}3839-(jfloat *) getScaleFactors {40return _normScaleFactors;41}42-(jfloat *) getOffsets {43return _normOffsets;44}4546- (id)init:(jboolean)isNonPremult factors:(unsigned char *)factors offsets:(unsigned char *)offsets {47self = [super init];48if (self) {49J2dTraceLn1(J2D_TRACE_INFO,"Created MTLRescaleOp: isNonPremult=%d", isNonPremult);5051_isNonPremult = isNonPremult;52_normScaleFactors[0] = NEXT_FLOAT(factors);53_normScaleFactors[1] = NEXT_FLOAT(factors);54_normScaleFactors[2] = NEXT_FLOAT(factors);55_normScaleFactors[3] = NEXT_FLOAT(factors);56_normOffsets[0] = NEXT_FLOAT(offsets);57_normOffsets[1] = NEXT_FLOAT(offsets);58_normOffsets[2] = NEXT_FLOAT(offsets);59_normOffsets[3] = NEXT_FLOAT(offsets);60}61return self;62}6364- (NSString *)getDescription {65return [NSString stringWithFormat:@"rescale: nonPremult=%d", _isNonPremult];66}67@end6869@implementation MTLConvolveOp {70id<MTLBuffer> _buffer;71float _imgEdge[4];72int _kernelSize;73jboolean _isEdgeZeroFill;74}7576- (id)init:(jboolean)edgeZeroFill kernelWidth:(jint)kernelWidth77kernelHeight:(jint)kernelHeight78srcWidth:(jint)srcWidth79srcHeight:(jint)srcHeight80kernel:(unsigned char *)kernel81device:(id<MTLDevice>)device {82self = [super init];83if (self) {84J2dTraceLn2(J2D_TRACE_INFO,"Created MTLConvolveOp: kernelW=%d kernelH=%d", kernelWidth, kernelHeight);85_isEdgeZeroFill = edgeZeroFill;8687_kernelSize = kernelWidth * kernelHeight;88_buffer = [device newBufferWithLength:_kernelSize*sizeof(vector_float3) options:MTLResourceStorageModeShared];8990float * kernelVals = [_buffer contents];91int kIndex = 0;92for (int i = -kernelHeight/2; i < kernelHeight/2+1; i++) {93for (int j = -kernelWidth/2; j < kernelWidth/2+1; j++) {94kernelVals[kIndex+0] = j/(float)srcWidth;95kernelVals[kIndex+1] = i/(float)srcHeight;96kernelVals[kIndex+2] = NEXT_FLOAT(kernel);97kIndex += 3;98}99}100101_imgEdge[0] = (kernelWidth/2)/(float)srcWidth;102_imgEdge[1] = (kernelHeight/2)/(float)srcHeight;103_imgEdge[2] = 1 - _imgEdge[0];104_imgEdge[3] = 1 - _imgEdge[1];105}106return self;107}108109- (void) dealloc {110[_buffer release];111[super dealloc];112}113114- (id<MTLBuffer>) getBuffer {115return _buffer;116}117118- (const float *) getImgEdge {119return _imgEdge;120}121122- (NSString *)getDescription {123return [NSString stringWithFormat:@"convolve: isEdgeZeroFill=%d", _isEdgeZeroFill];124}125@end126127128@implementation MTLLookupOp {129float _offset[4];130jboolean _isUseSrcAlpha;131jboolean _isNonPremult;132133id<MTLTexture> _lookupTex;134}135136- (id)init:(jboolean)nonPremult shortData:(jboolean)shortData137numBands:(jint)numBands138bandLength:(jint)bandLength139offset:(jint)offset140tableValues:(void *)tableValues141device:(id<MTLDevice>)device {142self = [super init];143if (self) {144J2dTraceLn4(J2D_TRACE_INFO,"Created MTLLookupOp: short=%d num=%d len=%d off=%d",145shortData, numBands, bandLength, offset);146147_isUseSrcAlpha = numBands != 4;148_isNonPremult = nonPremult;149150_offset[0] = offset / 255.0f;151_offset[1] = _offset[0];152_offset[2] = _offset[0];153_offset[3] = _offset[0];154155MTLTextureDescriptor *textureDescriptor =156[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatA8Unorm157width:(NSUInteger)256158height:(NSUInteger)4159mipmapped:NO];160161_lookupTex = [device newTextureWithDescriptor:textureDescriptor];162163void *bands[4];164for (int i = 0; i < 4; i++) {165bands[i] = NULL;166}167int bytesPerElem = (shortData ? 2 : 1);168if (numBands == 1) {169// replicate the single band for R/G/B; alpha band is unused170for (int i = 0; i < 3; i++) {171bands[i] = tableValues;172}173bands[3] = NULL;174} else if (numBands == 3) {175// user supplied band for each of R/G/B; alpha band is unused176for (int i = 0; i < 3; i++) {177bands[i] = PtrPixelsBand(tableValues, i, bandLength, bytesPerElem);178}179bands[3] = NULL;180} else if (numBands == 4) {181// user supplied band for each of R/G/B/A182for (int i = 0; i < 4; i++) {183bands[i] = PtrPixelsBand(tableValues, i, bandLength, bytesPerElem);184}185}186187for (int i = 0; i < 4; i++) {188if (bands[i] == NULL)189continue;190191MTLRegion region = {192{0, i, 0},193{bandLength, 1,1}194};195196[_lookupTex replaceRegion:region197mipmapLevel:0198withBytes:bands[i]199bytesPerRow:bandLength*bytesPerElem];200}201}202return self;203}204205- (void) dealloc {206[_lookupTex release];207[super dealloc];208}209210- (jfloat *) getOffset {211return _offset;212}213214- (id<MTLTexture>) getLookupTexture {215return _lookupTex;216}217218- (NSString *)getDescription {219return [NSString stringWithFormat:@"lookup: offset=%f", _offset[0]];220}221222@end223224225