View Javadoc
1   /*
2    * Copyright (c) 2001, 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.awt.image;
27  
28  import java.awt.Point;
29  import java.awt.Rectangle;
30  import java.awt.Image;
31  import java.awt.image.DataBuffer;
32  import java.awt.image.DataBufferByte;
33  import java.awt.image.DataBufferUShort;
34  import java.awt.image.DataBufferInt;
35  import java.awt.image.SampleModel;
36  import java.awt.image.WritableRaster;
37  
38  import sun.java2d.StateTrackable.State;
39  import sun.java2d.SurfaceData;
40  import sun.java2d.StateTrackableDelegate;
41  
42  /**
43   * This class exists as a middle layer between WritableRaster and its
44   * implementation specific subclasses (ByteComponentRaster, ShortBandedRaster,
45   * etc).
46   * It provides utilities to steal the data arrays from the standard DataBuffer
47   * types and also steals the StateTrackableDelegate from the associated
48   * DataBuffer so that it can be updated when the data is changed.
49   */
50  public class SunWritableRaster extends WritableRaster {
51      private static DataStealer stealer;
52  
53      public static interface DataStealer {
54          public byte[] getData(DataBufferByte dbb, int bank);
55          public short[] getData(DataBufferUShort dbus, int bank);
56          public int[] getData(DataBufferInt dbi, int bank);
57          public StateTrackableDelegate getTrackable(DataBuffer db);
58          public void setTrackable(DataBuffer db, StateTrackableDelegate trackable);
59      }
60  
61      public static void setDataStealer(DataStealer ds) {
62          if (stealer != null) {
63              throw new InternalError("Attempt to set DataStealer twice");
64          }
65          stealer = ds;
66      }
67  
68      public static byte[] stealData(DataBufferByte dbb, int bank) {
69          return stealer.getData(dbb, bank);
70      }
71  
72      public static short[] stealData(DataBufferUShort dbus, int bank) {
73          return stealer.getData(dbus, bank);
74      }
75  
76      public static int[] stealData(DataBufferInt dbi, int bank) {
77          return stealer.getData(dbi, bank);
78      }
79  
80      public static StateTrackableDelegate stealTrackable(DataBuffer db) {
81          return stealer.getTrackable(db);
82      }
83  
84      public static void setTrackable(DataBuffer db, StateTrackableDelegate trackable) {
85          stealer.setTrackable(db, trackable);
86      }
87  
88      public static void makeTrackable(DataBuffer db) {
89          stealer.setTrackable(db, StateTrackableDelegate.createInstance(State.STABLE));
90      }
91  
92      public static void markDirty(DataBuffer db) {
93          stealer.getTrackable(db).markDirty();
94      }
95  
96      public static void markDirty(WritableRaster wr) {
97          if (wr instanceof SunWritableRaster) {
98              ((SunWritableRaster) wr).markDirty();
99          } else {
100             markDirty(wr.getDataBuffer());
101         }
102     }
103 
104     public static void markDirty(Image img) {
105         SurfaceData.getPrimarySurfaceData(img).markDirty();
106     }
107 
108     private StateTrackableDelegate theTrackable;
109 
110     public SunWritableRaster(SampleModel sampleModel, Point origin) {
111         super(sampleModel, origin);
112         theTrackable = stealTrackable(dataBuffer);
113     }
114 
115     public SunWritableRaster(SampleModel sampleModel,
116                              DataBuffer dataBuffer,
117                              Point origin)
118     {
119         super(sampleModel, dataBuffer, origin);
120         theTrackable = stealTrackable(dataBuffer);
121     }
122 
123     public SunWritableRaster(SampleModel sampleModel,
124                              DataBuffer dataBuffer,
125                              Rectangle aRegion,
126                              Point sampleModelTranslate,
127                              WritableRaster parent)
128     {
129         super(sampleModel, dataBuffer, aRegion, sampleModelTranslate, parent);
130         theTrackable = stealTrackable(dataBuffer);
131     }
132 
133     /**
134      * Mark the TrackableDelegate of the associated DataBuffer dirty.
135      */
136     public final void markDirty() {
137         theTrackable.markDirty();
138     }
139 }