Skip to content

Commit

Permalink
Change the initial size of A8 font atlas to 2048x2048
Browse files Browse the repository at this point in the history
  • Loading branch information
BloCamLimb committed Apr 26, 2024
1 parent c225f79 commit 40b2c7c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/icyllis/modernui/ModernUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public void run(@NonNull Fragment fragment) {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_DEPTH_BITS, 0);
glfwWindowHint(GLFW_STENCIL_BITS, 0);
glfwWindowHintString(GLFW_X11_CLASS_NAME, NAME_CPT);
glfwWindowHintString(GLFW_X11_INSTANCE_NAME, NAME_CPT);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/icyllis/modernui/TestFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ private static class DView extends View {

public DView(Context context, TimeInterpolator interpolator) {
super(context);
mTextPaint.setTextSize(10);
mTextPaint.setTextSize(13);
/*animation = new Animation(200)
.applyTo(new Applier(0, 60, () -> offsetY, v -> {
offsetY = v;
Expand All @@ -1002,7 +1002,7 @@ protected void onDraw(@Nonnull Canvas canvas) {
Paint paint = Paint.obtain();
paint.setARGB(128, 140, 200, 240);
canvas.drawRoundRect(0, 1, getWidth(), getHeight() - 2, 4, paint);
TextUtils.drawTextRun(canvas, "18:52", 0, 5, 0, 5, getWidth() / 2f, offsetY + 24, false, mTextPaint);
TextUtils.drawTextRun(canvas, "18:52 modernui", 0, 14, 0, 14, getWidth() / 2f - 20f, offsetY + 24, false, mTextPaint);
paint.recycle();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ public GLSurfaceCanvas(GLDevice device) {
mRoundRectUBO.allocate(ROUND_RECT_UNIFORM_SIZE);

mLinearSampler = Objects.requireNonNull(
device.getResourceProvider().findOrCreateCompatibleSampler(SamplerState.DEFAULT),
device.getResourceProvider().findOrCreateCompatibleSampler(
SamplerState.make(SamplerState.FILTER_LINEAR, SamplerState.MIPMAP_MODE_LINEAR)),
"Failed to create font sampler");

{
Expand Down
36 changes: 32 additions & 4 deletions core/src/main/java/icyllis/modernui/graphics/font/GLFontAtlas.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
import icyllis.modernui.annotation.*;
import icyllis.modernui.core.Core;
import icyllis.modernui.graphics.Bitmap;
import icyllis.modernui.text.TextUtils;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import org.lwjgl.system.MemoryStack;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -188,8 +189,10 @@ public boolean stitch(@NonNull BakedGlyph glyph, long pixels) {

private boolean resize() {
if (mTexture == null) {
// initialize 4 chunks
mWidth = mHeight = CHUNK_SIZE * 2;
// initialize 4 or 16 chunks
mWidth = mHeight = mMaskFormat == Engine.MASK_FORMAT_A8
? CHUNK_SIZE * 4
: CHUNK_SIZE * 2;
mTexture = createTexture();
for (int x = 0; x < mWidth; x += CHUNK_SIZE) {
for (int y = 0; y < mHeight; y += CHUNK_SIZE) {
Expand Down Expand Up @@ -281,7 +284,6 @@ private boolean resize() {

if (mMaskFormat == Engine.MASK_FORMAT_A8) {
//XXX: un-premultiplied, so 111r rather than rrrr
// in case of some driver bugs, we don't use GL_TEXTURE_SWIZZLE_RGBA
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_ONE);
Expand Down Expand Up @@ -434,6 +436,32 @@ public long getMemorySize() {
return mTexture != null ? mTexture.getMemorySize() : 0;
}

public void dumpInfo(PrintWriter pw, String name) {
int validGlyphs = 0;
int emptyGlyphs = 0;
int evictedGlyphs = 0;
for (var glyph : mGlyphs.values()) {
if (glyph == null) {
emptyGlyphs++;
} else if (glyph.x == Short.MIN_VALUE) {
evictedGlyphs++;
} else {
validGlyphs++;
}
}
pw.print(name);
pw.printf(": NumGlyphs=%d (in-use: %d, empty: %d, evicted: %d)",
getGlyphCount(), validGlyphs, emptyGlyphs, evictedGlyphs);
pw.print(", Coverage=");
pw.printf("%.4f", getCoverage());
pw.print(", GPUMemorySize=");
long memorySize = getMemorySize();
TextUtils.binaryCompact(pw, memorySize);
pw.print(" (");
pw.print(memorySize);
pw.println(" bytes)");
}

/**
* @return 0..1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import icyllis.modernui.graphics.BitmapFactory;
import icyllis.modernui.graphics.text.Font;
import icyllis.modernui.graphics.text.*;
import icyllis.modernui.text.TextUtils;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
Expand Down Expand Up @@ -354,23 +353,11 @@ private static void debug(GLFontAtlas atlas, String name) {
}

public void dumpInfo(PrintWriter pw) {
dumpInfo(pw, mFontAtlas, "FontAtlas");
dumpInfo(pw, mEmojiAtlas, "EmojiAtlas");
}

private static void dumpInfo(PrintWriter pw, GLFontAtlas atlas, String name) {
if (atlas != null) {
pw.print(name);
pw.print(": Glyphs=");
pw.print(atlas.getGlyphCount());
pw.print(", Coverage=");
pw.printf("%.4f", atlas.getCoverage());
pw.print(", GPUMemorySize=");
long memorySize = atlas.getMemorySize();
TextUtils.binaryCompact(pw, memorySize);
pw.print(" (");
pw.print(memorySize);
pw.println(" bytes)");
if (mFontAtlas != null) {
mFontAtlas.dumpInfo(pw, "FontAtlas");
}
if (mEmojiAtlas != null) {
mEmojiAtlas.dumpInfo(pw, "EmojiAtlas");
}
}

Expand Down

0 comments on commit 40b2c7c

Please sign in to comment.