@papyrus-sdk/engine-native 0.2.15 → 0.2.16-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,6 +35,7 @@ final class PapyrusEngineStore {
35
35
  }
36
36
 
37
37
  static EngineState getEngine(String engineId) {
38
+ if (engineId == null) return null;
38
39
  return ENGINES.get(engineId);
39
40
  }
40
41
 
@@ -6,7 +6,7 @@ import com.facebook.react.bridge.ReactApplicationContext;
6
6
  import com.facebook.react.uimanager.ViewManager;
7
7
 
8
8
  import java.util.ArrayList;
9
- import java.util.Collections;
9
+ import java.util.Arrays;
10
10
  import java.util.List;
11
11
 
12
12
  public class PapyrusPackage implements ReactPackage {
@@ -19,6 +19,9 @@ public class PapyrusPackage implements ReactPackage {
19
19
 
20
20
  @Override
21
21
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
22
- return Collections.singletonList(new PapyrusPageViewManager());
22
+ return Arrays.<ViewManager>asList(
23
+ new PapyrusPageViewManager(),
24
+ new PapyrusPdfViewerViewManager()
25
+ );
23
26
  }
24
27
  }
@@ -8,6 +8,7 @@ import android.graphics.ColorMatrixColorFilter;
8
8
  import android.graphics.Paint;
9
9
  import android.graphics.Rect;
10
10
  import android.util.AttributeSet;
11
+ import android.util.LruCache;
11
12
  import android.util.Log;
12
13
  import android.view.View;
13
14
 
@@ -17,10 +18,24 @@ import java.util.concurrent.ExecutorService;
17
18
  import java.util.concurrent.Executors;
18
19
 
19
20
  public class PapyrusPageView extends View {
20
- static final long MAX_RENDER_PIXELS = 8L * 1024L * 1024L;
21
- static final int MAX_RENDER_EDGE = 4096;
21
+ static final long MAX_RENDER_PIXELS = PapyrusRenderMath.MAX_RENDER_PIXELS;
22
+ static final int MAX_RENDER_EDGE = PapyrusRenderMath.MAX_RENDER_EDGE;
23
+ static final int RENDER_CACHE_BYTES = 32 * 1024 * 1024;
22
24
  private static final String TAG = "PapyrusPageView";
23
25
  private static final ExecutorService RENDER_EXECUTOR = Executors.newSingleThreadExecutor();
26
+ private static final LruCache<String, Bitmap> RENDER_CACHE = new LruCache<String, Bitmap>(RENDER_CACHE_BYTES) {
27
+ @Override
28
+ protected int sizeOf(String key, Bitmap value) {
29
+ return value == null ? 0 : value.getByteCount();
30
+ }
31
+
32
+ @Override
33
+ protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
34
+ if (oldValue != null && oldValue != newValue && !oldValue.isRecycled()) {
35
+ oldValue.recycle();
36
+ }
37
+ }
38
+ };
24
39
  private static final ColorMatrixColorFilter SEPIA_FILTER = createSepiaFilter();
25
40
  private static final ColorMatrixColorFilter DARK_FILTER = createDarkFilter();
26
41
  private static final ColorMatrixColorFilter HIGH_CONTRAST_FILTER = createHighContrastFilter();
@@ -28,6 +43,8 @@ public class PapyrusPageView extends View {
28
43
  private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
29
44
  private Bitmap bitmap;
30
45
  private String pageTheme = "normal";
46
+ private int renderGeneration = 0;
47
+ private String currentRenderKey = null;
31
48
 
32
49
  public PapyrusPageView(Context context) {
33
50
  super(context);
@@ -60,20 +77,37 @@ public class PapyrusPageView extends View {
60
77
  final int viewHeight = getHeight();
61
78
  final float clampedZoom = Math.max(0.1f, Math.min(5.0f, zoom));
62
79
  final float targetScale = Math.max(0.1f, scale) * clampedZoom;
63
- final int[] renderSize = constrainRenderSize(
80
+ final int[] renderSize = PapyrusRenderMath.constrainRenderSize(
64
81
  Math.max(1, (int) (viewWidth * targetScale)),
65
82
  Math.max(1, (int) (viewHeight * targetScale))
66
83
  );
67
84
  final int renderWidth = renderSize[0];
68
85
  final int renderHeight = renderSize[1];
86
+ final String renderKey = buildRenderKey(state, pageIndex, renderWidth, renderHeight, targetScale, rotation);
87
+ final int renderToken = ++renderGeneration;
88
+
89
+ if (renderKey.equals(currentRenderKey) && bitmap != null && !bitmap.isRecycled()) {
90
+ invalidate();
91
+ return;
92
+ }
93
+
94
+ Bitmap cached = RENDER_CACHE.get(renderKey);
95
+ if (cached != null && !cached.isRecycled()) {
96
+ currentRenderKey = renderKey;
97
+ bitmap = cached;
98
+ invalidate();
99
+ return;
100
+ }
69
101
 
70
102
  RENDER_EXECUTOR.execute(() -> {
71
103
  PdfDocument doc = state.document;
72
104
  if (doc == null) return;
105
+ if (renderToken != renderGeneration) return;
73
106
 
74
107
  try {
75
108
  Bitmap rendered = null;
76
109
  synchronized (state.pdfiumLock) {
110
+ if (renderToken != renderGeneration) return;
77
111
  if (doc != state.document) return;
78
112
  int pageCount = state.pdfium.getPageCount(doc);
79
113
  if (pageIndex < 0 || pageIndex >= pageCount) return;
@@ -86,9 +120,14 @@ public class PapyrusPageView extends View {
86
120
  final Bitmap renderedBitmap = rendered;
87
121
 
88
122
  post(() -> {
89
- if (bitmap != null && !bitmap.isRecycled()) {
90
- bitmap.recycle();
123
+ if (renderToken != renderGeneration) {
124
+ if (!renderedBitmap.isRecycled()) {
125
+ renderedBitmap.recycle();
126
+ }
127
+ return;
91
128
  }
129
+ RENDER_CACHE.put(renderKey, renderedBitmap);
130
+ currentRenderKey = renderKey;
92
131
  bitmap = renderedBitmap;
93
132
  invalidate();
94
133
  });
@@ -116,29 +155,18 @@ public class PapyrusPageView extends View {
116
155
  }
117
156
 
118
157
  static int[] constrainRenderSize(int requestedWidth, int requestedHeight) {
119
- int width = Math.max(1, requestedWidth);
120
- int height = Math.max(1, requestedHeight);
121
- double scale = 1.0d;
122
-
123
- if (width > MAX_RENDER_EDGE || height > MAX_RENDER_EDGE) {
124
- scale = Math.min(
125
- scale,
126
- Math.min((double) MAX_RENDER_EDGE / width, (double) MAX_RENDER_EDGE / height)
127
- );
128
- }
129
-
130
- long pixels = (long) width * (long) height;
131
- if (pixels > MAX_RENDER_PIXELS) {
132
- scale = Math.min(scale, Math.sqrt((double) MAX_RENDER_PIXELS / (double) pixels));
133
- }
134
-
135
- if (scale >= 1.0d) {
136
- return new int[] { width, height };
137
- }
158
+ return PapyrusRenderMath.constrainRenderSize(requestedWidth, requestedHeight);
159
+ }
138
160
 
139
- int safeWidth = Math.max(1, (int) Math.floor(width * scale));
140
- int safeHeight = Math.max(1, (int) Math.floor(height * scale));
141
- return new int[] { safeWidth, safeHeight };
161
+ static String buildRenderKey(PapyrusEngineStore.EngineState state,
162
+ int pageIndex,
163
+ int renderWidth,
164
+ int renderHeight,
165
+ float targetScale,
166
+ int rotation) {
167
+ String source = state == null || state.sourcePath == null ? "" : state.sourcePath;
168
+ int documentIdentity = state == null || state.document == null ? 0 : System.identityHashCode(state.document);
169
+ return PapyrusRenderMath.buildRenderKey(source, documentIdentity, pageIndex, renderWidth, renderHeight, targetScale, rotation);
142
170
  }
143
171
 
144
172
  private static ColorMatrixColorFilter resolveThemeFilter(String theme) {