Skip to content

Commit

Permalink
Change hardware buffers flags
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Nov 16, 2024
1 parent c341c51 commit 326b429
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 58 deletions.
105 changes: 63 additions & 42 deletions app/src/main/java/com/awxkee/jxlcoder/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ class MainActivity : ComponentActivity() {
var imagesArray = remember {
mutableStateListOf<Bitmap>()
}
var drawablesArray = remember {
mutableStateListOf<Drawable>()
}
LaunchedEffect(key1 = Unit, block = {
lifecycleScope.launch(Dispatchers.IO) {
val buffer5 =
Expand Down Expand Up @@ -183,59 +186,66 @@ class MainActivity : ComponentActivity() {
// lifecycleScope.launch {
// drawables.add(BitmapDrawable(resources, bitmap))
// }
val space = bitmap.copy(Bitmap.Config.ARGB_8888, true)
val encoded =
JxlCoder.encode(
space,
channelsConfiguration = JxlChannelsConfiguration.RGB,
effort = JxlEffort.LIGHTNING,
compressionOption = JxlCompressionOption.LOSSLESS,
quality = 100,
)
val decoded = JxlCoder.decodeSampled(
encoded,
preferredColorConfig = PreferredColorConfig.RGBA_8888, width = 1305,
height = 1295
)
lifecycleScope.launch {
imagesArray.add(decoded)
}
// val space = bitmap.copy(Bitmap.Config.ARGB_8888, true)
// val encoded =
// JxlCoder.encode(
// space,
// channelsConfiguration = JxlChannelsConfiguration.RGB,
// effort = JxlEffort.LIGHTNING,
// compressionOption = JxlCompressionOption.LOSSLESS,
// quality = 100,
// )
// val decoded = JxlCoder.decodeSampled(
// encoded,
// preferredColorConfig = PreferredColorConfig.HARDWARE, width = 1305,
// height = 1295
// )
// lifecycleScope.launch {
// imagesArray.add(decoded)
// }

// var assets =
// ([email protected]("") ?: return@launch).toList()
//// assets = assets.filter { it.contains("20181110_213419__MMC1561-HDR.jxl") }
// for (asset in assets) {
// try {
// val buffer4 =
// [email protected](asset).source().buffer()
// .readByteArray()
//
// val largeImageSize = JxlCoder.getSize(buffer4)
// if (largeImageSize != null) {
// val time = measureTimeMillis {
var assets =
(this@MainActivity.assets.list("") ?: return@launch).toList()
assets = assets.filter { it.contains("20181110_213419__MMC1561-HDR.jxl") }
for (asset in assets) {
try {
val buffer4 =
this@MainActivity.assets.open(asset).source().buffer()
.readByteArray()

val largeImageSize = JxlCoder.getSize(buffer4)
if (largeImageSize != null) {
val time = measureTimeMillis {
// val srcImage = JxlCoder.decodeSampled(
// buffer4,
// largeImageSize.width / 2,
// largeImageSize.height / 2,
// preferredColorConfig = PreferredColorConfig.RGBA_8888,
// preferredColorConfig = PreferredColorConfig.HARDWARE,
// com.awxkee.jxlcoder.ScaleMode.FIT,
// toneMapper = JxlToneMapper.REC2408,
// )
//// val srcImage = JxlCoder.decode(buffer4,
//// preferredColorConfig = PreferredColorConfig.RGB_565,
//// toneMapper = JxlToneMapper.REC2408)
val animatedImage = JxlAnimatedImage(
buffer4,
preferredColorConfig = PreferredColorConfig.HARDWARE,
scaleMode = ScaleMode.FIT,
toneMapper = JxlToneMapper.REC2408,
)
drawablesArray.add(animatedImage.animatedDrawable)
// val srcImage = JxlCoder.decode(buffer4,
// preferredColorConfig = PreferredColorConfig.RGB_565,
// toneMapper = JxlToneMapper.REC2408)
// lifecycleScope.launch {
// imagesArray.add(srcImage)
// }
// }
// Log.d("JXLCoder", "Decoding done in ${time}ms")
// }
// } catch (e: Exception) {
// if (e !is FileNotFoundException) {
// throw e
// }
// }
// }
}
Log.d("JXLCoder", "Decoding done in ${time}ms")
}
} catch (e: Exception) {
if (e !is FileNotFoundException) {
throw e
}
}
}
}
})
// A surface container using the 'background' color from the theme
Expand Down Expand Up @@ -276,6 +286,17 @@ class MainActivity : ComponentActivity() {
contentDescription = "ok"
)
}

// items(drawablesArray.count(), key = {
// return@items UUID.randomUUID().toString()
// }) {
// Image(
// rememberDrawablePainter(drawable = drawablesArray[it]),
// modifier = Modifier.fillMaxWidth(),
// contentScale = ContentScale.FillWidth,
// contentDescription = "ok"
// )
// }
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions jxlcoder/src/main/cpp/HardwareBuffersCompat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,48 +61,44 @@ bool loadAHardwareBuffersAPI() {
&& AHardwareBuffer_describe_compat != nullptr;
}
alreadyHdBuffersLoaded = true;
void *hhl = dlopen("libandroid.so", RTLD_NOW);
if (!hhl) {
return false;
}

AHardwareBuffer_allocate_compat = (AHardwareBufferAllocateFunc) dlsym(hhl,
AHardwareBuffer_allocate_compat = (AHardwareBufferAllocateFunc) dlsym(RTLD_DEFAULT,
"AHardwareBuffer_allocate");
if (AHardwareBuffer_allocate_compat == nullptr) {
return false;
}

AHardwareBuffer_isSupported_compat = (AHardwareBufferIsSupportedFunc) dlsym(hhl,
AHardwareBuffer_isSupported_compat = (AHardwareBufferIsSupportedFunc) dlsym(RTLD_DEFAULT,
"AHardwareBuffer_isSupported");
if (AHardwareBuffer_isSupported_compat == nullptr) {
return false;
}

AHardwareBuffer_unlock_compat = (AHardwareBufferUnlockFunc) dlsym(hhl,
AHardwareBuffer_unlock_compat = (AHardwareBufferUnlockFunc) dlsym(RTLD_DEFAULT,
"AHardwareBuffer_unlock");
if (AHardwareBuffer_unlock_compat == nullptr) {
return false;
}

AHardwareBuffer_release_compat = (AHardwareBufferReleaseFunc) dlsym(hhl,
AHardwareBuffer_release_compat = (AHardwareBufferReleaseFunc) dlsym(RTLD_DEFAULT,
"AHardwareBuffer_release");
if (AHardwareBuffer_release_compat == nullptr) {
return false;
}

AHardwareBuffer_lock_compat = (AHardwareBufferLockFunc) dlsym(hhl,
AHardwareBuffer_lock_compat = (AHardwareBufferLockFunc) dlsym(RTLD_DEFAULT,
"AHardwareBuffer_lock");
if (AHardwareBuffer_lock_compat == nullptr) {
return false;
}

AHardwareBuffer_toHardwareBuffer_compat = (AHardwareBufferToHardwareBufferFunc) dlsym(hhl,
AHardwareBuffer_toHardwareBuffer_compat = (AHardwareBufferToHardwareBufferFunc) dlsym(RTLD_DEFAULT,
"AHardwareBuffer_toHardwareBuffer");
if (AHardwareBuffer_toHardwareBuffer_compat == nullptr) {
return false;
}

AHardwareBuffer_describe_compat = (AHardwareBufferDescribeFunc) dlsym(hhl,
AHardwareBuffer_describe_compat = (AHardwareBufferDescribeFunc) dlsym(RTLD_DEFAULT,
"AHardwareBuffer_describe");
if (AHardwareBuffer_describe_compat == nullptr) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion jxlcoder/src/main/cpp/HardwareBuffersCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ typedef int (*_Nullable AHardwareBufferIsSupportedFunc)(
const AHardwareBuffer_Desc *_Nonnull desc
);

typedef int (*_Nullable AHardwareBufferReleaseFunc)(
typedef void (*_Nullable AHardwareBufferReleaseFunc)(
AHardwareBuffer *_Nonnull buffer
);

Expand Down
5 changes: 3 additions & 2 deletions jxlcoder/src/main/cpp/ReformatBitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ ReformatColorConfig(JNIEnv *env, std::vector<uint8_t> &imageData, std::string &i
bufferDesc.format = (*useFloats) ? AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT
: AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;

bufferDesc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
bufferDesc.usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;

AHardwareBuffer *hardwareBuffer = nullptr;

Expand All @@ -216,7 +217,7 @@ ReformatColorConfig(JNIEnv *env, std::vector<uint8_t> &imageData, std::string &i
uint8_t *buffer = nullptr;

status = AHardwareBuffer_lock_compat(hardwareBuffer,
AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1,
AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1,
&rect, reinterpret_cast<void **>(&buffer));
if (status != 0) {
AHardwareBuffer_release_compat(hardwareBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ public class AnimatedDrawable(
}

override fun getIntrinsicHeight(): Int {
return bounds.height()
return frameStore.height
}

override fun getIntrinsicWidth(): Int {
return bounds.width()
return frameStore.width
}

override fun setColorFilter(colorFilter: ColorFilter?) {
Expand Down

0 comments on commit 326b429

Please sign in to comment.