1/*
2 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include "D3DMaskCache.h"
27
28HRESULT
29D3DMaskCache::CreateInstance(D3DContext *pCtx, D3DMaskCache **ppMaskCache)
30{
31    HRESULT res;
32
33    J2dTraceLn(J2D_TRACE_INFO, "D3DMaskCache::CreateInstance");
34
35    *ppMaskCache = new D3DMaskCache();
36    if (FAILED(res = (*ppMaskCache)->Init(pCtx))) {
37        delete *ppMaskCache;
38        *ppMaskCache = NULL;
39    }
40    return res;
41}
42
43D3DMaskCache::D3DMaskCache()
44{
45    J2dTraceLn(J2D_TRACE_INFO, "D3DMaskCache::D3DMaskCache");
46    this->pCtx = NULL;
47    maskCacheIndex = 0;
48}
49
50D3DMaskCache::~D3DMaskCache()
51{
52    J2dTraceLn(J2D_TRACE_INFO, "D3DMaskCache::~D3DMaskCache");
53    pCtx = NULL;
54    maskCacheIndex = 0;
55}
56
57HRESULT
58D3DMaskCache::Init(D3DContext *pCtx)
59{
60    J2dTraceLn1(J2D_TRACE_INFO, "D3DMaskCache::Init pCtx=%x", pCtx);
61    this->pCtx = pCtx;
62    this->maskCacheIndex = 0;
63    return S_OK;
64}
65
66HRESULT D3DMaskCache::Enable()
67{
68    HRESULT res;
69
70    J2dTraceLn(J2D_TRACE_INFO, "D3DMaskCache::Enable");
71
72    D3DResource *pMaskTexRes;
73    res = pCtx->GetResourceManager()->GetMaskTexture(&pMaskTexRes);
74    RETURN_STATUS_IF_FAILED(res);
75
76    res = pCtx->SetTexture(pMaskTexRes->GetTexture(), 0);
77
78    IDirect3DDevice9 *pd3dDevice = pCtx->Get3DDevice();
79    D3DTEXTUREFILTERTYPE fhint =
80        pCtx->IsTextureFilteringSupported(D3DTEXF_NONE) ?
81            D3DTEXF_NONE : D3DTEXF_POINT;
82    pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, fhint);
83    pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, fhint);
84
85    return res;
86}
87
88HRESULT D3DMaskCache::Disable()
89{
90    J2dTraceLn(J2D_TRACE_INFO, "D3DMaskCache::Disable");
91
92    maskCacheIndex = 0;
93
94    return pCtx->SetTexture(NULL, 0);
95}
96
97HRESULT D3DMaskCache::AddMaskQuad(int srcx, int srcy,
98                                  int dstx, int dsty,
99                                  int width, int height,
100                                  int maskscan, void *mask)
101{
102    HRESULT res;
103    float tx1, ty1, tx2, ty2;
104    float dx1, dy1, dx2, dy2;
105
106    J2dTraceLn1(J2D_TRACE_INFO, "D3DVertexCacher::AddMaskQuad: %d",
107                maskCacheIndex);
108
109    if (maskCacheIndex >= D3D_MASK_CACHE_MAX_INDEX ||
110        pCtx->pVCacher->GetFreeVertices() < 6)
111    {
112        res = pCtx->pVCacher->Render();
113        RETURN_STATUS_IF_FAILED(res);
114        maskCacheIndex = 0;
115    }
116
117    if (mask != NULL) {
118        int texx = D3D_MASK_CACHE_TILE_WIDTH *
119            (maskCacheIndex % D3D_MASK_CACHE_WIDTH_IN_TILES);
120        int texy = D3D_MASK_CACHE_TILE_HEIGHT *
121            (maskCacheIndex / D3D_MASK_CACHE_WIDTH_IN_TILES);
122        D3DResource *pMaskTexRes;
123
124        res = pCtx->GetResourceManager()->GetMaskTexture(&pMaskTexRes);
125        RETURN_STATUS_IF_FAILED(res);
126
127        // copy alpha mask into texture tile
128        pCtx->UploadTileToTexture(pMaskTexRes, mask,
129                                  texx, texy,
130                                  srcx, srcy,
131                                  width, height,
132                                  maskscan,
133                                  TILEFMT_1BYTE_ALPHA);
134
135        tx1 = ((float)texx) / D3D_MASK_CACHE_WIDTH_IN_TEXELS;
136        ty1 = ((float)texy) / D3D_MASK_CACHE_HEIGHT_IN_TEXELS;
137
138        maskCacheIndex++;
139    } else {
140        // use special fully opaque tile
141        tx1 = ((float)D3D_MASK_CACHE_SPECIAL_TILE_X) /
142            D3D_MASK_CACHE_WIDTH_IN_TEXELS;
143        ty1 = ((float)D3D_MASK_CACHE_SPECIAL_TILE_Y) /
144            D3D_MASK_CACHE_HEIGHT_IN_TEXELS;
145    }
146
147    tx2 = tx1 + (((float)width) / D3D_MASK_CACHE_WIDTH_IN_TEXELS);
148    ty2 = ty1 + (((float)height) / D3D_MASK_CACHE_HEIGHT_IN_TEXELS);
149
150    dx1 = (float)dstx;
151    dy1 = (float)dsty;
152    dx2 = dx1 + width;
153    dy2 = dy1 + height;
154
155    return pCtx->pVCacher->DrawTexture(dx1, dy1, dx2, dy2,
156                                       tx1, ty1, tx2, ty2);
157}
158