package net.minecraft.client.renderer.texture; import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.TextureUtil; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.logging.LogUtils; import java.io.IOException; import java.nio.file.Path; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @Environment(EnvType.CLIENT) public class DynamicTexture extends AbstractTexture implements Dumpable { private static final Logger LOGGER = LogUtils.getLogger(); @Nullable private NativeImage pixels; public DynamicTexture(NativeImage pixels) { this.pixels = pixels; if (!RenderSystem.isOnRenderThread()) { RenderSystem.recordRenderCall(() -> { TextureUtil.prepareImage(this.getId(), this.pixels.getWidth(), this.pixels.getHeight()); this.upload(); }); } else { TextureUtil.prepareImage(this.getId(), this.pixels.getWidth(), this.pixels.getHeight()); this.upload(); } } public DynamicTexture(int width, int height, boolean useCalloc) { this.pixels = new NativeImage(width, height, useCalloc); TextureUtil.prepareImage(this.getId(), this.pixels.getWidth(), this.pixels.getHeight()); } public void upload() { if (this.pixels != null) { this.bind(); this.pixels.upload(0, 0, 0, false); } else { LOGGER.warn("Trying to upload disposed texture {}", this.getId()); } } @Nullable public NativeImage getPixels() { return this.pixels; } public void setPixels(NativeImage pixels) { if (this.pixels != null) { this.pixels.close(); } this.pixels = pixels; } @Override public void close() { if (this.pixels != null) { this.pixels.close(); this.releaseId(); this.pixels = null; } } @Override public void dumpContents(ResourceLocation resourceLocation, Path path) throws IOException { if (this.pixels != null) { String string = resourceLocation.toDebugFileName() + ".png"; Path path2 = path.resolve(string); this.pixels.writeToFile(path2); } } }