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 <stdlib.h>
27#include <jlong.h>
28
29#include "D3DMaskBlit.h"
30#include "D3DRenderQueue.h"
31#include "D3DSurfaceData.h"
32
33/**
34 * REMIND: This method assumes that the dimensions of the incoming pixel
35 *         array are less than or equal to the cached blit texture tile;
36 *         these are rather fragile assumptions, and should be cleaned up...
37 */
38HRESULT
39D3DMaskBlit_MaskBlit(JNIEnv *env, D3DContext *d3dc,
40                     jint dstx, jint dsty,
41                     jint width, jint height,
42                     void *pPixels)
43{
44    HRESULT res = S_OK;
45    jfloat dx1, dy1, dx2, dy2;
46    jfloat tx1, ty1, tx2, ty2;
47
48    J2dTraceLn(J2D_TRACE_INFO, "D3DMaskBlit_MaskBlit");
49
50    if (width <= 0 || height <= 0) {
51        J2dTraceLn(J2D_TRACE_WARNING,
52                   "D3DMaskBlit_MaskBlit: invalid dimensions");
53        return res;
54    }
55
56    RETURN_STATUS_IF_NULL(pPixels, E_FAIL);
57    RETURN_STATUS_IF_NULL(d3dc, E_FAIL);
58    if (FAILED(res = d3dc->BeginScene(STATE_TEXTUREOP))) {
59        return res;
60    }
61
62    D3DResource *pBlitTexRes;
63    if (FAILED(res =
64               d3dc->GetResourceManager()->GetBlitTexture(&pBlitTexRes)))
65    {
66        return res;
67    }
68    IDirect3DTexture9 *pBlitTex = pBlitTexRes->GetTexture();
69
70    if (FAILED(res = d3dc->SetTexture(pBlitTex, 0))) {
71        return res;
72    }
73
74    IDirect3DDevice9 *pd3dDevice = d3dc->Get3DDevice();
75    D3DTEXTUREFILTERTYPE fhint =
76        d3dc->IsTextureFilteringSupported(D3DTEXF_NONE) ?
77            D3DTEXF_NONE : D3DTEXF_POINT;
78    pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, fhint);
79    pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, fhint);
80
81    // copy system memory IntArgbPre surface into cached texture
82    if (FAILED(res = d3dc->UploadTileToTexture(pBlitTexRes, pPixels,
83                                               0, 0, 0, 0,
84                                               width, height,
85                                               width*4,
86                                               TILEFMT_4BYTE_ARGB_PRE)))
87    {
88        return res;
89    }
90
91    dx1 = (jfloat)dstx;
92    dy1 = (jfloat)dsty;
93    dx2 = dx1 + width;
94    dy2 = dy1 + height;
95
96    tx1 = 0.0f;
97    ty1 = 0.0f;
98    tx2 = ((jfloat)width) / D3DC_BLIT_TILE_SIZE;
99    ty2 = ((jfloat)height) / D3DC_BLIT_TILE_SIZE;
100
101    // render cached texture to the destination surface
102    res = d3dc->pVCacher->DrawTexture(dx1, dy1, dx2, dy2,
103                                      tx1, ty1, tx2, ty2);
104    res = d3dc->pVCacher->Render();
105
106    return res;
107}
108