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 pixelScaleFactor;
  42 
  43     D3DSwapChain(D3DContext context, long pResource, D3DRTTexture rtt, float pixelScale) {
  44         super(new D3DRecord(context, pResource));
  45         texBackBuffer = rtt;
  46         pixelScaleFactor = pixelScale;
  47     }
  48 
  49     @Override
  50     public void dispose() {
  51         texBackBuffer.dispose();
  52         super.dispose();
  53     }
  54 
  55     @Override
  56     public boolean prepare(Rectangle dirtyregion) {
  57         D3DContext context = getContext();
  58         context.flushVertexBuffer();
  59         D3DGraphics g = (D3DGraphics) D3DGraphics.create(this, context);
  60         if (g == null) {
  61             return false;
  62         }
  63         int sw = texBackBuffer.getContentWidth();
  64         int sh = texBackBuffer.getContentHeight();
  65         int dw = this.getContentWidth();
  66         int dh = this.getContentHeight();
  67         if (isMSAA()) {
  68             context.flushVertexBuffer();
  69             g.blit(texBackBuffer, null, 0, 0, sw, sh, 0, 0, dw, dh);
  70         } else {
  71             g.setCompositeMode(CompositeMode.SRC);
  72             g.drawTexture(texBackBuffer, 0, 0, dw, dh, 0, 0, sw, sh);
  73         }
  74         context.flushVertexBuffer();
  75         texBackBuffer.unlock();
  76         return true;
  77     }
  78 
  79     public boolean present() {
  80         D3DContext context = getContext();
  81         int res = nPresent(context.getContextHandle(), d3dResRecord.getResource());
  82         return context.validatePresent(res);
  83     }
  84 
  85     public long getResourceHandle() {
  86         return d3dResRecord.getResource();
  87     }
  88 
  89     public int getPhysicalWidth() {
  90         return D3DResourceFactory.nGetTextureWidth(d3dResRecord.getResource());
  91     }
  92 
  93     public int getPhysicalHeight() {
  94         return D3DResourceFactory.nGetTextureHeight(d3dResRecord.getResource());
  95     }
  96 
  97     public int getContentWidth() {
  98         return getPhysicalWidth();
  99     }
 100 
 101     public int getContentHeight() {
 102         return getPhysicalHeight();
 103     }
 104 
 105     public int getContentX() {
 106         return 0;
 107     }
 108 
 109     public int getContentY() {
 110         return 0;
 111     }
 112 
 113     private static native int nPresent(long context, long pSwapChain);
 114 
 115     public D3DContext getContext() {
 116         return d3dResRecord.getContext();
 117     }
 118 
 119     public boolean lockResources(PresentableState pState) {
 120         if (pState.getRenderWidth() != texBackBuffer.getContentWidth() ||
 121             pState.getRenderHeight() != texBackBuffer.getContentHeight() ||
 122             pState.getRenderScale() != pixelScaleFactor)
 123         {
 124             return true;
 125         }
 126         texBackBuffer.lock();
 127         return texBackBuffer.isSurfaceLost();
 128     }
 129 
 130     public Graphics createGraphics() {
 131         Graphics g = D3DGraphics.create(texBackBuffer, getContext());
 132         g.scale(pixelScaleFactor, pixelScaleFactor);
 133         return g;
 134     }
 135 
 136     public RTTexture getRTTBackBuffer() {
 137         return texBackBuffer;
 138     }
 139 
 140     public Screen getAssociatedScreen() {
 141         return getContext().getAssociatedScreen();
 142     }
 143 
 144     public float getPixelScaleFactor() {
 145         return pixelScaleFactor;
 146     }
 147 
 148     public boolean isOpaque() {
 149         return texBackBuffer.isOpaque();
 150     }
 151 
 152     public void setOpaque(boolean opaque) {
 153         texBackBuffer.setOpaque(opaque);
 154     }
 155 
 156     public boolean isMSAA() {
 157         return texBackBuffer != null ? texBackBuffer.isMSAA() : false;
 158     }
 159 }