Skip to content

Commit 248e89c

Browse files
committed
Fix hardware fences
1 parent 47d0a57 commit 248e89c

File tree

2 files changed

+90
-35
lines changed

2 files changed

+90
-35
lines changed

app/src/main/java/com/radzivon/bartoshyk/avif/MainActivity.kt

+86-32
Original file line numberDiff line numberDiff line change
@@ -45,48 +45,102 @@ class MainActivity : AppCompatActivity() {
4545
// assert(HeifCoder().isAvif(buffer))
4646
val size = HeifCoder().getSize(buffer)!!
4747
assert(size != null)
48+
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
49+
// val time = measureTimeMillis {
50+
//// val bitmap = HeifCoder().decodeSampled(
51+
//// buffer,
52+
//// size.width / 2,
53+
//// size.height / 2,
54+
//// PreferredColorConfig.HARDWARE
55+
//// )
4856
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
49-
val time = measureTimeMillis {
50-
// val bitmap = HeifCoder().decodeSampled(
51-
// buffer,
52-
// size.width / 2,
53-
// size.height / 2,
54-
// PreferredColorConfig.HARDWARE
55-
// )
56-
val bitmap = HeifCoder().decodeSampled(
57-
buffer,
58-
350,
59-
600,
60-
PreferredColorConfig.HARDWARE,
61-
ScaleMode.FIT
62-
)
63-
// val opts = BitmapFactory.Options()
64-
// opts.inMutable = true
65-
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
66-
// opts.inPreferredConfig = Bitmap.Config.RGBA_F16
67-
// }
68-
binding.imageView.setImageBitmap(bitmap)
69-
}
70-
Log.i("MainActivity AVIF ", "Done in ${time}")
71-
// val encoded = HeifCoder().encodeAvif(bitmap)
72-
// val decodedSample = HeifCoder().decode(encoded)
73-
// binding.imageView.setImageBitmap(bitmap)
57+
val bitmap = HeifCoder().decodeSampled(
58+
buffer,
59+
350,
60+
600,
61+
PreferredColorConfig.HARDWARE,
62+
ScaleMode.FIT
63+
)
64+
bitmap.recycle()
65+
}
66+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
67+
val bitmap = HeifCoder().decodeSampled(
68+
buffer,
69+
350,
70+
600,
71+
PreferredColorConfig.HARDWARE,
72+
ScaleMode.FIT
73+
)
74+
bitmap.recycle()
75+
}
76+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
77+
val bitmap = HeifCoder().decodeSampled(
78+
buffer,
79+
350,
80+
600,
81+
PreferredColorConfig.HARDWARE,
82+
ScaleMode.FIT
83+
)
84+
bitmap.recycle()
85+
}
86+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
87+
val bitmap = HeifCoder().decodeSampled(
88+
buffer,
89+
350,
90+
600,
91+
PreferredColorConfig.HARDWARE,
92+
ScaleMode.FIT
93+
)
94+
bitmap.recycle()
95+
}
96+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
97+
val bitmap = HeifCoder().decodeSampled(
98+
buffer,
99+
350,
100+
600,
101+
PreferredColorConfig.HARDWARE,
102+
ScaleMode.FIT
103+
)
104+
bitmap.recycle()
74105
}
106+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
107+
val bitmap = HeifCoder().decodeSampled(
108+
buffer,
109+
350,
110+
600,
111+
PreferredColorConfig.HARDWARE,
112+
ScaleMode.FIT
113+
)
114+
bitmap.recycle()
115+
}
116+
//// val opts = BitmapFactory.Options()
117+
//// opts.inMutable = true
118+
//// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
119+
//// opts.inPreferredConfig = Bitmap.Config.RGBA_F16
120+
//// }
121+
// binding.imageView.setImageBitmap(bitmap)
122+
// }
123+
// Log.i("MainActivity AVIF ", "Done in ${time}")
124+
//// val encoded = HeifCoder().encodeAvif(bitmap)
125+
//// val decodedSample = HeifCoder().decode(encoded)
126+
//// binding.imageView.setImageBitmap(bitmap)
127+
// }
75128

76129
//https://wh.aimuse.online/creatives/IMUSE_03617fe2db82a584166_27/TT_a9d21ff1061d785347935fef/68f06252.avif
77130
//https://wh.aimuse.online/preset/federico-beccari.avif
131+
// https://wh.aimuse.online/preset/avif10bit.avif
78132

79133
// Glide.with(this)
80-
// .load("https://wh.aimuse.online/preset/avif10bit.avif")
134+
// .load("https://wh.aimuse.online/preset/federico-beccari.avif")
81135
// .skipMemoryCache(true)
82136
// .into(binding.imageView)
83137

84-
// binding.imageView.load("https://wh.aimuse.online/preset/avif10bit.avif",
85-
// imageLoader = ImageLoader.Builder(this)
86-
// .components {
87-
// add(HeifDecoder.Factory())
88-
// }
89-
// .build())
138+
binding.imageView.load("https://wh.aimuse.online/creatives/IMUSE_03617fe2db82a584166_27/TT_a9d21ff1061d785347935fef/68f06252.avif",
139+
imageLoader = ImageLoader.Builder(this)
140+
.components {
141+
add(HeifDecoder.Factory())
142+
}
143+
.build())
90144

91145
// binding.imageView.setImageBitmap(bitmap)
92146
// binding.imageView.setImageBitmap(cc16)

avif-coder/src/main/cpp/ReformatBitmap.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "Support.h"
1313
#include <android/bitmap.h>
1414
#include <HardwareBuffersCompat.h>
15+
#include <mutex>
1516

1617
void
1718
ReformatColorConfig(JNIEnv *env, std::shared_ptr<uint8_t> &imageData, std::string &imageConfig,
@@ -133,9 +134,9 @@ ReformatColorConfig(JNIEnv *env, std::shared_ptr<uint8_t> &imageData, std::strin
133134
}
134135
ARect rect = {0, 0, imageWidth, imageHeight};
135136
uint8_t *buffer;
136-
int fence = 1;
137+
137138
status = AHardwareBuffer_lock_compat(hardwareBuffer,
138-
AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, fence,
139+
AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1,
139140
&rect, reinterpret_cast<void **>(&buffer));
140141
if (status != 0) {
141142
AHardwareBuffer_release_compat(hardwareBuffer);
@@ -151,7 +152,7 @@ ReformatColorConfig(JNIEnv *env, std::shared_ptr<uint8_t> &imageData, std::strin
151152
(int)bufferDesc.height,
152153
(*useFloats) ? sizeof(uint16_t) : sizeof(uint8_t));
153154

154-
status = AHardwareBuffer_unlock_compat(hardwareBuffer, &fence);
155+
status = AHardwareBuffer_unlock_compat(hardwareBuffer, nullptr);
155156
if (status != 0) {
156157
AHardwareBuffer_release_compat(hardwareBuffer);
157158
return;

0 commit comments

Comments
 (0)