1 /* 2 * Copyright (c) 2007, 2013, 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 sun.java2d.d3d; 27 28 import java.lang.annotation.Native; 29 import sun.java2d.pipe.BufferedContext; 30 import sun.java2d.pipe.RenderBuffer; 31 import sun.java2d.pipe.RenderQueue; 32 import sun.java2d.pipe.hw.ContextCapabilities; 33 import static sun.java2d.pipe.BufferedOpCodes.*; 34 import static sun.java2d.pipe.hw.ContextCapabilities.*; 35 import static sun.java2d.d3d.D3DContext.D3DContextCaps.*; 36 37 /** 38 * Note that the RenderQueue lock must be acquired before calling any of 39 * the methods in this class. 40 */ 41 class D3DContext extends BufferedContext { 42 43 private final D3DGraphicsDevice device; 44 45 D3DContext(RenderQueue rq, D3DGraphicsDevice device) { 46 super(rq); 47 this.device = device; 48 } 49 50 /** 51 * Invalidates the currentContext field to ensure that we properly 52 * revalidate the D3DContext (make it current, etc.) next time through 53 * the validate() method. This is typically invoked from methods 54 * that affect the current context state (e.g. disposing a context or 55 * surface). 56 */ 57 static void invalidateCurrentContext() { 58 // assert D3DRenderQueue.getInstance().lock.isHeldByCurrentThread(); 59 60 // invalidate the current Java-level context so that we 61 // revalidate everything the next time around 62 if (currentContext != null) { 63 currentContext.invalidateContext(); 64 currentContext = null; 65 } 66 67 // invalidate the context reference at the native level, and 68 // then flush the queue so that we have no pending operations 69 // dependent on the current context 70 D3DRenderQueue rq = D3DRenderQueue.getInstance(); 71 rq.ensureCapacity(4); 72 rq.getBuffer().putInt(INVALIDATE_CONTEXT); 73 rq.flushNow(); 74 } 75 76 /** 77 * Sets the current context on the native level to be the one passed as 78 * the argument. 79 * If the context is not the same as the defaultContext the latter 80 * will be reset to null. 81 * 82 * This call is needed when copying from a SW surface to a Texture 83 * (the upload test) or copying from d3d to SW surface to make sure we 84 * have the correct current context. 85 * 86 * @param d3dc the context to be made current on the native level 87 */ 88 static void setScratchSurface(D3DContext d3dc) { 89 // assert D3DRenderQueue.getInstance().lock.isHeldByCurrentThread(); 90 91 // invalidate the current context 92 if (d3dc != currentContext) { 93 currentContext = null; 94 } 95 96 // set the scratch context 97 D3DRenderQueue rq = D3DRenderQueue.getInstance(); 98 RenderBuffer buf = rq.getBuffer(); 99 rq.ensureCapacity(8); 100 buf.putInt(SET_SCRATCH_SURFACE); 101 buf.putInt(d3dc.getDevice().getScreen()); 102 } 103 104 public RenderQueue getRenderQueue() { 105 return D3DRenderQueue.getInstance(); 106 } 107 108 @Override 109 public void saveState() { 110 // assert rq.lock.isHeldByCurrentThread(); 111 112 // reset all attributes of this and current contexts 113 invalidateContext(); 114 invalidateCurrentContext(); 115 116 setScratchSurface(this); 117 118 // save the state on the native level 119 rq.ensureCapacity(4); 120 buf.putInt(SAVE_STATE); 121 rq.flushNow(); 122 } 123 124 @Override 125 public void restoreState() { 126 // assert rq.lock.isHeldByCurrentThread(); 127 128 // reset all attributes of this and current contexts 129 invalidateContext(); 130 invalidateCurrentContext(); 131 132 setScratchSurface(this); 133 134 // restore the state on the native level 135 rq.ensureCapacity(4); 136 buf.putInt(RESTORE_STATE); 137 rq.flushNow(); 138 } 139 140 D3DGraphicsDevice getDevice() { 141 return device; 142 } 143 144 static class D3DContextCaps extends ContextCapabilities { 145 /** 146 * Indicates the presence of pixel shaders (v2.0 or greater). 147 * This cap will only be set if the hardware supports the minimum number 148 * of texture units. 149 */ 150 @Native static final int CAPS_LCD_SHADER = (FIRST_PRIVATE_CAP << 0); 151 /** 152 * Indicates the presence of pixel shaders (v2.0 or greater). 153 * This cap will only be set if the hardware meets our 154 * minimum requirements. 155 */ 156 @Native static final int CAPS_BIOP_SHADER = (FIRST_PRIVATE_CAP << 1); 157 /** 158 * Indicates that the device was successfully initialized and can 159 * be safely used. 160 */ 161 @Native static final int CAPS_DEVICE_OK = (FIRST_PRIVATE_CAP << 2); 162 /** 163 * Indicates that the device has all of the necessary capabilities 164 * to support the Antialiasing Pixel Shader program. 165 */ 166 @Native static final int CAPS_AA_SHADER = (FIRST_PRIVATE_CAP << 3); 167 168 D3DContextCaps(int caps, String adapterId) { 169 super(caps, adapterId); 170 } 171 172 @Override 173 public String toString() { 174 StringBuffer buf = new StringBuffer(super.toString()); 175 if ((caps & CAPS_LCD_SHADER) != 0) { 176 buf.append("CAPS_LCD_SHADER|"); 177 } 178 if ((caps & CAPS_BIOP_SHADER) != 0) { 179 buf.append("CAPS_BIOP_SHADER|"); 180 } 181 if ((caps & CAPS_AA_SHADER) != 0) { 182 buf.append("CAPS_AA_SHADER|"); 183 } 184 if ((caps & CAPS_DEVICE_OK) != 0) { 185 buf.append("CAPS_DEVICE_OK|"); 186 } 187 return buf.toString(); 188 } 189 } 190 }