1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 /** 18 * @author Igor V. Stolyarov 19 * @version $Revision$ 20 */ 21 22 package java.awt.image; 23 24 import org.apache.harmony.awt.gl.image.DataBufferListener; 25 import org.apache.harmony.awt.internal.nls.Messages; 26 27 /** 28 * The Class DataBuffer is a wrapper class for a data array to be used for the 29 * situation where a suite of functionality acts on a set of data in a 30 * consistent way even though the primitive type of the data may vary from one 31 * use to the next. 32 * 33 * @since Android 1.0 34 */ 35 public abstract class DataBuffer { 36 37 /** 38 * The Constant TYPE_BYTE. 39 */ 40 public static final int TYPE_BYTE = 0; 41 42 /** 43 * The Constant TYPE_USHORT. 44 */ 45 public static final int TYPE_USHORT = 1; 46 47 /** 48 * The Constant TYPE_SHORT. 49 */ 50 public static final int TYPE_SHORT = 2; 51 52 /** 53 * The Constant TYPE_INT. 54 */ 55 public static final int TYPE_INT = 3; 56 57 /** 58 * The Constant TYPE_FLOAT. 59 */ 60 public static final int TYPE_FLOAT = 4; 61 62 /** 63 * The Constant TYPE_DOUBLE. 64 */ 65 public static final int TYPE_DOUBLE = 5; 66 67 /** 68 * The Constant TYPE_UNDEFINED. 69 */ 70 public static final int TYPE_UNDEFINED = 32; 71 72 /** 73 * The data type indicates the primitive type of the data in this 74 * DataBuffer. 75 */ 76 protected int dataType; 77 78 /** 79 * The number of data arrays in this DataBuffer. 80 */ 81 protected int banks; 82 83 /** 84 * The starting index for reading the data from the first (or only) internal 85 * data array. 86 */ 87 protected int offset; 88 89 /** 90 * The length (number of elements) of the data arrays. 91 */ 92 protected int size; 93 94 /** 95 * The starting indices for reading the data from the internal data arrays. 96 */ 97 protected int offsets[]; 98 99 /** 100 * The data changed. 101 */ 102 boolean dataChanged = true; 103 104 /** 105 * The data taken. 106 */ 107 boolean dataTaken = false; 108 109 /** 110 * The listener. 111 */ 112 DataBufferListener listener; 113 114 static { AwtImageBackdoorAccessorImpl.init()115 AwtImageBackdoorAccessorImpl.init(); 116 } 117 118 /** 119 * Instantiates a new data buffer. 120 * 121 * @param dataType 122 * the data type. 123 * @param size 124 * the length (number of elements) of the data arrays. 125 * @param numBanks 126 * the number of data arrays to create. 127 * @param offsets 128 * the starting indices for reading the data from the internal 129 * data arrays. 130 */ DataBuffer(int dataType, int size, int numBanks, int[] offsets)131 protected DataBuffer(int dataType, int size, int numBanks, int[] offsets) { 132 this.dataType = dataType; 133 this.size = size; 134 this.banks = numBanks; 135 this.offsets = offsets.clone(); 136 this.offset = offsets[0]; 137 } 138 139 /** 140 * Instantiates a new data buffer with all of the data arrays starting at 141 * the same index. 142 * 143 * @param dataType 144 * the data type. 145 * @param size 146 * the length (number of elements) of the data arrays. 147 * @param numBanks 148 * the number of data arrays to create. 149 * @param offset 150 * the offset to use for all of the data arrays. 151 */ DataBuffer(int dataType, int size, int numBanks, int offset)152 protected DataBuffer(int dataType, int size, int numBanks, int offset) { 153 this.dataType = dataType; 154 this.size = size; 155 this.banks = numBanks; 156 this.offset = offset; 157 this.offsets = new int[numBanks]; 158 int i = 0; 159 while (i < numBanks) { 160 offsets[i++] = offset; 161 } 162 } 163 164 /** 165 * Instantiates a new data buffer with all of the data arrays read from the 166 * beginning (at offset zero). 167 * 168 * @param dataType 169 * the data type. 170 * @param size 171 * the length (number of elements) of the data arrays. 172 * @param numBanks 173 * the number of data arrays to create. 174 */ DataBuffer(int dataType, int size, int numBanks)175 protected DataBuffer(int dataType, int size, int numBanks) { 176 this.dataType = dataType; 177 this.size = size; 178 this.banks = numBanks; 179 this.offset = 0; 180 this.offsets = new int[numBanks]; 181 } 182 183 /** 184 * Instantiates a new data buffer with one internal data array read from the 185 * beginning (at offset zero). 186 * 187 * @param dataType 188 * the data type. 189 * @param size 190 * the length (number of elements) of the data arrays. 191 */ DataBuffer(int dataType, int size)192 protected DataBuffer(int dataType, int size) { 193 this.dataType = dataType; 194 this.size = size; 195 this.banks = 1; 196 this.offset = 0; 197 this.offsets = new int[1]; 198 } 199 200 /** 201 * Sets the data value in the specified array at the specified index. 202 * 203 * @param bank 204 * the internal array to the data to. 205 * @param i 206 * the index within the array where the data should be written. 207 * @param val 208 * the value to write into the array. 209 */ setElem(int bank, int i, int val)210 public abstract void setElem(int bank, int i, int val); 211 212 /** 213 * Sets the float data value in the specified array at the specified index. 214 * 215 * @param bank 216 * the internal array to the data to. 217 * @param i 218 * the index within the array where the data should be written. 219 * @param val 220 * the value to write into the array. 221 */ setElemFloat(int bank, int i, float val)222 public void setElemFloat(int bank, int i, float val) { 223 setElem(bank, i, (int)val); 224 } 225 226 /** 227 * Sets the double data value in the specified array at the specified index. 228 * 229 * @param bank 230 * the internal array to the data to. 231 * @param i 232 * the index within the array where the data should be written. 233 * @param val 234 * the value to write into the array. 235 */ setElemDouble(int bank, int i, double val)236 public void setElemDouble(int bank, int i, double val) { 237 setElem(bank, i, (int)val); 238 } 239 240 /** 241 * Sets the data value in the first array at the specified index. 242 * 243 * @param i 244 * the index within the array where the data should be written. 245 * @param val 246 * the value to write into the array. 247 */ setElem(int i, int val)248 public void setElem(int i, int val) { 249 setElem(0, i, val); 250 } 251 252 /** 253 * Gets the data value from the specified data array at the specified index. 254 * 255 * @param bank 256 * the data array to read from. 257 * @param i 258 * the index within the array where the data should be read. 259 * @return the data element. 260 */ getElem(int bank, int i)261 public abstract int getElem(int bank, int i); 262 263 /** 264 * Gets the float-type data value from the specified data array at the 265 * specified index. 266 * 267 * @param bank 268 * the data array to read from. 269 * @param i 270 * the index within the array where the data should be read. 271 * @return the data element. 272 */ getElemFloat(int bank, int i)273 public float getElemFloat(int bank, int i) { 274 return getElem(bank, i); 275 } 276 277 /** 278 * Gets the double-type data value from the specified data array at the 279 * specified index. 280 * 281 * @param bank 282 * the data array to read from. 283 * @param i 284 * the index within the array where the data should be read. 285 * @return the data element. 286 */ getElemDouble(int bank, int i)287 public double getElemDouble(int bank, int i) { 288 return getElem(bank, i); 289 } 290 291 /** 292 * Sets the float data value in the first array at the specified index. 293 * 294 * @param i 295 * the index within the array where the data should be written. 296 * @param val 297 * the value to write into the array. 298 */ setElemFloat(int i, float val)299 public void setElemFloat(int i, float val) { 300 setElemFloat(0, i, val); 301 } 302 303 /** 304 * Sets the double data value in the first array at the specified index. 305 * 306 * @param i 307 * the index within the array where the data should be written. 308 * @param val 309 * the value to write into the array. 310 */ setElemDouble(int i, double val)311 public void setElemDouble(int i, double val) { 312 setElemDouble(0, i, val); 313 } 314 315 /** 316 * Gets the data value from the first data array at the specified index and 317 * returns it as an integer. 318 * 319 * @param i 320 * the index within the array where the data should be read. 321 * @return the data element. 322 */ getElem(int i)323 public int getElem(int i) { 324 return getElem(0, i); 325 } 326 327 /** 328 * Gets the data value from the first data array at the specified index and 329 * returns it as a float. 330 * 331 * @param i 332 * the index within the array where the data should be read. 333 * @return the data element. 334 */ getElemFloat(int i)335 public float getElemFloat(int i) { 336 return getElem(0, i); 337 } 338 339 /** 340 * Gets the data value from the first data array at the specified index and 341 * returns it as a double. 342 * 343 * @param i 344 * the index within the array where the data should be read. 345 * @return the data element. 346 */ getElemDouble(int i)347 public double getElemDouble(int i) { 348 return getElem(i); 349 } 350 351 /** 352 * Gets the array giving the offsets corresponding to the internal data 353 * arrays. 354 * 355 * @return the array of offsets. 356 */ getOffsets()357 public int[] getOffsets() { 358 return offsets; 359 } 360 361 /** 362 * Gets the size in bits of the primitive data type. 363 * 364 * @return the size in bits of the primitive data type. 365 */ getSize()366 public int getSize() { 367 return size; 368 } 369 370 /** 371 * Gets the offset corresponding to the first internal data array. 372 * 373 * @return the offset. 374 */ getOffset()375 public int getOffset() { 376 return offset; 377 } 378 379 /** 380 * Gets the number of data arrays in this DataBuffer. 381 * 382 * @return the number of data arrays. 383 */ getNumBanks()384 public int getNumBanks() { 385 return banks; 386 } 387 388 /** 389 * Gets the primitive type of this buffer's data. 390 * 391 * @return the data type. 392 */ getDataType()393 public int getDataType() { 394 return this.dataType; 395 } 396 397 /** 398 * Gets the size in bits of the primitive data type. 399 * 400 * @param type 401 * the primitive type. 402 * @return the size in bits of the primitive data type. 403 */ getDataTypeSize(int type)404 public static int getDataTypeSize(int type) { 405 switch (type) { 406 407 case TYPE_BYTE: 408 return 8; 409 410 case TYPE_USHORT: 411 case TYPE_SHORT: 412 return 16; 413 414 case TYPE_INT: 415 case TYPE_FLOAT: 416 return 32; 417 418 case TYPE_DOUBLE: 419 return 64; 420 421 default: 422 // awt.22C=Unknown data type {0} 423 throw new IllegalArgumentException(Messages.getString("awt.22C", type)); //$NON-NLS-1$ 424 } 425 } 426 427 /** 428 * Notifies the listener that the data has changed. 429 */ notifyChanged()430 void notifyChanged() { 431 if (listener != null && !dataChanged) { 432 dataChanged = true; 433 listener.dataChanged(); 434 } 435 } 436 437 /** 438 * Notifies the listener that the data has been released. 439 */ notifyTaken()440 void notifyTaken() { 441 if (listener != null && !dataTaken) { 442 dataTaken = true; 443 listener.dataTaken(); 444 } 445 } 446 447 /** 448 * Release the data. 449 */ releaseData()450 void releaseData() { 451 if (listener != null && dataTaken) { 452 dataTaken = false; 453 listener.dataReleased(); 454 } 455 } 456 457 /** 458 * Adds the data buffer listener. 459 * 460 * @param listener 461 * the listener. 462 */ addDataBufferListener(DataBufferListener listener)463 void addDataBufferListener(DataBufferListener listener) { 464 this.listener = listener; 465 } 466 467 /** 468 * Removes the data buffer listener. 469 */ removeDataBufferListener()470 void removeDataBufferListener() { 471 listener = null; 472 } 473 474 /** 475 * Validate. 476 */ validate()477 void validate() { 478 dataChanged = false; 479 } 480 481 } 482