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