1 /*
   2  * Copyright (c) 2009, 2015, 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 package com.sun.prism.d3d;
  27 
  28 import com.sun.glass.ui.Screen;
  29 import com.sun.javafx.geom.Rectangle;
  30 import com.sun.prism.CompositeMode;
  31 import com.sun.prism.Graphics;
  32 import com.sun.prism.Presentable;
  33 import com.sun.prism.PresentableState;
  34 import com.sun.prism.RTTexture;
  35 
  36 class D3DSwapChain
  37     extends D3DResource
  38     implements D3DRenderTarget, Presentable, D3DContextSource {
  39 
  40     private final D3DRTTexture texBackBuffer;
  41     private final float pixelScaleFactorX;
  42     private final float pixelScaleFactorY;
  43 
  44     D3DSwapChain(D3DContext context, long pResource, D3DRTTexture rtt, float pixelScaleX, float pixelScaleY) {
  45         super(new D3DRecord(context, pResource));
  46         texBackBuffer = rtt;
  47         pixelScaleFactorX = pixelScaleX;
  48         pixelScaleFactorY = pixelScaleY;
  49     }
  50 
  51     @Override
  52     public void dispose() {
  53         texBackBuffer.dispose();
  54         super.dispose();
  55     }
  56 
  57     @Override
  58     public boolean prepare(Rectangle dirtyregion) {
  59         D3DContext context = getContext();
  60         context.flushVertexBuffer();
  61         D3DGraphics g = (D3DGraphics) D3DGraphics.create(this, context);
  62         if (g == null) {
  63             return false;
  64         }
  65         int sw = texBackBuffer.getContentWidth();
  66         int sh = texBackBuffer.getContentHeight();
  67         int dw = this.getContentWidth();
  68         int dh = this.getContentHeight();
  69         if (isMSAA()) {
  70             context.flushVertexBuffer();
  71             g.blit(texBackBuffer, null, 0, 0, sw, sh, 0, 0, dw, dh);
  72         } else {
  73             g.setCompositeMode(CompositeMode.SRC);
  74             g.drawTexture(texBackBuffer, 0, 0, dw, dh, 0, 0, sw, sh);
  75         }
  76         context.flushVertexBuffer();
  77         texBackBuffer.unlock();
  78         return true;
  79     }
  80 
  81     public boolean present() {
  82         D3DContext context = getContext();
  83         int res = nPresent(context.getContextHandle(), d3dResRecord.getResource());
  84         return context.validatePresent(res);
  85     }
  86 
  87     public long getResourceHandle() {
  88         return d3dResRecord.getResource();
  89     }
  90 
  91     public int getPhysicalWidth() {
  92         return D3DResourceFactory.nGetTextureWidth(d3dResRecord.getResource());
  93     }
  94 
  95     public int getPhysicalHeight() {
  96         return D3DResourceFactory.nGetTextureHeight(d3dResRecord.getResource());
  97     }
  98 
  99     public int getContentWidth() {
 100         return getPhysicalWidth();
 101     }
 102 
 103     public int getContentHeight() {
 104         return getPhysicalHeight();
 105     }
 106 
 107     public int getContentX() {
 108         return 0;
 109     }
 110 
 111     public int getContentY() {
 112         return 0;
 113     }
 114 
 115     private static native int nPresent(long context, long pSwapChain);
 116 
 117     public D3DContext getContext() {
 118         return d3dResRecord.getContext();
 119     }
 120 
 121     public boolean lockResources(PresentableState pState) {
 122         if (pState.getRenderWidth() != texBackBuffer.getContentWidth() ||
 123             pState.getRenderHeight() != texBackBuffer.getContentHeight() ||
 124             pState.getRenderScaleX() != pixelScaleFactorX ||
 125             pState.getRenderScaleY() != pixelScaleFactorY)
 126         {
 127             return true;
 128         }
 129         texBackBuffer.lock();
 130         return texBackBuffer.isSurfaceLost();
 131     }
 132 
 133     public Graphics createGraphics() {
 134         Graphics g = D3DGraphics.create(texBackBuffer, getContext());
 135         g.scale(pixelScaleFactorX, pixelScaleFactorY);
 136         return g;
 137     }
 138 
 139     public RTTexture getRTTBackBuffer() {
 140         return texBackBuffer;
 141     }
 142 
 143     public Screen getAssociatedScreen() {
 144         return getContext().getAssociatedScreen();
 145     }
 146 
 147     @Override
 148     public float getPixelScaleFactorX() {
 149         return pixelScaleFactorX;
 150     }
 151 
 152     @Override
 153     public float getPixelScaleFactorY() {
 154         return pixelScaleFactorY;
 155     }
 156 
 157     public boolean isOpaque() {
 158         return texBackBuffer.isOpaque();
 159     }
 160 
 161     public void setOpaque(boolean opaque) {
 162         texBackBuffer.setOpaque(opaque);
 163     }
 164 
 165     public boolean isMSAA() {
 166         return texBackBuffer != null ? texBackBuffer.isMSAA() : false;
 167     }
 168 }