author | aghaisas |
Wed, 20 Feb 2019 17:00:40 +0530 | |
branch | metal-prototype-branch |
changeset 57196 | a95707a39ff5 |
parent 47216 | 71c04702a3d5 |
child 57430 | 3d12309f78df |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57196
a95707a39ff5
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2019, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.java2d.pipe; |
|
27 |
||
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
25859
diff
changeset
|
28 |
import jdk.internal.misc.Unsafe; |
2 | 29 |
|
12813
c10ab96dcf41
7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents:
7668
diff
changeset
|
30 |
|
2 | 31 |
/** |
32 |
* The RenderBuffer class is a simplified, high-performance, Unsafe wrapper |
|
33 |
* used for buffering rendering operations in a single-threaded rendering |
|
34 |
* environment. It's functionality is similar to the ByteBuffer and related |
|
35 |
* NIO classes. However, the methods in this class perform little to no |
|
36 |
* alignment or bounds checks for performance reasons. Therefore, it is |
|
37 |
* the caller's responsibility to ensure that all put() calls are properly |
|
38 |
* aligned and within bounds: |
|
39 |
* - int and float values must be aligned on 4-byte boundaries |
|
40 |
* - long and double values must be aligned on 8-byte boundaries |
|
41 |
* |
|
42 |
* This class only includes the bare minimum of methods to support |
|
43 |
* single-threaded rendering. For example, there is no put(double[]) method |
|
44 |
* because we currently have no need for such a method in the STR classes. |
|
45 |
*/ |
|
46 |
public class RenderBuffer { |
|
47 |
||
48 |
/** |
|
49 |
* These constants represent the size of various data types (in bytes). |
|
50 |
*/ |
|
51 |
protected static final long SIZEOF_BYTE = 1L; |
|
52 |
protected static final long SIZEOF_SHORT = 2L; |
|
53 |
protected static final long SIZEOF_INT = 4L; |
|
54 |
protected static final long SIZEOF_FLOAT = 4L; |
|
55 |
protected static final long SIZEOF_LONG = 8L; |
|
56 |
protected static final long SIZEOF_DOUBLE = 8L; |
|
57 |
||
58 |
/** |
|
59 |
* Represents the number of elements at which we have empirically |
|
60 |
* determined that the average cost of a JNI call exceeds the expense |
|
61 |
* of an element by element copy. In other words, if the number of |
|
62 |
* elements in an array to be copied exceeds this value, then we should |
|
63 |
* use the copyFromArray() method to complete the bulk put operation. |
|
64 |
* (This value can be adjusted if the cost of JNI downcalls is reduced |
|
65 |
* in a future release.) |
|
66 |
*/ |
|
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
67 |
private static final int COPY_FROM_ARRAY_THRESHOLD = 6; |
2 | 68 |
|
69 |
protected final Unsafe unsafe; |
|
70 |
protected final long baseAddress; |
|
71 |
protected final long endAddress; |
|
72 |
protected long curAddress; |
|
73 |
protected final int capacity; |
|
74 |
||
75 |
protected RenderBuffer(int numBytes) { |
|
76 |
unsafe = Unsafe.getUnsafe(); |
|
77 |
curAddress = baseAddress = unsafe.allocateMemory(numBytes); |
|
78 |
endAddress = baseAddress + numBytes; |
|
79 |
capacity = numBytes; |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Allocates a fresh buffer using the machine endianness. |
|
84 |
*/ |
|
85 |
public static RenderBuffer allocate(int numBytes) { |
|
86 |
return new RenderBuffer(numBytes); |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Returns the base address of the underlying memory buffer. |
|
91 |
*/ |
|
92 |
public final long getAddress() { |
|
93 |
return baseAddress; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* The behavior (and names) of the following methods are nearly |
|
98 |
* identical to their counterparts in the various NIO Buffer classes. |
|
99 |
*/ |
|
100 |
||
101 |
public final int capacity() { |
|
102 |
return capacity; |
|
103 |
} |
|
104 |
||
105 |
public final int remaining() { |
|
106 |
return (int)(endAddress - curAddress); |
|
107 |
} |
|
108 |
||
109 |
public final int position() { |
|
110 |
return (int)(curAddress - baseAddress); |
|
111 |
} |
|
112 |
||
113 |
public final void position(long numBytes) { |
|
114 |
curAddress = baseAddress + numBytes; |
|
115 |
} |
|
116 |
||
117 |
public final void clear() { |
|
57196
a95707a39ff5
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
47216
diff
changeset
|
118 |
|
a95707a39ff5
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
47216
diff
changeset
|
119 |
System.out.println("RenderBuffer : clear()"); |
2 | 120 |
curAddress = baseAddress; |
121 |
} |
|
122 |
||
5579
1a5e995a710b
6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents:
2696
diff
changeset
|
123 |
public final RenderBuffer skip(long numBytes) { |
1a5e995a710b
6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents:
2696
diff
changeset
|
124 |
curAddress += numBytes; |
1a5e995a710b
6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents:
2696
diff
changeset
|
125 |
return this; |
1a5e995a710b
6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents:
2696
diff
changeset
|
126 |
} |
1a5e995a710b
6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents:
2696
diff
changeset
|
127 |
|
2 | 128 |
/** |
129 |
* putByte() methods... |
|
130 |
*/ |
|
131 |
||
132 |
public final RenderBuffer putByte(byte x) { |
|
133 |
unsafe.putByte(curAddress, x); |
|
134 |
curAddress += SIZEOF_BYTE; |
|
135 |
return this; |
|
136 |
} |
|
137 |
||
138 |
public RenderBuffer put(byte[] x) { |
|
139 |
return put(x, 0, x.length); |
|
140 |
} |
|
141 |
||
142 |
public RenderBuffer put(byte[] x, int offset, int length) { |
|
143 |
if (length > COPY_FROM_ARRAY_THRESHOLD) { |
|
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
144 |
long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET; |
2 | 145 |
long lengthInBytes = length * SIZEOF_BYTE; |
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
146 |
unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes); |
2 | 147 |
position(position() + lengthInBytes); |
148 |
} else { |
|
149 |
int end = offset + length; |
|
150 |
for (int i = offset; i < end; i++) { |
|
151 |
putByte(x[i]); |
|
152 |
} |
|
153 |
} |
|
154 |
return this; |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* putShort() methods... |
|
159 |
*/ |
|
160 |
||
161 |
public final RenderBuffer putShort(short x) { |
|
162 |
// assert (position() % SIZEOF_SHORT == 0); |
|
163 |
unsafe.putShort(curAddress, x); |
|
164 |
curAddress += SIZEOF_SHORT; |
|
165 |
return this; |
|
166 |
} |
|
167 |
||
168 |
public RenderBuffer put(short[] x) { |
|
169 |
return put(x, 0, x.length); |
|
170 |
} |
|
171 |
||
172 |
public RenderBuffer put(short[] x, int offset, int length) { |
|
173 |
// assert (position() % SIZEOF_SHORT == 0); |
|
174 |
if (length > COPY_FROM_ARRAY_THRESHOLD) { |
|
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
175 |
long offsetInBytes = offset * SIZEOF_SHORT + Unsafe.ARRAY_SHORT_BASE_OFFSET; |
2 | 176 |
long lengthInBytes = length * SIZEOF_SHORT; |
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
177 |
unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes); |
2 | 178 |
position(position() + lengthInBytes); |
179 |
} else { |
|
180 |
int end = offset + length; |
|
181 |
for (int i = offset; i < end; i++) { |
|
182 |
putShort(x[i]); |
|
183 |
} |
|
184 |
} |
|
185 |
return this; |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* putInt() methods... |
|
190 |
*/ |
|
191 |
||
192 |
public final RenderBuffer putInt(int pos, int x) { |
|
193 |
// assert (baseAddress + pos % SIZEOF_INT == 0); |
|
194 |
unsafe.putInt(baseAddress + pos, x); |
|
57196
a95707a39ff5
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
47216
diff
changeset
|
195 |
|
a95707a39ff5
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
47216
diff
changeset
|
196 |
System.out.println("RenderBuffer : putInt() --- added :"+x); |
a95707a39ff5
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
47216
diff
changeset
|
197 |
|
2 | 198 |
return this; |
199 |
} |
|
200 |
||
201 |
public final RenderBuffer putInt(int x) { |
|
202 |
// assert (position() % SIZEOF_INT == 0); |
|
203 |
unsafe.putInt(curAddress, x); |
|
204 |
curAddress += SIZEOF_INT; |
|
57196
a95707a39ff5
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
47216
diff
changeset
|
205 |
|
a95707a39ff5
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
47216
diff
changeset
|
206 |
System.out.println("RenderBuffer : putInt() --- added :"+x); |
a95707a39ff5
Description : Metal Rendering Pipeline - initial implementation of line and quad rendering
aghaisas
parents:
47216
diff
changeset
|
207 |
|
2 | 208 |
return this; |
209 |
} |
|
210 |
||
211 |
public RenderBuffer put(int[] x) { |
|
212 |
return put(x, 0, x.length); |
|
213 |
} |
|
214 |
||
215 |
public RenderBuffer put(int[] x, int offset, int length) { |
|
216 |
// assert (position() % SIZEOF_INT == 0); |
|
217 |
if (length > COPY_FROM_ARRAY_THRESHOLD) { |
|
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
218 |
long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET; |
2 | 219 |
long lengthInBytes = length * SIZEOF_INT; |
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
220 |
unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes); |
2 | 221 |
position(position() + lengthInBytes); |
222 |
} else { |
|
223 |
int end = offset + length; |
|
224 |
for (int i = offset; i < end; i++) { |
|
225 |
putInt(x[i]); |
|
226 |
} |
|
227 |
} |
|
228 |
return this; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* putFloat() methods... |
|
233 |
*/ |
|
234 |
||
235 |
public final RenderBuffer putFloat(float x) { |
|
236 |
// assert (position() % SIZEOF_FLOAT == 0); |
|
237 |
unsafe.putFloat(curAddress, x); |
|
238 |
curAddress += SIZEOF_FLOAT; |
|
239 |
return this; |
|
240 |
} |
|
241 |
||
242 |
public RenderBuffer put(float[] x) { |
|
243 |
return put(x, 0, x.length); |
|
244 |
} |
|
245 |
||
246 |
public RenderBuffer put(float[] x, int offset, int length) { |
|
247 |
// assert (position() % SIZEOF_FLOAT == 0); |
|
248 |
if (length > COPY_FROM_ARRAY_THRESHOLD) { |
|
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
249 |
long offsetInBytes = offset * SIZEOF_FLOAT + Unsafe.ARRAY_FLOAT_BASE_OFFSET; |
2 | 250 |
long lengthInBytes = length * SIZEOF_FLOAT; |
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
251 |
unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes); |
2 | 252 |
position(position() + lengthInBytes); |
253 |
} else { |
|
254 |
int end = offset + length; |
|
255 |
for (int i = offset; i < end; i++) { |
|
256 |
putFloat(x[i]); |
|
257 |
} |
|
258 |
} |
|
259 |
return this; |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* putLong() methods... |
|
264 |
*/ |
|
265 |
||
266 |
public final RenderBuffer putLong(long x) { |
|
267 |
// assert (position() % SIZEOF_LONG == 0); |
|
268 |
unsafe.putLong(curAddress, x); |
|
269 |
curAddress += SIZEOF_LONG; |
|
270 |
return this; |
|
271 |
} |
|
272 |
||
273 |
public RenderBuffer put(long[] x) { |
|
274 |
return put(x, 0, x.length); |
|
275 |
} |
|
276 |
||
277 |
public RenderBuffer put(long[] x, int offset, int length) { |
|
278 |
// assert (position() % SIZEOF_LONG == 0); |
|
279 |
if (length > COPY_FROM_ARRAY_THRESHOLD) { |
|
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
280 |
long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET; |
2 | 281 |
long lengthInBytes = length * SIZEOF_LONG; |
2696
1189480fe090
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
jgodinez
parents:
2
diff
changeset
|
282 |
unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes); |
2 | 283 |
position(position() + lengthInBytes); |
284 |
} else { |
|
285 |
int end = offset + length; |
|
286 |
for (int i = offset; i < end; i++) { |
|
287 |
putLong(x[i]); |
|
288 |
} |
|
289 |
} |
|
290 |
return this; |
|
291 |
} |
|
292 |
||
293 |
/** |
|
294 |
* putDouble() method(s)... |
|
295 |
*/ |
|
296 |
||
297 |
public final RenderBuffer putDouble(double x) { |
|
298 |
// assert (position() % SIZEOF_DOUBLE == 0); |
|
299 |
unsafe.putDouble(curAddress, x); |
|
300 |
curAddress += SIZEOF_DOUBLE; |
|
301 |
return this; |
|
302 |
} |
|
303 |
} |