Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLVertexCache.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 <stdlib.h>26#include <string.h>2728#include "sun_java2d_SunGraphics2D.h"2930#include "MTLPaints.h"31#include "MTLVertexCache.h"32#include "MTLTexturePool.h"33#include "MTLTextRenderer.h"34#include "common.h"3536typedef struct _J2DVertex {37float position[2];38float txtpos[2];39} J2DVertex;4041static J2DVertex *vertexCache = NULL;42static jint vertexCacheIndex = 0;4344static MTLPooledTextureHandle * maskCacheTex = NULL;45static jint maskCacheIndex = 0;46static id<MTLRenderCommandEncoder> encoder = NULL;4748#define MTLVC_ADD_VERTEX(TX, TY, DX, DY, DZ) \49do { \50J2DVertex *v = &vertexCache[vertexCacheIndex++]; \51v->txtpos[0] = TX; \52v->txtpos[1] = TY; \53v->position[0]= DX; \54v->position[1] = DY; \55} while (0)5657#define MTLVC_ADD_TRIANGLES(TX1, TY1, TX2, TY2, DX1, DY1, DX2, DY2) \58do { \59MTLVC_ADD_VERTEX(TX1, TY1, DX1, DY1, 0); \60MTLVC_ADD_VERTEX(TX2, TY1, DX2, DY1, 0); \61MTLVC_ADD_VERTEX(TX2, TY2, DX2, DY2, 0); \62MTLVC_ADD_VERTEX(TX2, TY2, DX2, DY2, 0); \63MTLVC_ADD_VERTEX(TX1, TY2, DX1, DY2, 0); \64MTLVC_ADD_VERTEX(TX1, TY1, DX1, DY1, 0); \65} while (0)6667jboolean68MTLVertexCache_InitVertexCache()69{70J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_InitVertexCache");7172if (vertexCache == NULL) {73J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_InitVertexCache : vertexCache == NULL");74vertexCache = (J2DVertex *)malloc(MTLVC_MAX_INDEX * sizeof(J2DVertex));75if (vertexCache == NULL) {76return JNI_FALSE;77}78}7980return JNI_TRUE;81}8283void84MTLVertexCache_FlushVertexCache(MTLContext *mtlc)85{86J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_FlushVertexCache");8788if (vertexCacheIndex > 0) {89[encoder setVertexBytes: vertexCache length:vertexCacheIndex * sizeof(J2DVertex)90atIndex:MeshVertexBuffer];9192[encoder setFragmentTexture:maskCacheTex.texture atIndex: 0];93J2dTraceLn1(J2D_TRACE_INFO,94"MTLVertexCache_FlushVertexCache : encode %d characters", (vertexCacheIndex / 6));95[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:vertexCacheIndex];96}97vertexCacheIndex = 0;98maskCacheIndex = 0;99100if (maskCacheTex != nil) {101[[mtlc getCommandBufferWrapper] registerPooledTexture:maskCacheTex];102[maskCacheTex release];103maskCacheTex = nil;104}105}106107void108MTLVertexCache_FlushGlyphVertexCache()109{110J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_FlushGlyphVertexCache");111112if (vertexCacheIndex > 0) {113[encoder setVertexBytes: vertexCache length:vertexCacheIndex * sizeof(J2DVertex)114atIndex:MeshVertexBuffer];115id<MTLTexture> glyphCacheTex = MTLTR_GetGlyphCacheTexture();116[encoder setFragmentTexture:glyphCacheTex atIndex: 0];117J2dTraceLn1(J2D_TRACE_INFO,118"MTLVertexCache_FlushGlyphVertexCache : encode %d characters", (vertexCacheIndex / 6));119[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:vertexCacheIndex];120}121vertexCacheIndex = 0;122}123124void MTLVertexCache_FreeVertexCache()125{126free(vertexCache);127vertexCache = NULL;128}129130static jboolean131MTLVertexCache_InitMaskCache(MTLContext *mtlc) {132J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_InitMaskCache");133if (maskCacheTex == NULL) {134maskCacheTex = [mtlc.texturePool getTexture:MTLVC_MASK_CACHE_WIDTH_IN_TEXELS135height:MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS136format:MTLPixelFormatA8Unorm];137[maskCacheTex retain];138if (maskCacheTex == nil) {139J2dTraceLn(J2D_TRACE_ERROR, "MTLVertexCache_InitMaskCache: can't obtain temporary texture object from pool");140return JNI_FALSE;141}142}143// init special fully opaque tile in the upper-right corner of144// the mask cache texture145146char tile[MTLVC_MASK_CACHE_TILE_SIZE];147memset(tile, 0xff, MTLVC_MASK_CACHE_TILE_SIZE);148149jint texx = MTLVC_MASK_CACHE_TILE_WIDTH * (MTLVC_MASK_CACHE_WIDTH_IN_TILES - 1);150151jint texy = MTLVC_MASK_CACHE_TILE_HEIGHT * (MTLVC_MASK_CACHE_HEIGHT_IN_TILES - 1);152153NSUInteger bytesPerRow = 1 * MTLVC_MASK_CACHE_TILE_WIDTH;154155MTLRegion region = {156{texx, texy, 0},157{MTLVC_MASK_CACHE_TILE_WIDTH, MTLVC_MASK_CACHE_TILE_HEIGHT, 1}158};159160161// do we really need this??162[maskCacheTex.texture replaceRegion:region163mipmapLevel:0164withBytes:tile165bytesPerRow:bytesPerRow];166167return JNI_TRUE;168}169170void171MTLVertexCache_EnableMaskCache(MTLContext *mtlc, BMTLSDOps *dstOps)172{173J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_EnableMaskCache");174175if (!MTLVertexCache_InitVertexCache()) {176return;177}178179if (maskCacheTex == NULL) {180if (!MTLVertexCache_InitMaskCache(mtlc)) {181return;182}183}184MTLVertexCache_CreateSamplingEncoder(mtlc, dstOps);185}186187void188MTLVertexCache_DisableMaskCache(MTLContext *mtlc)189{190// TODO : Once we enable check_previous_op191// we will start using DisableMaskCache until then192// we are force flushing vertexcache.193J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_DisableMaskCache");194MTLVertexCache_FlushVertexCache(mtlc);195maskCacheIndex = 0;196free(vertexCache);197vertexCache = NULL;198}199200void201MTLVertexCache_CreateSamplingEncoder(MTLContext *mtlc, BMTLSDOps *dstOps) {202J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_CreateSamplingEncoder");203encoder = [mtlc.encoderManager getTextEncoder:dstOps204isSrcOpaque:NO];205}206207void208MTLVertexCache_AddMaskQuad(MTLContext *mtlc,209jint srcx, jint srcy,210jint dstx, jint dsty,211jint width, jint height,212jint maskscan, void *mask,213BMTLSDOps *dstOps)214{215jfloat tx1, ty1, tx2, ty2;216jfloat dx1, dy1, dx2, dy2;217218J2dTraceLn1(J2D_TRACE_INFO, "MTLVertexCache_AddMaskQuad: %d",219maskCacheIndex);220221if (maskCacheIndex >= MTLVC_MASK_CACHE_MAX_INDEX)222{223J2dTraceLn2(J2D_TRACE_INFO, "maskCacheIndex = %d, vertexCacheIndex = %d", maskCacheIndex, vertexCacheIndex);224MTLVertexCache_FlushVertexCache(mtlc);225MTLVertexCache_EnableMaskCache(mtlc, dstOps);226maskCacheIndex = 0;227}228229if (mask != NULL) {230jint texx = MTLVC_MASK_CACHE_TILE_WIDTH *231(maskCacheIndex % MTLVC_MASK_CACHE_WIDTH_IN_TILES);232jint texy = MTLVC_MASK_CACHE_TILE_HEIGHT *233(maskCacheIndex / MTLVC_MASK_CACHE_WIDTH_IN_TILES);234J2dTraceLn5(J2D_TRACE_INFO, "texx = %d texy = %d width = %d height = %d maskscan = %d", texx, texy, width,235height, maskscan);236NSUInteger bytesPerRow = 1 * width;237NSUInteger slice = bytesPerRow * srcy + srcx;238MTLRegion region = {239{texx, texy, 0},240{width, height, 1}241};242243// Whenever we have source stride bigger that destination stride244// we need to pick appropriate source subtexture. In repalceRegion245// we can give destination subtexturing properly but we can't246// subtexture from system memory glyph we have. So in such247// cases we are creating seperate tile and scan the source248// stride into destination using memcpy. In case of OpenGL we249// can update source pointers, in case of D3D we ar doing memcpy.250// We can use MTLBuffer and then copy source subtexture but that251// adds extra blitting logic.252// TODO : Research more and try removing memcpy logic.253if (maskscan <= width) {254int height_offset = bytesPerRow * srcy;255[maskCacheTex.texture replaceRegion:region256mipmapLevel:0257withBytes:mask + height_offset258bytesPerRow:bytesPerRow];259} else {260int dst_offset, src_offset;261int size = 1 * width * height;262char tile[size];263dst_offset = 0;264for (int i = srcy; i < srcy + height; i++) {265J2dTraceLn2(J2D_TRACE_INFO, "srcx = %d srcy = %d", srcx, srcy);266src_offset = maskscan * i + srcx;267J2dTraceLn2(J2D_TRACE_INFO, "src_offset = %d dst_offset = %d", src_offset, dst_offset);268memcpy(tile + dst_offset, mask + src_offset, width);269dst_offset = dst_offset + width;270}271[maskCacheTex.texture replaceRegion:region272mipmapLevel:0273withBytes:tile274bytesPerRow:bytesPerRow];275}276277tx1 = ((jfloat) texx) / MTLVC_MASK_CACHE_WIDTH_IN_TEXELS;278ty1 = ((jfloat) texy) / MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS;279} else {280tx1 = ((jfloat)MTLVC_MASK_CACHE_SPECIAL_TILE_X) /281MTLVC_MASK_CACHE_WIDTH_IN_TEXELS;282ty1 = ((jfloat)MTLVC_MASK_CACHE_SPECIAL_TILE_Y) /283MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS;284}285maskCacheIndex++;286287tx2 = tx1 + (((jfloat)width) / MTLVC_MASK_CACHE_WIDTH_IN_TEXELS);288ty2 = ty1 + (((jfloat)height) / MTLVC_MASK_CACHE_HEIGHT_IN_TEXELS);289290dx1 = (jfloat)dstx;291dy1 = (jfloat)dsty;292dx2 = dx1 + width;293dy2 = dy1 + height;294295J2dTraceLn8(J2D_TRACE_INFO, "tx1 = %f ty1 = %f tx2 = %f ty2 = %f dx1 = %f dy1 = %f dx2 = %f dy2 = %f", tx1, ty1, tx2, ty2, dx1, dy1, dx2, dy2);296MTLVC_ADD_TRIANGLES(tx1, ty1, tx2, ty2,297dx1, dy1, dx2, dy2);298}299300void301MTLVertexCache_AddGlyphQuad(MTLContext *mtlc,302jfloat tx1, jfloat ty1, jfloat tx2, jfloat ty2,303jfloat dx1, jfloat dy1, jfloat dx2, jfloat dy2)304{305J2dTraceLn(J2D_TRACE_INFO, "MTLVertexCache_AddGlyphQuad");306307if (vertexCacheIndex >= MTLVC_MAX_INDEX)308{309J2dTraceLn2(J2D_TRACE_INFO, "maskCacheIndex = %d, vertexCacheIndex = %d", maskCacheIndex, vertexCacheIndex);310MTLVertexCache_FlushGlyphVertexCache();311}312313MTLVC_ADD_TRIANGLES(tx1, ty1, tx2, ty2,314dx1, dy1, dx2, dy2);315}316317318