private void drawBackingStoreImage(Graphics g) { Graphics2D g2d = (Graphics2D) g; GraphicsConfiguration gc = g2d.getDeviceConfiguration(); if (vImg == null || vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) { /* Create a new volatile image */ vImg = createVolatileImage(PANEL_WIDTH, PANEL_HEIGHT / 3); } Graphics vImgGraphics = vImg.createGraphics(); vImgGraphics.setColor(Color.WHITE); vImgGraphics.fillRect(0, 0, PANEL_WIDTH, PANEL_HEIGHT / 3); drawInfo(vImgGraphics, PANEL_X, PANEL_Y, "Backbuffer", Color.MAGENTA); g.drawImage(vImg, 0, PANEL_Y * 2, this); }
private int updateOffscreenImage() { // Update offscreen image reference if (offscreenImage == null) offscreenImage = offscreenImageReference.get(); // Offscreen image not available if (offscreenImage == null) return VolatileImage.IMAGE_INCOMPATIBLE; // Buffered image is always valid if (bufferType != BUFFER_VOLATILE_IMAGE) return VolatileImage.IMAGE_OK; // Determine GraphicsConfiguration context GraphicsConfiguration gConfiguration = getGraphicsConfiguration(); if (gConfiguration == null) return VolatileImage.IMAGE_INCOMPATIBLE; // Return Volatile image state return ((VolatileImage)offscreenImage).validate(gConfiguration); }
public void render(){ if(vImage.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE){ vImage = gc.createCompatibleVolatileImage(GAME_WIDTH, GAME_HEIGHT); } Graphics g = vImage.getGraphics(); g.setColor(Color.BLACK); g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); g.setColor(Color.WHITE); /**Draw stuff here*/ graph.render(g); // g.setColor(Color.GREEN); // g.drawString("FPS: " + String.valueOf(FPS) , 0, 10); // g.drawString("UPS: " + String.valueOf(UPS) , 60, 10); g = canvas.getGraphics(); g.drawImage(vImage, 0, 0, canvasWidth, canvasHeight, null); g.dispose(); }
public static void main(String[] args) { BufferedImage src = createSrc(); VolatileImage dst = createDst(); System.out.println("Dst: " + dst); boolean status; int count = max_rendering_count; do { System.out.println("render image: " + (max_rendering_count - count)); status = render(src, dst); } while (status && count-- > 0); if (!status || count > 0) { throw new RuntimeException("Test failed: " + count); } }
protected BufferedImage getBufferedImage(Image img) { if (img instanceof BufferedImage) { // Otherwise we expect a BufferedImage to behave as a standard BI return (BufferedImage)img; } else if (img instanceof ToolkitImage) { // This can be null if the image isn't loaded yet. // This is fine as in that case our caller will return // as it will only draw a fully loaded image return ((ToolkitImage)img).getBufferedImage(); } else if (img instanceof VolatileImage) { // VI needs to make a new BI: this is unavoidable but // I don't expect VI's to be "huge" in any case. return ((VolatileImage)img).getSnapshot(); } else { // may be null or may be some non-standard Image which // shouldn't happen as Image is implemented by the platform // not by applications // If you add a new Image implementation to the platform you // will need to support it here similarly to VI. return null; } }
private static void initVI(GraphicsConfiguration gc) { int res; if (destVI == null) { res = VolatileImage.IMAGE_INCOMPATIBLE; } else { res = destVI.validate(gc); } if (res == VolatileImage.IMAGE_INCOMPATIBLE) { if (destVI != null) destVI.flush(); destVI = gc.createCompatibleVolatileImage(IMAGE_SIZE, IMAGE_SIZE); destVI.validate(gc); res = VolatileImage.IMAGE_RESTORED; } if (res == VolatileImage.IMAGE_RESTORED) { Graphics vig = destVI.getGraphics(); vig.setColor(Color.red); vig.fillRect(0, 0, destVI.getWidth(), destVI.getHeight()); vig.dispose(); } }
public void render(Graphics g) { do { height = getBounds().height; width = getBounds().width; if (vimg == null) { vimg = createVolatileImage(width, height); renderOffscreen(); } int returnCode = vimg.validate(getGraphicsConfiguration()); if (returnCode == VolatileImage.IMAGE_RESTORED) { renderOffscreen(); } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) { vimg = getGraphicsConfiguration(). createCompatibleVolatileImage(width, height); renderOffscreen(); } else if (returnCode == VolatileImage.IMAGE_OK) { renderOffscreen(); } g.drawImage(vimg, 0, 0, this); } while (vimg.contentsLost()); }
/** * Gets the availableAcceleratedMemory attribute of the YassSheet object * * @return The availableAcceleratedMemory value */ public int getAvailableAcceleratedMemory() { GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); try { GraphicsDevice[] gs = ge.getScreenDevices(); // Get current amount of available memory in bytes for each screen for (GraphicsDevice g : gs) { // Workaround; see description VolatileImage im = g.getDefaultConfiguration() .createCompatibleVolatileImage(1, 1); // Retrieve available free accelerated image memory int bytes = g.getAvailableAcceleratedMemory(); // Release the temporary volatile image im.flush(); return bytes; } } catch (HeadlessException e) { // Is thrown if there are no screen devices } return 0; }
/** * Description of the Method * * @param g Description of the Parameter */ public void paintBackBuffer(Graphics2D g) { final int MAX_TRIES = 10; for (int i = 0; i < MAX_TRIES; i++) { g.drawImage(backVolImage, 0, 0, this); if (!backVolImage.contentsLost()) { return; } switch (backVolImage.validate(g.getDeviceConfiguration())) { case VolatileImage.IMAGE_INCOMPATIBLE: backVolImage.flush(); backVolImage = g.getDeviceConfiguration().createCompatibleVolatileImage(image.getWidth(), image.getHeight()); case VolatileImage.IMAGE_RESTORED: Graphics2D gc = backVolImage.createGraphics(); gc.drawImage(image, 0, 0, Color.white, null); gc.dispose(); break; } } g.drawImage(image, 0, 0, Color.white, null); }
/** * {@inheritDoc} * * @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage */ @Override public VolatileImage createCompatibleVolatileImage(int width, int height, int transparency, int type) { if ((type != FBOBJECT && type != TEXTURE) || transparency == Transparency.BITMASK || type == FBOBJECT && !isCapPresent(CAPS_EXT_FBOBJECT)) { return null; } SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height, transparency, type); Surface sd = vi.getDestSurface(); if (!(sd instanceof AccelSurface) || ((AccelSurface)sd).getType() != type) { vi.flush(); vi = null; } return vi; }
public static void main(final String[] args) { final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); final GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); final VolatileImage vi = gc.createCompatibleVolatileImage(200, 200); final SunGraphics2D sg2d = (SunGraphics2D) vi.createGraphics(); sg2d.constrain(0, 61, 100, 100); final AffineTransform expected = sg2d.cloneTransform(); sg2d.setTransform(sg2d.getTransform()); final AffineTransform actual = sg2d.cloneTransform(); sg2d.dispose(); vi.flush(); if (!expected.equals(actual)) { System.out.println("Expected = " + expected); System.out.println("Actual = " + actual); throw new RuntimeException("Wrong transform"); } }
void test() { createVImg(); BufferedImage bi = null; do { int valCode = vImg.validate(getDefaultGC()); if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) { createVImg(); } Graphics2D g = vImg.createGraphics(); draw(g); bi = vImg.getSnapshot(); } while (vImg.contentsLost()); if (bi != null) { test(bi); write(bi); } }
private void initBackbuffer() { createBackbuffer(); int res = bb.validate(getGraphicsConfiguration()); if (res == VolatileImage.IMAGE_INCOMPATIBLE) { bb = null; createBackbuffer(); bb.validate(getGraphicsConfiguration()); res = VolatileImage.IMAGE_RESTORED; } if (res == VolatileImage.IMAGE_RESTORED) { Graphics g = bb.getGraphics(); g.setColor(new Color(rnd.nextInt(0x00ffffff))); g.fillRect(0, 0, bb.getWidth(), bb.getHeight()); volSprite = createVolatileImage(100, 100); } volSprite.validate(getGraphicsConfiguration()); }
public VolatileImage accelerateImage(BufferedImage bi) { VolatileImage testVI = f.createVolatileImage(TEST_W, TEST_H); do { if (testVI.validate(f.getGraphicsConfiguration()) == VolatileImage.IMAGE_INCOMPATIBLE) { testVI = f.createVolatileImage(TEST_W, TEST_H); } Graphics2D g = testVI.createGraphics(); g.setComposite(AlphaComposite.Src); g.setColor(Color.green); g.fillRect(0, 0, TEST_W, TEST_H); g.drawImage(bi, 0, 0, null); g.drawImage(bi, 0, 0, null); g.drawImage(bi, 0, 0, null); g.dispose(); } while (testVI.contentsLost()); return testVI; }
public static void main(String[] args) { boolean iaeThrown = false; GraphicsEnvironment ge = GraphicsEnvironment. getLocalGraphicsEnvironment(); GraphicsConfiguration gc = ge.getDefaultScreenDevice(). getDefaultConfiguration(); try { VolatileImage volatileImage = gc.createCompatibleVolatileImage(0, 0); } catch (IllegalArgumentException iae) { iaeThrown = true; } if (!iaeThrown) { throw new RuntimeException ("IllegalArgumentException not thrown " + "for createCompatibleVolatileImage(0,0)"); } }
private static boolean render(BufferedImage src, VolatileImage dst) { int cnt = 5; do { Graphics2D g = dst.createGraphics(); g.setColor(dstColor); g.fillRect(0, 0, dst.getWidth(), dst.getHeight()); g.drawImage(src, 0, 0, null); g.dispose(); } while (dst.contentsLost() && (--cnt > 0)); if (cnt == 0) { System.err.println("Test failed: unable to render to volatile destination"); return false; } BufferedImage s = dst.getSnapshot(); return s.getRGB(1,1) == srcColor.getRGB(); }
public void validateSprite() { int result = ((VolatileImage)image).validate(getGraphicsConfiguration()); if (result == VolatileImage.IMAGE_INCOMPATIBLE) { image = createSprite(); result = VolatileImage.IMAGE_RESTORED; } if (result == VolatileImage.IMAGE_RESTORED) { Graphics g = image.getGraphics(); g.setColor(color); g.fillRect(0, 0, image.getWidth(null), image.getHeight(null)); } }
/** * Updates internal buffers (both volatile and non-volatile) * by requesting the back-buffer from the peer. */ private void updateInternalBuffers() { // get the images associated with the draw buffer drawBuffer = getBackBuffer(); if (drawBuffer instanceof VolatileImage) { drawVBuffer = (VolatileImage)drawBuffer; } else { drawVBuffer = null; } }
@Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Passed")) { passed = true; System.out.println("Test Passed"); } else if (e.getActionCommand().equals("Failed")) { System.out.println("Test Failed"); for (int i = 0; i < images.length; i++) { String f = "NonOpaqueDestLCDAATest_"+tr[i]; try { if (images[i] instanceof VolatileImage) { f += "_vi.png"; ImageIO.write(((VolatileImage)images[i]). getSnapshot(), "png", new File(f)); } else { f += "_bi.png"; ImageIO.write((BufferedImage)images[i], "png", new File(f)); } System.out.printf("Dumped %s image to %s\n", tr[i], f); } catch (Throwable t) {} } passed = false; } dispose(); complete.countDown(); }
void revalidate(boolean checkSize) { validatedContents = false; if (backBuffers == null) { return; } if (checkSize) { Insets insets = getInsets_NoClientCode(); if (getWidth() != width || getHeight() != height || !insets.equals(this.insets)) { // component has been resized; recreate the backbuffers createBackBuffers(backBuffers.length); validatedContents = true; } } // now validate the backbuffer GraphicsConfiguration gc = getGraphicsConfiguration_NoClientCode(); int returnCode = backBuffers[backBuffers.length - 1].validate(gc); if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) { if (checkSize) { createBackBuffers(backBuffers.length); // backbuffers were recreated, so validate again backBuffers[backBuffers.length - 1].validate(gc); } // else case means we're called from Swing on the toolkit // thread, don't recreate buffers as that'll deadlock // (creating VolatileImages invokes getting GraphicsConfig // which grabs treelock). validatedContents = true; } else if (returnCode == VolatileImage.IMAGE_RESTORED) { validatedContents = true; } }
public static void main(final String[] args) { for (final AffineTransform atfm : TRANSFORMS) { for (final int viType : TRANSPARENCIES) { for (final int biType : TYPES) { final BufferedImage bi = makeUnmanagedBI(biType); final VolatileImage vi = makeVI(viType); final long time = test(bi, vi, atfm) / 1000000000; if (time > 1) { throw new RuntimeException(String.format( "drawImage is slow: %d seconds", time)); } } } } }
private void test() { GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice().getDefaultConfiguration(); if (gc.getColorModel().getPixelSize() < 16) { System.out.println("<16 bit depth detected, test passed"); return; } VolatileImage vi = gc.createCompatibleVolatileImage(250, 4*120, Transparency.OPAQUE); BufferedImage res; do { vi.validate(gc); Graphics2D g2d = vi.createGraphics(); g2d.setColor(Color.white); g2d.fillRect(0, 0, vi.getWidth(), vi.getHeight()); render(g2d); res = vi.getSnapshot(); } while (vi.contentsLost()); for (int y = 0; y < bi.getHeight(); y++) { for (int x = 0; x < bi.getWidth(); x++) { if (res.getRGB(x, y) == Color.black.getRGB()) { System.err.printf("Test FAILED: found black at %d,%d\n", x, y); try { String fileName = "AccelPaintsTest.png"; ImageIO.write(res, "png", new File(fileName)); System.err.println("Dumped rendering to " + fileName); } catch (IOException e) {} throw new RuntimeException("Test FAILED: found black"); } } } }
/** * Return a volatile offscreen buffer that should be used as a * double buffer with the specified component <code>c</code>. * The image returned will be an instance of VolatileImage, or null * if a VolatileImage object could not be instantiated. * This buffer might be smaller than <code>(proposedWidth,proposedHeight)</code>. * This happens when the maximum double buffer size has been set for this * repaint manager. * * @see java.awt.image.VolatileImage * @since 1.4 */ public Image getVolatileOffscreenBuffer(Component c, int proposedWidth,int proposedHeight) { RepaintManager delegate = getDelegate(c); if (delegate != null) { return delegate.getVolatileOffscreenBuffer(c, proposedWidth, proposedHeight); } // If the window is non-opaque, it's double-buffered at peer's level Window w = (c instanceof Window) ? (Window)c : SwingUtilities.getWindowAncestor(c); if (!w.isOpaque()) { Toolkit tk = Toolkit.getDefaultToolkit(); if ((tk instanceof SunToolkit) && (((SunToolkit)tk).needUpdateWindow())) { return null; } } GraphicsConfiguration config = c.getGraphicsConfiguration(); if (config == null) { config = GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice().getDefaultConfiguration(); } Dimension maxSize = getDoubleBufferMaximumSize(); int width = proposedWidth < 1 ? 1 : (proposedWidth > maxSize.width? maxSize.width : proposedWidth); int height = proposedHeight < 1 ? 1 : (proposedHeight > maxSize.height? maxSize.height : proposedHeight); VolatileImage image = volatileMap.get(config); if (image == null || image.getWidth() < width || image.getHeight() < height) { if (image != null) { image.flush(); } image = config.createCompatibleVolatileImage(width, height, volatileBufferType); volatileMap.put(config, image); } return image; }
/** * Paints a region of a component * * @param paintingComponent Component to paint * @param bufferComponent Component to obtain buffer for * @param g Graphics to paint to * @param x X-coordinate * @param y Y-coordinate * @param w Width * @param h Height * @return true if painting was successful. */ public boolean paint(JComponent paintingComponent, JComponent bufferComponent, Graphics g, int x, int y, int w, int h) { // First attempt to use VolatileImage buffer for performance. // If this fails (which should rarely occur), fallback to a // standard Image buffer. boolean paintCompleted = false; Image offscreen; if (repaintManager.useVolatileDoubleBuffer() && (offscreen = getValidImage(repaintManager. getVolatileOffscreenBuffer(bufferComponent, w, h))) != null) { VolatileImage vImage = (java.awt.image.VolatileImage)offscreen; GraphicsConfiguration gc = bufferComponent. getGraphicsConfiguration(); for (int i = 0; !paintCompleted && i < RepaintManager.VOLATILE_LOOP_MAX; i++) { if (vImage.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) { repaintManager.resetVolatileDoubleBuffer(gc); offscreen = repaintManager.getVolatileOffscreenBuffer( bufferComponent,w, h); vImage = (java.awt.image.VolatileImage)offscreen; } paintDoubleBuffered(paintingComponent, vImage, g, x, y, w, h); paintCompleted = !vImage.contentsLost(); } } // VolatileImage painting loop failed, fallback to regular // offscreen buffer if (!paintCompleted && (offscreen = getValidImage( repaintManager.getOffscreenBuffer( bufferComponent, w, h))) != null) { paintDoubleBuffered(paintingComponent, offscreen, g, x, y, w, h); paintCompleted = true; } return paintCompleted; }
private static void test(final BufferedImage bi) throws IOException { GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsConfiguration gc = ge.getDefaultScreenDevice() .getDefaultConfiguration(); VolatileImage vi = gc.createCompatibleVolatileImage(500, 200, TRANSLUCENT); BufferedImage gold = gc.createCompatibleImage(500, 200, TRANSLUCENT); // draw to compatible Image draw(bi, gold); // draw to volatile image int attempt = 0; BufferedImage snapshot; while (true) { if (++attempt > 10) { throw new RuntimeException("Too many attempts: " + attempt); } vi.validate(gc); if (vi.validate(gc) != VolatileImage.IMAGE_OK) { continue; } draw(bi, vi); snapshot = vi.getSnapshot(); if (vi.contentsLost()) { continue; } break; } // validate images for (int x = 0; x < gold.getWidth(); ++x) { for (int y = 0; y < gold.getHeight(); ++y) { if (gold.getRGB(x, y) != snapshot.getRGB(x, y)) { ImageIO.write(gold, "png", new File("gold.png")); ImageIO.write(snapshot, "png", new File("bi.png")); throw new RuntimeException("Test failed."); } } } }
/** * Creates a WGL-based backbuffer for the given peer and returns the * image wrapper. */ @Override public VolatileImage createBackBuffer(WComponentPeer peer) { Component target = (Component)peer.getTarget(); return new SunVolatileImage(target, target.getWidth(), target.getHeight(), Boolean.TRUE); }
/** * {@inheritDoc} * * @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage */ @Override public VolatileImage createCompatibleVolatileImage(int width, int height, int transparency, int type) { if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED || transparency == Transparency.BITMASK) { return null; } if (type == FBOBJECT) { if (!isCapPresent(CAPS_EXT_FBOBJECT)) { return null; } } else if (type == PBUFFER) { boolean isOpaque = transparency == Transparency.OPAQUE; if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) { return null; } } SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height, transparency, type); Surface sd = vi.getDestSurface(); if (!(sd instanceof AccelSurface) || ((AccelSurface)sd).getType() != type) { vi.flush(); vi = null; } return vi; }
private static void test(VolatileImage vi, BufferedImage ci, GraphicsConfiguration gc) { for (int r = 0; r <= 255; ++r) { for (int a = 0; a <= 255; ++a) { fill(vi, new Color(r, 0, 0, a)); fill(ci, new Color(r, 0, 0, a)); validate(ci, vi.getSnapshot()); } } }
/** * Creates a VolatileImage that essentially wraps the target Component's * backbuffer (the provided backbuffer handle is essentially ignored). */ @Override public VolatileImage createBackBufferImage(Component target, long backBuffer) { return new SunVolatileImage(target, target.getWidth(), target.getHeight(), Boolean.TRUE); }
static VolatileImage getVImage(GraphicsConfiguration gc, int w, int h) { VolatileImage image = gc.createCompatibleVolatileImage(w, h, Transparency.OPAQUE); image.validate(gc); initImage(gc, image); return image; }
/** * Creates the back buffers * * @param numBuffers the number of buffers to create */ protected void createBackBuffers(int numBuffers) { if (numBuffers == 0) { backBuffers = null; } else { // save the current bounds width = getWidth(); height = getHeight(); insets = getInsets_NoClientCode(); int iWidth = width - insets.left - insets.right; int iHeight = height - insets.top - insets.bottom; // It is possible for the component's width and/or height // to be 0 here. Force the size of the backbuffers to // be > 0 so that creating the image won't fail. iWidth = Math.max(1, iWidth); iHeight = Math.max(1, iHeight); if (backBuffers == null) { backBuffers = new VolatileImage[numBuffers]; } else { // flush any existing backbuffers for (int i = 0; i < numBuffers; i++) { if (backBuffers[i] != null) { backBuffers[i].flush(); backBuffers[i] = null; } } } // create the backbuffers for (int i = 0; i < numBuffers; i++) { backBuffers[i] = createVolatileImage(iWidth, iHeight); } } }
/** * Creates a D3D-based backbuffer for the given peer and returns the * image wrapper. */ @Override public VolatileImage createBackBuffer(WComponentPeer peer) { Component target = (Component)peer.getTarget(); // it is possible for the component to have size 0x0, adjust it to // be at least 1x1 to avoid IAE int w = Math.max(1, target.getWidth()); int h = Math.max(1, target.getHeight()); return new SunVolatileImage(target, w, h, Boolean.TRUE); }