001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // This program is free software; you can redistribute it and/or modify
004: // it under the terms of the GNU General Public License and GNU Library
005: // General Public License as published by the Free Software Foundation;
006: // either version 2, or (at your option) any later version.
007: //
008: // This program is distributed in the hope that it will be useful,
009: // but WITHOUT ANY WARRANTY; without even the implied warranty of
010: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: // GNU General Public License and GNU Library General Public License
012: // for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // and GNU Library General Public License along with this program; if
016: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
017: // MA 02139, USA.
018: //
019: ///////////////////////////////////////////////////////////////////////////////
020:
021: package org.rdesktop.server.rdp;
022:
023: import java.awt.Cursor;
024: import java.awt.image.IndexColorModel;
025:
026: public class RdpCache {
027: private static final int RDPCACHE_COLOURMAPSIZE = 0x06; // unified patch
028:
029: private RdpBitmap[][] bitmapcache = new RdpBitmap[3][600];
030: private Cursor[] cursorcache = new Cursor[32];
031: private RdpGlyph[][] fontcache = new RdpGlyph[12][256];
032: private RdpDataBlob[] textcache = new RdpDataBlob[256];
033: private int[] highdeskcache = new int[921600];
034: private int num_bitmaps_in_memory[] = new int[3];
035: private IndexColorModel[] colourcache = new IndexColorModel[RDPCACHE_COLOURMAPSIZE];
036:
037: public RdpCache() {
038: }
039:
040: protected void removeLRUBitmap(int cache_id) {
041: int i;
042: int cache_idx = 0;
043: int m = 0xffffffff;
044:
045: for (i = 0; i < bitmapcache[cache_id].length; i++) {
046: if ((bitmapcache[cache_id][i] != null)
047: && (bitmapcache[cache_id][i].getBitmapData() != null)
048: && (bitmapcache[cache_id][i].usage < m)) {
049: cache_idx = i;
050: m = bitmapcache[cache_id][i].usage;
051: }
052: }
053:
054: bitmapcache[cache_id][cache_idx] = null;
055: --num_bitmaps_in_memory[cache_id];
056: }
057:
058: public IndexColorModel get_colourmap(int cache_id)
059: throws RdpDesktopException {
060: IndexColorModel map = null;
061: if (cache_id < colourcache.length) {
062: map = colourcache[cache_id];
063: if (map != null) {
064: return map;
065: }
066: }
067:
068: throw new RdpDesktopException(
069: "Could not get colourmap with cache_id=" + cache_id);
070: }
071:
072: public void put_colourmap(int cache_id, IndexColorModel map)
073: throws RdpDesktopException {
074: if (cache_id < colourcache.length) {
075: colourcache[cache_id] = map;
076: } else {
077: throw new RdpDesktopException(
078: "Could not put colourmap with cache_id=" + cache_id);
079: }
080: }
081:
082: public RdpBitmap getBitmap(int cache_id, int cache_idx)
083: throws RdpDesktopException {
084: RdpBitmap bitmap = null;
085:
086: if ((cache_id < bitmapcache.length)
087: && (cache_idx < bitmapcache[0].length)) {
088: bitmap = bitmapcache[cache_id][cache_idx];
089:
090: if (bitmap != null) {
091: return bitmap;
092: }
093: }
094:
095: throw new RdpDesktopException("Could not get Bitmap!");
096: }
097:
098: public void putBitmap(int cache_id, int cache_idx,
099: RdpBitmap bitmap, int stamp) throws RdpDesktopException {
100: if ((cache_id < bitmapcache.length)
101: && (cache_idx < bitmapcache[0].length)) {
102: bitmapcache[cache_id][cache_idx] = bitmap;
103: } else {
104: throw new RdpDesktopException("Could not put Bitmap!");
105: }
106: }
107:
108: public Cursor getCursor(int cache_idx) throws RdpDesktopException {
109: Cursor cursor = null;
110:
111: if (cache_idx < cursorcache.length) {
112: cursor = cursorcache[cache_idx];
113: if (cursor != null) {
114: return cursor;
115: }
116: }
117:
118: throw new RdpDesktopException("Cursor not found");
119: }
120:
121: public void putCursor(int cache_idx, Cursor cursor)
122: throws RdpDesktopException {
123: if (cache_idx < cursorcache.length) {
124: cursorcache[cache_idx] = cursor;
125: } else {
126: throw new RdpDesktopException("Could not put Cursor!");
127: }
128: }
129:
130: public void putFont(RdpGlyph glyph) throws RdpDesktopException {
131: if ((glyph.getFont() < fontcache.length)
132: && (glyph.getCharacter() < fontcache[0].length)) {
133: fontcache[glyph.getFont()][glyph.getCharacter()] = glyph;
134: } else {
135: throw new RdpDesktopException("Could not put font");
136: }
137: }
138:
139: public RdpGlyph getFont(int font, int character)
140: throws RdpDesktopException {
141: if ((font < fontcache.length)
142: && (character < fontcache[0].length)) {
143: RdpGlyph glyph = fontcache[font][character];
144:
145: if (glyph != null) {
146: return glyph;
147: }
148: }
149:
150: throw new RdpDesktopException("Could not get Font:" + font
151: + ", " + character);
152: }
153:
154: public RdpDataBlob getText(int cache_id) throws RdpDesktopException {
155: RdpDataBlob entry = null;
156:
157: if (cache_id < textcache.length) {
158: entry = textcache[cache_id];
159: if (entry != null) {
160: if (entry.getData() != null) {
161: return entry;
162: }
163: }
164: }
165:
166: throw new RdpDesktopException("Could not get Text:" + cache_id);
167: }
168:
169: public void putText(int cache_id, RdpDataBlob entry)
170: throws RdpDesktopException {
171: if (cache_id < textcache.length) {
172: textcache[cache_id] = entry;
173: } else {
174: throw new RdpDesktopException("Could not put Text");
175: }
176: }
177:
178: public void putDesktop(int offset, int cx, int cy, int[] data)
179: throws RdpDesktopException {
180: int length = cx * cy;
181: int pdata = 0;
182:
183: if (offset > highdeskcache.length) {
184: offset = 0;
185: }
186:
187: if ((int) offset + length <= highdeskcache.length) {
188: for (int i = 0; i < cy; i++) {
189: System
190: .arraycopy(data, pdata, highdeskcache, offset,
191: cx);
192: offset += cx;
193: pdata += cx;
194: }
195: } else {
196: throw new RdpDesktopException("Could not put Desktop");
197: }
198: }
199:
200: public int[] getDesktopInt(int offset, int cx, int cy)
201: throws RdpDesktopException {
202: int length = cx * cy;
203: int pdata = 0;
204: int[] data = new int[length];
205:
206: if (offset > highdeskcache.length) {
207: offset = 0;
208: }
209:
210: if ((int) offset + length <= highdeskcache.length) {
211: for (int i = 0; i < cy; i++) {
212: System
213: .arraycopy(highdeskcache, offset, data, pdata,
214: cx);
215: offset += cx;
216: pdata += cx;
217: }
218:
219: return data;
220: }
221:
222: throw new RdpDesktopException("Could not get Bitmap");
223: }
224: }
|