Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/java2d/d3d/D3DGlyphCache.cpp
32288 views
/*1* Copyright (c) 2007, 2008, 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 "D3DGlyphCache.h"26#include "D3DTextRenderer.h"27#include "D3DRenderQueue.h"2829void D3DGlyphCache_FlushGlyphVertexCache();3031// static32HRESULT33D3DGlyphCache::CreateInstance(D3DContext *pCtx, GlyphCacheType gcType,34D3DGlyphCache **ppGlyphCache)35{36HRESULT res;3738J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache::CreateInstance");3940*ppGlyphCache = new D3DGlyphCache(gcType);41if (FAILED(res = (*ppGlyphCache)->Init(pCtx))) {42delete *ppGlyphCache;43*ppGlyphCache = NULL;44}45return res;46}4748D3DGlyphCache::D3DGlyphCache(GlyphCacheType type)49{50J2dTraceLn1(J2D_TRACE_INFO, "D3DGlyphCache::D3DGlyphCache gcType=%d", type);5152pCtx = NULL;53gcType = type;54pGlyphCacheRes = NULL;55pGlyphCache = NULL;56tileFormat = (gcType == CACHE_GRAY) ? TILEFMT_1BYTE_ALPHA : TILEFMT_UNKNOWN;57lastRGBOrder = JNI_FALSE;58}5960D3DGlyphCache::~D3DGlyphCache()61{62J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache::~D3DGlyphCache");6364ReleaseDefPoolResources();6566pCtx = NULL;67if (pGlyphCache != NULL) {68AccelGlyphCache_Free(pGlyphCache);69pGlyphCache = NULL;70}71}7273void74D3DGlyphCache::ReleaseDefPoolResources()75{76J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache::ReleaseDefPoolResources");7778AccelGlyphCache_Invalidate(pGlyphCache);79// REMIND: the glyph cache texture is not in the default pool, so80// this can be optimized not to release the texture81pCtx->GetResourceManager()->ReleaseResource(pGlyphCacheRes);82pGlyphCacheRes = NULL;83}8485HRESULT86D3DGlyphCache::Init(D3DContext *pCtx)87{88D3DFORMAT format;8990RETURN_STATUS_IF_NULL(pCtx, E_FAIL);9192J2dTraceLn1(J2D_TRACE_INFO, "D3DGlyphCache::Init pCtx=%x", pCtx);9394this->pCtx = pCtx;9596if (pGlyphCache == NULL) {97// init glyph cache data structure98pGlyphCache = AccelGlyphCache_Init(D3DTR_CACHE_WIDTH,99D3DTR_CACHE_HEIGHT,100D3DTR_CACHE_CELL_WIDTH,101D3DTR_CACHE_CELL_HEIGHT,102D3DGlyphCache_FlushGlyphVertexCache);103if (pGlyphCache == NULL) {104J2dRlsTraceLn(J2D_TRACE_ERROR,105"D3DGlyphCache::Init: "\106"could not init D3D glyph cache");107return E_FAIL;108}109}110111if (gcType == CACHE_GRAY) {112format = pCtx->IsTextureFormatSupported(D3DFMT_A8) ?113D3DFMT_A8 : D3DFMT_A8R8G8B8;114} else { // gcType == CACHE_LCD115format = pCtx->IsTextureFormatSupported(D3DFMT_R8G8B8) ?116D3DFMT_R8G8B8 : D3DFMT_A8R8G8B8;117}118119HRESULT res = pCtx->GetResourceManager()->120CreateTexture(D3DTR_CACHE_WIDTH, D3DTR_CACHE_HEIGHT,121FALSE/*isRTT*/, FALSE/*isOpaque*/, &format, 0/*usage*/,122&pGlyphCacheRes);123if (FAILED(res)) {124J2dRlsTraceLn(J2D_TRACE_ERROR,125"D3DGlyphCache::Init: "\126"could not create glyph cache texture");127}128129return res;130}131132HRESULT133D3DGlyphCache::AddGlyph(GlyphInfo *glyph)134{135HRESULT res = S_OK;136137RETURN_STATUS_IF_NULL(pGlyphCacheRes, E_FAIL);138139CacheCellInfo *cellInfo = AccelGlyphCache_AddGlyph(pGlyphCache, glyph);140if (cellInfo != NULL) {141jint pixelsTouchedL = 0, pixelsTouchedR = 0;142// store glyph image in texture cell143res = pCtx->UploadTileToTexture(pGlyphCacheRes,144glyph->image,145cellInfo->x, cellInfo->y,1460, 0,147glyph->width, glyph->height,148glyph->rowBytes, tileFormat,149&pixelsTouchedL,150&pixelsTouchedR);151// LCD text rendering optimization: if the number of pixels touched on152// the first or last column of the glyph image is less than 1/3 of the153// height of the glyph we do not consider them touched.154// See D3DTextRenderer.cpp:UpdateCachedDestination for more information.155// The leftOff/rightOff are only used in LCD cache case.156if (gcType == CACHE_LCD) {157jint threshold = glyph->height/3;158159cellInfo->leftOff = pixelsTouchedL < threshold ? 1 : 0;160cellInfo->rightOff = pixelsTouchedR < threshold ? -1 : 0;161} else {162cellInfo->leftOff = 0;163cellInfo->rightOff = 0;164}165}166167return res;168}169170HRESULT171D3DGlyphCache::CheckGlyphCacheByteOrder(jboolean rgbOrder)172{173J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache::CheckGlyphCacheByteOrder");174175if (gcType != CACHE_LCD) {176J2dTraceLn(J2D_TRACE_ERROR, "D3DGlyphCache::CheckGlyphCacheByteOrder"\177" invoked on CACHE_GRAY cache type instance!");178return E_FAIL;179}180181if (rgbOrder != lastRGBOrder) {182// need to invalidate the cache in this case; see comments183// for lastRGBOrder184AccelGlyphCache_Invalidate(pGlyphCache);185lastRGBOrder = rgbOrder;186}187tileFormat = rgbOrder ? TILEFMT_3BYTE_RGB : TILEFMT_3BYTE_BGR;188189return S_OK;190}191192/**193* This method is invoked in the (relatively rare) case where one or194* more glyphs is about to be kicked out of the glyph cache texture.195* Here we simply flush the vertex queue of the current context in case196* any pending vertices are dependent upon the current glyph cache layout.197*/198static void199D3DGlyphCache_FlushGlyphVertexCache()200{201D3DContext *d3dc = D3DRQ_GetCurrentContext();202if (d3dc != NULL) {203J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache_FlushGlyphVertexCache");204d3dc->FlushVertexQueue();205}206}207208209