1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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  *
  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.
  24  */
  25 
  26 package com.sun.javafx.webkit.prism;
  27 
  28 import com.sun.javafx.iio.ImageFrame;
  29 import com.sun.prism.Graphics;
  30 import com.sun.prism.Image;
  31 import com.sun.prism.PrinterGraphics;
  32 import com.sun.prism.ResourceFactory;
  33 import com.sun.prism.Texture;
  34 import com.sun.prism.image.CompoundCoords;
  35 import com.sun.prism.image.CompoundTexture;
  36 import com.sun.prism.image.Coords;
  37 import com.sun.prism.image.ViewPort;
  38 import java.nio.ByteBuffer;
  39 import java.util.logging.Level;
  40 import java.util.logging.Logger;
  41 import javafx.scene.image.PixelFormat;
  42 
  43 /**
  44  * @author Alexey.Ushakov
  45  */
  46 final class WCImageImpl extends PrismImage {
  47     private final static Logger log =
  48         Logger.getLogger(WCImageImpl.class.getName());
  49 
  50     private final Image img;
  51     private Texture texture;
  52     private CompoundTexture compoundTexture;
  53 
  54 
  55     WCImageImpl(int w, int h) {
  56         if (log.isLoggable(Level.FINE)) {
  57             log.log(Level.FINE, "Creating empty image({0},{1})",
  58                     new Object[] {w, h});
  59         }
  60         img = Image.fromIntArgbPreData(new int[w*h], w, h);
  61     }
  62 
  63     WCImageImpl(int[] buffer, int w, int h) {
  64         if (log.isLoggable(Level.FINE)) {
  65             log.log(Level.FINE, "Creating image({0},{1}) from buffer",
  66                     new Object[] {w, h});
  67         }
  68         img = Image.fromIntArgbPreData(buffer, w, h);
  69     }
  70 
  71     WCImageImpl(ImageFrame frame) {
  72         if (log.isLoggable(Level.FINE)) {
  73             log.log(Level.FINE, "Creating image {0}x{1} of type {2} from buffer",
  74                     new Object[]{frame.getWidth(), frame.getHeight(), frame.getImageType()});
  75         }
  76         img = Image.convertImageFrame(frame);
  77     }
  78 
  79     Image getImage() {
  80         return img;
  81     }
  82 
  83     @Override
  84     Graphics getGraphics() {
  85         return null;
  86     }
  87 
  88     @Override
  89     void draw(Graphics g,
  90             int dstx1, int dsty1, int dstx2, int dsty2,
  91             int srcx1, int srcy1, int srcx2, int srcy2)
  92     {
  93         if (g instanceof PrinterGraphics) {
  94             // We're printing. Wrap [img] into a J2DTexture and draw it.
  95             Texture t = g.getResourceFactory().createTexture(
  96                     img, Texture.Usage.STATIC, Texture.WrapMode.CLAMP_NOT_NEEDED);
  97             g.drawTexture(t,
  98                     dstx1, dsty1, dstx2, dsty2,
  99                     srcx1, srcy1, srcx2, srcy2);
 100             t.dispose();
 101             return;
 102         }
 103 
 104         if (texture != null) {
 105             texture.lock();
 106             if (texture.isSurfaceLost()) {
 107                 texture = null;
 108             }
 109         }
 110         if (texture == null && compoundTexture == null) {
 111             ResourceFactory resourceFactory = g.getResourceFactory();
 112             int maxSize = resourceFactory.getMaximumTextureSize();
 113             if (img.getWidth() <= maxSize && img.getHeight() <= maxSize) {
 114                 texture = resourceFactory.createTexture(img, Texture.Usage.DEFAULT, Texture.WrapMode.CLAMP_TO_EDGE);
 115                 assert texture != null;
 116             } else {
 117                 compoundTexture = new CompoundTexture(img, maxSize);
 118             }
 119         }
 120 
 121         if (texture != null) {
 122             assert compoundTexture == null;
 123             g.drawTexture(
 124                     texture,
 125                     dstx1, dsty1, dstx2, dsty2,
 126                     srcx1, srcy1, srcx2, srcy2);
 127             texture.unlock();
 128         } else {
 129             assert compoundTexture != null;
 130             ViewPort viewPort = new ViewPort(
 131                     srcx1,
 132                     srcy1,
 133                     srcx2 - srcx1,
 134                     srcy2 - srcy1);
 135             Coords coords = new Coords(dstx2 - dstx1, dsty2 - dsty1, viewPort);
 136             CompoundCoords compoundCoords = new CompoundCoords(
 137                     compoundTexture,
 138                     coords);
 139             compoundCoords.draw(g, compoundTexture, dstx1, dsty1);
 140         }
 141     }
 142 
 143     @Override
 144     void dispose() {
 145         if (texture != null) {
 146             texture.dispose();
 147             texture = null;
 148         }
 149         if (compoundTexture != null) {
 150             compoundTexture.dispose();
 151             compoundTexture = null;
 152         }
 153     }
 154 
 155     @Override
 156     public int getWidth() {
 157         return img.getWidth();
 158     }
 159 
 160     @Override
 161     public int getHeight() {
 162         return img.getHeight();
 163     }
 164 
 165     @Override
 166     public ByteBuffer getPixelBuffer() {
 167         int w = img.getWidth();
 168         int h = img.getHeight();
 169         int s = w*4;
 170         ByteBuffer pixels = ByteBuffer.allocate(s * h);
 171         img.getPixels(0, 0, w, h,
 172             PixelFormat.getByteBgraInstance(),
 173             pixels, s);
 174         return pixels;
 175     }
 176 
 177     @Override
 178     public float getPixelScale() {
 179         return img.getPixelScale();
 180     }
 181 }