modules/graphics/src/main/java/com/sun/prism/impl/QueuedPixelSource.java

Print this page




  96         }
  97         enqueued = null;
  98     }
  99 
 100     private boolean usesSameBuffer(Pixels p1, Pixels p2) {
 101         if (p1 == p2) return true;
 102         if (p1 == null || p2 == null) return false;
 103         return (p1.getPixels() == p2.getPixels());
 104     }
 105 
 106     /**
 107      * Return an unused Pixels with the indicated dimensions and scale.
 108      * The returned object may either be saved from a previous use, but
 109      * currently not being consumed or in the queue.
 110      * Or it may be an object that reuses a buffer from a previously
 111      * used (but not active) {@code Pixels} object.
 112      * Or it may be a brand new object.
 113      *
 114      * @param w the width of the desired Pixels object
 115      * @param h the height of the desired Pixels object
 116      * @param scale the scale of the desired Pixels object

 117      * @return an unused {@code Pixels} object
 118      */
 119     public synchronized Pixels getUnusedPixels(int w, int h, float scale) {
 120         int i = 0;
 121         IntBuffer reuseBuffer = null;
 122         while (i < saved.size()) {
 123             WeakReference<Pixels> ref = saved.get(i);
 124             Pixels p = ref.get();
 125             if (p == null) {
 126                 saved.remove(i);
 127                 continue;
 128             }
 129             if (usesSameBuffer(p, beingConsumed) || usesSameBuffer(p, enqueued)) {
 130                 i++;
 131                 continue;
 132             }
 133             if (p.getWidthUnsafe() == w &&
 134                 p.getHeightUnsafe() == h &&
 135                 p.getScaleUnsafe() == scale)

 136             {
 137                 return p;
 138             }
 139             // Whether or not we reuse its buffer, this Pixels object is going away.
 140             saved.remove(i);
 141             reuseBuffer = (IntBuffer) p.getPixels();
 142             if (reuseBuffer.capacity() >= w * h) {
 143                 break;
 144             }
 145             reuseBuffer = null;
 146             // Loop around and see if there are any other buffers to reuse,
 147             // or get rid of all of the buffers that are too small before
 148             // we proceed on to the allocation code.
 149         }
 150         if (reuseBuffer == null) {
 151             int bufsize = w * h;
 152             if (useDirectBuffers) {
 153                 reuseBuffer = BufferUtil.newIntBuffer(bufsize);
 154             } else {
 155                 reuseBuffer = IntBuffer.allocate(bufsize);
 156             }
 157         }
 158         Pixels p = Application.GetApplication().createPixels(w, h, reuseBuffer, scale);
 159         saved.add(new WeakReference<>(p));
 160         return p;
 161     }
 162 
 163     /**
 164      * Place the indicated {@code Pixels} object into the enqueued state,
 165      * replacing any other objects that are currently enqueued but not yet
 166      * being used by the consumer.
 167      *
 168      * @param pixels the {@code Pixels} object to be enqueued
 169      */
 170     public synchronized void enqueuePixels(Pixels pixels) {
 171         enqueued = pixels;
 172     }
 173 }


  96         }
  97         enqueued = null;
  98     }
  99 
 100     private boolean usesSameBuffer(Pixels p1, Pixels p2) {
 101         if (p1 == p2) return true;
 102         if (p1 == null || p2 == null) return false;
 103         return (p1.getPixels() == p2.getPixels());
 104     }
 105 
 106     /**
 107      * Return an unused Pixels with the indicated dimensions and scale.
 108      * The returned object may either be saved from a previous use, but
 109      * currently not being consumed or in the queue.
 110      * Or it may be an object that reuses a buffer from a previously
 111      * used (but not active) {@code Pixels} object.
 112      * Or it may be a brand new object.
 113      *
 114      * @param w the width of the desired Pixels object
 115      * @param h the height of the desired Pixels object
 116      * @param scalex the horizontal scale of the desired Pixels object
 117      * @param scaley the vertical scale of the desired Pixels object
 118      * @return an unused {@code Pixels} object
 119      */
 120     public synchronized Pixels getUnusedPixels(int w, int h, float scalex, float scaley) {
 121         int i = 0;
 122         IntBuffer reuseBuffer = null;
 123         while (i < saved.size()) {
 124             WeakReference<Pixels> ref = saved.get(i);
 125             Pixels p = ref.get();
 126             if (p == null) {
 127                 saved.remove(i);
 128                 continue;
 129             }
 130             if (usesSameBuffer(p, beingConsumed) || usesSameBuffer(p, enqueued)) {
 131                 i++;
 132                 continue;
 133             }
 134             if (p.getWidthUnsafe() == w &&
 135                 p.getHeightUnsafe() == h &&
 136                 p.getScaleXUnsafe() == scalex &&
 137                 p.getScaleYUnsafe() == scaley)
 138             {
 139                 return p;
 140             }
 141             // Whether or not we reuse its buffer, this Pixels object is going away.
 142             saved.remove(i);
 143             reuseBuffer = (IntBuffer) p.getPixels();
 144             if (reuseBuffer.capacity() >= w * h) {
 145                 break;
 146             }
 147             reuseBuffer = null;
 148             // Loop around and see if there are any other buffers to reuse,
 149             // or get rid of all of the buffers that are too small before
 150             // we proceed on to the allocation code.
 151         }
 152         if (reuseBuffer == null) {
 153             int bufsize = w * h;
 154             if (useDirectBuffers) {
 155                 reuseBuffer = BufferUtil.newIntBuffer(bufsize);
 156             } else {
 157                 reuseBuffer = IntBuffer.allocate(bufsize);
 158             }
 159         }
 160         Pixels p = Application.GetApplication().createPixels(w, h, reuseBuffer, scalex, scaley);
 161         saved.add(new WeakReference<>(p));
 162         return p;
 163     }
 164 
 165     /**
 166      * Place the indicated {@code Pixels} object into the enqueued state,
 167      * replacing any other objects that are currently enqueued but not yet
 168      * being used by the consumer.
 169      *
 170      * @param pixels the {@code Pixels} object to be enqueued
 171      */
 172     public synchronized void enqueuePixels(Pixels pixels) {
 173         enqueued = pixels;
 174     }
 175 }