Skip to content

Commit

Permalink
8204060: [Canvas] Add API in GraphicsContext to control image smoothing
Browse files Browse the repository at this point in the history
Reviewed-by: kcr, aghaisas
  • Loading branch information
arapte committed Nov 30, 2018
1 parent f5b9a79 commit a043219
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -98,8 +98,9 @@ public class NGCanvas extends NGNode {
public static final byte DASH_ARRAY = ATTR_BASE + 17;
public static final byte DASH_OFFSET = ATTR_BASE + 18;
public static final byte FONT_SMOOTH = ATTR_BASE + 19;
public static final byte IMAGE_SMOOTH = ATTR_BASE + 20;

public static final byte OP_BASE = 20;
public static final byte OP_BASE = 25;
public static final byte FILL_RECT = OP_BASE + 0;
public static final byte STROKE_RECT = OP_BASE + 1;
public static final byte CLEAR_RECT = OP_BASE + 2;
Expand Down Expand Up @@ -334,6 +335,7 @@ private void restore(Graphics g, int tw, int th) {
private PrismTextLayout textLayout;
private PGFont pgfont;
private int smoothing;
private boolean imageSmoothing;
private int align;
private int baseline;
private Affine2D transform;
Expand Down Expand Up @@ -389,6 +391,7 @@ private void initAttributes() {
// textLayout stores no state between render operations
pgfont = (PGFont) FontHelper.getNativeFont(Font.getDefault());
smoothing = SMOOTH_GRAY;
imageSmoothing = true;
align = ALIGN_LEFT;
baseline = VPos.BASELINE.ordinal();
transform.setToScale(highestPixelScale, highestPixelScale);
Expand Down Expand Up @@ -1003,6 +1006,9 @@ private void renderStream(GrowableDataBuffer buf) {
case FONT_SMOOTH:
smoothing = buf.getUByte();
break;
case IMAGE_SMOOTH:
imageSmoothing = buf.getBoolean();
break;
case TEXT_ALIGN:
align = buf.getUByte();
break;
Expand Down Expand Up @@ -1345,9 +1351,16 @@ public void handleRenderOp(int token, GrowableDataBuffer buf,
ResourceFactory factory = gr.getResourceFactory();
Texture tex =
factory.getCachedTexture(img, Texture.WrapMode.CLAMP_TO_EDGE);
boolean isSmooth = tex.getLinearFiltering();
if (imageSmoothing != isSmooth) {
tex.setLinearFiltering(imageSmoothing);
}
gr.drawTexture(tex,
dx, dy, dx+dw, dy+dh,
sx, sy, sx+sw, sy+sh);
if (imageSmoothing != isSmooth) {
tex.setLinearFiltering(isSmooth);
}
tex.unlock();
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -281,14 +281,14 @@ public void setRadialGradient(int cx, int cy, int fx, int fy,
}

public void setTexture(int imageType, int data[], int width, int height, int stride,
Transform6 textureTransform, boolean repeat, boolean hasAlpha)
Transform6 textureTransform, boolean repeat, boolean linearFiltering, boolean hasAlpha)
{
this.inputImageCheck(width, height, 0, stride, data.length);
this.setTextureImpl(imageType, data, width, height, stride, textureTransform, repeat, hasAlpha);
this.setTextureImpl(imageType, data, width, height, stride, textureTransform, repeat, linearFiltering, hasAlpha);
}

private native void setTextureImpl(int imageType, int data[], int width, int height, int stride,
Transform6 textureTransform, boolean repeat, boolean hasAlpha);
Transform6 textureTransform, boolean repeat, boolean linearFiltering, boolean hasAlpha);

/**
* Sets a clip rectangle for all primitives. Each primitive will be
Expand Down Expand Up @@ -389,23 +389,23 @@ public void fillLCDAlphaMask(byte[] mask, int x, int y, int width, int height, i
private native void fillLCDAlphaMaskImpl(byte[] mask, int x, int y, int width, int height, int offset, int stride);

public void drawImage(int imageType, int imageMode, int data[], int width, int height, int offset, int stride,
Transform6 textureTransform, boolean repeat,
Transform6 textureTransform, boolean repeat, boolean linearFiltering,
int bboxX, int bboxY, int bboxW, int bboxH,
int lEdge, int rEdge, int tEdge, int bEdge,
int txMin, int tyMin, int txMax, int tyMax,
boolean hasAlpha)
{
this.inputImageCheck(width, height, offset, stride, data.length);
this.drawImageImpl(imageType, imageMode, data, width, height, offset, stride,
textureTransform, repeat,
textureTransform, repeat, linearFiltering,
bboxX, bboxY, bboxW, bboxH,
lEdge, rEdge, tEdge, bEdge,
txMin, tyMin, txMax, tyMax,
hasAlpha);
}

private native void drawImageImpl(int imageType, int imageMode, int data[], int width, int height, int offset, int stride,
Transform6 textureTransform, boolean repeat,
Transform6 textureTransform, boolean repeat, boolean linearFiltering,
int bboxX, int bboxY, int bboxW, int bboxH,
int lEdge, int rEdge, int tEdge, int bEdge,
int txMin, int tyMin, int txMax, int tyMax,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -444,6 +444,7 @@ public void fillRect(float x, float y, float width, float height) {
tex.getOffset(), tex.getPhysicalWidth(),
piscesTx,
tex.getWrapMode() == Texture.WrapMode.REPEAT,
tex.getLinearFiltering(),
(int)(Math.min(p1.x, p2.x) * SWUtils.TO_PISCES), (int)(Math.min(p1.y, p2.y) * SWUtils.TO_PISCES),
(int)(Math.abs(p2.x - p1.x) * SWUtils.TO_PISCES), (int)(Math.abs(p2.y - p1.y) * SWUtils.TO_PISCES),
RendererBase.IMAGE_FRAC_EDGE_KEEP, RendererBase.IMAGE_FRAC_EDGE_KEEP,
Expand Down Expand Up @@ -739,6 +740,7 @@ private void drawTexture(Texture tex, int imageMode,
swTex.getOffset(), tex.getPhysicalWidth(),
piscesTx,
tex.getWrapMode() == Texture.WrapMode.REPEAT,
tex.getLinearFiltering(),
(int)(SWUtils.TO_PISCES * dstBBox.getMinX()), (int)(SWUtils.TO_PISCES * dstBBox.getMinY()),
(int)(SWUtils.TO_PISCES * dstBBox.getWidth()), (int)(SWUtils.TO_PISCES * dstBBox.getHeight()),
lEdge, rEdge, tEdge, bEdge,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -189,6 +189,7 @@ void setPaintBeforeDraw(Paint p, BaseTransform tx, float x, float y, float width
tex.getContentWidth(), tex.getContentHeight(), tex.getPhysicalWidth(),
piscesTx,
tex.getWrapMode() == Texture.WrapMode.REPEAT,
tex.getLinearFiltering(),
tex.hasAlpha());
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -46,6 +46,7 @@ static Texture create(SWResourceFactory factory, PixelFormat formatHint, WrapMod
private SWResourceFactory factory;
private int lastImageSerial;
private final WrapMode wrapMode;
private boolean linearFiltering = true;

SWTexture(SWResourceFactory factory, WrapMode wrapMode, int w, int h) {
this.factory = factory;
Expand All @@ -66,6 +67,7 @@ static Texture create(SWResourceFactory factory, PixelFormat formatHint, WrapMod
this.factory = sharedTex.factory;
// REMIND: Use indirection to share the serial number?
this.lastImageSerial = sharedTex.lastImageSerial;
this.linearFiltering = sharedTex.linearFiltering;
this.wrapMode = altMode;
lock();
}
Expand Down Expand Up @@ -252,11 +254,13 @@ public Texture getSharedTexture(WrapMode altMode) {

@Override
public boolean getLinearFiltering() {
return false;
return linearFiltering;
}

@Override
public void setLinearFiltering(boolean linear) { }
public void setLinearFiltering(boolean linear) {
linearFiltering = linear;
}

void allocate() {
if (allocated) {
Expand Down
Loading

0 comments on commit a043219

Please sign in to comment.