20a21d525e25e2b8bcf0c00b0dcc46c000ae147f
[scilab.git] / scilab / modules / renderer / src / java / org / scilab / modules / renderer / JoGLView / ScilabTextureManager.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009-2012 - DIGITEO - Pierre Lando
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  */
11
12 package org.scilab.modules.renderer.JoGLView;
13
14 import org.scilab.forge.scirenderer.Canvas;
15 import org.scilab.forge.scirenderer.data.AbstractDataProvider;
16 import org.scilab.forge.scirenderer.texture.AbstractTextureDataProvider;
17 import org.scilab.forge.scirenderer.texture.Texture;
18 import org.scilab.modules.graphic_objects.MainDataLoader;
19 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
20 import org.scilab.modules.graphic_objects.graphicView.GraphicView;
21 import org.scilab.modules.renderer.JoGLView.util.BufferAllocation;
22 import org.scilab.modules.renderer.JoGLView.util.OutOfMemoryException;
23
24 import java.awt.Dimension;
25 import java.nio.ByteBuffer;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 /**
30  * @author Pierre Lando
31  */
32 public class ScilabTextureManager {
33
34     private final Map<String, Texture> hashMap = new HashMap<String, Texture>();
35     private final DrawerVisitor drawerVisitor;
36     private final Canvas canvas;
37
38
39     public ScilabTextureManager(DrawerVisitor drawerVisitor) {
40         this.drawerVisitor = drawerVisitor;
41         this.canvas = drawerVisitor.getCanvas();
42     }
43
44     /**
45      * {@link Texture} getter.
46      * @param identifier object identifier.
47      * @return the {@link Texture} used to drawn the object who have the given identifier.
48      */
49     public Texture getTexture(String identifier) {
50         Texture texture = hashMap.get(identifier);
51         if (texture == null) {
52             texture = canvas.getTextureManager().createTexture();
53             texture.setDataProvider(new ScilabTextureDataProvider(identifier));
54             texture.setMagnificationFilter(Texture.Filter.NEAREST);
55             texture.setMinifyingFilter(Texture.Filter.NEAREST);
56             hashMap.put(identifier, texture);
57         }
58         return texture;
59     }
60
61     public void dispose(String identifier) {
62         Texture texture = hashMap.get(identifier);
63         if (texture != null) {
64             hashMap.remove(identifier);
65             canvas.getTextureManager().dispose(texture);
66         }
67     }
68
69     public void disposeAll() {
70         for (Map.Entry<String, Texture> entry : hashMap.entrySet()) {
71             Texture t = entry.getValue();
72             if (t != null) {
73                 canvas.getTextureManager().dispose(t);
74             }
75         }
76
77         hashMap.clear();
78     }
79
80     private class ScilabTextureDataProvider extends AbstractTextureDataProvider implements GraphicView {
81         private final String identifier;
82         private Dimension dimension;
83         private boolean isValid;
84
85         public ScilabTextureDataProvider(String identifier) {
86             this.identifier = identifier;
87             this.isValid = (identifier != null);
88
89             if (isValid()) {
90                 updateData();
91             }
92             GraphicController.getController().register(this);
93         }
94
95         private void updateData() {
96             int width = MainDataLoader.getTextureWidth(identifier);
97             int height = MainDataLoader.getTextureHeight(identifier);
98             dimension = new Dimension(width, height);
99             fireUpdate();
100         }
101
102         @Override
103         public Dimension getTextureSize() {
104             if (isValid()) {
105                 return new Dimension(dimension);
106             } else {
107                 return null;
108             }
109         }
110
111         @Override
112         public ByteBuffer getData() {
113             int bufferLength = dimension.width * dimension.height * 4;
114             ByteBuffer buffer;
115             try {
116                 buffer = BufferAllocation.newByteBuffer(bufferLength);
117             } catch (OutOfMemoryException exception) {
118                 drawerVisitor.invalidate(GraphicController.getController().getObjectFromId(identifier), exception);
119                 return null;
120             }
121             MainDataLoader.fillTextureData(identifier, buffer, bufferLength);
122             buffer.rewind();
123             return buffer;
124         }
125
126         @Override
127         public ByteBuffer getSubData(int x, int y, int width, int height) {
128             int bufferLength = width * height * 4;
129             ByteBuffer buffer;
130             try {
131                 buffer = BufferAllocation.newByteBuffer(bufferLength);
132             } catch (OutOfMemoryException exception) {
133                 drawerVisitor.invalidate(GraphicController.getController().getObjectFromId(identifier), exception);
134                 return null;
135             }
136             MainDataLoader.fillTextureData(identifier, buffer, bufferLength, x, y, width, height);
137             buffer.rewind();
138             return buffer;
139         }
140
141         @Override
142         public boolean isValid() {
143             return isValid;
144         }
145
146         @Override
147         public void updateObject(String id, int property) {
148             if (isValid() && identifier.equals(id)) {
149                 // TODO check Property.
150                 updateData();
151             }
152         }
153
154         @Override
155         public void deleteObject(String id) {
156             if (isValid() && identifier.equals(id)) {
157                 isValid = false;
158                 GraphicController.getController().unregister(this);
159             }
160         }
161
162         @Override
163         public void createObject(String id) {
164         }
165     }
166 }