D3DBlitLoops.java 33.7 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * 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
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * 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.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31
 */

package sun.java2d.d3d;

import java.awt.Composite;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
32 33 34
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.lang.ref.WeakReference;
35
import java.lang.annotation.Native;
36 37 38 39
import sun.java2d.ScreenUpdateManager;
import sun.java2d.SurfaceData;
import sun.java2d.loops.Blit;
import sun.java2d.loops.CompositeType;
D
duke 已提交
40 41 42
import sun.java2d.loops.GraphicsPrimitive;
import sun.java2d.loops.GraphicsPrimitiveMgr;
import sun.java2d.loops.ScaledBlit;
43
import sun.java2d.loops.SurfaceType;
D
duke 已提交
44 45
import sun.java2d.loops.TransformBlit;
import sun.java2d.pipe.Region;
46 47 48 49
import sun.java2d.pipe.RenderBuffer;
import sun.java2d.pipe.RenderQueue;
import static sun.java2d.pipe.BufferedOpCodes.*;
import sun.java2d.windows.GDIWindowSurfaceData;
D
duke 已提交
50

51
final class D3DBlitLoops {
D
duke 已提交
52

53 54 55 56 57 58 59
    static void register() {
        Blit blitIntArgbPreToSurface =
            new D3DSwToSurfaceBlit(SurfaceType.IntArgbPre,
                                   D3DSurfaceData.ST_INT_ARGB_PRE);
        Blit blitIntArgbPreToTexture =
            new D3DSwToTextureBlit(SurfaceType.IntArgbPre,
                                   D3DSurfaceData.ST_INT_ARGB_PRE);
60 61 62
        TransformBlit transformBlitIntArgbPreToSurface =
            new D3DSwToSurfaceTransform(SurfaceType.IntArgbPre,
                                        D3DSurfaceData.ST_INT_ARGB_PRE);
D
duke 已提交
63
        GraphicsPrimitive[] primitives = {
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
            // prevent D3DSurface -> Screen blits
            new D3DSurfaceToGDIWindowSurfaceBlit(),
            new D3DSurfaceToGDIWindowSurfaceScale(),
            new D3DSurfaceToGDIWindowSurfaceTransform(),

            // surface->surface ops
            new D3DSurfaceToSurfaceBlit(),
            new D3DSurfaceToSurfaceScale(),
            new D3DSurfaceToSurfaceTransform(),

            // render-to-texture surface->surface ops
            new D3DRTTSurfaceToSurfaceBlit(),
            new D3DRTTSurfaceToSurfaceScale(),
            new D3DRTTSurfaceToSurfaceTransform(),

            // surface->sw ops
            new D3DSurfaceToSwBlit(SurfaceType.IntArgb,
                                   D3DSurfaceData.ST_INT_ARGB),

            // sw->surface ops
            blitIntArgbPreToSurface,
            new D3DSwToSurfaceBlit(SurfaceType.IntArgb,
                                   D3DSurfaceData.ST_INT_ARGB),
            new D3DSwToSurfaceBlit(SurfaceType.IntRgb,
                                   D3DSurfaceData.ST_INT_RGB),
            new D3DSwToSurfaceBlit(SurfaceType.IntBgr,
                                   D3DSurfaceData.ST_INT_BGR),
91 92
            new D3DSwToSurfaceBlit(SurfaceType.ThreeByteBgr,
                                   D3DSurfaceData.ST_3BYTE_BGR),
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
            new D3DSwToSurfaceBlit(SurfaceType.Ushort565Rgb,
                                   D3DSurfaceData.ST_USHORT_565_RGB),
            new D3DSwToSurfaceBlit(SurfaceType.Ushort555Rgb,
                                   D3DSurfaceData.ST_USHORT_555_RGB),
            new D3DSwToSurfaceBlit(SurfaceType.ByteIndexed,
                                   D3DSurfaceData.ST_BYTE_INDEXED),
            // REMIND: we don't have a native sw loop to back this loop up
//            new D3DSwToSurfaceBlit(SurfaceType.ByteIndexedBm,
//                                   D3DSurfaceData.ST_BYTE_INDEXED_BM),
            new D3DGeneralBlit(D3DSurfaceData.D3DSurface,
                               CompositeType.AnyAlpha,
                               blitIntArgbPreToSurface),

            new D3DSwToSurfaceScale(SurfaceType.IntArgb,
                                    D3DSurfaceData.ST_INT_ARGB),
            new D3DSwToSurfaceScale(SurfaceType.IntArgbPre,
                                    D3DSurfaceData.ST_INT_ARGB_PRE),
            new D3DSwToSurfaceScale(SurfaceType.IntRgb,
                                    D3DSurfaceData.ST_INT_RGB),
            new D3DSwToSurfaceScale(SurfaceType.IntBgr,
                                    D3DSurfaceData.ST_INT_BGR),
114 115
            new D3DSwToSurfaceScale(SurfaceType.ThreeByteBgr,
                                    D3DSurfaceData.ST_3BYTE_BGR),
116 117 118 119 120 121 122 123 124
            new D3DSwToSurfaceScale(SurfaceType.Ushort565Rgb,
                                    D3DSurfaceData.ST_USHORT_565_RGB),
            new D3DSwToSurfaceScale(SurfaceType.Ushort555Rgb,
                                    D3DSurfaceData.ST_USHORT_555_RGB),
            new D3DSwToSurfaceScale(SurfaceType.ByteIndexed,
                                    D3DSurfaceData.ST_BYTE_INDEXED),
            // REMIND: we don't have a native sw loop to back this loop up
//            new D3DSwToSurfaceScale(SurfaceType.ByteIndexedBm,
//                                    D3DSurfaceData.ST_BYTE_INDEXED_BM),
D
duke 已提交
125

126 127 128 129 130 131
            new D3DSwToSurfaceTransform(SurfaceType.IntArgb,
                                        D3DSurfaceData.ST_INT_ARGB),
            new D3DSwToSurfaceTransform(SurfaceType.IntRgb,
                                        D3DSurfaceData.ST_INT_RGB),
            new D3DSwToSurfaceTransform(SurfaceType.IntBgr,
                                        D3DSurfaceData.ST_INT_BGR),
132 133
            new D3DSwToSurfaceTransform(SurfaceType.ThreeByteBgr,
                                        D3DSurfaceData.ST_3BYTE_BGR),
134 135 136 137 138 139 140 141 142
            new D3DSwToSurfaceTransform(SurfaceType.Ushort565Rgb,
                                        D3DSurfaceData.ST_USHORT_565_RGB),
            new D3DSwToSurfaceTransform(SurfaceType.Ushort555Rgb,
                                        D3DSurfaceData.ST_USHORT_555_RGB),
            new D3DSwToSurfaceTransform(SurfaceType.ByteIndexed,
                                        D3DSurfaceData.ST_BYTE_INDEXED),
            // REMIND: we don't have a native sw loop to back this loop up
//            new D3DSwToSurfaceTransform(SurfaceType.ByteIndexedBm,
//                                        D3DSurfaceData.ST_BYTE_INDEXED_BM),
143 144 145
            transformBlitIntArgbPreToSurface,

            new D3DGeneralTransformedBlit(transformBlitIntArgbPreToSurface),
146 147 148 149 150 151 152 153 154 155 156 157 158 159

            // texture->surface ops
            new D3DTextureToSurfaceBlit(),
            new D3DTextureToSurfaceScale(),
            new D3DTextureToSurfaceTransform(),

            // sw->texture ops
            blitIntArgbPreToTexture,
            new D3DSwToTextureBlit(SurfaceType.IntRgb,
                                   D3DSurfaceData.ST_INT_RGB),
            new D3DSwToTextureBlit(SurfaceType.IntArgb,
                                   D3DSurfaceData.ST_INT_ARGB),
            new D3DSwToTextureBlit(SurfaceType.IntBgr,
                                   D3DSurfaceData.ST_INT_BGR),
160 161
            new D3DSwToTextureBlit(SurfaceType.ThreeByteBgr,
                                   D3DSurfaceData.ST_3BYTE_BGR),
162 163 164 165 166 167 168 169 170 171 172 173
            new D3DSwToTextureBlit(SurfaceType.Ushort565Rgb,
                                   D3DSurfaceData.ST_USHORT_565_RGB),
            new D3DSwToTextureBlit(SurfaceType.Ushort555Rgb,
                                   D3DSurfaceData.ST_USHORT_555_RGB),
            new D3DSwToTextureBlit(SurfaceType.ByteIndexed,
                                   D3DSurfaceData.ST_BYTE_INDEXED),
            // REMIND: we don't have a native sw loop to back this loop up
//            new D3DSwToTextureBlit(SurfaceType.ByteIndexedBm,
//                                   D3DSurfaceData.ST_BYTE_INDEXED_BM),
            new D3DGeneralBlit(D3DSurfaceData.D3DTexture,
                               CompositeType.SrcNoEa,
                               blitIntArgbPreToTexture),
D
duke 已提交
174 175 176 177
        };
        GraphicsPrimitiveMgr.register(primitives);
    }

178 179 180 181 182
    /**
     * The following offsets are used to pack the parameters in
     * createPackedParams().  (They are also used at the native level when
     * unpacking the params.)
     */
183 184 185 186 187 188
    @Native private static final int OFFSET_SRCTYPE = 16;
    @Native private static final int OFFSET_HINT    =  8;
    @Native private static final int OFFSET_TEXTURE =  3;
    @Native private static final int OFFSET_RTT     =  2;
    @Native private static final int OFFSET_XFORM   =  1;
    @Native private static final int OFFSET_ISOBLIT =  0;
189 190 191 192 193 194 195 196

    /**
     * Packs the given parameters into a single int value in order to save
     * space on the rendering queue.
     */
    private static int createPackedParams(boolean isoblit, boolean texture,
                                          boolean rtt, boolean xform,
                                          int hint, int srctype)
D
duke 已提交
197
    {
198 199 200 201 202 203 204 205
        return
            ((srctype           << OFFSET_SRCTYPE) |
             (hint              << OFFSET_HINT   ) |
             ((texture ? 1 : 0) << OFFSET_TEXTURE) |
             ((rtt     ? 1 : 0) << OFFSET_RTT    ) |
             ((xform   ? 1 : 0) << OFFSET_XFORM  ) |
             ((isoblit ? 1 : 0) << OFFSET_ISOBLIT));
    }
D
duke 已提交
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    /**
     * Enqueues a BLIT operation with the given parameters.  Note that the
     * RenderQueue lock must be held before calling this method.
     */
    private static void enqueueBlit(RenderQueue rq,
                                    SurfaceData src, SurfaceData dst,
                                    int packedParams,
                                    int sx1, int sy1,
                                    int sx2, int sy2,
                                    double dx1, double dy1,
                                    double dx2, double dy2)
    {
        // assert rq.lock.isHeldByCurrentThread();
        RenderBuffer buf = rq.getBuffer();
        rq.ensureCapacityAndAlignment(72, 24);
        buf.putInt(BLIT);
        buf.putInt(packedParams);
        buf.putInt(sx1).putInt(sy1);
        buf.putInt(sx2).putInt(sy2);
        buf.putDouble(dx1).putDouble(dy1);
        buf.putDouble(dx2).putDouble(dy2);
        buf.putLong(src.getNativeOps());
        buf.putLong(dst.getNativeOps());
D
duke 已提交
230 231
    }

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    static void Blit(SurfaceData srcData, SurfaceData dstData,
                     Composite comp, Region clip,
                     AffineTransform xform, int hint,
                     int sx1, int sy1,
                     int sx2, int sy2,
                     double dx1, double dy1,
                     double dx2, double dy2,
                     int srctype, boolean texture)
    {
        int ctxflags = 0;
        if (srcData.getTransparency() == Transparency.OPAQUE) {
            ctxflags |= D3DContext.SRC_IS_OPAQUE;
        }

        D3DSurfaceData d3dDst = (D3DSurfaceData)dstData;
        D3DRenderQueue rq = D3DRenderQueue.getInstance();
        rq.lock();
        try {
            // make sure the RenderQueue keeps a hard reference to the
            // source (sysmem) SurfaceData to prevent it from being
            // disposed while the operation is processed on the QFT
            rq.addReference(srcData);

            if (texture) {
                // make sure we have a current context before uploading
                // the sysmem data to the texture object
                D3DContext.setScratchSurface(d3dDst.getContext());
            } else {
                D3DContext.validateContext(d3dDst, d3dDst,
                                           clip, comp, xform, null, null,
                                           ctxflags);
            }

            int packedParams = createPackedParams(false, texture,
                                                  false, xform != null,
                                                  hint, srctype);
            enqueueBlit(rq, srcData, dstData,
                        packedParams,
                        sx1, sy1, sx2, sy2,
                        dx1, dy1, dx2, dy2);

            // always flush immediately, since we (currently) have no means
            // of tracking changes to the system memory surface
            rq.flushNow();
        } finally {
            rq.unlock();
        }

        if (d3dDst.getType() == D3DSurfaceData.WINDOW) {
            // flush immediately when copying to the screen to improve
            // responsiveness of applications using VI or BI backbuffers
            D3DScreenUpdateManager mgr =
                (D3DScreenUpdateManager)ScreenUpdateManager.getInstance();
            mgr.runUpdateNow();
        }
D
duke 已提交
287 288 289
    }

    /**
290 291 292 293
     * Note: The srcImg and biop parameters are only used when invoked
     * from the D3DBufImgOps.renderImageWithOp() method; in all other cases,
     * this method can be called with null values for those two parameters,
     * and they will be effectively ignored.
D
duke 已提交
294
     */
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    static void IsoBlit(SurfaceData srcData, SurfaceData dstData,
                        BufferedImage srcImg, BufferedImageOp biop,
                        Composite comp, Region clip,
                        AffineTransform xform, int hint,
                        int sx1, int sy1,
                        int sx2, int sy2,
                        double dx1, double dy1,
                        double dx2, double dy2,
                        boolean texture)
    {
        int ctxflags = 0;
        if (srcData.getTransparency() == Transparency.OPAQUE) {
            ctxflags |= D3DContext.SRC_IS_OPAQUE;
        }

        D3DSurfaceData d3dDst = (D3DSurfaceData)dstData;
        D3DRenderQueue rq = D3DRenderQueue.getInstance();
        boolean rtt = false;
        rq.lock();
        try {
            D3DSurfaceData d3dSrc = (D3DSurfaceData)srcData;
            int srctype = d3dSrc.getType();
            D3DSurfaceData srcCtxData = d3dSrc;
            if (srctype == D3DSurfaceData.TEXTURE) {
                rtt = false;
            } else {
                // the source is a backbuffer, or render-to-texture
                // surface; we set rtt to true to differentiate this kind
                // of surface from a regular texture object
                rtt = true;
            }

            D3DContext.validateContext(srcCtxData, d3dDst,
                                       clip, comp, xform, null, null,
                                       ctxflags);

            if (biop != null) {
                D3DBufImgOps.enableBufImgOp(rq, d3dSrc, srcImg, biop);
            }

            int packedParams = createPackedParams(true, texture,
                                                  rtt, xform != null,
                                                  hint, 0 /*unused*/);
            enqueueBlit(rq, srcData, dstData,
                        packedParams,
                        sx1, sy1, sx2, sy2,
                        dx1, dy1, dx2, dy2);

            if (biop != null) {
                D3DBufImgOps.disableBufImgOp(rq, biop);
            }
        } finally {
            rq.unlock();
        }

        if (rtt && (d3dDst.getType() == D3DSurfaceData.WINDOW)) {
            // we only have to flush immediately when copying from a
            // (non-texture) surface to the screen; otherwise Swing apps
            // might appear unresponsive until the auto-flush completes
            D3DScreenUpdateManager mgr =
                (D3DScreenUpdateManager)ScreenUpdateManager.getInstance();
            mgr.runUpdateNow();
        }
    }
}

class D3DSurfaceToSurfaceBlit extends Blit {

    D3DSurfaceToSurfaceBlit() {
        super(D3DSurfaceData.D3DSurface,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
    }

D
duke 已提交
369 370 371 372
    public void Blit(SurfaceData src, SurfaceData dst,
                     Composite comp, Region clip,
                     int sx, int sy, int dx, int dy, int w, int h)
    {
373 374 375 376 377 378 379
        D3DBlitLoops.IsoBlit(src, dst,
                             null, null,
                             comp, clip, null,
                             AffineTransformOp.TYPE_NEAREST_NEIGHBOR,
                             sx, sy, sx+w, sy+h,
                             dx, dy, dx+w, dy+h,
                             false);
D
duke 已提交
380 381 382
    }
}

383 384 385 386 387 388 389
class D3DSurfaceToSurfaceScale extends ScaledBlit {

    D3DSurfaceToSurfaceScale() {
        super(D3DSurfaceData.D3DSurface,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
    }
D
duke 已提交
390

391 392 393 394 395 396
    public void Scale(SurfaceData src, SurfaceData dst,
                      Composite comp, Region clip,
                      int sx1, int sy1,
                      int sx2, int sy2,
                      double dx1, double dy1,
                      double dx2, double dy2)
D
duke 已提交
397
    {
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
        D3DBlitLoops.IsoBlit(src, dst,
                             null, null,
                             comp, clip, null,
                             AffineTransformOp.TYPE_NEAREST_NEIGHBOR,
                             sx1, sy1, sx2, sy2,
                             dx1, dy1, dx2, dy2,
                             false);
    }
}

class D3DSurfaceToSurfaceTransform extends TransformBlit {

    D3DSurfaceToSurfaceTransform() {
        super(D3DSurfaceData.D3DSurface,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
D
duke 已提交
414 415 416 417 418 419 420 421
    }

    public void Transform(SurfaceData src, SurfaceData dst,
                          Composite comp, Region clip,
                          AffineTransform at, int hint,
                          int sx, int sy, int dx, int dy,
                          int w, int h)
    {
422 423 424 425 426 427
        D3DBlitLoops.IsoBlit(src, dst,
                             null, null,
                             comp, clip, at, hint,
                             sx, sy, sx+w, sy+h,
                             dx, dy, dx+w, dy+h,
                             false);
D
duke 已提交
428 429 430
    }
}

431
class D3DRTTSurfaceToSurfaceBlit extends Blit {
D
duke 已提交
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    D3DRTTSurfaceToSurfaceBlit() {
        super(D3DSurfaceData.D3DSurfaceRTT,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
    }

    public void Blit(SurfaceData src, SurfaceData dst,
                     Composite comp, Region clip,
                     int sx, int sy, int dx, int dy, int w, int h)
    {
        D3DBlitLoops.IsoBlit(src, dst,
                             null, null,
                             comp, clip, null,
                             AffineTransformOp.TYPE_NEAREST_NEIGHBOR,
                             sx, sy, sx+w, sy+h,
                             dx, dy, dx+w, dy+h,
                             true);
    }
}

class D3DRTTSurfaceToSurfaceScale extends ScaledBlit {

    D3DRTTSurfaceToSurfaceScale() {
        super(D3DSurfaceData.D3DSurfaceRTT,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
D
duke 已提交
459 460 461 462 463 464 465 466 467
    }

    public void Scale(SurfaceData src, SurfaceData dst,
                      Composite comp, Region clip,
                      int sx1, int sy1,
                      int sx2, int sy2,
                      double dx1, double dy1,
                      double dx2, double dy2)
    {
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
        D3DBlitLoops.IsoBlit(src, dst,
                             null, null,
                             comp, clip, null,
                             AffineTransformOp.TYPE_NEAREST_NEIGHBOR,
                             sx1, sy1, sx2, sy2,
                             dx1, dy1, dx2, dy2,
                             true);
    }
}

class D3DRTTSurfaceToSurfaceTransform extends TransformBlit {

    D3DRTTSurfaceToSurfaceTransform() {
        super(D3DSurfaceData.D3DSurfaceRTT,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
    }

    public void Transform(SurfaceData src, SurfaceData dst,
                          Composite comp, Region clip,
                          AffineTransform at, int hint,
                          int sx, int sy, int dx, int dy, int w, int h)
    {
        D3DBlitLoops.IsoBlit(src, dst,
                             null, null,
                             comp, clip, at, hint,
                             sx, sy, sx+w, sy+h,
                             dx, dy, dx+w, dy+h,
                             true);
    }
}

class D3DSurfaceToSwBlit extends Blit {

    private int typeval;

    // REMIND: destination will actually be opaque/premultiplied...
    D3DSurfaceToSwBlit(SurfaceType dstType, int typeval) {
        super(D3DSurfaceData.D3DSurface,
              CompositeType.SrcNoEa,
              dstType);
        this.typeval = typeval;
    }

    public void Blit(SurfaceData src, SurfaceData dst,
                     Composite comp, Region clip,
                     int sx, int sy, int dx, int dy,
                     int w, int h)
    {
        D3DRenderQueue rq = D3DRenderQueue.getInstance();
        rq.lock();
        try {
            // make sure the RenderQueue keeps a hard reference to the
            // destination (sysmem) SurfaceData to prevent it from being
            // disposed while the operation is processed on the QFT
            rq.addReference(dst);

            RenderBuffer buf = rq.getBuffer();
            D3DContext.setScratchSurface(((D3DSurfaceData)src).getContext());

            rq.ensureCapacityAndAlignment(48, 32);
            buf.putInt(SURFACE_TO_SW_BLIT);
            buf.putInt(sx).putInt(sy);
            buf.putInt(dx).putInt(dy);
            buf.putInt(w).putInt(h);
            buf.putInt(typeval);
            buf.putLong(src.getNativeOps());
            buf.putLong(dst.getNativeOps());

            // always flush immediately
            rq.flushNow();
        } finally {
            rq.unlock();
D
duke 已提交
541 542 543 544
        }
    }
}

545 546 547
class D3DSwToSurfaceBlit extends Blit {

    private int typeval;
D
duke 已提交
548

549 550 551 552 553
    D3DSwToSurfaceBlit(SurfaceType srcType, int typeval) {
        super(srcType,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
        this.typeval = typeval;
D
duke 已提交
554 555 556 557 558 559
    }

    public void Blit(SurfaceData src, SurfaceData dst,
                     Composite comp, Region clip,
                     int sx, int sy, int dx, int dy, int w, int h)
    {
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
        D3DBlitLoops.Blit(src, dst,
                          comp, clip, null,
                          AffineTransformOp.TYPE_NEAREST_NEIGHBOR,
                          sx, sy, sx+w, sy+h,
                          dx, dy, dx+w, dy+h,
                          typeval, false);
    }
}

class D3DSwToSurfaceScale extends ScaledBlit {

    private int typeval;

    D3DSwToSurfaceScale(SurfaceType srcType, int typeval) {
        super(srcType,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
        this.typeval = typeval;
    }

    public void Scale(SurfaceData src, SurfaceData dst,
                      Composite comp, Region clip,
                      int sx1, int sy1,
                      int sx2, int sy2,
                      double dx1, double dy1,
                      double dx2, double dy2)
    {
        D3DBlitLoops.Blit(src, dst,
                          comp, clip, null,
                          AffineTransformOp.TYPE_NEAREST_NEIGHBOR,
                          sx1, sy1, sx2, sy2,
                          dx1, dy1, dx2, dy2,
                          typeval, false);
    }
}

class D3DSwToSurfaceTransform extends TransformBlit {

    private int typeval;

    D3DSwToSurfaceTransform(SurfaceType srcType, int typeval) {
        super(srcType,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
        this.typeval = typeval;
    }

    public void Transform(SurfaceData src, SurfaceData dst,
                          Composite comp, Region clip,
                          AffineTransform at, int hint,
                          int sx, int sy, int dx, int dy, int w, int h)
    {
        D3DBlitLoops.Blit(src, dst,
                          comp, clip, at, hint,
                          sx, sy, sx+w, sy+h,
                          dx, dy, dx+w, dy+h,
                          typeval, false);
    }
}

class D3DSwToTextureBlit extends Blit {

    private int typeval;

    D3DSwToTextureBlit(SurfaceType srcType, int typeval) {
        super(srcType,
              CompositeType.SrcNoEa,
              D3DSurfaceData.D3DTexture);
        this.typeval = typeval;
    }

    public void Blit(SurfaceData src, SurfaceData dst,
                     Composite comp, Region clip,
                     int sx, int sy, int dx, int dy, int w, int h)
    {
        D3DBlitLoops.Blit(src, dst,
                          comp, clip, null,
                          AffineTransformOp.TYPE_NEAREST_NEIGHBOR,
                          sx, sy, sx+w, sy+h,
                          dx, dy, dx+w, dy+h,
                          typeval, true);
    }
}

class D3DTextureToSurfaceBlit extends Blit {

    D3DTextureToSurfaceBlit() {
        super(D3DSurfaceData.D3DTexture,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
    }

    public void Blit(SurfaceData src, SurfaceData dst,
                     Composite comp, Region clip,
                     int sx, int sy, int dx, int dy, int w, int h)
    {
        D3DBlitLoops.IsoBlit(src, dst,
                             null, null,
                             comp, clip, null,
                             AffineTransformOp.TYPE_NEAREST_NEIGHBOR,
                             sx, sy, sx+w, sy+h,
                             dx, dy, dx+w, dy+h,
                             true);
    }
}

class D3DTextureToSurfaceScale extends ScaledBlit {

    D3DTextureToSurfaceScale() {
        super(D3DSurfaceData.D3DTexture,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
    }

    public void Scale(SurfaceData src, SurfaceData dst,
                      Composite comp, Region clip,
                      int sx1, int sy1,
                      int sx2, int sy2,
                      double dx1, double dy1,
                      double dx2, double dy2)
    {
        D3DBlitLoops.IsoBlit(src, dst,
                             null, null,
                             comp, clip, null,
                             AffineTransformOp.TYPE_NEAREST_NEIGHBOR,
                             sx1, sy1, sx2, sy2,
                             dx1, dy1, dx2, dy2,
                             true);
    }
}

class D3DTextureToSurfaceTransform extends TransformBlit {

    D3DTextureToSurfaceTransform() {
        super(D3DSurfaceData.D3DTexture,
              CompositeType.AnyAlpha,
              D3DSurfaceData.D3DSurface);
    }

    public void Transform(SurfaceData src, SurfaceData dst,
                          Composite comp, Region clip,
                          AffineTransform at, int hint,
                          int sx, int sy, int dx, int dy,
                          int w, int h)
    {
        D3DBlitLoops.IsoBlit(src, dst,
                             null, null,
                             comp, clip, at, hint,
                             sx, sy, sx+w, sy+h,
                             dx, dy, dx+w, dy+h,
                             true);
    }
}

/**
715
 * This general Blit implementation converts any source surface to an
716 717
 * intermediate IntArgbPre surface, and then uses the more specific
 * IntArgbPre->D3DSurface/Texture loop to get the intermediate
718
 * (premultiplied) surface down to D3D using simple blit.
719 720 721
 */
class D3DGeneralBlit extends Blit {

722
    private final Blit performop;
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
    private WeakReference srcTmp;

    D3DGeneralBlit(SurfaceType dstType,
                   CompositeType compType,
                   Blit performop)
    {
        super(SurfaceType.Any, compType, dstType);
        this.performop = performop;
    }

    public synchronized void Blit(SurfaceData src, SurfaceData dst,
                                  Composite comp, Region clip,
                                  int sx, int sy, int dx, int dy,
                                  int w, int h)
    {
        Blit convertsrc = Blit.getFromCache(src.getSurfaceType(),
                                            CompositeType.SrcNoEa,
                                            SurfaceType.IntArgbPre);

        SurfaceData cachedSrc = null;
        if (srcTmp != null) {
            // use cached intermediate surface, if available
            cachedSrc = (SurfaceData)srcTmp.get();
D
duke 已提交
746 747
        }

748 749 750 751 752 753 754 755 756 757 758
        // convert source to IntArgbPre
        src = convertFrom(convertsrc, src, sx, sy, w, h,
                          cachedSrc, BufferedImage.TYPE_INT_ARGB_PRE);

        // copy IntArgbPre intermediate surface to D3D surface
        performop.Blit(src, dst, comp, clip,
                       0, 0, dx, dy, w, h);

        if (src != cachedSrc) {
            // cache the intermediate surface
            srcTmp = new WeakReference(src);
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
        }
    }
}

/**
 * This general TransformedBlit implementation converts any source surface to an
 * intermediate IntArgbPre surface, and then uses the more specific
 * IntArgbPre->D3DSurface/Texture loop to get the intermediate
 * (premultiplied) surface down to D3D using simple transformBlit.
 */
final class D3DGeneralTransformedBlit extends TransformBlit {

    private final TransformBlit performop;
    private WeakReference<SurfaceData> srcTmp;

    D3DGeneralTransformedBlit(final TransformBlit performop) {
        super(SurfaceType.Any, CompositeType.AnyAlpha,
                D3DSurfaceData.D3DSurface);
        this.performop = performop;
    }

    @Override
    public synchronized void Transform(SurfaceData src, SurfaceData dst,
                                       Composite comp, Region clip,
                                       AffineTransform at, int hint, int srcx,
                                       int srcy, int dstx, int dsty, int width,
                                       int height){
        Blit convertsrc = Blit.getFromCache(src.getSurfaceType(),
                                            CompositeType.SrcNoEa,
                                            SurfaceType.IntArgbPre);
        // use cached intermediate surface, if available
        final SurfaceData cachedSrc = srcTmp != null ? srcTmp.get() : null;
        // convert source to IntArgbPre
        src = convertFrom(convertsrc, src, srcx, srcy, width, height, cachedSrc,
                          BufferedImage.TYPE_INT_ARGB_PRE);

        // transform IntArgbPre intermediate surface to D3D surface
        performop.Transform(src, dst, comp, clip, at, hint, 0, 0, dstx, dsty,
                            width, height);

        if (src != cachedSrc) {
            // cache the intermediate surface
            srcTmp = new WeakReference<>(src);
D
duke 已提交
802 803 804
        }
    }
}
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888

/*
 * The following classes prohibit copying D3DSurfaces to the screen
 * (the D3D->sysmem->GDI path is known to be very very slow).
 *
 * Note: we used to disable hw acceleration for the surafce manager associated
 * with the source surface in these loops but it proved to be too cautious.
 *
 * In most cases d3d->screen copy happens only during some transitional
 * period where the accelerated destination surface is being recreated or
 * restored (for example, when Swing's backbuffer VI is copied to the screen
 * but the D3DScreenSurfaceManager couldn't restore its surface).
 *
 * An exception is if for some reason we could not enable accelerated on-screen
 * rendering for this window for some permanent reason (like window being too
 * small, or a present BufferStrategy).
 *
 * This meant that we'd disable hw acceleration after the first failure
 * completely (at least until the src image is recreated which in case of
 * Swing back-buffer happens only after resize).
 *
 * Now we delegate to the VISM to figure out if the acceleration needs to
 * be disabled or if we can wait for a while until the onscreen accelerated
 * can resume (by marking the source surface lost and making sure the
 * VISM has a chance to use the backup surface).
 *
 */

class D3DSurfaceToGDIWindowSurfaceBlit extends Blit {

    D3DSurfaceToGDIWindowSurfaceBlit() {
        super(D3DSurfaceData.D3DSurface,
              CompositeType.AnyAlpha,
              GDIWindowSurfaceData.AnyGdi);
    }
    @Override
    public void Blit(SurfaceData src, SurfaceData dst,
                     Composite comp, Region clip,
                     int sx, int sy, int dx, int dy, int w, int h)
    {
        // see comment above
        D3DVolatileSurfaceManager.handleVItoScreenOp(src, dst);
    }

}

class D3DSurfaceToGDIWindowSurfaceScale extends ScaledBlit {

    D3DSurfaceToGDIWindowSurfaceScale() {
        super(D3DSurfaceData.D3DSurface,
              CompositeType.AnyAlpha,
              GDIWindowSurfaceData.AnyGdi);
    }
    @Override
    public void Scale(SurfaceData src, SurfaceData dst,
                      Composite comp, Region clip,
                      int sx1, int sy1,
                      int sx2, int sy2,
                      double dx1, double dy1,
                      double dx2, double dy2)
    {
        // see comment above
        D3DVolatileSurfaceManager.handleVItoScreenOp(src, dst);
    }
}

class D3DSurfaceToGDIWindowSurfaceTransform extends TransformBlit {

    D3DSurfaceToGDIWindowSurfaceTransform() {
        super(D3DSurfaceData.D3DSurface,
              CompositeType.AnyAlpha,
              GDIWindowSurfaceData.AnyGdi);
    }
    @Override
    public void Transform(SurfaceData src, SurfaceData dst,
                          Composite comp, Region clip,
                          AffineTransform at, int hint,
                          int sx, int sy, int dx, int dy,
                          int w, int h)
    {
        // see comment above
        D3DVolatileSurfaceManager.handleVItoScreenOp(src, dst);
    }
}