2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2012 - DIGITEO - Pierre Lando
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
12 package org.scilab.modules.renderer.JoGLView;
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;
24 import java.awt.Dimension;
25 import java.nio.ByteBuffer;
26 import java.util.HashMap;
30 * @author Pierre Lando
32 public class ScilabTextureManager {
34 private final Map<String, Texture> hashMap = new HashMap<String, Texture>();
35 private final DrawerVisitor drawerVisitor;
36 private final Canvas canvas;
39 public ScilabTextureManager(DrawerVisitor drawerVisitor) {
40 this.drawerVisitor = drawerVisitor;
41 this.canvas = drawerVisitor.getCanvas();
45 * {@link Texture} getter.
46 * @param identifier object identifier.
47 * @return the {@link Texture} used to drawn the object who have the given identifier.
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);
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);
69 public void disposeAll() {
70 for (Map.Entry<String, Texture> entry : hashMap.entrySet()) {
71 Texture t = entry.getValue();
73 canvas.getTextureManager().dispose(t);
80 private class ScilabTextureDataProvider extends AbstractTextureDataProvider implements GraphicView {
81 private final String identifier;
82 private Dimension dimension;
83 private boolean isValid;
85 public ScilabTextureDataProvider(String identifier) {
86 this.identifier = identifier;
87 this.isValid = (identifier != null);
92 GraphicController.getController().register(this);
95 private void updateData() {
96 int width = MainDataLoader.getTextureWidth(identifier);
97 int height = MainDataLoader.getTextureHeight(identifier);
98 dimension = new Dimension(width, height);
103 public Dimension getTextureSize() {
105 return new Dimension(dimension);
112 public ByteBuffer getData() {
113 int bufferLength = dimension.width * dimension.height * 4;
116 buffer = BufferAllocation.newByteBuffer(bufferLength);
117 } catch (OutOfMemoryException exception) {
118 drawerVisitor.invalidate(GraphicController.getController().getObjectFromId(identifier), exception);
121 MainDataLoader.fillTextureData(identifier, buffer, bufferLength);
127 public ByteBuffer getSubData(int x, int y, int width, int height) {
128 int bufferLength = width * height * 4;
131 buffer = BufferAllocation.newByteBuffer(bufferLength);
132 } catch (OutOfMemoryException exception) {
133 drawerVisitor.invalidate(GraphicController.getController().getObjectFromId(identifier), exception);
136 MainDataLoader.fillTextureData(identifier, buffer, bufferLength, x, y, width, height);
142 public boolean isValid() {
147 public void updateObject(String id, int property) {
148 if (isValid() && identifier.equals(id)) {
149 // TODO check Property.
155 public void deleteObject(String id) {
156 if (isValid() && identifier.equals(id)) {
158 GraphicController.getController().unregister(this);
163 public void createObject(String id) {