aboutsummaryrefslogtreecommitdiff
path: root/src/windows/native/sun/java2d/d3d/D3DGlyphCache.cpp
blob: 09cb1f5953ba210f97a5aec2b3c08294a56f0dd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

#include "D3DGlyphCache.h"
#include "D3DTextRenderer.h"
#include "D3DRenderQueue.h"

void D3DGlyphCache_FlushGlyphVertexCache();

// static
HRESULT
D3DGlyphCache::CreateInstance(D3DContext *pCtx, GlyphCacheType gcType,
                              D3DGlyphCache **ppGlyphCache)
{
    HRESULT res;

    J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache::CreateInstance");

    *ppGlyphCache = new D3DGlyphCache(gcType);
    if (FAILED(res = (*ppGlyphCache)->Init(pCtx))) {
        delete *ppGlyphCache;
        *ppGlyphCache = NULL;
    }
    return res;
}

D3DGlyphCache::D3DGlyphCache(GlyphCacheType type)
{
    J2dTraceLn1(J2D_TRACE_INFO, "D3DGlyphCache::D3DGlyphCache gcType=%d", type);

    pCtx = NULL;
    gcType = type;
    pGlyphCacheRes = NULL;
    pGlyphCache = NULL;
    tileFormat = (gcType == CACHE_GRAY) ? TILEFMT_1BYTE_ALPHA : TILEFMT_UNKNOWN;
    lastRGBOrder = JNI_FALSE;
}

D3DGlyphCache::~D3DGlyphCache()
{
    J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache::~D3DGlyphCache");

    ReleaseDefPoolResources();

    pCtx = NULL;
    if (pGlyphCache != NULL) {
        AccelGlyphCache_Free(pGlyphCache);
        pGlyphCache = NULL;
    }
}

void
D3DGlyphCache::ReleaseDefPoolResources()
{
    J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache::ReleaseDefPoolResources");

    AccelGlyphCache_Invalidate(pGlyphCache);
    // REMIND: the glyph cache texture is not in the default pool, so
    // this can be optimized not to release the texture
    pCtx->GetResourceManager()->ReleaseResource(pGlyphCacheRes);
    pGlyphCacheRes = NULL;
}

HRESULT
D3DGlyphCache::Init(D3DContext *pCtx)
{
    D3DFORMAT format;

    RETURN_STATUS_IF_NULL(pCtx, E_FAIL);

    J2dTraceLn1(J2D_TRACE_INFO, "D3DGlyphCache::Init pCtx=%x", pCtx);

    this->pCtx = pCtx;

    if (pGlyphCache == NULL) {
        // init glyph cache data structure
        pGlyphCache = AccelGlyphCache_Init(D3DTR_CACHE_WIDTH,
                                           D3DTR_CACHE_HEIGHT,
                                           D3DTR_CACHE_CELL_WIDTH,
                                           D3DTR_CACHE_CELL_HEIGHT,
                                           D3DGlyphCache_FlushGlyphVertexCache);
        if (pGlyphCache == NULL) {
            J2dRlsTraceLn(J2D_TRACE_ERROR,
                          "D3DGlyphCache::Init: "\
                          "could not init D3D glyph cache");
            return E_FAIL;
        }
    }

    if (gcType == CACHE_GRAY) {
        format = pCtx->IsTextureFormatSupported(D3DFMT_A8) ?
            D3DFMT_A8 : D3DFMT_A8R8G8B8;
    } else { // gcType == CACHE_LCD
        format = pCtx->IsTextureFormatSupported(D3DFMT_R8G8B8) ?
            D3DFMT_R8G8B8 : D3DFMT_A8R8G8B8;
    }

    HRESULT res = pCtx->GetResourceManager()->
        CreateTexture(D3DTR_CACHE_WIDTH, D3DTR_CACHE_HEIGHT,
                      FALSE/*isRTT*/, FALSE/*isOpaque*/, &format, 0/*usage*/,
                      &pGlyphCacheRes);
    if (FAILED(res)) {
        J2dRlsTraceLn(J2D_TRACE_ERROR,
                      "D3DGlyphCache::Init: "\
                      "could not create glyph cache texture");
    }

    return res;
}

HRESULT
D3DGlyphCache::AddGlyph(GlyphInfo *glyph)
{
    HRESULT res = S_OK;

    RETURN_STATUS_IF_NULL(pGlyphCacheRes, E_FAIL);

    CacheCellInfo *cellInfo = AccelGlyphCache_AddGlyph(pGlyphCache, glyph);
    if (cellInfo != NULL) {
        jint pixelsTouchedL = 0, pixelsTouchedR = 0;
        // store glyph image in texture cell
        res = pCtx->UploadTileToTexture(pGlyphCacheRes,
                                        glyph->image,
                                        cellInfo->x, cellInfo->y,
                                        0, 0,
                                        glyph->width, glyph->height,
                                        glyph->rowBytes, tileFormat,
                                        &pixelsTouchedL,
                                        &pixelsTouchedR);
        // LCD text rendering optimization: if the number of pixels touched on
        // the first or last column of the glyph image is less than 1/3 of the
        // height of the glyph we do not consider them touched.
        // See D3DTextRenderer.cpp:UpdateCachedDestination for more information.
        // The leftOff/rightOff are only used in LCD cache case.
        if (gcType == CACHE_LCD) {
            jint threshold = glyph->height/3;

            cellInfo->leftOff  = pixelsTouchedL < threshold ?  1 : 0;
            cellInfo->rightOff = pixelsTouchedR < threshold ? -1 : 0;
        } else {
            cellInfo->leftOff  = 0;
            cellInfo->rightOff = 0;
        }
    }

    return res;
}

HRESULT
D3DGlyphCache::CheckGlyphCacheByteOrder(jboolean rgbOrder)
{
    J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache::CheckGlyphCacheByteOrder");

    if (gcType != CACHE_LCD) {
        J2dTraceLn(J2D_TRACE_ERROR, "D3DGlyphCache::CheckGlyphCacheByteOrder"\
                   " invoked on CACHE_GRAY cache type instance!");
        return E_FAIL;
    }

    if (rgbOrder != lastRGBOrder) {
        // need to invalidate the cache in this case; see comments
        // for lastRGBOrder
        AccelGlyphCache_Invalidate(pGlyphCache);
        lastRGBOrder = rgbOrder;
    }
    tileFormat = rgbOrder ? TILEFMT_3BYTE_RGB : TILEFMT_3BYTE_BGR;

    return S_OK;
}

/**
 * This method is invoked in the (relatively rare) case where one or
 * more glyphs is about to be kicked out of the glyph cache texture.
 * Here we simply flush the vertex queue of the current context in case
 * any pending vertices are dependent upon the current glyph cache layout.
 */
static void
D3DGlyphCache_FlushGlyphVertexCache()
{
    D3DContext *d3dc = D3DRQ_GetCurrentContext();
    if (d3dc != NULL) {
        J2dTraceLn(J2D_TRACE_INFO, "D3DGlyphCache_FlushGlyphVertexCache");
        d3dc->FlushVertexQueue();
    }
}