1 /*
   2  * Copyright (c) 2019, 2019, 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.metal;
  27 
  28 import sun.awt.CGraphicsConfig;
  29 import sun.awt.CGraphicsDevice;
  30 import sun.java2d.Disposer;
  31 import sun.java2d.SurfaceData;
  32 import sun.java2d.pipe.hw.ContextCapabilities;
  33 import sun.lwawt.LWComponentPeer;
  34 import sun.lwawt.macosx.CPlatformView;
  35 import sun.java2d.opengl.CGLLayer;
  36 import sun.java2d.metal.MetalContext;
  37 import sun.java2d.metal.MetalRenderQueue;
  38 
  39 import java.awt.Transparency;
  40 import java.awt.color.ColorSpace;
  41 import java.awt.image.BufferedImage;
  42 import java.awt.image.ColorModel;
  43 import java.awt.image.DataBuffer;
  44 import java.awt.image.DirectColorModel;
  45 import java.awt.image.VolatileImage;
  46 import java.awt.image.WritableRaster;
  47 
  48 import java.awt.*;
  49 
  50 public final class MetalGraphicsConfig extends CGraphicsConfig {
  51 
  52     private int pixfmt;
  53     public long pConfigInfo; //public access is a hack
  54     private static native long getMetalConfigInfo(int displayID, int visualnum);
  55 
  56     private MetalContext context;
  57     
  58     long getNativeConfigInfo() {
  59         return pConfigInfo;
  60     }
  61 
  62     @Override
  63     public int getMaxTextureWidth() {
  64         return 16384; 
  65         /* Getting Device Caps from Metal Device is required. 
  66            There is no API for this. We need to refer manual and populate these values. 
  67            Refer https://wiki.se.oracle.com/display/JPGC/Metal+Rendering+Pipeline */
  68     }
  69 
  70     @Override
  71     public int getMaxTextureHeight() {
  72         return 16384;
  73     }
  74 
  75     @Override
  76     public void assertOperationSupported(int numBuffers, BufferCapabilities caps) throws AWTException {
  77 
  78     }
  79 
  80     @Override
  81     public Image createBackBuffer(LWComponentPeer<?, ?> peer) {
  82         return null;
  83     }
  84 
  85     @Override
  86     public void destroyBackBuffer(Image backBuffer) {
  87 
  88     }
  89 
  90     @Override
  91     public void flip(LWComponentPeer<?, ?> peer, Image backBuffer, int x1, int y1, int x2, int y2, BufferCapabilities.FlipContents flipAction) {
  92 
  93     }
  94 
  95     @Override
  96     public Image createAcceleratedImage(Component target, int width, int height) {
  97         return null;
  98     }
  99 
 100     private MetalGraphicsConfig(CGraphicsDevice device, int pixfmt,
 101                                 long configInfo) {
 102         super(device);
 103 
 104         this.pixfmt = pixfmt;
 105         this.pConfigInfo = configInfo;
 106         //this.oglCaps = oglCaps;
 107         //this.maxTextureSize = maxTextureSize;
 108         // TODO : We are using MetalContext as of now.
 109         // We need to verify its usage in future to keep/remove it.
 110         context = new MetalContext(MetalRenderQueue.getInstance(), this);
 111 
 112         // add a record to the Disposer so that we destroy the native
 113         // CGLGraphicsConfigInfo data when this object goes away
 114         //Disposer.addRecord(disposerReferent,
 115                 //new CGLGraphicsConfig.CGLGCDisposerRecord(pConfigInfo));
 116     }
 117 
 118     public static MetalGraphicsConfig getConfig(CGraphicsDevice device, int displayID,
 119                                               int pixfmt)
 120     {
 121         //if (!cglAvailable) {
 122             //return null;
 123         //}
 124 
 125         long cfginfo = 0;
 126         //int textureSize = 0;
 127         //final String ids[] = new String[1];
 128         //OGLRenderQueue rq = OGLRenderQueue.getInstance();
 129         //rq.lock();
 130         //try {
 131             // getCGLConfigInfo() creates and destroys temporary
 132             // surfaces/contexts, so we should first invalidate the current
 133             // Java-level context and flush the queue...
 134             //OGLContext.invalidateCurrentContext();
 135 
 136             cfginfo = getMetalConfigInfo(displayID, pixfmt);
 137             /*if (cfginfo != 0L) {
 138                 textureSize = nativeGetMaxTextureSize();
 139                 // 7160609: GL still fails to create a square texture of this
 140                 // size. Half should be safe enough.
 141                 // Explicitly not support a texture more than 2^14, see 8010999.
 142                 textureSize = textureSize <= 16384 ? textureSize / 2 : 8192;
 143                 OGLContext.setScratchSurface(cfginfo);
 144                 rq.flushAndInvokeNow(() -> {
 145                     ids[0] = OGLContext.getOGLIdString();
 146                 });
 147             }
 148         } finally {
 149             rq.unlock();
 150         }
 151         if (cfginfo == 0) {
 152             return null;
 153         }
 154 
 155         int oglCaps = getOGLCapabilities(cfginfo);
 156         ContextCapabilities caps = new OGLContext.OGLContextCaps(oglCaps, ids[0]);*/
 157         return new MetalGraphicsConfig(device, pixfmt, cfginfo);
 158     }
 159 
 160     @Override
 161     public SurfaceData createSurfaceData(CPlatformView pView) {
 162         return null;
 163     }
 164 
 165     @Override
 166     public SurfaceData createSurfaceData(CGLLayer layer) {
 167         return null;
 168     }
 169 
 170     @Override
 171     public SurfaceData createSurfaceData(MetalLayer layer) {
 172         return MetalSurfaceData.createData(layer);
 173     }
 174 
 175     @Override
 176     public ColorModel getColorModel(int transparency) {
 177         switch (transparency) {
 178         case Transparency.OPAQUE:
 179             // REMIND: once the ColorModel spec is changed, this should be
 180             //         an opaque premultiplied DCM...
 181             return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
 182         case Transparency.BITMASK:
 183             return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
 184         case Transparency.TRANSLUCENT:
 185             ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
 186             return new DirectColorModel(cs, 32,
 187                                         0xff0000, 0xff00, 0xff, 0xff000000,
 188                                         true, DataBuffer.TYPE_INT);
 189         default:
 190             return null;
 191         }
 192     }
 193 
 194     public MetalContext getContext() {
 195         return context;
 196     }
 197 }