a758d479d34b58bcee5c34099574d850f9c71a60
[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
28 /**
29  * @author Pierre Lando
30  */
31 public class ScilabTextureManager {
32
33     private final HashMap<String, Texture> hashMap = new HashMap<String, Texture>();
34     private final DrawerVisitor drawerVisitor;
35     private final Canvas canvas;
36
37
38     public ScilabTextureManager(DrawerVisitor drawerVisitor) {
39         this.drawerVisitor = drawerVisitor;
40         this.canvas = drawerVisitor.getCanvas();
41     }
42
43     /**
44      * {@link Texture} getter.
45      * @param identifier object identifier.
46      * @return the {@link Texture} used to drawn the object who have the given identifier.
47      */
48     public Texture getTexture(String identifier) {
49         Texture texture = hashMap.get(identifier);
50         if (texture == null) {
51             texture = canvas.getTextureManager().createTexture();
52             texture.setDataProvider(new ScilabTextureDataProvider(identifier));
53             texture.setMagnificationFilter(Texture.Filter.NEAREST);
54             texture.setMinifyingFilter(Texture.Filter.NEAREST);
55             hashMap.put(identifier, texture);
56         }
57         return texture;
58     }
59
60     public void dispose(String identifier) {
61         Texture texture = hashMap.get(identifier);
62         if (texture != null) {
63             hashMap.remove(identifier);
64             canvas.getTextureManager().dispose(texture);
65         }
66     }
67
68     private class ScilabTextureDataProvider extends AbstractTextureDataProvider implements GraphicView {
69         private final String identifier;
70         private Dimension dimension;
71         private boolean isValid;
72
73         public ScilabTextureDataProvider(String identifier) {
74             this.identifier = identifier;
75             this.isValid = (identifier != null);
76
77             if (isValid()) {
78                 updateData();
79             }
80             GraphicController.getController().register(this);
81         }
82
83         private void updateData() {
84             int width = MainDataLoader.getTextureWidth(identifier);
85             int height = MainDataLoader.getTextureHeight(identifier);
86             dimension = new Dimension(width, height);
87             fireUpdate();
88         }
89
90         @Override
91         public Dimension getTextureSize() {
92             if (isValid()) {
93                 return new Dimension(dimension);
94             } else {
95                 return null;
96             }
97         }
98
99         @Override
100         public ByteBuffer getData() {
101             int bufferLength = dimension.width * dimension.height * 4;
102             ByteBuffer buffer;
103             try {
104                 buffer = BufferAllocation.newByteBuffer(bufferLength);
105             } catch (OutOfMemoryException exception) {
106                 drawerVisitor.invalidate(GraphicController.getController().getObjectFromId(identifier), exception);
107                 return null;
108             }
109             MainDataLoader.fillTextureData(identifier, buffer, bufferLength);
110             buffer.rewind();
111             return buffer;
112         }
113
114         @Override
115         public ByteBuffer getSubData(int x, int y, int width, int height) {
116             int bufferLength = width * height * 4;
117             ByteBuffer buffer;
118             try {
119                 buffer = BufferAllocation.newByteBuffer(bufferLength);
120             } catch (OutOfMemoryException exception) {
121                 drawerVisitor.invalidate(GraphicController.getController().getObjectFromId(identifier), exception);
122                 return null;
123             }
124             MainDataLoader.fillTextureData(identifier, buffer, bufferLength, x, y, width, height);
125             buffer.rewind();
126             return buffer;
127         }
128
129         @Override
130         public boolean isValid() {
131             return isValid;
132         }
133
134         @Override
135         public void updateObject(String id, int property) {
136             if (isValid() && identifier.equals(id)) {
137                 // TODO check Property.
138                 updateData();
139             }
140         }
141
142         @Override
143         public void deleteObject(String id) {
144             if (isValid() && identifier.equals(id)) {
145                 isValid = false;
146                 GraphicController.getController().unregister(this);
147             }
148         }
149
150         @Override
151         public void createObject(String id) {
152         }
153     }
154 }