| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.pipeline.RenderTarget;
|
| 4 | import com.mojang.blaze3d.platform.NativeImage;
|
| 5 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 8 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 9 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 10 | import java.io.File;
|
| 11 | import java.util.function.Consumer;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.ChatFormatting;
|
| 15 | import net.minecraft.SharedConstants;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.network.chat.ClickEvent.OpenFile;
|
| 18 | import net.minecraft.util.ARGB;
|
| 19 | import net.minecraft.util.Util;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class Screenshot {
|
| 25 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 26 | public static final String SCREENSHOT_DIR = "screenshots";
|
| 27 |
|
| 28 | public static void grab(final File workDir, final RenderTarget target, final Consumer<Component> callback) {
|
| 29 | grab(workDir, null, target, 1, callback);
|
| 30 | }
|
| 31 |
|
| 32 | public static void grab(final Minecraft minecraft, final boolean debugPanoramaRequested) {
|
| 33 | if (debugPanoramaRequested && SharedConstants.DEBUG_PANORAMA_SCREENSHOT) {
|
| 34 | minecraft.showDebugChat(minecraft.grabPanoramixScreenshot(minecraft.gameDirectory));
|
| 35 | } else {
|
| 36 | grab(minecraft.gameDirectory, minecraft.gameRenderer.mainRenderTarget(), message -> minecraft.execute(() -> minecraft.showDebugChat(message)));
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | public static void grab(
|
| 41 | final File workDir, final @Nullable String forceName, final RenderTarget target, final int downscaleFactor, final Consumer<Component> callback
|
| 42 | ) {
|
| 43 | takeScreenshot(
|
| 44 | target,
|
| 45 | downscaleFactor,
|
| 46 | image -> {
|
| 47 | File picDir = new File(workDir, "screenshots");
|
| 48 | picDir.mkdir();
|
| 49 | File file;
|
| 50 | if (forceName == null) {
|
| 51 | file = getFile(picDir);
|
| 52 | } else {
|
| 53 | file = new File(picDir, forceName);
|
| 54 | }
|
| 55 |
|
| 56 | Util.ioPool()
|
| 57 | .execute(
|
| 58 | () -> {
|
| 59 | try (image) {
|
| 60 | image.writeToFile(file);
|
| 61 | Component component = Component.literal(file.getName())
|
| 62 | .withStyle(ChatFormatting.UNDERLINE)
|
| 63 | .withStyle(s -> s.withClickEvent(new OpenFile(file.getAbsoluteFile())));
|
| 64 | callback.accept(Component.translatable("screenshot.success", component));
|
| 65 | } catch (Exception e) {
|
| 66 | LOGGER.warn("Couldn't save screenshot", e);
|
| 67 | callback.accept(Component.translatable("screenshot.failure", e.getMessage()));
|
| 68 | }
|
| 69 | }
|
| 70 | );
|
| 71 | }
|
| 72 | );
|
| 73 | }
|
| 74 |
|
| 75 | public static void takeScreenshot(final RenderTarget target, final Consumer<NativeImage> callback) {
|
| 76 | takeScreenshot(target, 1, callback);
|
| 77 | }
|
| 78 |
|
| 79 | public static void takeScreenshot(final RenderTarget target, final int downscaleFactor, final Consumer<NativeImage> callback) {
|
| 80 | int width = target.width;
|
| 81 | int height = target.height;
|
| 82 | GpuTexture sourceTexture = target.getColorTexture();
|
| 83 | if (sourceTexture == null) {
|
| 84 | throw new IllegalStateException("Tried to capture screenshot of an incomplete framebuffer");
|
| 85 | }
|
| 86 |
|
| 87 | if (width % downscaleFactor == 0 && height % downscaleFactor == 0) {
|
| 88 | GpuBuffer buffer = RenderSystem.getDevice().createBuffer(() -> "Screenshot buffer", 9, (long)width * height * sourceTexture.getFormat().blockSize());
|
| 89 | RenderSystem.getDevice().createCommandEncoder().copyTextureToBuffer(sourceTexture, buffer, 0L, () -> {
|
| 90 | try (GpuBufferSlice.MappedView read = buffer.map(true, false)) {
|
| 91 | int outputHeight = height / downscaleFactor;
|
| 92 | int outputWidth = width / downscaleFactor;
|
| 93 | NativeImage image = new NativeImage(outputWidth, outputHeight, false);
|
| 94 |
|
| 95 | for (int y = 0; y < outputHeight; y++) {
|
| 96 | for (int x = 0; x < outputWidth; x++) {
|
| 97 | if (downscaleFactor == 1) {
|
| 98 | int argb = read.data().getInt((x + y * width) * sourceTexture.getFormat().blockSize());
|
| 99 | image.setPixelABGR(x, height - y - 1, argb | 0xFF000000);
|
| 100 | } else {
|
| 101 | int red = 0;
|
| 102 | int green = 0;
|
| 103 | int blue = 0;
|
| 104 |
|
| 105 | for (int i = 0; i < downscaleFactor; i++) {
|
| 106 | for (int j = 0; j < downscaleFactor; j++) {
|
| 107 | int argb = read.data().getInt((x * downscaleFactor + i + (y * downscaleFactor + j) * width) * sourceTexture.getFormat().blockSize());
|
| 108 | red += ARGB.red(argb);
|
| 109 | green += ARGB.green(argb);
|
| 110 | blue += ARGB.blue(argb);
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | int sampleCount = downscaleFactor * downscaleFactor;
|
| 115 | image.setPixelABGR(x, outputHeight - y - 1, ARGB.color(255, red / sampleCount, green / sampleCount, blue / sampleCount));
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | callback.accept(image);
|
| 121 | }
|
| 122 |
|
| 123 | buffer.close();
|
| 124 | }, 0);
|
| 125 | } else {
|
| 126 | throw new IllegalArgumentException("Image size is not divisible by downscale factor");
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | private static File getFile(final File picDir) {
|
| 131 | String name = Util.getFilenameFormattedDateTime();
|
| 132 | int count = 1;
|
| 133 |
|
| 134 | while (true) {
|
| 135 | File file = new File(picDir, name + (count == 1 ? "" : "_" + count) + ".png");
|
| 136 | if (!file.exists()) {
|
| 137 | return file;
|
| 138 | }
|
| 139 |
|
| 140 | count++;
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 |
|