1 /*
2 * Copyright (c) 1997, 2014, 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
86
87 /** Tag for double data. Placeholder for future use. */
88 @Native public static final int TYPE_DOUBLE = 5;
89
90 /** Tag for undefined data. */
91 @Native public static final int TYPE_UNDEFINED = 32;
92
93 /** The data type of this DataBuffer. */
94 protected int dataType;
95
96 /** The number of banks in this DataBuffer. */
97 protected int banks;
98
99 /** Offset into default (first) bank from which to get the first element. */
100 protected int offset;
101
102 /** Usable size of all banks. */
103 protected int size;
104
105 /** Offsets into all banks. */
106 protected int offsets[];
107
108 /* The current StateTrackable state. */
109 StateTrackableDelegate theTrackable;
110
111 /** Size of the data types indexed by DataType tags defined above. */
112 private static final int dataTypeSize[] = {8,16,16,32,32,64};
113
114 /** Returns the size (in bits) of the data type, given a datatype tag.
115 * @param type the value of one of the defined datatype tags
116 * @return the size of the data type
117 * @throws IllegalArgumentException if {@code type} is less than
118 * zero or greater than {@link #TYPE_DOUBLE}
119 */
120 public static int getDataTypeSize(int type) {
121 if (type < TYPE_BYTE || type > TYPE_DOUBLE) {
122 throw new IllegalArgumentException("Unknown data type "+type);
123 }
124 return dataTypeSize[type];
125 }
126
127 /**
128 * Constructs a DataBuffer containing one bank of the specified
129 * data type and size.
130 *
131 * @param dataType the data type of this {@code DataBuffer}
132 * @param size the size of the banks
229 this.offsets = new int[numBanks];
230 for (int i = 0; i < numBanks; i++) {
231 this.offsets[i] = offset;
232 }
233 }
234
235 /**
236 * Constructs a DataBuffer which contains the specified number
237 * of banks. Each bank has the specified datatype and size. The
238 * offset for each bank is specified by its respective entry in
239 * the offsets array.
240 *
241 * @param dataType the data type of this {@code DataBuffer}
242 * @param size the size of the banks
243 * @param numBanks the number of banks in this
244 * {@code DataBuffer}
245 * @param offsets an array containing an offset for each bank.
246 * @throws ArrayIndexOutOfBoundsException if {@code numBanks}
247 * does not equal the length of {@code offsets}
248 */
249 protected DataBuffer(int dataType, int size, int numBanks, int offsets[]) {
250 this(UNTRACKABLE, dataType, size, numBanks, offsets);
251 }
252
253 /**
254 * Constructs a DataBuffer which contains the specified number
255 * of banks with the indicated initial {@link State State}.
256 * Each bank has the specified datatype and size. The
257 * offset for each bank is specified by its respective entry in
258 * the offsets array.
259 *
260 * @param initialState the initial {@link State State} state of the data
261 * @param dataType the data type of this {@code DataBuffer}
262 * @param size the size of the banks
263 * @param numBanks the number of banks in this
264 * {@code DataBuffer}
265 * @param offsets an array containing an offset for each bank.
266 * @throws ArrayIndexOutOfBoundsException if {@code numBanks}
267 * does not equal the length of {@code offsets}
268 * @since 1.7
269 */
270 DataBuffer(State initialState,
271 int dataType, int size, int numBanks, int offsets[])
272 {
273 if (numBanks != offsets.length) {
274 throw new ArrayIndexOutOfBoundsException("Number of banks" +
275 " does not match number of bank offsets");
276 }
277 this.theTrackable = StateTrackableDelegate.createInstance(initialState);
278 this.dataType = dataType;
279 this.banks = numBanks;
280 this.size = size;
281 this.offset = offsets[0];
282 this.offsets = offsets.clone();
283 }
284
285 /** Returns the data type of this DataBuffer.
286 * @return the data type of this {@code DataBuffer}.
287 */
288 public int getDataType() {
289 return dataType;
290 }
291
481 * from the given double. The implementation in this class is to cast
482 * val to an int and call {@link #setElem(int, int)}. Subclasses can
483 * override this method if another implementation is needed.
484 * @param bank the specified bank
485 * @param i the specified index
486 * @param val the value to set the element in the specified bank
487 * at the specified index of the data array
488 * @see #getElemDouble(int)
489 * @see #getElemDouble(int, int)
490 */
491 public void setElemDouble(int bank, int i, double val) {
492 setElem(bank,i,(int)val);
493 }
494
495 static int[] toIntArray(Object obj) {
496 if (obj instanceof int[]) {
497 return (int[])obj;
498 } else if (obj == null) {
499 return null;
500 } else if (obj instanceof short[]) {
501 short sdata[] = (short[])obj;
502 int idata[] = new int[sdata.length];
503 for (int i = 0; i < sdata.length; i++) {
504 idata[i] = (int)sdata[i] & 0xffff;
505 }
506 return idata;
507 } else if (obj instanceof byte[]) {
508 byte bdata[] = (byte[])obj;
509 int idata[] = new int[bdata.length];
510 for (int i = 0; i < bdata.length; i++) {
511 idata[i] = 0xff & (int)bdata[i];
512 }
513 return idata;
514 }
515 return null;
516 }
517
518 static {
519 SunWritableRaster.setDataStealer(new SunWritableRaster.DataStealer() {
520 public byte[] getData(DataBufferByte dbb, int bank) {
521 return dbb.bankdata[bank];
522 }
523
524 public short[] getData(DataBufferUShort dbus, int bank) {
525 return dbus.bankdata[bank];
526 }
527
528 public int[] getData(DataBufferInt dbi, int bank) {
529 return dbi.bankdata[bank];
|
1 /*
2 * Copyright (c) 1997, 2018, 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
86
87 /** Tag for double data. Placeholder for future use. */
88 @Native public static final int TYPE_DOUBLE = 5;
89
90 /** Tag for undefined data. */
91 @Native public static final int TYPE_UNDEFINED = 32;
92
93 /** The data type of this DataBuffer. */
94 protected int dataType;
95
96 /** The number of banks in this DataBuffer. */
97 protected int banks;
98
99 /** Offset into default (first) bank from which to get the first element. */
100 protected int offset;
101
102 /** Usable size of all banks. */
103 protected int size;
104
105 /** Offsets into all banks. */
106 protected int[] offsets;
107
108 /* The current StateTrackable state. */
109 StateTrackableDelegate theTrackable;
110
111 /** Size of the data types indexed by DataType tags defined above. */
112 private static final int[] dataTypeSize = {8,16,16,32,32,64};
113
114 /** Returns the size (in bits) of the data type, given a datatype tag.
115 * @param type the value of one of the defined datatype tags
116 * @return the size of the data type
117 * @throws IllegalArgumentException if {@code type} is less than
118 * zero or greater than {@link #TYPE_DOUBLE}
119 */
120 public static int getDataTypeSize(int type) {
121 if (type < TYPE_BYTE || type > TYPE_DOUBLE) {
122 throw new IllegalArgumentException("Unknown data type "+type);
123 }
124 return dataTypeSize[type];
125 }
126
127 /**
128 * Constructs a DataBuffer containing one bank of the specified
129 * data type and size.
130 *
131 * @param dataType the data type of this {@code DataBuffer}
132 * @param size the size of the banks
229 this.offsets = new int[numBanks];
230 for (int i = 0; i < numBanks; i++) {
231 this.offsets[i] = offset;
232 }
233 }
234
235 /**
236 * Constructs a DataBuffer which contains the specified number
237 * of banks. Each bank has the specified datatype and size. The
238 * offset for each bank is specified by its respective entry in
239 * the offsets array.
240 *
241 * @param dataType the data type of this {@code DataBuffer}
242 * @param size the size of the banks
243 * @param numBanks the number of banks in this
244 * {@code DataBuffer}
245 * @param offsets an array containing an offset for each bank.
246 * @throws ArrayIndexOutOfBoundsException if {@code numBanks}
247 * does not equal the length of {@code offsets}
248 */
249 protected DataBuffer(int dataType, int size, int numBanks, int[] offsets) {
250 this(UNTRACKABLE, dataType, size, numBanks, offsets);
251 }
252
253 /**
254 * Constructs a DataBuffer which contains the specified number
255 * of banks with the indicated initial {@link State State}.
256 * Each bank has the specified datatype and size. The
257 * offset for each bank is specified by its respective entry in
258 * the offsets array.
259 *
260 * @param initialState the initial {@link State State} state of the data
261 * @param dataType the data type of this {@code DataBuffer}
262 * @param size the size of the banks
263 * @param numBanks the number of banks in this
264 * {@code DataBuffer}
265 * @param offsets an array containing an offset for each bank.
266 * @throws ArrayIndexOutOfBoundsException if {@code numBanks}
267 * does not equal the length of {@code offsets}
268 * @since 1.7
269 */
270 DataBuffer(State initialState,
271 int dataType, int size, int numBanks, int[] offsets)
272 {
273 if (numBanks != offsets.length) {
274 throw new ArrayIndexOutOfBoundsException("Number of banks" +
275 " does not match number of bank offsets");
276 }
277 this.theTrackable = StateTrackableDelegate.createInstance(initialState);
278 this.dataType = dataType;
279 this.banks = numBanks;
280 this.size = size;
281 this.offset = offsets[0];
282 this.offsets = offsets.clone();
283 }
284
285 /** Returns the data type of this DataBuffer.
286 * @return the data type of this {@code DataBuffer}.
287 */
288 public int getDataType() {
289 return dataType;
290 }
291
481 * from the given double. The implementation in this class is to cast
482 * val to an int and call {@link #setElem(int, int)}. Subclasses can
483 * override this method if another implementation is needed.
484 * @param bank the specified bank
485 * @param i the specified index
486 * @param val the value to set the element in the specified bank
487 * at the specified index of the data array
488 * @see #getElemDouble(int)
489 * @see #getElemDouble(int, int)
490 */
491 public void setElemDouble(int bank, int i, double val) {
492 setElem(bank,i,(int)val);
493 }
494
495 static int[] toIntArray(Object obj) {
496 if (obj instanceof int[]) {
497 return (int[])obj;
498 } else if (obj == null) {
499 return null;
500 } else if (obj instanceof short[]) {
501 short[] sdata = (short[])obj;
502 int[] idata = new int[sdata.length];
503 for (int i = 0; i < sdata.length; i++) {
504 idata[i] = (int)sdata[i] & 0xffff;
505 }
506 return idata;
507 } else if (obj instanceof byte[]) {
508 byte[] bdata = (byte[])obj;
509 int[] idata = new int[bdata.length];
510 for (int i = 0; i < bdata.length; i++) {
511 idata[i] = 0xff & (int)bdata[i];
512 }
513 return idata;
514 }
515 return null;
516 }
517
518 static {
519 SunWritableRaster.setDataStealer(new SunWritableRaster.DataStealer() {
520 public byte[] getData(DataBufferByte dbb, int bank) {
521 return dbb.bankdata[bank];
522 }
523
524 public short[] getData(DataBufferUShort dbus, int bank) {
525 return dbus.bankdata[bank];
526 }
527
528 public int[] getData(DataBufferInt dbi, int bank) {
529 return dbi.bankdata[bank];
|