Skip to content

Commit

Permalink
temporary commit - Removal of handle and making getHandle non static
Browse files Browse the repository at this point in the history
  • Loading branch information
amartya4256 committed Jun 19, 2024
1 parent 934b2c5 commit 43f31d9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class TransformWin32Tests extends Win32AutoscaleTestBase {
public void testShouldHaveDifferentHandlesAtDifferentZoomLevels() {
int zoom = DPIUtil.getDeviceZoom();
Transform transform = new Transform(display);
long scaledHandle = Transform.win32_getHandle(transform, zoom * 2);
assertNotEquals("There should be different handles for different zoom levels", scaledHandle, transform.handle);
long scaledHandle2 = Transform.win32_getHandle(transform, zoom * 3);
long scaledHandle = transform.getHandle(zoom * 2);
assertNotEquals("There should be different handles for different zoom levels", scaledHandle, transform.getHandle(zoom));
long scaledHandle2 = transform.getHandle(zoom * 3);
assertNotEquals("There should be different handles for different zoom levels", scaledHandle, scaledHandle2);
}

Expand All @@ -39,7 +39,7 @@ public void testScaledTrasformMustHaveScaledValues() {
Transform transform = new Transform(display, 0, 0, 0, 0, 4, 2);
float[] elements = new float[6];
transform.getElements(elements);
long scaledHandle = Transform.win32_getHandle(transform, zoom * 2);
long scaledHandle = transform.getHandle(zoom * 2);
float[] scaledElements = new float[6];
Gdip.Matrix_GetElements(scaledHandle, scaledElements);
assertEquals(elements[4] * 2, scaledElements[4], 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3790,10 +3790,10 @@ public void getTransform(Transform transform) {
if (transform.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
long gdipGraphics = data.gdipGraphics;
if (gdipGraphics != 0) {
Gdip.Graphics_GetTransform(gdipGraphics, Transform.win32_getHandle(transform, getZoom()));
Gdip.Graphics_GetTransform(gdipGraphics, transform.getHandle(getZoom()));
long identity = identity();
Gdip.Matrix_Invert(identity);
Gdip.Matrix_Multiply(Transform.win32_getHandle(transform, getZoom()), identity, Gdip.MatrixOrderAppend);
Gdip.Matrix_Multiply(transform.getHandle(getZoom()), identity, Gdip.MatrixOrderAppend);
Gdip.Matrix_delete(identity);
} else {
transform.setElements(1, 0, 0, 1, 0, 0);
Expand Down Expand Up @@ -4928,7 +4928,7 @@ public void setTransform(Transform transform) {
initGdip();
long identity = identity();
if (transform != null) {
Gdip.Matrix_Multiply(identity, Transform.win32_getHandle(transform, getZoom()), Gdip.MatrixOrderPrepend);
Gdip.Matrix_Multiply(identity, transform.getHandle(getZoom()), Gdip.MatrixOrderPrepend);
}
Gdip.Graphics_SetTransform(data.gdipGraphics, identity);
Gdip.Matrix_delete(identity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@
*/
public class Transform extends Resource {

/**
* the OS resource for the Transform
* (Warning: This field is platform dependent)
* <p>
* <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
* public API. It is marked public only so that it can be shared
* within the packages provided by SWT. It is not available on all
* platforms and should never be accessed from application code.
* </p>
*
* @noreference This field is not intended to be referenced by clients.
*/
public long handle;

private int initialZoom;

private HashMap<Integer, Long> zoomLevelToHandle = new HashMap<>();
Expand All @@ -77,7 +63,7 @@ public class Transform extends Resource {
* <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
* </ul>
* @exception SWTError <ul>
* <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li>
* <li>ERROR_NO_HANDLES if a getHandle(initialZoom) for the Transform could not be obtained</li>
* </ul>
*
* @see #dispose()
Expand Down Expand Up @@ -109,7 +95,7 @@ public Transform (Device device) {
* <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
* </ul>
* @exception SWTError <ul>
* <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li>
* <li>ERROR_NO_HANDLES if a getHandle(initialZoom) for the Transform could not be obtained</li>
* </ul>
*
* @see #dispose()
Expand Down Expand Up @@ -145,7 +131,7 @@ public Transform(Device device, float[] elements) {
* <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
* </ul>
* @exception SWTError <ul>
* <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li>
* <li>ERROR_NO_HANDLES if a getHandle(initialZoom) for the Transform could not be obtained</li>
* </ul>
*
* @see #dispose()
Expand All @@ -154,7 +140,7 @@ public Transform (Device device, float m11, float m12, float m21, float m22, flo
super(device);
initialZoom = DPIUtil.getDeviceZoom();
this.device.checkGDIP();
handle = Gdip.Matrix_new(m11, m12, m21, m22,
long handle = Gdip.Matrix_new(m11, m12, m21, m22,
DPIUtil.autoScaleUp(this.device, dx, initialZoom), DPIUtil.autoScaleUp(this.device, dy, initialZoom));
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
zoomLevelToHandle.put(initialZoom, handle);
Expand All @@ -169,8 +155,7 @@ static float[] checkTransform(float[] elements) {

@Override
void destroy() {
zoomLevelToHandle.values().forEach(handle -> Gdip.Matrix_delete(handle));
handle = 0;
zoomLevelToHandle.values().forEach(Gdip::Matrix_delete);
}

/**
Expand All @@ -191,7 +176,7 @@ public void getElements(float[] elements) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (elements == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (elements.length < 6) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
Gdip.Matrix_GetElements(handle, elements);
Gdip.Matrix_GetElements(getHandle(initialZoom), elements);
Drawable drawable = getDevice();
elements[4] = DPIUtil.scaleDown(drawable, elements[4], initialZoom);
elements[5] = DPIUtil.scaleDown(drawable, elements[5], initialZoom);
Expand All @@ -209,7 +194,7 @@ public void getElements(float[] elements) {
*/
public void identity() {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
Gdip.Matrix_SetElements(handle, 1, 0, 0, 1, 0, 0);
Gdip.Matrix_SetElements(getHandle(initialZoom), 1, 0, 0, 1, 0, 0);
}

/**
Expand All @@ -223,7 +208,7 @@ public void identity() {
*/
public void invert() {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (Gdip.Matrix_Invert(handle) != 0) SWT.error(SWT.ERROR_CANNOT_INVERT_MATRIX);
if (Gdip.Matrix_Invert(getHandle(initialZoom)) != 0) SWT.error(SWT.ERROR_CANNOT_INVERT_MATRIX);
}

/**
Expand All @@ -238,7 +223,7 @@ public void invert() {
*/
@Override
public boolean isDisposed() {
return handle == 0;
return zoomLevelToHandle.isEmpty();
}

/**
Expand All @@ -249,7 +234,7 @@ public boolean isDisposed() {
*/
public boolean isIdentity() {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
return Gdip.Matrix_IsIdentity(handle);
return Gdip.Matrix_IsIdentity(getHandle(initialZoom));
}

/**
Expand All @@ -271,7 +256,7 @@ public void multiply(Transform matrix) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (matrix == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (matrix.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
Gdip.Matrix_Multiply(handle, matrix.handle, Gdip.MatrixOrderPrepend);
Gdip.Matrix_Multiply(getHandle(initialZoom), matrix.getHandle(initialZoom), Gdip.MatrixOrderPrepend);
}

/**
Expand All @@ -289,7 +274,7 @@ public void multiply(Transform matrix) {
*/
public void rotate(float angle) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
Gdip.Matrix_Rotate(handle, angle, Gdip.MatrixOrderPrepend);
Gdip.Matrix_Rotate(getHandle(initialZoom), angle, Gdip.MatrixOrderPrepend);
}

/**
Expand All @@ -305,7 +290,7 @@ public void rotate(float angle) {
*/
public void scale(float scaleX, float scaleY) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
Gdip.Matrix_Scale(handle, DPIUtil.autoScaleUp(scaleX, initialZoom), DPIUtil.autoScaleUp(scaleY, initialZoom), Gdip.MatrixOrderPrepend);
Gdip.Matrix_Scale(getHandle(initialZoom), DPIUtil.autoScaleUp(scaleX, initialZoom), DPIUtil.autoScaleUp(scaleY, initialZoom), Gdip.MatrixOrderPrepend);
}

/**
Expand All @@ -326,7 +311,7 @@ public void scale(float scaleX, float scaleY) {
public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
Drawable drawable = getDevice();
Gdip.Matrix_SetElements(handle, m11, m12, m21, m22, DPIUtil.autoScaleUp(drawable, dx, initialZoom), DPIUtil.autoScaleUp(drawable, dy, initialZoom));
Gdip.Matrix_SetElements(getHandle(initialZoom), m11, m12, m21, m22, DPIUtil.autoScaleUp(drawable, dx, initialZoom), DPIUtil.autoScaleUp(drawable, dy, initialZoom));
}

/**
Expand All @@ -344,7 +329,7 @@ public void setElements(float m11, float m12, float m21, float m22, float dx, fl
*/
public void shear(float shearX, float shearY) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
Gdip.Matrix_Shear(handle, shearX, shearY, Gdip.MatrixOrderPrepend);
Gdip.Matrix_Shear(getHandle(initialZoom), shearX, shearY, Gdip.MatrixOrderPrepend);
}

/**
Expand All @@ -369,7 +354,7 @@ public void transform(float[] pointArray) {
for (int i = 0; i < length; i++) {
pointArray[i] = DPIUtil.autoScaleUp(drawable, pointArray[i], initialZoom);
}
Gdip.Matrix_TransformPoints(handle, pointArray, length / 2);
Gdip.Matrix_TransformPoints(getHandle(initialZoom), pointArray, length / 2);
for (int i = 0; i < length; i++) {
pointArray[i] = DPIUtil.scaleDown(drawable, pointArray[i], initialZoom);
}
Expand All @@ -389,7 +374,7 @@ public void transform(float[] pointArray) {
public void translate(float offsetX, float offsetY) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
Drawable drawable = getDevice();
Gdip.Matrix_Translate(handle, DPIUtil.autoScaleUp(drawable, offsetX, initialZoom), DPIUtil.autoScaleUp(drawable, offsetY, initialZoom), Gdip.MatrixOrderPrepend);
Gdip.Matrix_Translate(getHandle(initialZoom), DPIUtil.autoScaleUp(drawable, offsetX, initialZoom), DPIUtil.autoScaleUp(drawable, offsetY, initialZoom), Gdip.MatrixOrderPrepend);
}

/**
Expand All @@ -407,7 +392,7 @@ public String toString() {
}

/**
* Get the handle to the OS resource for the given zoom level
* Get the getHandle(initialZoom) to the OS resource for the given zoom level
* <p>
* <b>IMPORTANT:</b> This method is <em>not</em> part of the SWT
* public API. It is marked public only so that it can be shared
Expand All @@ -417,16 +402,16 @@ public String toString() {
*
* @noreference This method is not intended to be referenced by clients.
*/
public static long win32_getHandle(Transform transform, int zoomLevel) {
if(transform.zoomLevelToHandle.get(zoomLevel) == null) {
public long getHandle(int zoomLevel) {
if(zoomLevelToHandle.get(zoomLevel) == null) {
float[] elements = new float[6];
transform.getElements(elements);
elements[4] = DPIUtil.autoScaleUp(transform.device, DPIUtil.scaleDown(transform.device, elements[4], transform.initialZoom), zoomLevel);
elements[5] = DPIUtil.autoScaleUp(transform.device, DPIUtil.scaleDown(transform.device, elements[5], transform.initialZoom), zoomLevel);
getElements(elements);
elements[4] = DPIUtil.autoScaleUp(device, DPIUtil.scaleDown(device, elements[4], initialZoom), zoomLevel);
elements[5] = DPIUtil.autoScaleUp(device, DPIUtil.scaleDown(device, elements[5], initialZoom), zoomLevel);

transform.zoomLevelToHandle.put(zoomLevel, Gdip.Matrix_new(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5]));
zoomLevelToHandle.put(zoomLevel, Gdip.Matrix_new(elements[0], elements[1], elements[2], elements[3], elements[4], elements[5]));
}
return transform.zoomLevelToHandle.get(zoomLevel);
return zoomLevelToHandle.get(zoomLevel);
}

}

0 comments on commit 43f31d9

Please sign in to comment.