1 /*
   2  * Copyright (c) 2011, 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 package com.sun.glass.ui.mac;
  26 
  27 import java.nio.Buffer;
  28 import java.nio.ByteBuffer;
  29 import java.nio.IntBuffer;
  30 
  31 import com.sun.glass.ui.Application;
  32 import com.sun.glass.ui.Pixels;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 
  36 /**
  37  * MacOSX platform implementation class for Pixels.
  38  */
  39 final class MacPixels extends Pixels {
  40 
  41     private native static int _initIDs(); // returns the native format
  42     static {
  43         nativeFormat = _initIDs();
  44     }
  45 
  46     private static final int nativeFormat;
  47 
  48     static int getNativeFormat_impl() {
  49         return nativeFormat;
  50     }
  51 
  52     protected MacPixels(int width, int height, ByteBuffer data) {
  53         super(width, height, data);
  54     }
  55 
  56     protected MacPixels(int width, int height, IntBuffer data) {
  57         super(width, height, data);
  58     }
  59 
  60     protected MacPixels(int width, int height, IntBuffer data, float scalex, float scaley) {
  61         super(width, height, data, scalex, scaley);
  62     }
  63 
  64     @Override
  65     protected void _fillDirectByteBuffer(ByteBuffer bb) {
  66         if (this.bytes != null) {
  67             this.bytes.rewind();
  68             if (this.bytes.isDirect() == true) {
  69                 _copyPixels(bb, this.bytes, getWidth()*getHeight());
  70             } else {
  71                 bb.put(this.bytes);
  72             }
  73             this.bytes.rewind();
  74         } else {
  75             this.ints.rewind();
  76             if (this.ints.isDirect() == true) {
  77                 _copyPixels(bb, this.ints, getWidth()*getHeight());
  78             } else {
  79                 for (int i=0; i<this.ints.capacity(); i++) {
  80                     int data = this.ints.get();
  81                     bb.put((byte)((data>>0)&0xff));
  82                     bb.put((byte)((data>>8)&0xff));
  83                     bb.put((byte)((data>>16)&0xff));
  84                     bb.put((byte)((data>>24)&0xff));
  85                 }
  86             }
  87             this.ints.rewind();
  88         }
  89     }
  90     native protected void _copyPixels(Buffer src, Buffer dst, int size);
  91     @Override native protected void _attachInt(long ptr, int w, int h, IntBuffer ints, int[] array, int offset);
  92     @Override native protected void _attachByte(long ptr, int w, int h, ByteBuffer bytes, byte[] array, int offset);
  93 
  94     @Override
  95     public String toString() {
  96         return "MacPixels ["+getWidth()+"x"+getHeight()+"]: "+super.toString();
  97     }
  98 }