Merge "Allow unsafe operations in unsafe functions in generated code." into main
diff --git a/cmds/installd/otapreopt.cpp b/cmds/installd/otapreopt.cpp
index 7cabdb0..407cb24 100644
--- a/cmds/installd/otapreopt.cpp
+++ b/cmds/installd/otapreopt.cpp
@@ -471,13 +471,18 @@
// TODO(calin): embed the profile name in the parameters.
int Dexopt() {
std::string error;
+
+ int dexopt_flags = parameters_.dexopt_flags;
+ // Make sure dex2oat is run with background priority.
+ dexopt_flags |= DEXOPT_BOOTCOMPLETE | DEXOPT_IDLE_BACKGROUND_JOB;
+
int res = dexopt(parameters_.apk_path,
parameters_.uid,
parameters_.pkgName,
parameters_.instruction_set,
parameters_.dexopt_needed,
parameters_.oat_dir,
- parameters_.dexopt_flags,
+ dexopt_flags,
parameters_.compiler_filter,
parameters_.volume_uuid,
parameters_.shared_libraries,
diff --git a/cmds/installd/otapreopt_script.sh b/cmds/installd/otapreopt_script.sh
index db5c34e..e483d54 100644
--- a/cmds/installd/otapreopt_script.sh
+++ b/cmds/installd/otapreopt_script.sh
@@ -72,7 +72,6 @@
PROGRESS=$(cmd otadexopt progress)
print -u${STATUS_FD} "global_progress $PROGRESS"
- sleep 1
i=$((i+1))
done
diff --git a/libs/binder/tests/Android.bp b/libs/binder/tests/Android.bp
index 41856f9..cd3e7c0 100644
--- a/libs/binder/tests/Android.bp
+++ b/libs/binder/tests/Android.bp
@@ -77,6 +77,8 @@
static_libs: [
"binderRecordReplayTestIface-cpp",
"binderReadParcelIface-cpp",
+ "libbinder_random_parcel_seeds",
+ "libbinder_random_parcel",
],
test_suites: ["general-tests"],
require_root: true,
diff --git a/libs/binder/tests/binderRecordReplayTest.cpp b/libs/binder/tests/binderRecordReplayTest.cpp
index 17d5c8a..6773c95 100644
--- a/libs/binder/tests/binderRecordReplayTest.cpp
+++ b/libs/binder/tests/binderRecordReplayTest.cpp
@@ -15,6 +15,7 @@
*/
#include <BnBinderRecordReplayTest.h>
+#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <binder/Binder.h>
@@ -23,6 +24,11 @@
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/RecordedTransaction.h>
+
+#include <fuzzbinder/libbinder_driver.h>
+#include <fuzzer/FuzzedDataProvider.h>
+#include <fuzzseeds/random_parcel_seeds.h>
+
#include <gtest/gtest.h>
#include <sys/prctl.h>
@@ -30,6 +36,7 @@
#include "parcelables/SingleDataParcelable.h"
using namespace android;
+using android::generateSeedsFromRecording;
using android::binder::Status;
using android::binder::debug::RecordedTransaction;
using parcelables::SingleDataParcelable;
@@ -84,6 +91,44 @@
GENERATE_GETTER_SETTER(SingleDataParcelableArray, std::vector<SingleDataParcelable>);
};
+std::vector<uint8_t> retrieveData(base::borrowed_fd fd) {
+ struct stat fdStat;
+ EXPECT_TRUE(fstat(fd.get(), &fdStat) != -1);
+ EXPECT_TRUE(fdStat.st_size != 0);
+
+ std::vector<uint8_t> buffer(fdStat.st_size);
+ auto readResult = android::base::ReadFully(fd, buffer.data(), fdStat.st_size);
+ EXPECT_TRUE(readResult != 0);
+ return std::move(buffer);
+}
+
+void replayFuzzService(const sp<BpBinder>& binder, const RecordedTransaction& transaction) {
+ base::unique_fd seedFd(open("/data/local/tmp/replayFuzzService",
+ O_RDWR | O_CREAT | O_CLOEXEC | O_TRUNC, 0666));
+ ASSERT_TRUE(seedFd.ok());
+
+ // generate corpus from this transaction.
+ generateSeedsFromRecording(seedFd, transaction);
+
+ // Read the data which has been written to seed corpus
+ ASSERT_EQ(0, lseek(seedFd.get(), 0, SEEK_SET));
+ std::vector<uint8_t> seedData = retrieveData(seedFd);
+
+ // use fuzzService to replay the corpus
+ FuzzedDataProvider provider(seedData.data(), seedData.size());
+ fuzzService(binder, std::move(provider));
+}
+
+void replayBinder(const sp<BpBinder>& binder, const RecordedTransaction& transaction) {
+ // TODO: move logic to replay RecordedTransaction into RecordedTransaction
+ Parcel data;
+ data.setData(transaction.getDataParcel().data(), transaction.getDataParcel().dataSize());
+ auto result = binder->transact(transaction.getCode(), data, nullptr, transaction.getFlags());
+
+ // make sure recording does the thing we expect it to do
+ EXPECT_EQ(OK, result);
+}
+
class BinderRecordReplayTest : public ::testing::Test {
public:
void SetUp() override {
@@ -98,48 +143,46 @@
template <typename T, typename U>
void recordReplay(Status (IBinderRecordReplayTest::*set)(T), U recordedValue,
Status (IBinderRecordReplayTest::*get)(U*), U changedValue) {
- base::unique_fd fd(open("/data/local/tmp/binderRecordReplayTest.rec",
- O_RDWR | O_CREAT | O_CLOEXEC, 0666));
- ASSERT_TRUE(fd.ok());
+ auto replayFunctions = {&replayBinder, &replayFuzzService};
+ for (auto replayFunc : replayFunctions) {
+ base::unique_fd fd(open("/data/local/tmp/binderRecordReplayTest.rec",
+ O_RDWR | O_CREAT | O_CLOEXEC, 0666));
+ ASSERT_TRUE(fd.ok());
- // record a transaction
- mBpBinder->startRecordingBinder(fd);
- auto status = (*mInterface.*set)(recordedValue);
- EXPECT_TRUE(status.isOk());
- mBpBinder->stopRecordingBinder();
+ // record a transaction
+ mBpBinder->startRecordingBinder(fd);
+ auto status = (*mInterface.*set)(recordedValue);
+ EXPECT_TRUE(status.isOk());
+ mBpBinder->stopRecordingBinder();
- // test transaction does the thing we expect it to do
- U output;
- status = (*mInterface.*get)(&output);
- EXPECT_TRUE(status.isOk());
- EXPECT_EQ(output, recordedValue);
+ // test transaction does the thing we expect it to do
+ U output;
+ status = (*mInterface.*get)(&output);
+ EXPECT_TRUE(status.isOk());
+ EXPECT_EQ(output, recordedValue);
- // write over the existing state
- status = (*mInterface.*set)(changedValue);
- EXPECT_TRUE(status.isOk());
+ // write over the existing state
+ status = (*mInterface.*set)(changedValue);
+ EXPECT_TRUE(status.isOk());
- status = (*mInterface.*get)(&output);
- EXPECT_TRUE(status.isOk());
+ status = (*mInterface.*get)(&output);
+ EXPECT_TRUE(status.isOk());
- EXPECT_EQ(output, changedValue);
+ EXPECT_EQ(output, changedValue);
- // replay transaction
- ASSERT_EQ(0, lseek(fd.get(), 0, SEEK_SET));
- std::optional<RecordedTransaction> transaction = RecordedTransaction::fromFile(fd);
- ASSERT_NE(transaction, std::nullopt);
+ // replay transaction
+ ASSERT_EQ(0, lseek(fd.get(), 0, SEEK_SET));
+ std::optional<RecordedTransaction> transaction = RecordedTransaction::fromFile(fd);
+ ASSERT_NE(transaction, std::nullopt);
- // TODO: move logic to replay RecordedTransaction into RecordedTransaction
- Parcel data;
- data.setData(transaction->getDataParcel().data(), transaction->getDataParcel().dataSize());
- auto result =
- mBpBinder->transact(transaction->getCode(), data, nullptr, transaction->getFlags());
+ const RecordedTransaction& recordedTransaction = *transaction;
+ // call replay function with recorded transaction
+ (*replayFunc)(mBpBinder, recordedTransaction);
- // make sure recording does the thing we expect it to do
- EXPECT_EQ(OK, result);
-
- status = (*mInterface.*get)(&output);
- EXPECT_TRUE(status.isOk());
- EXPECT_EQ(output, recordedValue);
+ status = (*mInterface.*get)(&output);
+ EXPECT_TRUE(status.isOk());
+ EXPECT_EQ(output, recordedValue);
+ }
}
private:
diff --git a/libs/binder/tests/parcel_fuzzer/Android.bp b/libs/binder/tests/parcel_fuzzer/Android.bp
index 35866ad..0d1503e 100644
--- a/libs/binder/tests/parcel_fuzzer/Android.bp
+++ b/libs/binder/tests/parcel_fuzzer/Android.bp
@@ -104,3 +104,28 @@
local_include_dirs: ["include_random_parcel"],
export_include_dirs: ["include_random_parcel"],
}
+
+cc_library {
+ name: "libbinder_random_parcel_seeds",
+ host_supported: true,
+ vendor_available: true,
+ target: {
+ darwin: {
+ enabled: false,
+ },
+ },
+ srcs: [
+ "random_parcel_seeds.cpp",
+ ],
+ shared_libs: [
+ "libbase",
+ "libbinder",
+ "libbinder_ndk",
+ "libcutils",
+ "libutils",
+ ],
+ local_include_dirs: [
+ "include_random_parcel_seeds",
+ ],
+ export_include_dirs: ["include_random_parcel_seeds"],
+}
diff --git a/libs/binder/tests/parcel_fuzzer/include_random_parcel_seeds/fuzzseeds/random_parcel_seeds.h b/libs/binder/tests/parcel_fuzzer/include_random_parcel_seeds/fuzzseeds/random_parcel_seeds.h
new file mode 100644
index 0000000..5755239
--- /dev/null
+++ b/libs/binder/tests/parcel_fuzzer/include_random_parcel_seeds/fuzzseeds/random_parcel_seeds.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <android-base/file.h>
+#include <android-base/hex.h>
+#include <android-base/logging.h>
+
+#include <binder/Binder.h>
+#include <binder/Parcel.h>
+#include <binder/RecordedTransaction.h>
+
+#include <private/android_filesystem_config.h>
+
+#include <vector>
+
+using android::Parcel;
+using android::base::HexString;
+using std::vector;
+
+namespace android {
+namespace impl {
+// computes the bytes so that if they are passed to FuzzedDataProvider and
+// provider.ConsumeIntegralInRange<T>(min, max) is called, it will return val
+template <typename T>
+void writeReversedBuffer(std::vector<std::byte>& integralBuffer, T min, T max, T val);
+
+// Calls writeInBuffer method with min and max numeric limits of type T. This method
+// is reversal of ConsumeIntegral<T>() in FuzzedDataProvider
+template <typename T>
+void writeReversedBuffer(std::vector<std::byte>& integralBuffer, T val);
+} // namespace impl
+void generateSeedsFromRecording(base::borrowed_fd fd,
+ const binder::debug::RecordedTransaction& transaction);
+} // namespace android
diff --git a/libs/binder/tests/parcel_fuzzer/libbinder_driver.cpp b/libs/binder/tests/parcel_fuzzer/libbinder_driver.cpp
index b268c5d..47d2a0a 100644
--- a/libs/binder/tests/parcel_fuzzer/libbinder_driver.cpp
+++ b/libs/binder/tests/parcel_fuzzer/libbinder_driver.cpp
@@ -35,6 +35,11 @@
.extraFds = {},
};
+ // Reserved bytes so that we don't have to change fuzzers and seed corpus if
+ // we introduce anything new in fuzzService.
+ std::vector<uint8_t> reservedBytes = provider.ConsumeBytes<uint8_t>(8);
+ (void)reservedBytes;
+
// always refresh the calling identity, because we sometimes set it below, but also,
// the code we're fuzzing might reset it
IPCThreadState::self()->clearCallingIdentity();
diff --git a/libs/binder/tests/parcel_fuzzer/random_parcel_seeds.cpp b/libs/binder/tests/parcel_fuzzer/random_parcel_seeds.cpp
new file mode 100644
index 0000000..9e3e2ab
--- /dev/null
+++ b/libs/binder/tests/parcel_fuzzer/random_parcel_seeds.cpp
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+
+#include <binder/RecordedTransaction.h>
+
+#include <fuzzseeds/random_parcel_seeds.h>
+
+using android::base::WriteFully;
+
+namespace android {
+namespace impl {
+template <typename T>
+std::vector<uint8_t> reverseBytes(T min, T max, T val) {
+ uint64_t range = static_cast<uint64_t>(max) - min;
+ uint64_t result = val - min;
+ size_t offset = 0;
+
+ std::vector<uint8_t> reverseData;
+ uint8_t reversed = 0;
+ reversed |= result;
+
+ while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0) {
+ reverseData.push_back(reversed);
+ reversed = 0;
+ reversed |= (result >> CHAR_BIT);
+ result = result >> CHAR_BIT;
+ offset += CHAR_BIT;
+ }
+
+ return std::move(reverseData);
+}
+
+template <typename T>
+void writeReversedBuffer(std::vector<uint8_t>& integralBuffer, T min, T max, T val) {
+ std::vector<uint8_t> reversedData = reverseBytes(min, max, val);
+ // ConsumeIntegral Calls read buffer from the end. Keep inserting at the front of the buffer
+ // so that we can align fuzzService operations with seed generation for readability.
+ integralBuffer.insert(integralBuffer.begin(), reversedData.begin(), reversedData.end());
+}
+
+template <typename T>
+void writeReversedBuffer(std::vector<uint8_t>& integralBuffer, T val) {
+ // For ConsumeIntegral<T>() calls, FuzzedDataProvider uses numeric limits min and max
+ // as range
+ writeReversedBuffer(integralBuffer, std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max(), val);
+}
+
+} // namespace impl
+
+void generateSeedsFromRecording(base::borrowed_fd fd,
+ const binder::debug::RecordedTransaction& transaction) {
+ // Write Reserved bytes for future use
+ std::vector<uint8_t> reservedBytes(8);
+ CHECK(WriteFully(fd, reservedBytes.data(), reservedBytes.size())) << fd.get();
+
+ std::vector<uint8_t> integralBuffer;
+
+ // Write UID array : Array elements are initialized in the order that they are declared
+ // UID array index 2 element
+ // int64_t aidRoot = 0;
+ impl::writeReversedBuffer(integralBuffer, static_cast<int64_t>(AID_ROOT) << 32,
+ static_cast<int64_t>(AID_USER) << 32,
+ static_cast<int64_t>(AID_ROOT) << 32);
+
+ // UID array index 3 element
+ impl::writeReversedBuffer(integralBuffer, static_cast<int64_t>(AID_ROOT) << 32);
+
+ // always pick AID_ROOT -> index 0
+ size_t uidIndex = 0;
+ impl::writeReversedBuffer(integralBuffer, static_cast<size_t>(0), static_cast<size_t>(3),
+ uidIndex);
+
+ // Never set uid in seed corpus
+ uint8_t writeUid = 0;
+ impl::writeReversedBuffer(integralBuffer, writeUid);
+
+ // Read random code. this will be from recorded transaction
+ uint8_t selectCode = 1;
+ impl::writeReversedBuffer(integralBuffer, selectCode);
+
+ // Get from recorded transaction
+ uint32_t code = transaction.getCode();
+ impl::writeReversedBuffer(integralBuffer, code);
+
+ // Get from recorded transaction
+ uint32_t flags = transaction.getFlags();
+ impl::writeReversedBuffer(integralBuffer, flags);
+
+ // always fuzz primary binder
+ size_t extraBindersIndex = 0;
+ impl::writeReversedBuffer(integralBuffer, static_cast<size_t>(0), static_cast<size_t>(0),
+ extraBindersIndex);
+
+ const Parcel& dataParcel = transaction.getDataParcel();
+
+ // This buffer holds the bytes which will be used for fillRandomParcel API
+ std::vector<uint8_t> fillParcelBuffer;
+
+ // Don't take rpc path
+ uint8_t rpcBranch = 0;
+ impl::writeReversedBuffer(fillParcelBuffer, rpcBranch);
+
+ // Implicit branch on this path -> options->writeHeader(p, provider)
+ uint8_t writeHeaderInternal = 0;
+ impl::writeReversedBuffer(fillParcelBuffer, writeHeaderInternal);
+
+ // Choose to write data in parcel
+ size_t fillFuncIndex = 0;
+ impl::writeReversedBuffer(fillParcelBuffer, static_cast<size_t>(0), static_cast<size_t>(2),
+ fillFuncIndex);
+
+ // Write parcel data size from recorded transaction
+ size_t toWrite = transaction.getDataParcel().dataBufferSize();
+ impl::writeReversedBuffer(fillParcelBuffer, static_cast<size_t>(0), toWrite, toWrite);
+
+ // Write parcel data with size towrite from recorded transaction
+ CHECK(WriteFully(fd, dataParcel.data(), toWrite)) << fd.get();
+
+ // Write Fill Parcel buffer size in integralBuffer so that fuzzService knows size of data
+ size_t subDataSize = toWrite + fillParcelBuffer.size();
+ impl::writeReversedBuffer(integralBuffer, static_cast<size_t>(0), subDataSize, subDataSize);
+
+ // Write fill parcel buffer
+ CHECK(WriteFully(fd, fillParcelBuffer.data(), fillParcelBuffer.size())) << fd.get();
+
+ // Write the integralBuffer to data
+ CHECK(WriteFully(fd, integralBuffer.data(), integralBuffer.size())) << fd.get();
+}
+} // namespace android
diff --git a/libs/graphicsenv/GraphicsEnv.cpp b/libs/graphicsenv/GraphicsEnv.cpp
index 715822b..732ca36 100644
--- a/libs/graphicsenv/GraphicsEnv.cpp
+++ b/libs/graphicsenv/GraphicsEnv.cpp
@@ -512,7 +512,11 @@
return mShouldUseAngle;
}
-void GraphicsEnv::setAngleInfo(const std::string& path, const bool shouldUseSystemAngle,
+// Set ANGLE information.
+// If path is "system", it means system ANGLE must be used for the process.
+// If shouldUseNativeDriver is true, it means native GLES drivers must be used for the process.
+// If path is set to nonempty and shouldUseNativeDriver is true, ANGLE will be used regardless.
+void GraphicsEnv::setAngleInfo(const std::string& path, const bool shouldUseNativeDriver,
const std::string& packageName,
const std::vector<std::string> eglFeatures) {
if (mShouldUseAngle) {
@@ -529,8 +533,13 @@
mAnglePath = std::move(path);
ALOGV("setting app package name to '%s'", packageName.c_str());
mPackageName = std::move(packageName);
- mShouldUseAngle = true;
- mShouldUseSystemAngle = shouldUseSystemAngle;
+ if (mAnglePath == "system") {
+ mShouldUseSystemAngle = true;
+ }
+ if (!mAnglePath.empty()) {
+ mShouldUseAngle = true;
+ }
+ mShouldUseNativeDriver = shouldUseNativeDriver;
}
std::string& GraphicsEnv::getPackageName() {
@@ -607,6 +616,10 @@
return mShouldUseSystemAngle;
}
+bool GraphicsEnv::shouldUseNativeDriver() {
+ return mShouldUseNativeDriver;
+}
+
/**
* APIs for debuggable layers
*/
diff --git a/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h b/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h
index fbf2902..6cce3f6 100644
--- a/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h
+++ b/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h
@@ -108,7 +108,10 @@
// (libraries must be stored uncompressed and page aligned); such elements
// in the search path must have a '!' after the zip filename, e.g.
// /system/app/ANGLEPrebuilt/ANGLEPrebuilt.apk!/lib/arm64-v8a
- void setAngleInfo(const std::string& path, const bool useSystemAngle,
+ // If the search patch is "system", then it means the system ANGLE should be used.
+ // If shouldUseNativeDriver is true, it means native GLES drivers must be used for the process.
+ // If path is set to nonempty and shouldUseNativeDriver is true, ANGLE will be used regardless.
+ void setAngleInfo(const std::string& path, const bool shouldUseNativeDriver,
const std::string& packageName, const std::vector<std::string> eglFeatures);
// Get the ANGLE driver namespace.
android_namespace_t* getAngleNamespace();
@@ -118,6 +121,7 @@
// Set the persist.graphics.egl system property value.
void nativeToggleAngleAsSystemDriver(bool enabled);
bool shouldUseSystemAngle();
+ bool shouldUseNativeDriver();
/*
* Apis for debug layer
@@ -175,6 +179,8 @@
bool mShouldUseAngle = false;
// Whether loader should load system ANGLE.
bool mShouldUseSystemAngle = false;
+ // Whether loader should load native GLES driver.
+ bool mShouldUseNativeDriver = false;
// ANGLE namespace.
android_namespace_t* mAngleNamespace = nullptr;
diff --git a/libs/gui/SurfaceComposerClient.cpp b/libs/gui/SurfaceComposerClient.cpp
index db99726..3c508c7 100644
--- a/libs/gui/SurfaceComposerClient.cpp
+++ b/libs/gui/SurfaceComposerClient.cpp
@@ -2513,38 +2513,41 @@
outInfo->secure = ginfo.secure;
outInfo->installOrientation = static_cast<ui::Rotation>(ginfo.installOrientation);
- DeviceProductInfo info;
- std::optional<gui::DeviceProductInfo> dpi = ginfo.deviceProductInfo;
- gui::DeviceProductInfo::ManufactureOrModelDate& date = dpi->manufactureOrModelDate;
- info.name = dpi->name;
- if (dpi->manufacturerPnpId.size() > 0) {
- // copid from PnpId = std::array<char, 4> in ui/DeviceProductInfo.h
- constexpr int kMaxPnpIdSize = 4;
- size_t count = std::max<size_t>(kMaxPnpIdSize, dpi->manufacturerPnpId.size());
- std::copy_n(dpi->manufacturerPnpId.begin(), count, info.manufacturerPnpId.begin());
- }
- if (dpi->relativeAddress.size() > 0) {
- std::copy(dpi->relativeAddress.begin(), dpi->relativeAddress.end(),
- std::back_inserter(info.relativeAddress));
- }
- info.productId = dpi->productId;
- if (date.getTag() == Tag::modelYear) {
- DeviceProductInfo::ModelYear modelYear;
- modelYear.year = static_cast<uint32_t>(date.get<Tag::modelYear>().year);
- info.manufactureOrModelDate = modelYear;
- } else if (date.getTag() == Tag::manufactureYear) {
- DeviceProductInfo::ManufactureYear manufactureYear;
- manufactureYear.year = date.get<Tag::manufactureYear>().modelYear.year;
- info.manufactureOrModelDate = manufactureYear;
- } else if (date.getTag() == Tag::manufactureWeekAndYear) {
- DeviceProductInfo::ManufactureWeekAndYear weekAndYear;
- weekAndYear.year =
- date.get<Tag::manufactureWeekAndYear>().manufactureYear.modelYear.year;
- weekAndYear.week = date.get<Tag::manufactureWeekAndYear>().week;
- info.manufactureOrModelDate = weekAndYear;
- }
+ if (const std::optional<gui::DeviceProductInfo> dpi = ginfo.deviceProductInfo) {
+ DeviceProductInfo info;
+ info.name = dpi->name;
+ if (dpi->manufacturerPnpId.size() > 0) {
+ // copid from PnpId = std::array<char, 4> in ui/DeviceProductInfo.h
+ constexpr int kMaxPnpIdSize = 4;
+ size_t count = std::max<size_t>(kMaxPnpIdSize, dpi->manufacturerPnpId.size());
+ std::copy_n(dpi->manufacturerPnpId.begin(), count, info.manufacturerPnpId.begin());
+ }
+ if (dpi->relativeAddress.size() > 0) {
+ std::copy(dpi->relativeAddress.begin(), dpi->relativeAddress.end(),
+ std::back_inserter(info.relativeAddress));
+ }
+ info.productId = dpi->productId;
- outInfo->deviceProductInfo = info;
+ const gui::DeviceProductInfo::ManufactureOrModelDate& date =
+ dpi->manufactureOrModelDate;
+ if (date.getTag() == Tag::modelYear) {
+ DeviceProductInfo::ModelYear modelYear;
+ modelYear.year = static_cast<uint32_t>(date.get<Tag::modelYear>().year);
+ info.manufactureOrModelDate = modelYear;
+ } else if (date.getTag() == Tag::manufactureYear) {
+ DeviceProductInfo::ManufactureYear manufactureYear;
+ manufactureYear.year = date.get<Tag::manufactureYear>().modelYear.year;
+ info.manufactureOrModelDate = manufactureYear;
+ } else if (date.getTag() == Tag::manufactureWeekAndYear) {
+ DeviceProductInfo::ManufactureWeekAndYear weekAndYear;
+ weekAndYear.year =
+ date.get<Tag::manufactureWeekAndYear>().manufactureYear.modelYear.year;
+ weekAndYear.week = date.get<Tag::manufactureWeekAndYear>().week;
+ info.manufactureOrModelDate = weekAndYear;
+ }
+
+ outInfo->deviceProductInfo = info;
+ }
}
return statusTFromBinderStatus(status);
}
diff --git a/libs/gui/aidl/android/gui/ISurfaceComposer.aidl b/libs/gui/aidl/android/gui/ISurfaceComposer.aidl
index 539a1c1..5e8e904 100644
--- a/libs/gui/aidl/android/gui/ISurfaceComposer.aidl
+++ b/libs/gui/aidl/android/gui/ISurfaceComposer.aidl
@@ -230,20 +230,20 @@
* The subregion can be optionally rotated. It will also be scaled to
* match the size of the output buffer.
*/
- void captureDisplay(in DisplayCaptureArgs args, IScreenCaptureListener listener);
+ oneway void captureDisplay(in DisplayCaptureArgs args, IScreenCaptureListener listener);
/**
* Capture the specified screen. This requires the READ_FRAME_BUFFER
* permission.
*/
- void captureDisplayById(long displayId, IScreenCaptureListener listener);
+ oneway void captureDisplayById(long displayId, IScreenCaptureListener listener);
/**
* Capture a subtree of the layer hierarchy, potentially ignoring the root node.
* This requires READ_FRAME_BUFFER permission. This function will fail if there
* is a secure window on screen
*/
- void captureLayers(in LayerCaptureArgs args, IScreenCaptureListener listener);
+ oneway void captureLayers(in LayerCaptureArgs args, IScreenCaptureListener listener);
/**
* Clears the frame statistics for animations.
diff --git a/libs/gui/tests/BLASTBufferQueue_test.cpp b/libs/gui/tests/BLASTBufferQueue_test.cpp
index cc6f3dd..9618502 100644
--- a/libs/gui/tests/BLASTBufferQueue_test.cpp
+++ b/libs/gui/tests/BLASTBufferQueue_test.cpp
@@ -351,24 +351,6 @@
sp<CountProducerListener> mProducerListener;
};
-// Helper class to ensure the provided BBQ frame number has been committed in SurfaceFlinger.
-class BBQSyncHelper {
-public:
- BBQSyncHelper(BLASTBufferQueueHelper& bbqHelper, uint64_t frameNumber) {
- t.addTransactionCompletedCallback(callbackHelper.function, callbackHelper.getContext());
- bbqHelper.mergeWithNextTransaction(&t, frameNumber);
- }
-
- void wait() {
- CallbackData callbackData;
- callbackHelper.getCallbackData(&callbackData);
- }
-
-private:
- Transaction t;
- CallbackHelper callbackHelper;
-};
-
TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
// create BLASTBufferQueue adapter associated with this surface
BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
@@ -440,7 +422,6 @@
BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
sp<IGraphicBufferProducer> igbProducer;
setUpProducer(adapter, igbProducer);
- BBQSyncHelper syncHelper{adapter, 1};
int slot;
sp<Fence> fence;
@@ -466,7 +447,8 @@
igbProducer->queueBuffer(slot, input, &qbOutput);
ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
- syncHelper.wait();
+ // ensure the buffer queue transaction has been committed
+ Transaction().apply(true /* synchronous */);
// capture screen and verify that it is red
ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
@@ -526,7 +508,6 @@
BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
sp<IGraphicBufferProducer> igbProducer;
setUpProducer(adapter, igbProducer);
- BBQSyncHelper syncHelper{adapter, 1};
int slot;
sp<Fence> fence;
sp<GraphicBuffer> buf;
@@ -551,7 +532,9 @@
igbProducer->queueBuffer(slot, input, &qbOutput);
ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
- syncHelper.wait();
+ // ensure the buffer queue transaction has been committed
+ Transaction().apply(true /* synchronous */);
+
// capture screen and verify that it is red
ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
@@ -582,7 +565,6 @@
BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
sp<IGraphicBufferProducer> igbProducer;
setUpProducer(adapter, igbProducer);
- BBQSyncHelper syncHelper{adapter, 1};
int slot;
sp<Fence> fence;
sp<GraphicBuffer> buf;
@@ -611,7 +593,9 @@
igbProducer->queueBuffer(slot, input, &qbOutput);
ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
- syncHelper.wait();
+ // ensure the buffer queue transaction has been committed
+ Transaction().apply(true /* synchronous */);
+
// capture screen and verify that it is red
ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b,
@@ -642,7 +626,6 @@
BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
sp<IGraphicBufferProducer> igbProducer;
setUpProducer(adapter, igbProducer);
- BBQSyncHelper syncHelper{adapter, 1};
int slot;
sp<Fence> fence;
sp<GraphicBuffer> buf;
@@ -675,7 +658,8 @@
igbProducer->queueBuffer(slot, input, &qbOutput);
ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
- syncHelper.wait();
+ // ensure the buffer queue transaction has been committed
+ Transaction().apply(true /* synchronous */);
ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
@@ -710,7 +694,6 @@
BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
sp<IGraphicBufferProducer> igbProducer;
setUpProducer(adapter, igbProducer);
- BBQSyncHelper syncHelper{adapter, 1};
int slot;
sp<Fence> fence;
sp<GraphicBuffer> buf;
@@ -743,7 +726,8 @@
igbProducer->queueBuffer(slot, input, &qbOutput);
ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
- syncHelper.wait();
+ // ensure the buffer queue transaction has been committed
+ Transaction().apply(true /* synchronous */);
ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
// Verify cropped region is scaled correctly.
@@ -769,7 +753,6 @@
sp<IGraphicBufferProducer> igbProducer;
setUpProducer(adapter, igbProducer);
{
- BBQSyncHelper syncHelper{adapter, 1};
int slot;
sp<Fence> fence;
sp<GraphicBuffer> buf;
@@ -791,7 +774,9 @@
NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
Fence::NO_FENCE);
igbProducer->queueBuffer(slot, input, &qbOutput);
- syncHelper.wait();
+
+ // ensure the buffer queue transaction has been committed
+ Transaction().apply(true /* synchronous */);
}
// capture screen and verify that it is red
ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
@@ -804,7 +789,6 @@
adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight / 2);
{
- BBQSyncHelper syncHelper{adapter, 1};
int slot;
sp<Fence> fence;
sp<GraphicBuffer> buf;
@@ -827,7 +811,8 @@
NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
0, Fence::NO_FENCE);
igbProducer->queueBuffer(slot, input, &qbOutput);
- syncHelper.wait();
+ // ensure the buffer queue transaction has been committed
+ Transaction().apply(true /* synchronous */);
}
// capture screen and verify that it is red
ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
@@ -1420,7 +1405,7 @@
igbProducer->queueBuffer(slot, input, &qbOutput);
ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
- adapter.waitForCallbacks();
+ Transaction().apply(true /* synchronous */);
ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
switch (tr) {
diff --git a/libs/input/InputTransport.cpp b/libs/input/InputTransport.cpp
index d9b7700..3446540 100644
--- a/libs/input/InputTransport.cpp
+++ b/libs/input/InputTransport.cpp
@@ -75,10 +75,20 @@
/**
* Log debug messages about touch event resampling.
- * Enable this via "adb shell setprop log.tag.InputTransportResampling DEBUG" (requires restart)
+ *
+ * Enable this via "adb shell setprop log.tag.InputTransportResampling DEBUG".
+ * This requires a restart on non-debuggable (e.g. user) builds, but should take effect immediately
+ * on debuggable builds (e.g. userdebug).
*/
-const bool DEBUG_RESAMPLING =
- __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", ANDROID_LOG_INFO);
+bool debugResampling() {
+ if (!IS_DEBUGGABLE_BUILD) {
+ static const bool DEBUG_TRANSPORT_RESAMPLING =
+ __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling",
+ ANDROID_LOG_INFO);
+ return DEBUG_TRANSPORT_RESAMPLING;
+ }
+ return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", ANDROID_LOG_INFO);
+}
} // namespace
@@ -1158,7 +1168,7 @@
state.recentCoordinatesAreIdentical(id)) {
PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
- ALOGD_IF(DEBUG_RESAMPLING, "[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
+ ALOGD_IF(debugResampling(), "[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
resampleCoords.getX(), resampleCoords.getY(), msgCoords.getX(),
msgCoords.getY());
msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
@@ -1181,13 +1191,13 @@
ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
if (index < 0) {
- ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, no touch state for device.");
+ ALOGD_IF(debugResampling(), "Not resampled, no touch state for device.");
return;
}
TouchState& touchState = mTouchStates[index];
if (touchState.historySize < 1) {
- ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, no history for device.");
+ ALOGD_IF(debugResampling(), "Not resampled, no history for device.");
return;
}
@@ -1197,7 +1207,7 @@
for (size_t i = 0; i < pointerCount; i++) {
uint32_t id = event->getPointerId(i);
if (!current->idBits.hasBit(id)) {
- ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, missing id %d", id);
+ ALOGD_IF(debugResampling(), "Not resampled, missing id %d", id);
return;
}
}
@@ -1213,7 +1223,7 @@
other = &future;
nsecs_t delta = future.eventTime - current->eventTime;
if (delta < RESAMPLE_MIN_DELTA) {
- ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, delta time is too small: %" PRId64 " ns.",
+ ALOGD_IF(debugResampling(), "Not resampled, delta time is too small: %" PRId64 " ns.",
delta);
return;
}
@@ -1224,17 +1234,17 @@
other = touchState.getHistory(1);
nsecs_t delta = current->eventTime - other->eventTime;
if (delta < RESAMPLE_MIN_DELTA) {
- ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, delta time is too small: %" PRId64 " ns.",
+ ALOGD_IF(debugResampling(), "Not resampled, delta time is too small: %" PRId64 " ns.",
delta);
return;
} else if (delta > RESAMPLE_MAX_DELTA) {
- ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, delta time is too large: %" PRId64 " ns.",
+ ALOGD_IF(debugResampling(), "Not resampled, delta time is too large: %" PRId64 " ns.",
delta);
return;
}
nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
if (sampleTime > maxPredict) {
- ALOGD_IF(DEBUG_RESAMPLING,
+ ALOGD_IF(debugResampling(),
"Sample time is too far in the future, adjusting prediction "
"from %" PRId64 " to %" PRId64 " ns.",
sampleTime - current->eventTime, maxPredict - current->eventTime);
@@ -1242,7 +1252,7 @@
}
alpha = float(current->eventTime - sampleTime) / delta;
} else {
- ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, insufficient data.");
+ ALOGD_IF(debugResampling(), "Not resampled, insufficient data.");
return;
}
@@ -1284,13 +1294,13 @@
resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
lerp(currentCoords.getY(), otherCoords.getY(), alpha));
resampledCoords.isResampled = true;
- ALOGD_IF(DEBUG_RESAMPLING,
+ ALOGD_IF(debugResampling(),
"[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
"other (%0.3f, %0.3f), alpha %0.3f",
id, resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(),
currentCoords.getY(), otherCoords.getX(), otherCoords.getY(), alpha);
} else {
- ALOGD_IF(DEBUG_RESAMPLING, "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", id,
+ ALOGD_IF(debugResampling(), "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", id,
resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(),
currentCoords.getY());
}
diff --git a/libs/renderengine/skia/SkiaRenderEngine.cpp b/libs/renderengine/skia/SkiaRenderEngine.cpp
index 29d8ba7..1f4c989 100644
--- a/libs/renderengine/skia/SkiaRenderEngine.cpp
+++ b/libs/renderengine/skia/SkiaRenderEngine.cpp
@@ -511,7 +511,8 @@
auto effect =
shaders::LinearEffect{.inputDataspace = parameters.layer.sourceDataspace,
.outputDataspace = parameters.outputDataSpace,
- .undoPremultipliedAlpha = parameters.undoPremultipliedAlpha};
+ .undoPremultipliedAlpha = parameters.undoPremultipliedAlpha,
+ .fakeOutputDataspace = parameters.fakeOutputDataspace};
auto effectIter = mRuntimeEffects.find(effect);
sk_sp<SkRuntimeEffect> runtimeEffect = nullptr;
@@ -907,12 +908,14 @@
(display.outputDataspace & ui::Dataspace::TRANSFER_MASK) ==
static_cast<int32_t>(ui::Dataspace::TRANSFER_SRGB);
- const ui::Dataspace runtimeEffectDataspace = !dimInLinearSpace && isExtendedHdr
+ const bool useFakeOutputDataspaceForRuntimeEffect = !dimInLinearSpace && isExtendedHdr;
+
+ const ui::Dataspace fakeDataspace = useFakeOutputDataspaceForRuntimeEffect
? static_cast<ui::Dataspace>(
(display.outputDataspace & ui::Dataspace::STANDARD_MASK) |
ui::Dataspace::TRANSFER_GAMMA2_2 |
(display.outputDataspace & ui::Dataspace::RANGE_MASK))
- : display.outputDataspace;
+ : ui::Dataspace::UNKNOWN;
// If the input dataspace is range extended, the output dataspace transfer is sRGB
// and dimmingStage is GAMMA_OETF, dim in linear space instead, and
@@ -1019,7 +1022,8 @@
.layerDimmingRatio = dimInLinearSpace
? layerDimmingRatio
: 1.f,
- .outputDataSpace = runtimeEffectDataspace}));
+ .outputDataSpace = display.outputDataspace,
+ .fakeOutputDataspace = fakeDataspace}));
// Turn on dithering when dimming beyond this (arbitrary) threshold...
static constexpr float kDimmingThreshold = 0.2f;
@@ -1083,7 +1087,8 @@
.undoPremultipliedAlpha = false,
.requiresLinearEffect = requiresLinearEffect,
.layerDimmingRatio = layerDimmingRatio,
- .outputDataSpace = runtimeEffectDataspace}));
+ .outputDataSpace = display.outputDataspace,
+ .fakeOutputDataspace = fakeDataspace}));
}
if (layer.disableBlending) {
diff --git a/libs/renderengine/skia/SkiaRenderEngine.h b/libs/renderengine/skia/SkiaRenderEngine.h
index 6457bfa..723e73c 100644
--- a/libs/renderengine/skia/SkiaRenderEngine.h
+++ b/libs/renderengine/skia/SkiaRenderEngine.h
@@ -157,6 +157,7 @@
bool requiresLinearEffect;
float layerDimmingRatio;
const ui::Dataspace outputDataSpace;
+ const ui::Dataspace fakeOutputDataspace;
};
sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&);
diff --git a/libs/renderengine/skia/filters/KawaseBlurFilter.cpp b/libs/renderengine/skia/filters/KawaseBlurFilter.cpp
index 0c7335c..7bf2b0c 100644
--- a/libs/renderengine/skia/filters/KawaseBlurFilter.cpp
+++ b/libs/renderengine/skia/filters/KawaseBlurFilter.cpp
@@ -61,25 +61,7 @@
// Draws the given runtime shader on a GPU (Ganesh) surface and returns the result as an
// SkImage.
-static sk_sp<SkImage> makeImage(GrRecordingContext* context, SkRuntimeShaderBuilder* builder,
- const SkImageInfo& resultInfo) {
- if (resultInfo.alphaType() == kUnpremul_SkAlphaType ||
- resultInfo.alphaType() == kUnknown_SkAlphaType) {
- return nullptr;
- }
- constexpr int kSampleCount = 1;
- constexpr bool kMipmapped = false;
-
- sk_sp<SkSurface> surface = SkSurfaces::RenderTarget(context,
- skgpu::Budgeted::kYes,
- resultInfo,
- kSampleCount,
- kTopLeft_GrSurfaceOrigin,
- nullptr,
- kMipmapped);
- if (!surface) {
- return nullptr;
- }
+static sk_sp<SkImage> makeImage(SkSurface* surface, SkRuntimeShaderBuilder* builder) {
sk_sp<SkShader> shader = builder->makeShader(nullptr);
if (!shader) {
return nullptr;
@@ -97,11 +79,16 @@
const SkRect& blurRect) const {
LOG_ALWAYS_FATAL_IF(context == nullptr, "%s: Needs GPU context", __func__);
LOG_ALWAYS_FATAL_IF(input == nullptr, "%s: Invalid input image", __func__);
+
+ if (blurRadius == 0) {
+ return input;
+ }
+
// Kawase is an approximation of Gaussian, but it behaves differently from it.
// A radius transformation is required for approximating them, and also to introduce
// non-integer steps, necessary to smoothly interpolate large radii.
float tmpRadius = (float)blurRadius / 2.0f;
- float numberOfPasses = std::min(kMaxPasses, (uint32_t)ceil(tmpRadius));
+ uint32_t numberOfPasses = std::min(kMaxPasses, (uint32_t)ceil(tmpRadius));
float radiusByPasses = tmpRadius / (float)numberOfPasses;
// create blur surface with the bit depth and colorspace of the original surface
@@ -121,15 +108,33 @@
input->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linear, blurMatrix);
blurBuilder.uniform("in_blurOffset") = radiusByPasses * kInputScale;
- sk_sp<SkImage> tmpBlur = makeImage(context, &blurBuilder, scaledInfo);
+ constexpr int kSampleCount = 1;
+ constexpr bool kMipmapped = false;
+ constexpr SkSurfaceProps* kProps = nullptr;
+ sk_sp<SkSurface> surface =
+ SkSurfaces::RenderTarget(context, skgpu::Budgeted::kYes, scaledInfo, kSampleCount,
+ kTopLeft_GrSurfaceOrigin, kProps, kMipmapped);
+ LOG_ALWAYS_FATAL_IF(!surface, "%s: Failed to create surface for blurring!", __func__);
+ sk_sp<SkImage> tmpBlur = makeImage(surface.get(), &blurBuilder);
- // And now we'll build our chain of scaled blur stages
- for (auto i = 1; i < numberOfPasses; i++) {
- LOG_ALWAYS_FATAL_IF(tmpBlur == nullptr, "%s: tmpBlur is null for pass %d", __func__, i);
- blurBuilder.child("child") =
- tmpBlur->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linear);
- blurBuilder.uniform("in_blurOffset") = (float) i * radiusByPasses * kInputScale;
- tmpBlur = makeImage(context, &blurBuilder, scaledInfo);
+ // And now we'll build our chain of scaled blur stages. If there is more than one pass,
+ // create a second surface and ping pong between them.
+ sk_sp<SkSurface> surfaceTwo;
+ if (numberOfPasses <= 1) {
+ LOG_ALWAYS_FATAL_IF(tmpBlur == nullptr, "%s: tmpBlur is null", __func__);
+ } else {
+ surfaceTwo = surface->makeSurface(scaledInfo);
+ LOG_ALWAYS_FATAL_IF(!surfaceTwo, "%s: Failed to create second blur surface!", __func__);
+
+ for (auto i = 1; i < numberOfPasses; i++) {
+ LOG_ALWAYS_FATAL_IF(tmpBlur == nullptr, "%s: tmpBlur is null for pass %d", __func__, i);
+ blurBuilder.child("child") =
+ tmpBlur->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linear);
+ blurBuilder.uniform("in_blurOffset") = (float) i * radiusByPasses * kInputScale;
+ tmpBlur = makeImage(surfaceTwo.get(), &blurBuilder);
+ using std::swap;
+ swap(surface, surfaceTwo);
+ }
}
return tmpBlur;
diff --git a/libs/shaders/shaders.cpp b/libs/shaders/shaders.cpp
index c85517a..ef039e5 100644
--- a/libs/shaders/shaders.cpp
+++ b/libs/shaders/shaders.cpp
@@ -168,8 +168,8 @@
void generateOETF(std::string& shader) {
// Only support gamma 2.2 for now
shader.append(R"(
- float OETF(float3 linear) {
- return sign(linear) * pow(abs(linear), (1.0 / 2.2));
+ float3 OETF(float3 linear) {
+ return sign(linear) * pow(abs(linear), float3(1.0 / 2.2));
}
)");
}
diff --git a/libs/ultrahdr/fuzzer/ultrahdr_dec_fuzzer.cpp b/libs/ultrahdr/fuzzer/ultrahdr_dec_fuzzer.cpp
index ad1d57a..f1f4035 100644
--- a/libs/ultrahdr/fuzzer/ultrahdr_dec_fuzzer.cpp
+++ b/libs/ultrahdr/fuzzer/ultrahdr_dec_fuzzer.cpp
@@ -54,7 +54,7 @@
std::cout << "input buffer size " << jpegImgR.length << std::endl;
std::cout << "image dimensions " << info.width << " x " << info.width << std::endl;
#endif
- size_t outSize = info.width * info.height * ((of == ULTRAHDR_OUTPUT_SDR) ? 4 : 8);
+ size_t outSize = info.width * info.height * ((of == ULTRAHDR_OUTPUT_HDR_LINEAR) ? 8 : 4);
jpegr_uncompressed_struct decodedJpegR;
auto decodedRaw = std::make_unique<uint8_t[]>(outSize);
decodedJpegR.data = decodedRaw.get();
diff --git a/libs/ultrahdr/fuzzer/ultrahdr_enc_fuzzer.cpp b/libs/ultrahdr/fuzzer/ultrahdr_enc_fuzzer.cpp
index bbe58e0..bf9b031 100644
--- a/libs/ultrahdr/fuzzer/ultrahdr_enc_fuzzer.cpp
+++ b/libs/ultrahdr/fuzzer/ultrahdr_enc_fuzzer.cpp
@@ -23,22 +23,12 @@
// User include files
#include "ultrahdr/gainmapmath.h"
+#include "ultrahdr/jpegdecoderhelper.h"
#include "ultrahdr/jpegencoderhelper.h"
#include "utils/Log.h"
using namespace android::ultrahdr;
-// constants
-const int kMinWidth = 8;
-const int kMaxWidth = 7680;
-
-const int kMinHeight = 8;
-const int kMaxHeight = 4320;
-
-const int kScaleFactor = 4;
-
-const int kJpegBlock = 16;
-
// Color gamuts for image data, sync with ultrahdr.h
const int kCgMin = ULTRAHDR_COLORGAMUT_UNSPECIFIED + 1;
const int kCgMax = ULTRAHDR_COLORGAMUT_MAX;
@@ -60,7 +50,7 @@
UltraHdrEncFuzzer(const uint8_t* data, size_t size) : mFdp(data, size){};
void process();
void fillP010Buffer(uint16_t* data, int width, int height, int stride);
- void fill420Buffer(uint8_t* data, int size);
+ void fill420Buffer(uint8_t* data, int width, int height, int stride);
private:
FuzzedDataProvider mFdp;
@@ -70,11 +60,12 @@
uint16_t* tmp = data;
std::vector<uint16_t> buffer(16);
for (int i = 0; i < buffer.size(); i++) {
- buffer[i] = mFdp.ConsumeIntegralInRange<int>(0, (1 << 10) - 1);
+ buffer[i] = (mFdp.ConsumeIntegralInRange<int>(0, (1 << 10) - 1)) << 6;
}
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i += buffer.size()) {
- memcpy(data + i, buffer.data(), std::min((int)buffer.size(), (width - i)));
+ memcpy(tmp + i, buffer.data(),
+ std::min((int)buffer.size(), (width - i)) * sizeof(*data));
std::shuffle(buffer.begin(), buffer.end(),
std::default_random_engine(std::random_device{}()));
}
@@ -82,13 +73,18 @@
}
}
-void UltraHdrEncFuzzer::fill420Buffer(uint8_t* data, int size) {
+void UltraHdrEncFuzzer::fill420Buffer(uint8_t* data, int width, int height, int stride) {
+ uint8_t* tmp = data;
std::vector<uint8_t> buffer(16);
mFdp.ConsumeData(buffer.data(), buffer.size());
- for (int i = 0; i < size; i += buffer.size()) {
- memcpy(data + i, buffer.data(), std::min((int)buffer.size(), (size - i)));
- std::shuffle(buffer.begin(), buffer.end(),
- std::default_random_engine(std::random_device{}()));
+ for (int j = 0; j < height; j++) {
+ for (int i = 0; i < width; i += buffer.size()) {
+ memcpy(tmp + i, buffer.data(),
+ std::min((int)buffer.size(), (width - i)) * sizeof(*data));
+ std::shuffle(buffer.begin(), buffer.end(),
+ std::default_random_engine(std::random_device{}()));
+ }
+ tmp += stride;
}
}
@@ -129,9 +125,10 @@
int height = mFdp.ConsumeIntegralInRange<int>(kMinHeight, kMaxHeight);
height = (height >> 1) << 1;
- std::unique_ptr<uint16_t[]> bufferY = nullptr;
- std::unique_ptr<uint16_t[]> bufferUV = nullptr;
- std::unique_ptr<uint8_t[]> yuv420ImgRaw = nullptr;
+ std::unique_ptr<uint16_t[]> bufferYHdr = nullptr;
+ std::unique_ptr<uint16_t[]> bufferUVHdr = nullptr;
+ std::unique_ptr<uint8_t[]> bufferYSdr = nullptr;
+ std::unique_ptr<uint8_t[]> bufferUVSdr = nullptr;
std::unique_ptr<uint8_t[]> grayImgRaw = nullptr;
if (muxSwitch != 4) {
// init p010 image
@@ -145,30 +142,29 @@
int bppP010 = 2;
if (isUVContiguous) {
size_t p010Size = yStride * height * 3 / 2;
- bufferY = std::make_unique<uint16_t[]>(p010Size);
- p010Img.data = bufferY.get();
+ bufferYHdr = std::make_unique<uint16_t[]>(p010Size);
+ p010Img.data = bufferYHdr.get();
p010Img.chroma_data = nullptr;
p010Img.chroma_stride = 0;
- fillP010Buffer(bufferY.get(), width, height, yStride);
- fillP010Buffer(bufferY.get() + yStride * height, width, height / 2, yStride);
+ fillP010Buffer(bufferYHdr.get(), width, height, yStride);
+ fillP010Buffer(bufferYHdr.get() + yStride * height, width, height / 2, yStride);
} else {
int uvStride = mFdp.ConsumeIntegralInRange<int>(width, width + 128);
size_t p010YSize = yStride * height;
- bufferY = std::make_unique<uint16_t[]>(p010YSize);
- p010Img.data = bufferY.get();
- fillP010Buffer(bufferY.get(), width, height, yStride);
+ bufferYHdr = std::make_unique<uint16_t[]>(p010YSize);
+ p010Img.data = bufferYHdr.get();
+ fillP010Buffer(bufferYHdr.get(), width, height, yStride);
size_t p010UVSize = uvStride * p010Img.height / 2;
- bufferUV = std::make_unique<uint16_t[]>(p010UVSize);
- p010Img.chroma_data = bufferUV.get();
+ bufferUVHdr = std::make_unique<uint16_t[]>(p010UVSize);
+ p010Img.chroma_data = bufferUVHdr.get();
p010Img.chroma_stride = uvStride;
- fillP010Buffer(bufferUV.get(), width, height / 2, uvStride);
+ fillP010Buffer(bufferUVHdr.get(), width, height / 2, uvStride);
}
} else {
- int map_width = width / kScaleFactor;
- int map_height = height / kScaleFactor;
- map_width = static_cast<size_t>(floor((map_width + kJpegBlock - 1) / kJpegBlock)) *
- kJpegBlock;
- map_height = ((map_height + 1) >> 1) << 1;
+ size_t map_width = static_cast<size_t>(
+ floor((width + kMapDimensionScaleFactor - 1) / kMapDimensionScaleFactor));
+ size_t map_height = static_cast<size_t>(
+ floor((height + kMapDimensionScaleFactor - 1) / kMapDimensionScaleFactor));
// init 400 image
grayImg.width = map_width;
grayImg.height = map_height;
@@ -177,7 +173,7 @@
const size_t graySize = map_width * map_height;
grayImgRaw = std::make_unique<uint8_t[]>(graySize);
grayImg.data = grayImgRaw.get();
- fill420Buffer(grayImgRaw.get(), graySize);
+ fill420Buffer(grayImgRaw.get(), map_width, map_height, map_width);
grayImg.chroma_data = nullptr;
grayImg.luma_stride = 0;
grayImg.chroma_stride = 0;
@@ -185,17 +181,38 @@
if (muxSwitch > 0) {
// init 420 image
+ bool isUVContiguous = mFdp.ConsumeBool();
+ bool hasYStride = mFdp.ConsumeBool();
+ int yStride = hasYStride ? mFdp.ConsumeIntegralInRange<int>(width, width + 128) : width;
yuv420Img.width = width;
yuv420Img.height = height;
yuv420Img.colorGamut = yuv420Cg;
-
- const size_t yuv420Size = (yuv420Img.width * yuv420Img.height * 3) / 2;
- yuv420ImgRaw = std::make_unique<uint8_t[]>(yuv420Size);
- yuv420Img.data = yuv420ImgRaw.get();
- fill420Buffer(yuv420ImgRaw.get(), yuv420Size);
- yuv420Img.chroma_data = nullptr;
- yuv420Img.luma_stride = 0;
- yuv420Img.chroma_stride = 0;
+ yuv420Img.luma_stride = hasYStride ? yStride : 0;
+ if (isUVContiguous) {
+ size_t yuv420Size = yStride * height * 3 / 2;
+ bufferYSdr = std::make_unique<uint8_t[]>(yuv420Size);
+ yuv420Img.data = bufferYSdr.get();
+ yuv420Img.chroma_data = nullptr;
+ yuv420Img.chroma_stride = 0;
+ fill420Buffer(bufferYSdr.get(), width, height, yStride);
+ fill420Buffer(bufferYSdr.get() + yStride * height, width / 2, height / 2,
+ yStride / 2);
+ fill420Buffer(bufferYSdr.get() + yStride * height * 5 / 4, width / 2, height / 2,
+ yStride / 2);
+ } else {
+ int uvStride = mFdp.ConsumeIntegralInRange<int>(width / 2, width / 2 + 128);
+ size_t yuv420YSize = yStride * height;
+ bufferYSdr = std::make_unique<uint8_t[]>(yuv420YSize);
+ yuv420Img.data = bufferYSdr.get();
+ fill420Buffer(bufferYSdr.get(), width, height, yStride);
+ size_t yuv420UVSize = uvStride * yuv420Img.height / 2 * 2;
+ bufferUVSdr = std::make_unique<uint8_t[]>(yuv420UVSize);
+ yuv420Img.chroma_data = bufferYSdr.get();
+ yuv420Img.chroma_stride = uvStride;
+ fill420Buffer(bufferUVSdr.get(), width / 2, height / 2, uvStride);
+ fill420Buffer(bufferUVSdr.get() + uvStride * height / 2, width / 2, height / 2,
+ uvStride);
+ }
}
// dest
@@ -212,6 +229,8 @@
std::cout << "p010 luma stride " << p010Img.luma_stride << std::endl;
std::cout << "p010 chroma stride " << p010Img.chroma_stride << std::endl;
std::cout << "420 color gamut " << yuv420Img.colorGamut << std::endl;
+ std::cout << "420 luma stride " << yuv420Img.luma_stride << std::endl;
+ std::cout << "420 chroma stride " << yuv420Img.chroma_stride << std::endl;
std::cout << "quality factor " << quality << std::endl;
#endif
@@ -226,8 +245,19 @@
} else {
// compressed img
JpegEncoderHelper encoder;
- if (encoder.compressImage(yuv420Img.data, yuv420Img.width, yuv420Img.height, quality,
- nullptr, 0)) {
+ struct jpegr_uncompressed_struct yuv420ImgCopy = yuv420Img;
+ if (yuv420ImgCopy.luma_stride == 0) yuv420ImgCopy.luma_stride = yuv420Img.width;
+ if (!yuv420ImgCopy.chroma_data) {
+ uint8_t* data = reinterpret_cast<uint8_t*>(yuv420Img.data);
+ yuv420ImgCopy.chroma_data = data + yuv420Img.luma_stride * yuv420Img.height;
+ yuv420ImgCopy.chroma_stride = yuv420Img.luma_stride >> 1;
+ }
+
+ if (encoder.compressImage(reinterpret_cast<uint8_t*>(yuv420ImgCopy.data),
+ reinterpret_cast<uint8_t*>(yuv420ImgCopy.chroma_data),
+ yuv420ImgCopy.width, yuv420ImgCopy.height,
+ yuv420ImgCopy.luma_stride, yuv420ImgCopy.chroma_stride,
+ quality, nullptr, 0)) {
jpegImg.length = encoder.getCompressedImageSize();
jpegImg.maxLength = jpegImg.length;
jpegImg.data = encoder.getCompressedImagePtr();
@@ -242,14 +272,15 @@
} else if (muxSwitch == 4) { // api 4
jpegImgR.length = 0;
JpegEncoderHelper gainMapEncoder;
- if (gainMapEncoder.compressImage(grayImg.data, grayImg.width, grayImg.height,
- quality, nullptr, 0, true)) {
+ if (gainMapEncoder.compressImage(reinterpret_cast<uint8_t*>(grayImg.data),
+ nullptr, grayImg.width, grayImg.height,
+ grayImg.width, 0, quality, nullptr, 0)) {
jpegGainMap.length = gainMapEncoder.getCompressedImageSize();
jpegGainMap.maxLength = jpegImg.length;
jpegGainMap.data = gainMapEncoder.getCompressedImagePtr();
jpegGainMap.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
ultrahdr_metadata_struct metadata;
- metadata.version = "1.0";
+ metadata.version = kJpegrVersion;
if (tf == ULTRAHDR_TF_HLG) {
metadata.maxContentBoost = kHlgMaxNits / kSdrWhiteNits;
} else if (tf == ULTRAHDR_TF_PQ) {
@@ -274,7 +305,8 @@
jpegr_info_struct info{0, 0, &iccData, &exifData};
status = jpegHdr.getJPEGRInfo(&jpegImgR, &info);
if (status == android::OK) {
- size_t outSize = info.width * info.height * ((of == ULTRAHDR_OUTPUT_SDR) ? 4 : 8);
+ size_t outSize =
+ info.width * info.height * ((of == ULTRAHDR_OUTPUT_HDR_LINEAR) ? 8 : 4);
jpegr_uncompressed_struct decodedJpegR;
auto decodedRaw = std::make_unique<uint8_t[]>(outSize);
decodedJpegR.data = decodedRaw.get();
diff --git a/libs/ultrahdr/gainmapmath.cpp b/libs/ultrahdr/gainmapmath.cpp
index 8015a4e..ae9c4ca 100644
--- a/libs/ultrahdr/gainmapmath.cpp
+++ b/libs/ultrahdr/gainmapmath.cpp
@@ -168,7 +168,7 @@
// See IEC 61966-2-1, Equations F.5 and F.6.
float srgbInvOetfLUT(float e_gamma) {
- uint32_t value = static_cast<uint32_t>(e_gamma * kSrgbInvOETFNumEntries);
+ uint32_t value = static_cast<uint32_t>(e_gamma * (kSrgbInvOETFNumEntries - 1) + 0.5);
//TODO() : Remove once conversion modules have appropriate clamping in place
value = CLIP3(value, 0, kSrgbInvOETFNumEntries - 1);
return kSrgbInvOETF[value];
@@ -288,7 +288,7 @@
}
float hlgOetfLUT(float e) {
- uint32_t value = static_cast<uint32_t>(e * kHlgOETFNumEntries);
+ uint32_t value = static_cast<uint32_t>(e * (kHlgOETFNumEntries - 1) + 0.5);
//TODO() : Remove once conversion modules have appropriate clamping in place
value = CLIP3(value, 0, kHlgOETFNumEntries - 1);
@@ -315,7 +315,7 @@
}
float hlgInvOetfLUT(float e_gamma) {
- uint32_t value = static_cast<uint32_t>(e_gamma * kHlgInvOETFNumEntries);
+ uint32_t value = static_cast<uint32_t>(e_gamma * (kHlgInvOETFNumEntries - 1) + 0.5);
//TODO() : Remove once conversion modules have appropriate clamping in place
value = CLIP3(value, 0, kHlgInvOETFNumEntries - 1);
@@ -344,7 +344,7 @@
}
float pqOetfLUT(float e) {
- uint32_t value = static_cast<uint32_t>(e * kPqOETFNumEntries);
+ uint32_t value = static_cast<uint32_t>(e * (kPqOETFNumEntries - 1) + 0.5);
//TODO() : Remove once conversion modules have appropriate clamping in place
value = CLIP3(value, 0, kPqOETFNumEntries - 1);
@@ -376,7 +376,7 @@
}
float pqInvOetfLUT(float e_gamma) {
- uint32_t value = static_cast<uint32_t>(e_gamma * kPqInvOETFNumEntries);
+ uint32_t value = static_cast<uint32_t>(e_gamma * (kPqInvOETFNumEntries - 1) + 0.5);
//TODO() : Remove once conversion modules have appropriate clamping in place
value = CLIP3(value, 0, kPqInvOETFNumEntries - 1);
@@ -531,21 +531,21 @@
Color new_uv = (yuv1 + yuv2 + yuv3 + yuv4) / 4.0f;
- size_t pixel_y1_idx = x_chroma * 2 + y_chroma * 2 * image->width;
- size_t pixel_y2_idx = (x_chroma * 2 + 1) + y_chroma * 2 * image->width;
- size_t pixel_y3_idx = x_chroma * 2 + (y_chroma * 2 + 1) * image->width;
- size_t pixel_y4_idx = (x_chroma * 2 + 1) + (y_chroma * 2 + 1) * image->width;
+ size_t pixel_y1_idx = x_chroma * 2 + y_chroma * 2 * image->luma_stride;
+ size_t pixel_y2_idx = (x_chroma * 2 + 1) + y_chroma * 2 * image->luma_stride;
+ size_t pixel_y3_idx = x_chroma * 2 + (y_chroma * 2 + 1) * image->luma_stride;
+ size_t pixel_y4_idx = (x_chroma * 2 + 1) + (y_chroma * 2 + 1) * image->luma_stride;
uint8_t& y1_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y1_idx];
uint8_t& y2_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y2_idx];
uint8_t& y3_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y3_idx];
uint8_t& y4_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y4_idx];
- size_t pixel_count = image->width * image->height;
- size_t pixel_uv_idx = x_chroma + y_chroma * (image->width / 2);
+ size_t pixel_count = image->chroma_stride * image->height / 2;
+ size_t pixel_uv_idx = x_chroma + y_chroma * (image->chroma_stride);
- uint8_t& u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
- uint8_t& v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
+ uint8_t& u_uint = reinterpret_cast<uint8_t*>(image->chroma_data)[pixel_uv_idx];
+ uint8_t& v_uint = reinterpret_cast<uint8_t*>(image->chroma_data)[pixel_count + pixel_uv_idx];
y1_uint = static_cast<uint8_t>(CLIP3((yuv1.y * 255.0f + 0.5f), 0, 255));
y2_uint = static_cast<uint8_t>(CLIP3((yuv2.y * 255.0f + 0.5f), 0, 255));
@@ -598,14 +598,18 @@
}
Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
- size_t pixel_count = image->width * image->height;
+ uint8_t* luma_data = reinterpret_cast<uint8_t*>(image->data);
+ size_t luma_stride = image->luma_stride;
+ uint8_t* chroma_data = reinterpret_cast<uint8_t*>(image->chroma_data);
+ size_t chroma_stride = image->chroma_stride;
- size_t pixel_y_idx = x + y * image->width;
- size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
+ size_t offset_cr = chroma_stride * (image->height / 2);
+ size_t pixel_y_idx = x + y * luma_stride;
+ size_t pixel_chroma_idx = x / 2 + (y / 2) * chroma_stride;
- uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y_idx];
- uint8_t u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
- uint8_t v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
+ uint8_t y_uint = luma_data[pixel_y_idx];
+ uint8_t u_uint = chroma_data[pixel_chroma_idx];
+ uint8_t v_uint = chroma_data[offset_cr + pixel_chroma_idx];
// 128 bias for UV given we are using jpeglib; see:
// https://github.com/kornelski/libjpeg/blob/master/structure.doc
@@ -615,20 +619,10 @@
}
Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
- size_t luma_stride = image->luma_stride;
- size_t chroma_stride = image->chroma_stride;
uint16_t* luma_data = reinterpret_cast<uint16_t*>(image->data);
+ size_t luma_stride = image->luma_stride == 0 ? image->width : image->luma_stride;
uint16_t* chroma_data = reinterpret_cast<uint16_t*>(image->chroma_data);
-
- if (luma_stride == 0) {
- luma_stride = image->width;
- }
- if (chroma_stride == 0) {
- chroma_stride = luma_stride;
- }
- if (chroma_data == nullptr) {
- chroma_data = &reinterpret_cast<uint16_t*>(image->data)[luma_stride * image->height];
- }
+ size_t chroma_stride = image->chroma_stride;
size_t pixel_y_idx = y * luma_stride + x;
size_t pixel_u_idx = (y >> 1) * chroma_stride + (x & ~0x1);
diff --git a/libs/ultrahdr/include/ultrahdr/gainmapmath.h b/libs/ultrahdr/include/ultrahdr/gainmapmath.h
index 50b4d2f..9f1238f 100644
--- a/libs/ultrahdr/include/ultrahdr/gainmapmath.h
+++ b/libs/ultrahdr/include/ultrahdr/gainmapmath.h
@@ -172,7 +172,7 @@
}
float getGainFactor(float gain) {
- uint32_t idx = static_cast<uint32_t>(gain * (kGainFactorNumEntries - 1));
+ uint32_t idx = static_cast<uint32_t>(gain * (kGainFactorNumEntries - 1) + 0.5);
//TODO() : Remove once conversion modules have appropriate clamping in place
idx = CLIP3(idx, 0, kGainFactorNumEntries - 1);
return mGainTable[idx];
diff --git a/libs/ultrahdr/include/ultrahdr/jpegencoderhelper.h b/libs/ultrahdr/include/ultrahdr/jpegencoderhelper.h
index 2c6778e..9d06415 100644
--- a/libs/ultrahdr/include/ultrahdr/jpegencoderhelper.h
+++ b/libs/ultrahdr/include/ultrahdr/jpegencoderhelper.h
@@ -19,6 +19,7 @@
// We must include cstdio before jpeglib.h. It is a requirement of libjpeg.
#include <cstdio>
+#include <vector>
extern "C" {
#include <jerror.h>
@@ -26,10 +27,11 @@
}
#include <utils/Errors.h>
-#include <vector>
namespace android::ultrahdr {
+#define ALIGNM(x, m) ((((x) + ((m)-1)) / (m)) * (m))
+
/*
* Encapsulates a converter from raw image (YUV420planer or grey-scale) to JPEG format.
* This class is not thread-safe.
@@ -46,8 +48,9 @@
* ICC segment which will be added to the compressed image.
* Returns false if errors occur during compression.
*/
- bool compressImage(const void* image, int width, int height, int quality,
- const void* iccBuffer, unsigned int iccSize, bool isSingleChannel = false);
+ bool compressImage(const uint8_t* yBuffer, const uint8_t* uvBuffer, int width, int height,
+ int lumaStride, int chromaStride, int quality, const void* iccBuffer,
+ unsigned int iccSize);
/*
* Returns the compressed JPEG buffer pointer. This method must be called only after calling
@@ -66,6 +69,7 @@
* We must pass at least 16 scanlines according to libjpeg documentation.
*/
static const int kCompressBatchSize = 16;
+
private:
// initDestination(), emptyOutputBuffer() and emptyOutputBuffer() are callback functions to be
// passed into jpeg library.
@@ -75,15 +79,16 @@
static void outputErrorMessage(j_common_ptr cinfo);
// Returns false if errors occur.
- bool encode(const void* inYuv, int width, int height, int jpegQuality,
- const void* iccBuffer, unsigned int iccSize, bool isSingleChannel);
+ bool encode(const uint8_t* yBuffer, const uint8_t* uvBuffer, int width, int height,
+ int lumaStride, int chromaStride, int quality, const void* iccBuffer,
+ unsigned int iccSize);
void setJpegDestination(jpeg_compress_struct* cinfo);
void setJpegCompressStruct(int width, int height, int quality, jpeg_compress_struct* cinfo,
bool isSingleChannel);
// Returns false if errors occur.
- bool compress(jpeg_compress_struct* cinfo, const uint8_t* image, bool isSingleChannel);
- bool compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yuv);
- bool compressSingleChannel(jpeg_compress_struct* cinfo, const uint8_t* image);
+ bool compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yBuffer, const uint8_t* uvBuffer,
+ int lumaStride, int chromaStride);
+ bool compressY(jpeg_compress_struct* cinfo, const uint8_t* yBuffer, int lumaStride);
// The block size for encoded jpeg image buffer.
static const int kBlockSize = 16384;
diff --git a/libs/ultrahdr/include/ultrahdr/jpegr.h b/libs/ultrahdr/include/ultrahdr/jpegr.h
index f80496a..850cb32 100644
--- a/libs/ultrahdr/include/ultrahdr/jpegr.h
+++ b/libs/ultrahdr/include/ultrahdr/jpegr.h
@@ -17,9 +17,13 @@
#ifndef ANDROID_ULTRAHDR_JPEGR_H
#define ANDROID_ULTRAHDR_JPEGR_H
-#include "jpegencoderhelper.h"
-#include "jpegrerrorcode.h"
-#include "ultrahdr.h"
+#include <cstdint>
+#include <vector>
+
+#include "ultrahdr/jpegdecoderhelper.h"
+#include "ultrahdr/jpegencoderhelper.h"
+#include "ultrahdr/jpegrerrorcode.h"
+#include "ultrahdr/ultrahdr.h"
#ifndef FLT_MAX
#define FLT_MAX 0x1.fffffep127f
@@ -27,6 +31,27 @@
namespace android::ultrahdr {
+// The current JPEGR version that we encode to
+static const char* const kJpegrVersion = "1.0";
+
+// Map is quarter res / sixteenth size
+static const size_t kMapDimensionScaleFactor = 4;
+
+// Gain Map width is (image_width / kMapDimensionScaleFactor). If we were to
+// compress 420 GainMap in jpeg, then we need at least 2 samples. For Grayscale
+// 1 sample is sufficient. We are using 2 here anyways
+static const int kMinWidth = 2 * kMapDimensionScaleFactor;
+static const int kMinHeight = 2 * kMapDimensionScaleFactor;
+
+// Minimum Codec Unit(MCU) for 420 sub-sampling is decided by JPEG encoder by parameter
+// JpegEncoderHelper::kCompressBatchSize.
+// The width and height of image under compression is expected to be a multiple of MCU size.
+// If this criteria is not satisfied, padding is done.
+static const size_t kJpegBlock = JpegEncoderHelper::kCompressBatchSize;
+
+/*
+ * Holds information of jpegr image
+ */
struct jpegr_info_struct {
size_t width;
size_t height;
@@ -49,16 +74,19 @@
// Values below are optional
// Pointer to chroma data, if it's NULL, chroma plane is considered to be immediately
- // following after the luma plane.
- // Note: currently this feature is only supported for P010 image (HDR input).
+ // after the luma plane.
void* chroma_data = nullptr;
- // Strides of Y plane in number of pixels, using 0 to present uninitialized, must be
- // larger than or equal to luma width.
- // Note: currently this feature is only supported for P010 image (HDR input).
+ // Stride of Y plane in number of pixels. 0 indicates the member is uninitialized. If
+ // non-zero this value must be larger than or equal to luma width. If stride is
+ // uninitialized then it is assumed to be equal to luma width.
int luma_stride = 0;
- // Strides of UV plane in number of pixels, using 0 to present uninitialized, must be
- // larger than or equal to chroma width.
- // Note: currently this feature is only supported for P010 image (HDR input).
+ // Stride of UV plane in number of pixels.
+ // 1. If this handle points to P010 image then this value must be larger than
+ // or equal to luma width.
+ // 2. If this handle points to 420 image then this value must be larger than
+ // or equal to (luma width / 2).
+ // NOTE: if chroma_data is nullptr, chroma_stride is irrelevant. Just as the way,
+ // chroma_data is derived from luma ptr, chroma stride is derived from luma stride.
int chroma_stride = 0;
};
@@ -102,10 +130,10 @@
* Tonemap the HDR input to a SDR image, generate gain map from the HDR and SDR images,
* compress SDR YUV to 8-bit JPEG and append the gain map to the end of the compressed
* JPEG.
- * @param uncompressed_p010_image uncompressed HDR image in P010 color format
+ * @param p010_image_ptr uncompressed HDR image in P010 color format
* @param hdr_tf transfer function of the HDR image
* @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
- * represents the maximum available size of the desitination buffer, and it must be
+ * represents the maximum available size of the destination buffer, and it must be
* set before calling this method. If the encoded JPEGR size exceeds
* {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
* @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is
@@ -113,11 +141,8 @@
* @param exif pointer to the exif metadata.
* @return NO_ERROR if encoding succeeds, error code if error occurs.
*/
- status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
- ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest,
- int quality,
- jr_exif_ptr exif);
+ status_t encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, ultrahdr_transfer_function hdr_tf,
+ jr_compressed_ptr dest, int quality, jr_exif_ptr exif);
/*
* Encode API-1
@@ -126,8 +151,8 @@
* Generate gain map from the HDR and SDR inputs, compress SDR YUV to 8-bit JPEG and append
* the gain map to the end of the compressed JPEG. HDR and SDR inputs must be the same
* resolution. SDR input is assumed to use the sRGB transfer function.
- * @param uncompressed_p010_image uncompressed HDR image in P010 color format
- * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
+ * @param p010_image_ptr uncompressed HDR image in P010 color format
+ * @param yuv420_image_ptr uncompressed SDR image in YUV_420 color format
* @param hdr_tf transfer function of the HDR image
* @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
* represents the maximum available size of the desitination buffer, and it must be
@@ -138,11 +163,8 @@
* @param exif pointer to the exif metadata.
* @return NO_ERROR if encoding succeeds, error code if error occurs.
*/
- status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
- jr_uncompressed_ptr uncompressed_yuv_420_image,
- ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest,
- int quality,
+ status_t encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, jr_uncompressed_ptr yuv420_image_ptr,
+ ultrahdr_transfer_function hdr_tf, jr_compressed_ptr dest, int quality,
jr_exif_ptr exif);
/*
@@ -155,11 +177,11 @@
* compressed JPEG. Adds an ICC profile if one isn't present in the input JPEG image. HDR and
* SDR inputs must be the same resolution and color space. SDR image is assumed to use the sRGB
* transfer function.
- * @param uncompressed_p010_image uncompressed HDR image in P010 color format
- * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
- * Note: the SDR image must be the decoded version of the JPEG
- * input
- * @param compressed_jpeg_image compressed 8-bit JPEG image
+ * @param p010_image_ptr uncompressed HDR image in P010 color format
+ * @param yuv420_image_ptr uncompressed SDR image in YUV_420 color format
+ * @param yuv420jpg_image_ptr SDR image compressed in jpeg format
+ * Note: the compressed SDR image must be the compressed
+ * yuv420_image_ptr image in JPEG format.
* @param hdr_tf transfer function of the HDR image
* @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
* represents the maximum available size of the desitination buffer, and it must be
@@ -167,10 +189,8 @@
* {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
* @return NO_ERROR if encoding succeeds, error code if error occurs.
*/
- status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
- jr_uncompressed_ptr uncompressed_yuv_420_image,
- jr_compressed_ptr compressed_jpeg_image,
- ultrahdr_transfer_function hdr_tf,
+ status_t encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, jr_uncompressed_ptr yuv420_image_ptr,
+ jr_compressed_ptr yuv420jpg_image_ptr, ultrahdr_transfer_function hdr_tf,
jr_compressed_ptr dest);
/*
@@ -183,8 +203,8 @@
* and the decoded SDR result, append the gain map to the end of the compressed JPEG. Adds an
* ICC profile if one isn't present in the input JPEG image. HDR and SDR inputs must be the same
* resolution. JPEG image is assumed to use the sRGB transfer function.
- * @param uncompressed_p010_image uncompressed HDR image in P010 color format
- * @param compressed_jpeg_image compressed 8-bit JPEG image
+ * @param p010_image_ptr uncompressed HDR image in P010 color format
+ * @param yuv420jpg_image_ptr SDR image compressed in jpeg format
* @param hdr_tf transfer function of the HDR image
* @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
* represents the maximum available size of the desitination buffer, and it must be
@@ -192,10 +212,8 @@
* {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
* @return NO_ERROR if encoding succeeds, error code if error occurs.
*/
- status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
- jr_compressed_ptr compressed_jpeg_image,
- ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest);
+ status_t encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, jr_compressed_ptr yuv420jpg_image_ptr,
+ ultrahdr_transfer_function hdr_tf, jr_compressed_ptr dest);
/*
* Encode API-4
@@ -203,8 +221,8 @@
*
* Assemble the primary JPEG image, the gain map and the metadata to JPEG/R format. Adds an ICC
* profile if one isn't present in the input JPEG image.
- * @param compressed_jpeg_image compressed 8-bit JPEG image
- * @param compressed_gainmap compressed 8-bit JPEG single channel image
+ * @param yuv420jpg_image_ptr SDR image compressed in jpeg format
+ * @param gainmapjpg_image_ptr gain map image compressed in jpeg format
* @param metadata metadata to be written in XMP of the primary jpeg
* @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
* represents the maximum available size of the desitination buffer, and it must be
@@ -212,9 +230,8 @@
* {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
* @return NO_ERROR if encoding succeeds, error code if error occurs.
*/
- status_t encodeJPEGR(jr_compressed_ptr compressed_jpeg_image,
- jr_compressed_ptr compressed_gainmap,
- ultrahdr_metadata_ptr metadata,
+ status_t encodeJPEGR(jr_compressed_ptr yuv420jpg_image_ptr,
+ jr_compressed_ptr gainmapjpg_image_ptr, ultrahdr_metadata_ptr metadata,
jr_compressed_ptr dest);
/*
@@ -227,8 +244,7 @@
*
* This method only supports single gain map metadata values for fields that allow multi-channel
* metadata values.
- *
- * @param compressed_jpegr_image compressed JPEGR image.
+ * @param jpegr_image_ptr compressed JPEGR image.
* @param dest destination of the uncompressed JPEGR image.
* @param max_display_boost (optional) the maximum available boost supported by a display,
* the value must be greater than or equal to 1.0.
@@ -248,57 +264,55 @@
----------------------------------------------------------------------
| JPEGR_OUTPUT_HDR_HLG | RGBA_1010102 HLG |
----------------------------------------------------------------------
- * @param gain_map destination of the decoded gain map. The default value is NULL where
- the decoder will do nothing about it. If configured not NULL the decoder
- will write the decoded gain_map data into this structure. The format
- is defined in {@code jpegr_uncompressed_struct}.
+ * @param gainmap_image_ptr destination of the decoded gain map. The default value is NULL
+ where the decoder will do nothing about it. If configured not NULL
+ the decoder will write the decoded gain_map data into this
+ structure. The format is defined in
+ {@code jpegr_uncompressed_struct}.
* @param metadata destination of the decoded metadata. The default value is NULL where the
decoder will do nothing about it. If configured not NULL the decoder will
write metadata into this structure. the format of metadata is defined in
{@code ultrahdr_metadata_struct}.
* @return NO_ERROR if decoding succeeds, error code if error occurs.
*/
- status_t decodeJPEGR(jr_compressed_ptr compressed_jpegr_image,
- jr_uncompressed_ptr dest,
- float max_display_boost = FLT_MAX,
- jr_exif_ptr exif = nullptr,
+ status_t decodeJPEGR(jr_compressed_ptr jpegr_image_ptr, jr_uncompressed_ptr dest,
+ float max_display_boost = FLT_MAX, jr_exif_ptr exif = nullptr,
ultrahdr_output_format output_format = ULTRAHDR_OUTPUT_HDR_LINEAR,
- jr_uncompressed_ptr gain_map = nullptr,
+ jr_uncompressed_ptr gainmap_image_ptr = nullptr,
ultrahdr_metadata_ptr metadata = nullptr);
/*
- * Gets Info from JPEGR file without decoding it.
- *
- * This method only supports single gain map metadata values for fields that allow multi-channel
- * metadata values.
- *
- * The output is filled jpegr_info structure
- * @param compressed_jpegr_image compressed JPEGR image
- * @param jpegr_info pointer to output JPEGR info. Members of jpegr_info
- * are owned by the caller
- * @return NO_ERROR if JPEGR parsing succeeds, error code otherwise
- */
- status_t getJPEGRInfo(jr_compressed_ptr compressed_jpegr_image,
- jr_info_ptr jpegr_info);
+ * Gets Info from JPEGR file without decoding it.
+ *
+ * This method only supports single gain map metadata values for fields that allow multi-channel
+ * metadata values.
+ *
+ * The output is filled jpegr_info structure
+ * @param jpegr_image_ptr compressed JPEGR image
+ * @param jpeg_image_info_ptr pointer to jpegr info struct. Members of jpegr_info
+ * are owned by the caller
+ * @return NO_ERROR if JPEGR parsing succeeds, error code otherwise
+ */
+ status_t getJPEGRInfo(jr_compressed_ptr jpegr_image_ptr, jr_info_ptr jpeg_image_info_ptr);
+
protected:
/*
* This method is called in the encoding pipeline. It will take the uncompressed 8-bit and
* 10-bit yuv images as input, and calculate the uncompressed gain map. The input images
* must be the same resolution. The SDR input is assumed to use the sRGB transfer function.
*
- * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
- * @param uncompressed_p010_image uncompressed HDR image in P010 color format
+ * @param yuv420_image_ptr uncompressed SDR image in YUV_420 color format
+ * @param p010_image_ptr uncompressed HDR image in P010 color format
* @param hdr_tf transfer function of the HDR image
- * @param dest gain map; caller responsible for memory of data
- * @param metadata max_content_boost is filled in
+ * @param metadata everything but "version" is filled in this struct
+ * @param dest location at which gain map image is stored (caller responsible for memory
+ of data).
* @param sdr_is_601 if true, then use BT.601 decoding of YUV regardless of SDR image gamut
* @return NO_ERROR if calculation succeeds, error code if error occurs.
*/
- status_t generateGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
- jr_uncompressed_ptr uncompressed_p010_image,
- ultrahdr_transfer_function hdr_tf,
- ultrahdr_metadata_ptr metadata,
- jr_uncompressed_ptr dest,
+ status_t generateGainMap(jr_uncompressed_ptr yuv420_image_ptr,
+ jr_uncompressed_ptr p010_image_ptr, ultrahdr_transfer_function hdr_tf,
+ ultrahdr_metadata_ptr metadata, jr_uncompressed_ptr dest,
bool sdr_is_601 = false);
/*
@@ -309,8 +323,8 @@
* The SDR image is assumed to use the sRGB transfer function. The SDR image is also assumed to
* be a decoded JPEG for the purpose of YUV interpration.
*
- * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
- * @param uncompressed_gain_map uncompressed gain map
+ * @param yuv420_image_ptr uncompressed SDR image in YUV_420 color format
+ * @param gainmap_image_ptr pointer to uncompressed gain map image struct.
* @param metadata JPEG/R metadata extracted from XMP.
* @param output_format flag for setting output color format. if set to
* {@code JPEGR_OUTPUT_SDR}, decoder will only decode the primary image
@@ -319,35 +333,33 @@
* @param dest reconstructed HDR image
* @return NO_ERROR if calculation succeeds, error code if error occurs.
*/
- status_t applyGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
- jr_uncompressed_ptr uncompressed_gain_map,
- ultrahdr_metadata_ptr metadata,
- ultrahdr_output_format output_format,
- float max_display_boost,
+ status_t applyGainMap(jr_uncompressed_ptr yuv420_image_ptr,
+ jr_uncompressed_ptr gainmap_image_ptr, ultrahdr_metadata_ptr metadata,
+ ultrahdr_output_format output_format, float max_display_boost,
jr_uncompressed_ptr dest);
private:
/*
* This method is called in the encoding pipeline. It will encode the gain map.
*
- * @param uncompressed_gain_map uncompressed gain map
- * @param resource to compress gain map
+ * @param gainmap_image_ptr pointer to uncompressed gain map image struct
+ * @param jpeg_enc_obj_ptr helper resource to compress gain map
* @return NO_ERROR if encoding succeeds, error code if error occurs.
*/
- status_t compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
- JpegEncoderHelper* jpeg_encoder);
+ status_t compressGainMap(jr_uncompressed_ptr gainmap_image_ptr,
+ JpegEncoderHelper* jpeg_enc_obj_ptr);
/*
- * This methoud is called to separate primary image and gain map image from JPEGR
+ * This method is called to separate primary image and gain map image from JPEGR
*
- * @param compressed_jpegr_image compressed JPEGR image
- * @param primary_image destination of primary image
- * @param gain_map destination of compressed gain map
+ * @param jpegr_image_ptr pointer to compressed JPEGR image.
+ * @param primary_jpg_image_ptr destination of primary image
+ * @param gainmap_jpg_image_ptr destination of compressed gain map image
* @return NO_ERROR if calculation succeeds, error code if error occurs.
- */
- status_t extractPrimaryImageAndGainMap(jr_compressed_ptr compressed_jpegr_image,
- jr_compressed_ptr primary_image,
- jr_compressed_ptr gain_map);
+ */
+ status_t extractPrimaryImageAndGainMap(jr_compressed_ptr jpegr_image_ptr,
+ jr_compressed_ptr primary_jpg_image_ptr,
+ jr_compressed_ptr gainmap_jpg_image_ptr);
/*
* This method is called in the encoding pipeline. It will take the standard 8-bit JPEG image,
@@ -358,8 +370,9 @@
* API-3 this parameter is null, but the primary image in JPEG/R may still have EXIF as long as
* the input JPEG has EXIF.
*
- * @param compressed_jpeg_image compressed 8-bit JPEG image
- * @param compress_gain_map compressed recover map
+
+ * @param primary_jpg_image_ptr destination of primary image
+ * @param gainmap_jpg_image_ptr destination of compressed gain map image
* @param (nullable) exif EXIF package
* @param (nullable) icc ICC package
* @param icc_size length in bytes of ICC package
@@ -367,22 +380,18 @@
* @param dest compressed JPEGR image
* @return NO_ERROR if calculation succeeds, error code if error occurs.
*/
- status_t appendGainMap(jr_compressed_ptr compressed_jpeg_image,
- jr_compressed_ptr compressed_gain_map,
- jr_exif_ptr exif,
- void* icc, size_t icc_size,
- ultrahdr_metadata_ptr metadata,
- jr_compressed_ptr dest);
+ status_t appendGainMap(jr_compressed_ptr primary_jpg_image_ptr,
+ jr_compressed_ptr gainmap_jpg_image_ptr, jr_exif_ptr exif, void* icc,
+ size_t icc_size, ultrahdr_metadata_ptr metadata, jr_compressed_ptr dest);
/*
* This method will tone map a HDR image to an SDR image.
*
- * @param src (input) uncompressed P010 image
- * @param dest (output) tone mapping result as a YUV_420 image
- * @return NO_ERROR if calculation succeeds, error code if error occurs.
+ * @param src pointer to uncompressed HDR image struct. HDR image is expected to be
+ * in p010 color format
+ * @param dest pointer to store tonemapped SDR image
*/
- status_t toneMap(jr_uncompressed_ptr src,
- jr_uncompressed_ptr dest);
+ status_t toneMap(jr_uncompressed_ptr src, jr_uncompressed_ptr dest);
/*
* This method will convert a YUV420 image from one YUV encoding to another in-place (eg.
@@ -396,15 +405,15 @@
* @param dest_encoding output YUV encoding
* @return NO_ERROR if calculation succeeds, error code if error occurs.
*/
- status_t convertYuv(jr_uncompressed_ptr image,
- ultrahdr_color_gamut src_encoding,
+ status_t convertYuv(jr_uncompressed_ptr image, ultrahdr_color_gamut src_encoding,
ultrahdr_color_gamut dest_encoding);
/*
* This method will check the validity of the input arguments.
*
- * @param uncompressed_p010_image uncompressed HDR image in P010 color format
- * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
+ * @param p010_image_ptr uncompressed HDR image in P010 color format
+ * @param yuv420_image_ptr pointer to uncompressed SDR image struct. HDR image is expected to
+ * be in 420p color format
* @param hdr_tf transfer function of the HDR image
* @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
* represents the maximum available size of the desitination buffer, and it must be
@@ -412,32 +421,30 @@
* {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
* @return NO_ERROR if the input args are valid, error code is not valid.
*/
- status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
- jr_uncompressed_ptr uncompressed_yuv_420_image,
- ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest);
+ status_t areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr,
+ jr_uncompressed_ptr yuv420_image_ptr,
+ ultrahdr_transfer_function hdr_tf, jr_compressed_ptr dest_ptr);
/*
* This method will check the validity of the input arguments.
*
- * @param uncompressed_p010_image uncompressed HDR image in P010 color format
- * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
+ * @param p010_image_ptr uncompressed HDR image in P010 color format
+ * @param yuv420_image_ptr pointer to uncompressed SDR image struct. HDR image is expected to
+ * be in 420p color format
* @param hdr_tf transfer function of the HDR image
* @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
- * represents the maximum available size of the desitination buffer, and it must be
+ * represents the maximum available size of the destination buffer, and it must be
* set before calling this method. If the encoded JPEGR size exceeds
* {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
* @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is
* the highest quality
* @return NO_ERROR if the input args are valid, error code is not valid.
*/
- status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
- jr_uncompressed_ptr uncompressed_yuv_420_image,
- ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest,
- int quality);
+ status_t areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr,
+ jr_uncompressed_ptr yuv420_image_ptr,
+ ultrahdr_transfer_function hdr_tf, jr_compressed_ptr dest,
+ int quality);
};
-
} // namespace android::ultrahdr
#endif // ANDROID_ULTRAHDR_JPEGR_H
diff --git a/libs/ultrahdr/include/ultrahdr/ultrahdr.h b/libs/ultrahdr/include/ultrahdr/ultrahdr.h
index 17cc971..66f7088 100644
--- a/libs/ultrahdr/include/ultrahdr/ultrahdr.h
+++ b/libs/ultrahdr/include/ultrahdr/ultrahdr.h
@@ -17,6 +17,8 @@
#ifndef ANDROID_ULTRAHDR_ULTRAHDR_H
#define ANDROID_ULTRAHDR_ULTRAHDR_H
+#include <string>
+
namespace android::ultrahdr {
// Color gamuts for image data
typedef enum {
diff --git a/libs/ultrahdr/jpegdecoderhelper.cpp b/libs/ultrahdr/jpegdecoderhelper.cpp
index fef5444..33bf9ef 100644
--- a/libs/ultrahdr/jpegdecoderhelper.cpp
+++ b/libs/ultrahdr/jpegdecoderhelper.cpp
@@ -227,10 +227,20 @@
mHeight = cinfo.image_height;
if (decodeToRGBA) {
- if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
- // We don't intend to support decoding grayscale to RGBA
- status = false;
- ALOGE("%s: decoding grayscale to RGBA is unsupported", __func__);
+ // The primary image is expected to be yuv420 sampling
+ if (cinfo.jpeg_color_space != JCS_YCbCr) {
+ status = false;
+ ALOGE("%s: decodeToRGBA unexpected jpeg color space ", __func__);
+ goto CleanUp;
+ }
+ if (cinfo.comp_info[0].h_samp_factor != 2 ||
+ cinfo.comp_info[1].h_samp_factor != 1 ||
+ cinfo.comp_info[2].h_samp_factor != 1 ||
+ cinfo.comp_info[0].v_samp_factor != 2 ||
+ cinfo.comp_info[1].v_samp_factor != 1 ||
+ cinfo.comp_info[2].v_samp_factor != 1 ) {
+ status = false;
+ ALOGE("%s: decodeToRGBA unexpected primary image sub-sampling", __func__);
goto CleanUp;
}
// 4 bytes per pixel
@@ -251,12 +261,16 @@
mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 3 / 2, 0);
} else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
mResultBuffer.resize(cinfo.image_width * cinfo.image_height, 0);
+ } else {
+ status = false;
+ ALOGE("%s: decodeToYUV unexpected jpeg color space", __func__);
+ goto CleanUp;
}
cinfo.out_color_space = cinfo.jpeg_color_space;
cinfo.raw_data_out = TRUE;
}
- cinfo.dct_method = JDCT_IFAST;
+ cinfo.dct_method = JDCT_ISLOW;
jpeg_start_decompress(&cinfo);
diff --git a/libs/ultrahdr/jpegencoderhelper.cpp b/libs/ultrahdr/jpegencoderhelper.cpp
index a03547b..13ae742 100644
--- a/libs/ultrahdr/jpegencoderhelper.cpp
+++ b/libs/ultrahdr/jpegencoderhelper.cpp
@@ -14,38 +14,35 @@
* limitations under the License.
*/
+#include <cstring>
+#include <memory>
+#include <vector>
+
#include <ultrahdr/jpegencoderhelper.h>
-
#include <utils/Log.h>
-#include <errno.h>
-
namespace android::ultrahdr {
-#define ALIGNM(x, m) ((((x) + ((m) - 1)) / (m)) * (m))
-
// The destination manager that can access |mResultBuffer| in JpegEncoderHelper.
struct destination_mgr {
-public:
struct jpeg_destination_mgr mgr;
JpegEncoderHelper* encoder;
};
-JpegEncoderHelper::JpegEncoderHelper() {
-}
+JpegEncoderHelper::JpegEncoderHelper() {}
-JpegEncoderHelper::~JpegEncoderHelper() {
-}
+JpegEncoderHelper::~JpegEncoderHelper() {}
-bool JpegEncoderHelper::compressImage(const void* image, int width, int height, int quality,
- const void* iccBuffer, unsigned int iccSize,
- bool isSingleChannel) {
+bool JpegEncoderHelper::compressImage(const uint8_t* yBuffer, const uint8_t* uvBuffer, int width,
+ int height, int lumaStride, int chromaStride, int quality,
+ const void* iccBuffer, unsigned int iccSize) {
mResultBuffer.clear();
- if (!encode(image, width, height, quality, iccBuffer, iccSize, isSingleChannel)) {
+ if (!encode(yBuffer, uvBuffer, width, height, lumaStride, chromaStride, quality, iccBuffer,
+ iccSize)) {
return false;
}
- ALOGI("Compressed JPEG: %d[%dx%d] -> %zu bytes",
- (width * height * 12) / 8, width, height, mResultBuffer.size());
+ ALOGI("Compressed JPEG: %d[%dx%d] -> %zu bytes", (width * height * 12) / 8, width, height,
+ mResultBuffer.size());
return true;
}
@@ -85,29 +82,28 @@
char buffer[JMSG_LENGTH_MAX];
/* Create the message */
- (*cinfo->err->format_message) (cinfo, buffer);
+ (*cinfo->err->format_message)(cinfo, buffer);
ALOGE("%s\n", buffer);
}
-bool JpegEncoderHelper::encode(const void* image, int width, int height, int jpegQuality,
- const void* iccBuffer, unsigned int iccSize, bool isSingleChannel) {
+bool JpegEncoderHelper::encode(const uint8_t* yBuffer, const uint8_t* uvBuffer, int width,
+ int height, int lumaStride, int chromaStride, int quality,
+ const void* iccBuffer, unsigned int iccSize) {
jpeg_compress_struct cinfo;
jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
- // Override output_message() to print error log with ALOGE().
cinfo.err->output_message = &outputErrorMessage;
jpeg_create_compress(&cinfo);
setJpegDestination(&cinfo);
-
- setJpegCompressStruct(width, height, jpegQuality, &cinfo, isSingleChannel);
+ setJpegCompressStruct(width, height, quality, &cinfo, uvBuffer == nullptr);
jpeg_start_compress(&cinfo, TRUE);
-
if (iccBuffer != nullptr && iccSize > 0) {
jpeg_write_marker(&cinfo, JPEG_APP0 + 2, static_cast<const JOCTET*>(iccBuffer), iccSize);
}
-
- bool status = compress(&cinfo, static_cast<const uint8_t*>(image), isSingleChannel);
+ bool status = cinfo.num_components == 1
+ ? compressY(&cinfo, yBuffer, lumaStride)
+ : compressYuv(&cinfo, yBuffer, uvBuffer, lumaStride, chromaStride);
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
@@ -115,8 +111,9 @@
}
void JpegEncoderHelper::setJpegDestination(jpeg_compress_struct* cinfo) {
- destination_mgr* dest = static_cast<struct destination_mgr *>((*cinfo->mem->alloc_small) (
- (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(destination_mgr)));
+ destination_mgr* dest = static_cast<struct destination_mgr*>(
+ (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
+ sizeof(destination_mgr)));
dest->encoder = this;
dest->mgr.init_destination = &initDestination;
dest->mgr.empty_output_buffer = &emptyOutputBuffer;
@@ -125,59 +122,40 @@
}
void JpegEncoderHelper::setJpegCompressStruct(int width, int height, int quality,
- jpeg_compress_struct* cinfo, bool isSingleChannel) {
+ jpeg_compress_struct* cinfo, bool isSingleChannel) {
cinfo->image_width = width;
cinfo->image_height = height;
- if (isSingleChannel) {
- cinfo->input_components = 1;
- cinfo->in_color_space = JCS_GRAYSCALE;
- } else {
- cinfo->input_components = 3;
- cinfo->in_color_space = JCS_YCbCr;
- }
+ cinfo->input_components = isSingleChannel ? 1 : 3;
+ cinfo->in_color_space = isSingleChannel ? JCS_GRAYSCALE : JCS_YCbCr;
jpeg_set_defaults(cinfo);
-
jpeg_set_quality(cinfo, quality, TRUE);
- jpeg_set_colorspace(cinfo, isSingleChannel ? JCS_GRAYSCALE : JCS_YCbCr);
cinfo->raw_data_in = TRUE;
- cinfo->dct_method = JDCT_IFAST;
-
- if (!isSingleChannel) {
- // Configure sampling factors. The sampling factor is JPEG subsampling 420 because the
- // source format is YUV420.
- cinfo->comp_info[0].h_samp_factor = 2;
- cinfo->comp_info[0].v_samp_factor = 2;
- cinfo->comp_info[1].h_samp_factor = 1;
- cinfo->comp_info[1].v_samp_factor = 1;
- cinfo->comp_info[2].h_samp_factor = 1;
- cinfo->comp_info[2].v_samp_factor = 1;
+ cinfo->dct_method = JDCT_ISLOW;
+ cinfo->comp_info[0].h_samp_factor = cinfo->in_color_space == JCS_GRAYSCALE ? 1 : 2;
+ cinfo->comp_info[0].v_samp_factor = cinfo->in_color_space == JCS_GRAYSCALE ? 1 : 2;
+ for (int i = 1; i < cinfo->num_components; i++) {
+ cinfo->comp_info[i].h_samp_factor = 1;
+ cinfo->comp_info[i].v_samp_factor = 1;
}
}
-bool JpegEncoderHelper::compress(
- jpeg_compress_struct* cinfo, const uint8_t* image, bool isSingleChannel) {
- if (isSingleChannel) {
- return compressSingleChannel(cinfo, image);
- }
- return compressYuv(cinfo, image);
-}
-
-bool JpegEncoderHelper::compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yuv) {
+bool JpegEncoderHelper::compressYuv(jpeg_compress_struct* cinfo, const uint8_t* yBuffer,
+ const uint8_t* uvBuffer, int lumaStride, int chromaStride) {
JSAMPROW y[kCompressBatchSize];
JSAMPROW cb[kCompressBatchSize / 2];
JSAMPROW cr[kCompressBatchSize / 2];
- JSAMPARRAY planes[3] {y, cb, cr};
+ JSAMPARRAY planes[3]{y, cb, cr};
- size_t y_plane_size = cinfo->image_width * cinfo->image_height;
- size_t uv_plane_size = y_plane_size / 4;
- uint8_t* y_plane = const_cast<uint8_t*>(yuv);
- uint8_t* u_plane = const_cast<uint8_t*>(yuv + y_plane_size);
- uint8_t* v_plane = const_cast<uint8_t*>(yuv + y_plane_size + uv_plane_size);
+ size_t y_plane_size = lumaStride * cinfo->image_height;
+ size_t u_plane_size = chromaStride * cinfo->image_height / 2;
+ uint8_t* y_plane = const_cast<uint8_t*>(yBuffer);
+ uint8_t* u_plane = const_cast<uint8_t*>(uvBuffer);
+ uint8_t* v_plane = const_cast<uint8_t*>(u_plane + u_plane_size);
std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
memset(empty.get(), 0, cinfo->image_width);
const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
- const bool is_width_aligned = (aligned_width == cinfo->image_width);
+ const bool need_padding = (lumaStride < aligned_width);
std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
uint8_t* y_plane_intrm = nullptr;
uint8_t* u_plane_intrm = nullptr;
@@ -186,7 +164,7 @@
JSAMPROW cb_intrm[kCompressBatchSize / 2];
JSAMPROW cr_intrm[kCompressBatchSize / 2];
JSAMPARRAY planes_intrm[3]{y_intrm, cb_intrm, cr_intrm};
- if (!is_width_aligned) {
+ if (need_padding) {
size_t mcu_row_size = aligned_width * kCompressBatchSize * 3 / 2;
buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
y_plane_intrm = buffer_intrm.get();
@@ -211,11 +189,11 @@
for (int i = 0; i < kCompressBatchSize; ++i) {
size_t scanline = cinfo->next_scanline + i;
if (scanline < cinfo->image_height) {
- y[i] = y_plane + scanline * cinfo->image_width;
+ y[i] = y_plane + scanline * lumaStride;
} else {
y[i] = empty.get();
}
- if (!is_width_aligned) {
+ if (need_padding) {
memcpy(y_intrm[i], y[i], cinfo->image_width);
}
}
@@ -223,18 +201,18 @@
for (int i = 0; i < kCompressBatchSize / 2; ++i) {
size_t scanline = cinfo->next_scanline / 2 + i;
if (scanline < cinfo->image_height / 2) {
- int offset = scanline * (cinfo->image_width / 2);
+ int offset = scanline * chromaStride;
cb[i] = u_plane + offset;
cr[i] = v_plane + offset;
} else {
cb[i] = cr[i] = empty.get();
}
- if (!is_width_aligned) {
+ if (need_padding) {
memcpy(cb_intrm[i], cb[i], cinfo->image_width / 2);
memcpy(cr_intrm[i], cr[i], cinfo->image_width / 2);
}
}
- int processed = jpeg_write_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
+ int processed = jpeg_write_raw_data(cinfo, need_padding ? planes_intrm : planes,
kCompressBatchSize);
if (processed != kCompressBatchSize) {
ALOGE("Number of processed lines does not equal input lines.");
@@ -244,22 +222,23 @@
return true;
}
-bool JpegEncoderHelper::compressSingleChannel(jpeg_compress_struct* cinfo, const uint8_t* image) {
+bool JpegEncoderHelper::compressY(jpeg_compress_struct* cinfo, const uint8_t* yBuffer,
+ int lumaStride) {
JSAMPROW y[kCompressBatchSize];
- JSAMPARRAY planes[1] {y};
+ JSAMPARRAY planes[1]{y};
- uint8_t* y_plane = const_cast<uint8_t*>(image);
+ uint8_t* y_plane = const_cast<uint8_t*>(yBuffer);
std::unique_ptr<uint8_t[]> empty = std::make_unique<uint8_t[]>(cinfo->image_width);
memset(empty.get(), 0, cinfo->image_width);
const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize);
- bool is_width_aligned = (aligned_width == cinfo->image_width);
+ const bool need_padding = (lumaStride < aligned_width);
std::unique_ptr<uint8_t[]> buffer_intrm = nullptr;
uint8_t* y_plane_intrm = nullptr;
uint8_t* u_plane_intrm = nullptr;
JSAMPROW y_intrm[kCompressBatchSize];
JSAMPARRAY planes_intrm[]{y_intrm};
- if (!is_width_aligned) {
+ if (need_padding) {
size_t mcu_row_size = aligned_width * kCompressBatchSize;
buffer_intrm = std::make_unique<uint8_t[]>(mcu_row_size);
y_plane_intrm = buffer_intrm.get();
@@ -273,15 +252,15 @@
for (int i = 0; i < kCompressBatchSize; ++i) {
size_t scanline = cinfo->next_scanline + i;
if (scanline < cinfo->image_height) {
- y[i] = y_plane + scanline * cinfo->image_width;
+ y[i] = y_plane + scanline * lumaStride;
} else {
y[i] = empty.get();
}
- if (!is_width_aligned) {
+ if (need_padding) {
memcpy(y_intrm[i], y[i], cinfo->image_width);
}
}
- int processed = jpeg_write_raw_data(cinfo, is_width_aligned ? planes : planes_intrm,
+ int processed = jpeg_write_raw_data(cinfo, need_padding ? planes_intrm : planes,
kCompressBatchSize);
if (processed != kCompressBatchSize / 2) {
ALOGE("Number of processed lines does not equal input lines.");
@@ -291,4 +270,4 @@
return true;
}
-} // namespace ultrahdr
+} // namespace android::ultrahdr
diff --git a/libs/ultrahdr/jpegr.cpp b/libs/ultrahdr/jpegr.cpp
index 5a601bd..dc439d7 100644
--- a/libs/ultrahdr/jpegr.cpp
+++ b/libs/ultrahdr/jpegr.cpp
@@ -14,31 +14,26 @@
* limitations under the License.
*/
-#include <ultrahdr/jpegr.h>
-#include <ultrahdr/jpegencoderhelper.h>
-#include <ultrahdr/jpegdecoderhelper.h>
-#include <ultrahdr/gainmapmath.h>
-#include <ultrahdr/jpegrutils.h>
-#include <ultrahdr/multipictureformat.h>
-#include <ultrahdr/icc.h>
-
-#include <image_io/jpeg/jpeg_marker.h>
-#include <image_io/jpeg/jpeg_info.h>
-#include <image_io/jpeg/jpeg_scanner.h>
-#include <image_io/jpeg/jpeg_info_builder.h>
-#include <image_io/base/data_segment_data_source.h>
-#include <utils/Log.h>
-
-#include <map>
-#include <memory>
-#include <sstream>
-#include <string>
#include <cmath>
#include <condition_variable>
#include <deque>
+#include <memory>
#include <mutex>
#include <thread>
-#include <unistd.h>
+
+#include <ultrahdr/gainmapmath.h>
+#include <ultrahdr/icc.h>
+#include <ultrahdr/jpegr.h>
+#include <ultrahdr/jpegrutils.h>
+#include <ultrahdr/multipictureformat.h>
+
+#include <image_io/base/data_segment_data_source.h>
+#include <image_io/jpeg/jpeg_info.h>
+#include <image_io/jpeg/jpeg_info_builder.h>
+#include <image_io/jpeg/jpeg_marker.h>
+#include <image_io/jpeg/jpeg_scanner.h>
+
+#include <utils/Log.h>
using namespace std;
using namespace photos_editing_formats::image_io;
@@ -60,25 +55,6 @@
} \
}
-// The current JPEGR version that we encode to
-static const char* const kJpegrVersion = "1.0";
-
-// Map is quarter res / sixteenth size
-static const size_t kMapDimensionScaleFactor = 4;
-
-// Gain Map width is (image_width / kMapDimensionScaleFactor). If we were to
-// compress 420 GainMap in jpeg, then we need at least 2 samples. For Grayscale
-// 1 sample is sufficient. We are using 2 here anyways
-static const int kMinWidth = 2 * kMapDimensionScaleFactor;
-static const int kMinHeight = 2 * kMapDimensionScaleFactor;
-
-// JPEG block size.
-// JPEG encoding / decoding will require block based DCT transform 16 x 16 for luma,
-// and 8 x 8 for chroma.
-// Width must be 16 dividable for luma, and 8 dividable for chroma.
-// If this criteria is not facilitated, we will pad zeros based to each line on the
-// required block size.
-static const size_t kJpegBlock = JpegEncoderHelper::kCompressBatchSize;
// JPEG compress quality (0 ~ 100) for gain map
static const int kMapCompressQuality = 85;
@@ -96,184 +72,176 @@
return cpuCoreCount;
}
-status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
- jr_uncompressed_ptr uncompressed_yuv_420_image,
+status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr,
+ jr_uncompressed_ptr yuv420_image_ptr,
ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest) {
- if (uncompressed_p010_image == nullptr || uncompressed_p010_image->data == nullptr) {
- ALOGE("received nullptr for uncompressed p010 image");
+ jr_compressed_ptr dest_ptr) {
+ if (p010_image_ptr == nullptr || p010_image_ptr->data == nullptr) {
+ ALOGE("Received nullptr for input p010 image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (uncompressed_p010_image->width % 2 != 0
- || uncompressed_p010_image->height % 2 != 0) {
- ALOGE("Image dimensions cannot be odd, image dimensions %dx%d",
- uncompressed_p010_image->width, uncompressed_p010_image->height);
+ if (p010_image_ptr->width % 2 != 0 || p010_image_ptr->height % 2 != 0) {
+ ALOGE("Image dimensions cannot be odd, image dimensions %dx%d", p010_image_ptr->width,
+ p010_image_ptr->height);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
- if (uncompressed_p010_image->width < kMinWidth
- || uncompressed_p010_image->height < kMinHeight) {
- ALOGE("Image dimensions cannot be less than %dx%d, image dimensions %dx%d",
- kMinWidth, kMinHeight, uncompressed_p010_image->width, uncompressed_p010_image->height);
+ if (p010_image_ptr->width < kMinWidth || p010_image_ptr->height < kMinHeight) {
+ ALOGE("Image dimensions cannot be less than %dx%d, image dimensions %dx%d", kMinWidth,
+ kMinHeight, p010_image_ptr->width, p010_image_ptr->height);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
- if (uncompressed_p010_image->width > kMaxWidth
- || uncompressed_p010_image->height > kMaxHeight) {
- ALOGE("Image dimensions cannot be larger than %dx%d, image dimensions %dx%d",
- kMaxWidth, kMaxHeight, uncompressed_p010_image->width, uncompressed_p010_image->height);
+ if (p010_image_ptr->width > kMaxWidth || p010_image_ptr->height > kMaxHeight) {
+ ALOGE("Image dimensions cannot be larger than %dx%d, image dimensions %dx%d", kMaxWidth,
+ kMaxHeight, p010_image_ptr->width, p010_image_ptr->height);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
- if (uncompressed_p010_image->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED
- || uncompressed_p010_image->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
- ALOGE("Unrecognized p010 color gamut %d", uncompressed_p010_image->colorGamut);
+ if (p010_image_ptr->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
+ p010_image_ptr->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
+ ALOGE("Unrecognized p010 color gamut %d", p010_image_ptr->colorGamut);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
- if (uncompressed_p010_image->luma_stride != 0
- && uncompressed_p010_image->luma_stride < uncompressed_p010_image->width) {
- ALOGE("Luma stride can not be smaller than width, stride=%d, width=%d",
- uncompressed_p010_image->luma_stride, uncompressed_p010_image->width);
+ if (p010_image_ptr->luma_stride != 0 && p010_image_ptr->luma_stride < p010_image_ptr->width) {
+ ALOGE("Luma stride must not be smaller than width, stride=%d, width=%d",
+ p010_image_ptr->luma_stride, p010_image_ptr->width);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
- if (uncompressed_p010_image->chroma_data != nullptr
- && uncompressed_p010_image->chroma_stride < uncompressed_p010_image->width) {
- ALOGE("Chroma stride can not be smaller than width, stride=%d, width=%d",
- uncompressed_p010_image->chroma_stride,
- uncompressed_p010_image->width);
+ if (p010_image_ptr->chroma_data != nullptr &&
+ p010_image_ptr->chroma_stride < p010_image_ptr->width) {
+ ALOGE("Chroma stride must not be smaller than width, stride=%d, width=%d",
+ p010_image_ptr->chroma_stride, p010_image_ptr->width);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
- if (dest == nullptr || dest->data == nullptr) {
- ALOGE("received nullptr for destination");
+ if (dest_ptr == nullptr || dest_ptr->data == nullptr) {
+ ALOGE("Received nullptr for destination");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (hdr_tf <= ULTRAHDR_TF_UNSPECIFIED || hdr_tf > ULTRAHDR_TF_MAX
- || hdr_tf == ULTRAHDR_TF_SRGB) {
+ if (hdr_tf <= ULTRAHDR_TF_UNSPECIFIED || hdr_tf > ULTRAHDR_TF_MAX || hdr_tf == ULTRAHDR_TF_SRGB) {
ALOGE("Invalid hdr transfer function %d", hdr_tf);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
- if (uncompressed_yuv_420_image == nullptr) {
+ if (yuv420_image_ptr == nullptr) {
return NO_ERROR;
}
-
- if (uncompressed_yuv_420_image->data == nullptr) {
- ALOGE("received nullptr for uncompressed 420 image");
+ if (yuv420_image_ptr->data == nullptr) {
+ ALOGE("Received nullptr for uncompressed 420 image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (uncompressed_yuv_420_image->luma_stride != 0) {
- ALOGE("Stride is not supported for YUV420 image");
- return ERROR_JPEGR_UNSUPPORTED_FEATURE;
- }
-
- if (uncompressed_yuv_420_image->chroma_data != nullptr) {
- ALOGE("Pointer to chroma plane is not supported for YUV420 image, chroma data must"
- "be immediately after the luma data.");
- return ERROR_JPEGR_UNSUPPORTED_FEATURE;
- }
-
- if (uncompressed_p010_image->width != uncompressed_yuv_420_image->width
- || uncompressed_p010_image->height != uncompressed_yuv_420_image->height) {
- ALOGE("Image resolutions mismatch: P010: %dx%d, YUV420: %dx%d",
- uncompressed_p010_image->width,
- uncompressed_p010_image->height,
- uncompressed_yuv_420_image->width,
- uncompressed_yuv_420_image->height);
- return ERROR_JPEGR_RESOLUTION_MISMATCH;
- }
-
- if (uncompressed_yuv_420_image->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED
- || uncompressed_yuv_420_image->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
- ALOGE("Unrecognized 420 color gamut %d", uncompressed_yuv_420_image->colorGamut);
+ if (yuv420_image_ptr->luma_stride != 0 &&
+ yuv420_image_ptr->luma_stride < yuv420_image_ptr->width) {
+ ALOGE("Luma stride must not be smaller than width, stride=%d, width=%d",
+ yuv420_image_ptr->luma_stride, yuv420_image_ptr->width);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
+ if (yuv420_image_ptr->chroma_data != nullptr &&
+ yuv420_image_ptr->chroma_stride < yuv420_image_ptr->width / 2) {
+ ALOGE("Chroma stride must not be smaller than (width / 2), stride=%d, width=%d",
+ yuv420_image_ptr->chroma_stride, yuv420_image_ptr->width);
+ return ERROR_JPEGR_INVALID_INPUT_TYPE;
+ }
+ if (p010_image_ptr->width != yuv420_image_ptr->width ||
+ p010_image_ptr->height != yuv420_image_ptr->height) {
+ ALOGE("Image resolutions mismatch: P010: %dx%d, YUV420: %dx%d", p010_image_ptr->width,
+ p010_image_ptr->height, yuv420_image_ptr->width, yuv420_image_ptr->height);
+ return ERROR_JPEGR_RESOLUTION_MISMATCH;
+ }
+ if (yuv420_image_ptr->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
+ yuv420_image_ptr->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
+ ALOGE("Unrecognized 420 color gamut %d", yuv420_image_ptr->colorGamut);
+ return ERROR_JPEGR_INVALID_INPUT_TYPE;
+ }
return NO_ERROR;
}
-status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
- jr_uncompressed_ptr uncompressed_yuv_420_image,
+status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr,
+ jr_uncompressed_ptr yuv420_image_ptr,
ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest,
- int quality) {
- if (status_t ret = areInputArgumentsValid(
- uncompressed_p010_image, uncompressed_yuv_420_image, hdr_tf, dest) != NO_ERROR) {
- return ret;
- }
-
+ jr_compressed_ptr dest_ptr, int quality) {
if (quality < 0 || quality > 100) {
ALOGE("quality factor is out side range [0-100], quality factor : %d", quality);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
- return NO_ERROR;
+ return areInputArgumentsValid(p010_image_ptr, yuv420_image_ptr, hdr_tf, dest_ptr);
}
/* Encode API-0 */
-status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
- ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest,
- int quality,
- jr_exif_ptr exif) {
- if (status_t ret = areInputArgumentsValid(
- uncompressed_p010_image, /* uncompressed_yuv_420_image */ nullptr,
- hdr_tf, dest, quality) != NO_ERROR) {
+status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, ultrahdr_transfer_function hdr_tf,
+ jr_compressed_ptr dest, int quality, jr_exif_ptr exif) {
+ // validate input arguments
+ if (auto ret = areInputArgumentsValid(p010_image_ptr, nullptr, hdr_tf, dest, quality);
+ ret != NO_ERROR) {
return ret;
}
-
if (exif != nullptr && exif->data == nullptr) {
ALOGE("received nullptr for exif metadata");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
- ultrahdr_metadata_struct metadata;
- metadata.version = kJpegrVersion;
+ // clean up input structure for later usage
+ jpegr_uncompressed_struct p010_image = *p010_image_ptr;
+ if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width;
+ if (!p010_image.chroma_data) {
+ uint16_t* data = reinterpret_cast<uint16_t*>(p010_image.data);
+ p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height;
+ p010_image.chroma_stride = p010_image.luma_stride;
+ }
- jpegr_uncompressed_struct uncompressed_yuv_420_image;
- unique_ptr<uint8_t[]> uncompressed_yuv_420_image_data = make_unique<uint8_t[]>(
- uncompressed_p010_image->width * uncompressed_p010_image->height * 3 / 2);
- uncompressed_yuv_420_image.data = uncompressed_yuv_420_image_data.get();
- JPEGR_CHECK(toneMap(uncompressed_p010_image, &uncompressed_yuv_420_image));
+ const int yu420_luma_stride = ALIGNM(p010_image.width, kJpegBlock);
+ unique_ptr<uint8_t[]> yuv420_image_data =
+ make_unique<uint8_t[]>(yu420_luma_stride * p010_image.height * 3 / 2);
+ jpegr_uncompressed_struct yuv420_image = {.data = yuv420_image_data.get(),
+ .width = p010_image.width,
+ .height = p010_image.height,
+ .colorGamut = p010_image.colorGamut,
+ .luma_stride = yu420_luma_stride,
+ .chroma_data = nullptr,
+ .chroma_stride = yu420_luma_stride >> 1};
+ uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
+ yuv420_image.chroma_data = data + yuv420_image.luma_stride * yuv420_image.height;
- jpegr_uncompressed_struct map;
- JPEGR_CHECK(generateGainMap(
- &uncompressed_yuv_420_image, uncompressed_p010_image, hdr_tf, &metadata, &map));
+ // tone map
+ JPEGR_CHECK(toneMap(&p010_image, &yuv420_image));
+
+ // gain map
+ ultrahdr_metadata_struct metadata = {.version = kJpegrVersion};
+ jpegr_uncompressed_struct gainmap_image;
+ JPEGR_CHECK(generateGainMap(&yuv420_image, &p010_image, hdr_tf, &metadata, &gainmap_image));
std::unique_ptr<uint8_t[]> map_data;
- map_data.reset(reinterpret_cast<uint8_t*>(map.data));
+ map_data.reset(reinterpret_cast<uint8_t*>(gainmap_image.data));
- JpegEncoderHelper jpeg_encoder_gainmap;
- JPEGR_CHECK(compressGainMap(&map, &jpeg_encoder_gainmap));
- jpegr_compressed_struct compressed_map;
- compressed_map.maxLength = jpeg_encoder_gainmap.getCompressedImageSize();
- compressed_map.length = compressed_map.maxLength;
- compressed_map.data = jpeg_encoder_gainmap.getCompressedImagePtr();
- compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
+ // compress gain map
+ JpegEncoderHelper jpeg_enc_obj_gm;
+ JPEGR_CHECK(compressGainMap(&gainmap_image, &jpeg_enc_obj_gm));
+ jpegr_compressed_struct compressed_map = {.data = jpeg_enc_obj_gm.getCompressedImagePtr(),
+ .length = static_cast<int>(
+ jpeg_enc_obj_gm.getCompressedImageSize()),
+ .maxLength = static_cast<int>(
+ jpeg_enc_obj_gm.getCompressedImageSize()),
+ .colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED};
- sp<DataStruct> icc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB,
- uncompressed_yuv_420_image.colorGamut);
+ sp<DataStruct> icc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB, yuv420_image.colorGamut);
- // Convert to Bt601 YUV encoding for JPEG encode
- JPEGR_CHECK(convertYuv(&uncompressed_yuv_420_image, uncompressed_yuv_420_image.colorGamut,
- ULTRAHDR_COLORGAMUT_P3));
+ // convert to Bt601 YUV encoding for JPEG encode
+ if (yuv420_image.colorGamut != ULTRAHDR_COLORGAMUT_P3) {
+ JPEGR_CHECK(convertYuv(&yuv420_image, yuv420_image.colorGamut, ULTRAHDR_COLORGAMUT_P3));
+ }
- JpegEncoderHelper jpeg_encoder;
- if (!jpeg_encoder.compressImage(uncompressed_yuv_420_image.data,
- uncompressed_yuv_420_image.width,
- uncompressed_yuv_420_image.height, quality,
- icc->getData(), icc->getLength())) {
+ // compress 420 image
+ JpegEncoderHelper jpeg_enc_obj_yuv420;
+ if (!jpeg_enc_obj_yuv420.compressImage(reinterpret_cast<uint8_t*>(yuv420_image.data),
+ reinterpret_cast<uint8_t*>(yuv420_image.chroma_data),
+ yuv420_image.width, yuv420_image.height,
+ yuv420_image.luma_stride, yuv420_image.chroma_stride,
+ quality, icc->getData(), icc->getLength())) {
return ERROR_JPEGR_ENCODE_ERROR;
}
- jpegr_compressed_struct jpeg;
- jpeg.data = jpeg_encoder.getCompressedImagePtr();
- jpeg.length = jpeg_encoder.getCompressedImageSize();
+ jpegr_compressed_struct jpeg = {.data = jpeg_enc_obj_yuv420.getCompressedImagePtr(),
+ .length = static_cast<int>(
+ jpeg_enc_obj_yuv420.getCompressedImageSize()),
+ .maxLength = static_cast<int>(
+ jpeg_enc_obj_yuv420.getCompressedImageSize()),
+ .colorGamut = yuv420_image.colorGamut};
- // No ICC since JPEG encode already did it
+ // append gain map, no ICC since JPEG encode already did it
JPEGR_CHECK(appendGainMap(&jpeg, &compressed_map, exif, /* icc */ nullptr, /* icc size */ 0,
&metadata, dest));
@@ -281,226 +249,277 @@
}
/* Encode API-1 */
-status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
- jr_uncompressed_ptr uncompressed_yuv_420_image,
- ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest,
- int quality,
- jr_exif_ptr exif) {
- if (uncompressed_yuv_420_image == nullptr) {
+status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr,
+ jr_uncompressed_ptr yuv420_image_ptr, ultrahdr_transfer_function hdr_tf,
+ jr_compressed_ptr dest, int quality, jr_exif_ptr exif) {
+ // validate input arguments
+ if (yuv420_image_ptr == nullptr) {
ALOGE("received nullptr for uncompressed 420 image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
if (exif != nullptr && exif->data == nullptr) {
ALOGE("received nullptr for exif metadata");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (status_t ret = areInputArgumentsValid(
- uncompressed_p010_image, uncompressed_yuv_420_image, hdr_tf,
- dest, quality) != NO_ERROR) {
+ if (auto ret = areInputArgumentsValid(p010_image_ptr, yuv420_image_ptr, hdr_tf, dest, quality);
+ ret != NO_ERROR) {
return ret;
}
- ultrahdr_metadata_struct metadata;
- metadata.version = kJpegrVersion;
+ // clean up input structure for later usage
+ jpegr_uncompressed_struct p010_image = *p010_image_ptr;
+ if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width;
+ if (!p010_image.chroma_data) {
+ uint16_t* data = reinterpret_cast<uint16_t*>(p010_image.data);
+ p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height;
+ p010_image.chroma_stride = p010_image.luma_stride;
+ }
+ jpegr_uncompressed_struct yuv420_image = *yuv420_image_ptr;
+ if (yuv420_image.luma_stride == 0) yuv420_image.luma_stride = yuv420_image.width;
+ if (!yuv420_image.chroma_data) {
+ uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
+ yuv420_image.chroma_data = data + yuv420_image.luma_stride * yuv420_image.height;
+ yuv420_image.chroma_stride = yuv420_image.luma_stride >> 1;
+ }
- jpegr_uncompressed_struct map;
- JPEGR_CHECK(generateGainMap(
- uncompressed_yuv_420_image, uncompressed_p010_image, hdr_tf, &metadata, &map));
+ // gain map
+ ultrahdr_metadata_struct metadata = {.version = kJpegrVersion};
+ jpegr_uncompressed_struct gainmap_image;
+ JPEGR_CHECK(generateGainMap(&yuv420_image, &p010_image, hdr_tf, &metadata, &gainmap_image));
std::unique_ptr<uint8_t[]> map_data;
- map_data.reset(reinterpret_cast<uint8_t*>(map.data));
+ map_data.reset(reinterpret_cast<uint8_t*>(gainmap_image.data));
- JpegEncoderHelper jpeg_encoder_gainmap;
- JPEGR_CHECK(compressGainMap(&map, &jpeg_encoder_gainmap));
- jpegr_compressed_struct compressed_map;
- compressed_map.maxLength = jpeg_encoder_gainmap.getCompressedImageSize();
- compressed_map.length = compressed_map.maxLength;
- compressed_map.data = jpeg_encoder_gainmap.getCompressedImagePtr();
- compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
+ // compress gain map
+ JpegEncoderHelper jpeg_enc_obj_gm;
+ JPEGR_CHECK(compressGainMap(&gainmap_image, &jpeg_enc_obj_gm));
+ jpegr_compressed_struct compressed_map = {.data = jpeg_enc_obj_gm.getCompressedImagePtr(),
+ .length = static_cast<int>(
+ jpeg_enc_obj_gm.getCompressedImageSize()),
+ .maxLength = static_cast<int>(
+ jpeg_enc_obj_gm.getCompressedImageSize()),
+ .colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED};
- sp<DataStruct> icc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB,
- uncompressed_yuv_420_image->colorGamut);
+ sp<DataStruct> icc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB, yuv420_image.colorGamut);
- // Convert to Bt601 YUV encoding for JPEG encode; make a copy so as to no clobber client data
- unique_ptr<uint8_t[]> yuv_420_bt601_data = make_unique<uint8_t[]>(
- uncompressed_yuv_420_image->width * uncompressed_yuv_420_image->height * 3 / 2);
- memcpy(yuv_420_bt601_data.get(), uncompressed_yuv_420_image->data,
- uncompressed_yuv_420_image->width * uncompressed_yuv_420_image->height * 3 / 2);
+ jpegr_uncompressed_struct yuv420_bt601_image = yuv420_image;
+ unique_ptr<uint8_t[]> yuv_420_bt601_data;
+ // Convert to bt601 YUV encoding for JPEG encode
+ if (yuv420_image.colorGamut != ULTRAHDR_COLORGAMUT_P3) {
+ const int yuv_420_bt601_luma_stride = ALIGNM(yuv420_image.width, kJpegBlock);
+ yuv_420_bt601_data =
+ make_unique<uint8_t[]>(yuv_420_bt601_luma_stride * yuv420_image.height * 3 / 2);
+ yuv420_bt601_image.data = yuv_420_bt601_data.get();
+ yuv420_bt601_image.colorGamut = yuv420_image.colorGamut;
+ yuv420_bt601_image.luma_stride = yuv_420_bt601_luma_stride;
+ uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_bt601_image.data);
+ yuv420_bt601_image.chroma_data = data + yuv_420_bt601_luma_stride * yuv420_image.height;
+ yuv420_bt601_image.chroma_stride = yuv_420_bt601_luma_stride >> 1;
- jpegr_uncompressed_struct yuv_420_bt601_image = {
- yuv_420_bt601_data.get(), uncompressed_yuv_420_image->width, uncompressed_yuv_420_image->height,
- uncompressed_yuv_420_image->colorGamut };
- JPEGR_CHECK(convertYuv(&yuv_420_bt601_image, yuv_420_bt601_image.colorGamut,
- ULTRAHDR_COLORGAMUT_P3));
+ {
+ // copy luma
+ uint8_t* y_dst = reinterpret_cast<uint8_t*>(yuv420_bt601_image.data);
+ uint8_t* y_src = reinterpret_cast<uint8_t*>(yuv420_image.data);
+ if (yuv420_bt601_image.luma_stride == yuv420_image.luma_stride) {
+ memcpy(y_dst, y_src, yuv420_bt601_image.luma_stride * yuv420_image.height);
+ } else {
+ for (size_t i = 0; i < yuv420_image.height; i++) {
+ memcpy(y_dst, y_src, yuv420_image.width);
+ if (yuv420_image.width != yuv420_bt601_image.luma_stride) {
+ memset(y_dst + yuv420_image.width, 0,
+ yuv420_bt601_image.luma_stride - yuv420_image.width);
+ }
+ y_dst += yuv420_bt601_image.luma_stride;
+ y_src += yuv420_image.luma_stride;
+ }
+ }
+ }
- JpegEncoderHelper jpeg_encoder;
- if (!jpeg_encoder.compressImage(yuv_420_bt601_image.data,
- yuv_420_bt601_image.width,
- yuv_420_bt601_image.height, quality,
- icc->getData(), icc->getLength())) {
+ if (yuv420_bt601_image.chroma_stride == yuv420_image.chroma_stride) {
+ // copy luma
+ uint8_t* ch_dst = reinterpret_cast<uint8_t*>(yuv420_bt601_image.chroma_data);
+ uint8_t* ch_src = reinterpret_cast<uint8_t*>(yuv420_image.chroma_data);
+ memcpy(ch_dst, ch_src, yuv420_bt601_image.chroma_stride * yuv420_image.height);
+ } else {
+ // copy cb & cr
+ uint8_t* cb_dst = reinterpret_cast<uint8_t*>(yuv420_bt601_image.chroma_data);
+ uint8_t* cb_src = reinterpret_cast<uint8_t*>(yuv420_image.chroma_data);
+ uint8_t* cr_dst = cb_dst + (yuv420_bt601_image.chroma_stride * yuv420_bt601_image.height / 2);
+ uint8_t* cr_src = cb_src + (yuv420_image.chroma_stride * yuv420_image.height / 2);
+ for (size_t i = 0; i < yuv420_image.height / 2; i++) {
+ memcpy(cb_dst, cb_src, yuv420_image.width / 2);
+ memcpy(cr_dst, cr_src, yuv420_image.width / 2);
+ if (yuv420_bt601_image.width / 2 != yuv420_bt601_image.chroma_stride) {
+ memset(cb_dst + yuv420_image.width / 2, 0,
+ yuv420_bt601_image.chroma_stride - yuv420_image.width / 2);
+ memset(cr_dst + yuv420_image.width / 2, 0,
+ yuv420_bt601_image.chroma_stride - yuv420_image.width / 2);
+ }
+ cb_dst += yuv420_bt601_image.chroma_stride;
+ cb_src += yuv420_image.chroma_stride;
+ cr_dst += yuv420_bt601_image.chroma_stride;
+ cr_src += yuv420_image.chroma_stride;
+ }
+ }
+ JPEGR_CHECK(convertYuv(&yuv420_bt601_image, yuv420_image.colorGamut, ULTRAHDR_COLORGAMUT_P3));
+ }
+
+ // compress 420 image
+ JpegEncoderHelper jpeg_enc_obj_yuv420;
+ if (!jpeg_enc_obj_yuv420.compressImage(reinterpret_cast<uint8_t*>(yuv420_bt601_image.data),
+ reinterpret_cast<uint8_t*>(yuv420_bt601_image.chroma_data),
+ yuv420_bt601_image.width, yuv420_bt601_image.height,
+ yuv420_bt601_image.luma_stride,
+ yuv420_bt601_image.chroma_stride, quality, icc->getData(),
+ icc->getLength())) {
return ERROR_JPEGR_ENCODE_ERROR;
}
- jpegr_compressed_struct jpeg;
- jpeg.data = jpeg_encoder.getCompressedImagePtr();
- jpeg.length = jpeg_encoder.getCompressedImageSize();
- // No ICC since jpeg encode already did it
+ jpegr_compressed_struct jpeg = {.data = jpeg_enc_obj_yuv420.getCompressedImagePtr(),
+ .length = static_cast<int>(
+ jpeg_enc_obj_yuv420.getCompressedImageSize()),
+ .maxLength = static_cast<int>(
+ jpeg_enc_obj_yuv420.getCompressedImageSize()),
+ .colorGamut = yuv420_image.colorGamut};
+
+ // append gain map, no ICC since JPEG encode already did it
JPEGR_CHECK(appendGainMap(&jpeg, &compressed_map, exif, /* icc */ nullptr, /* icc size */ 0,
&metadata, dest));
-
return NO_ERROR;
}
/* Encode API-2 */
-status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
- jr_uncompressed_ptr uncompressed_yuv_420_image,
- jr_compressed_ptr compressed_jpeg_image,
- ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest) {
- if (uncompressed_yuv_420_image == nullptr) {
+status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr,
+ jr_uncompressed_ptr yuv420_image_ptr,
+ jr_compressed_ptr yuv420jpg_image_ptr,
+ ultrahdr_transfer_function hdr_tf, jr_compressed_ptr dest) {
+ // validate input arguments
+ if (yuv420_image_ptr == nullptr) {
ALOGE("received nullptr for uncompressed 420 image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (compressed_jpeg_image == nullptr || compressed_jpeg_image->data == nullptr) {
+ if (yuv420jpg_image_ptr == nullptr || yuv420jpg_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpeg image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (status_t ret = areInputArgumentsValid(
- uncompressed_p010_image, uncompressed_yuv_420_image, hdr_tf, dest) != NO_ERROR) {
+ if (auto ret = areInputArgumentsValid(p010_image_ptr, yuv420_image_ptr, hdr_tf, dest);
+ ret != NO_ERROR) {
return ret;
}
- ultrahdr_metadata_struct metadata;
- metadata.version = kJpegrVersion;
-
- jpegr_uncompressed_struct map;
- JPEGR_CHECK(generateGainMap(
- uncompressed_yuv_420_image, uncompressed_p010_image, hdr_tf, &metadata, &map));
- std::unique_ptr<uint8_t[]> map_data;
- map_data.reset(reinterpret_cast<uint8_t*>(map.data));
-
- JpegEncoderHelper jpeg_encoder_gainmap;
- JPEGR_CHECK(compressGainMap(&map, &jpeg_encoder_gainmap));
- jpegr_compressed_struct compressed_map;
- compressed_map.maxLength = jpeg_encoder_gainmap.getCompressedImageSize();
- compressed_map.length = compressed_map.maxLength;
- compressed_map.data = jpeg_encoder_gainmap.getCompressedImagePtr();
- compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
-
- // We just want to check if ICC is present, so don't do a full decode. Note,
- // this doesn't verify that the ICC is valid.
- JpegDecoderHelper decoder;
- std::vector<uint8_t> icc;
- decoder.getCompressedImageParameters(compressed_jpeg_image->data, compressed_jpeg_image->length,
- /* pWidth */ nullptr, /* pHeight */ nullptr,
- &icc, /* exifData */ nullptr);
-
- // Add ICC if not already present.
- if (icc.size() > 0) {
- JPEGR_CHECK(appendGainMap(compressed_jpeg_image, &compressed_map, /* exif */ nullptr,
- /* icc */ nullptr, /* icc size */ 0, &metadata, dest));
- } else {
- sp<DataStruct> newIcc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB,
- uncompressed_yuv_420_image->colorGamut);
- JPEGR_CHECK(appendGainMap(compressed_jpeg_image, &compressed_map, /* exif */ nullptr,
- newIcc->getData(), newIcc->getLength(), &metadata, dest));
+ // clean up input structure for later usage
+ jpegr_uncompressed_struct p010_image = *p010_image_ptr;
+ if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width;
+ if (!p010_image.chroma_data) {
+ uint16_t* data = reinterpret_cast<uint16_t*>(p010_image.data);
+ p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height;
+ p010_image.chroma_stride = p010_image.luma_stride;
+ }
+ jpegr_uncompressed_struct yuv420_image = *yuv420_image_ptr;
+ if (yuv420_image.luma_stride == 0) yuv420_image.luma_stride = yuv420_image.width;
+ if (!yuv420_image.chroma_data) {
+ uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
+ yuv420_image.chroma_data = data + yuv420_image.luma_stride * p010_image.height;
+ yuv420_image.chroma_stride = yuv420_image.luma_stride >> 1;
}
- return NO_ERROR;
+ // gain map
+ ultrahdr_metadata_struct metadata = {.version = kJpegrVersion};
+ jpegr_uncompressed_struct gainmap_image;
+ JPEGR_CHECK(generateGainMap(&yuv420_image, &p010_image, hdr_tf, &metadata, &gainmap_image));
+ std::unique_ptr<uint8_t[]> map_data;
+ map_data.reset(reinterpret_cast<uint8_t*>(gainmap_image.data));
+
+ // compress gain map
+ JpegEncoderHelper jpeg_enc_obj_gm;
+ JPEGR_CHECK(compressGainMap(&gainmap_image, &jpeg_enc_obj_gm));
+ jpegr_compressed_struct gainmapjpg_image = {.data = jpeg_enc_obj_gm.getCompressedImagePtr(),
+ .length = static_cast<int>(
+ jpeg_enc_obj_gm.getCompressedImageSize()),
+ .maxLength = static_cast<int>(
+ jpeg_enc_obj_gm.getCompressedImageSize()),
+ .colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED};
+
+ return encodeJPEGR(yuv420jpg_image_ptr, &gainmapjpg_image, &metadata, dest);
}
/* Encode API-3 */
-status_t JpegR::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
- jr_compressed_ptr compressed_jpeg_image,
- ultrahdr_transfer_function hdr_tf,
- jr_compressed_ptr dest) {
- if (compressed_jpeg_image == nullptr || compressed_jpeg_image->data == nullptr) {
+status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr,
+ jr_compressed_ptr yuv420jpg_image_ptr,
+ ultrahdr_transfer_function hdr_tf, jr_compressed_ptr dest) {
+ // validate input arguments
+ if (yuv420jpg_image_ptr == nullptr || yuv420jpg_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpeg image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (status_t ret = areInputArgumentsValid(
- uncompressed_p010_image, /* uncompressed_yuv_420_image */ nullptr,
- hdr_tf, dest) != NO_ERROR) {
+ if (auto ret = areInputArgumentsValid(p010_image_ptr, nullptr, hdr_tf, dest); ret != NO_ERROR) {
return ret;
}
- // Note: output is Bt.601 YUV encoded regardless of gamut, due to jpeg decode.
- JpegDecoderHelper jpeg_decoder;
- if (!jpeg_decoder.decompressImage(compressed_jpeg_image->data, compressed_jpeg_image->length)) {
+ // clean up input structure for later usage
+ jpegr_uncompressed_struct p010_image = *p010_image_ptr;
+ if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width;
+ if (!p010_image.chroma_data) {
+ uint16_t* data = reinterpret_cast<uint16_t*>(p010_image.data);
+ p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height;
+ p010_image.chroma_stride = p010_image.luma_stride;
+ }
+
+ // decode input jpeg, gamut is going to be bt601.
+ JpegDecoderHelper jpeg_dec_obj_yuv420;
+ if (!jpeg_dec_obj_yuv420.decompressImage(yuv420jpg_image_ptr->data,
+ yuv420jpg_image_ptr->length)) {
return ERROR_JPEGR_DECODE_ERROR;
}
- jpegr_uncompressed_struct uncompressed_yuv_420_image;
- uncompressed_yuv_420_image.data = jpeg_decoder.getDecompressedImagePtr();
- uncompressed_yuv_420_image.width = jpeg_decoder.getDecompressedImageWidth();
- uncompressed_yuv_420_image.height = jpeg_decoder.getDecompressedImageHeight();
- uncompressed_yuv_420_image.colorGamut = compressed_jpeg_image->colorGamut;
+ jpegr_uncompressed_struct yuv420_image{};
+ yuv420_image.data = jpeg_dec_obj_yuv420.getDecompressedImagePtr();
+ yuv420_image.width = jpeg_dec_obj_yuv420.getDecompressedImageWidth();
+ yuv420_image.height = jpeg_dec_obj_yuv420.getDecompressedImageHeight();
+ yuv420_image.colorGamut = yuv420jpg_image_ptr->colorGamut;
+ if (yuv420_image.luma_stride == 0) yuv420_image.luma_stride = yuv420_image.width;
+ if (!yuv420_image.chroma_data) {
+ uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
+ yuv420_image.chroma_data = data + yuv420_image.luma_stride * p010_image.height;
+ yuv420_image.chroma_stride = yuv420_image.luma_stride >> 1;
+ }
- if (uncompressed_p010_image->width != uncompressed_yuv_420_image.width
- || uncompressed_p010_image->height != uncompressed_yuv_420_image.height) {
+ if (p010_image_ptr->width != yuv420_image.width ||
+ p010_image_ptr->height != yuv420_image.height) {
return ERROR_JPEGR_RESOLUTION_MISMATCH;
}
- ultrahdr_metadata_struct metadata;
- metadata.version = kJpegrVersion;
-
- jpegr_uncompressed_struct map;
- // Indicate that the SDR image is Bt.601 YUV encoded.
- JPEGR_CHECK(generateGainMap(
- &uncompressed_yuv_420_image, uncompressed_p010_image, hdr_tf, &metadata, &map,
- true /* sdr_is_601 */ ));
+ // gain map
+ ultrahdr_metadata_struct metadata = {.version = kJpegrVersion};
+ jpegr_uncompressed_struct gainmap_image;
+ JPEGR_CHECK(generateGainMap(&yuv420_image, &p010_image, hdr_tf, &metadata, &gainmap_image,
+ true /* sdr_is_601 */));
std::unique_ptr<uint8_t[]> map_data;
- map_data.reset(reinterpret_cast<uint8_t*>(map.data));
+ map_data.reset(reinterpret_cast<uint8_t*>(gainmap_image.data));
- JpegEncoderHelper jpeg_encoder_gainmap;
- JPEGR_CHECK(compressGainMap(&map, &jpeg_encoder_gainmap));
- jpegr_compressed_struct compressed_map;
- compressed_map.maxLength = jpeg_encoder_gainmap.getCompressedImageSize();
- compressed_map.length = compressed_map.maxLength;
- compressed_map.data = jpeg_encoder_gainmap.getCompressedImagePtr();
- compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
+ // compress gain map
+ JpegEncoderHelper jpeg_enc_obj_gm;
+ JPEGR_CHECK(compressGainMap(&gainmap_image, &jpeg_enc_obj_gm));
+ jpegr_compressed_struct gainmapjpg_image = {.data = jpeg_enc_obj_gm.getCompressedImagePtr(),
+ .length = static_cast<int>(
+ jpeg_enc_obj_gm.getCompressedImageSize()),
+ .maxLength = static_cast<int>(
+ jpeg_enc_obj_gm.getCompressedImageSize()),
+ .colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED};
- // We just want to check if ICC is present, so don't do a full decode. Note,
- // this doesn't verify that the ICC is valid.
- JpegDecoderHelper decoder;
- std::vector<uint8_t> icc;
- decoder.getCompressedImageParameters(compressed_jpeg_image->data, compressed_jpeg_image->length,
- /* pWidth */ nullptr, /* pHeight */ nullptr,
- &icc, /* exifData */ nullptr);
-
- // Add ICC if not already present.
- if (icc.size() > 0) {
- JPEGR_CHECK(appendGainMap(compressed_jpeg_image, &compressed_map, /* exif */ nullptr,
- /* icc */ nullptr, /* icc size */ 0, &metadata, dest));
- } else {
- sp<DataStruct> newIcc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB,
- uncompressed_yuv_420_image.colorGamut);
- JPEGR_CHECK(appendGainMap(compressed_jpeg_image, &compressed_map, /* exif */ nullptr,
- newIcc->getData(), newIcc->getLength(), &metadata, dest));
- }
-
- return NO_ERROR;
+ return encodeJPEGR(yuv420jpg_image_ptr, &gainmapjpg_image, &metadata, dest);
}
/* Encode API-4 */
-status_t JpegR::encodeJPEGR(jr_compressed_ptr compressed_jpeg_image,
- jr_compressed_ptr compressed_gainmap,
- ultrahdr_metadata_ptr metadata,
+status_t JpegR::encodeJPEGR(jr_compressed_ptr yuv420jpg_image_ptr,
+ jr_compressed_ptr gainmapjpg_image_ptr, ultrahdr_metadata_ptr metadata,
jr_compressed_ptr dest) {
- if (compressed_jpeg_image == nullptr || compressed_jpeg_image->data == nullptr) {
+ if (yuv420jpg_image_ptr == nullptr || yuv420jpg_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpeg image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (compressed_gainmap == nullptr || compressed_gainmap->data == nullptr) {
+ if (gainmapjpg_image_ptr == nullptr || gainmapjpg_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed gain map");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
if (dest == nullptr || dest->data == nullptr) {
ALOGE("received nullptr for destination");
return ERROR_JPEGR_INVALID_NULL_PTR;
@@ -510,46 +529,46 @@
// this doesn't verify that the ICC is valid.
JpegDecoderHelper decoder;
std::vector<uint8_t> icc;
- decoder.getCompressedImageParameters(compressed_jpeg_image->data, compressed_jpeg_image->length,
- /* pWidth */ nullptr, /* pHeight */ nullptr,
- &icc, /* exifData */ nullptr);
+ decoder.getCompressedImageParameters(yuv420jpg_image_ptr->data, yuv420jpg_image_ptr->length,
+ /* pWidth */ nullptr, /* pHeight */ nullptr, &icc,
+ /* exifData */ nullptr);
// Add ICC if not already present.
if (icc.size() > 0) {
- JPEGR_CHECK(appendGainMap(compressed_jpeg_image, compressed_gainmap, /* exif */ nullptr,
- /* icc */ nullptr, /* icc size */ 0, metadata, dest));
+ JPEGR_CHECK(appendGainMap(yuv420jpg_image_ptr, gainmapjpg_image_ptr, /* exif */ nullptr,
+ /* icc */ nullptr, /* icc size */ 0, metadata, dest));
} else {
- sp<DataStruct> newIcc = IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB,
- compressed_jpeg_image->colorGamut);
- JPEGR_CHECK(appendGainMap(compressed_jpeg_image, compressed_gainmap, /* exif */ nullptr,
- newIcc->getData(), newIcc->getLength(), metadata, dest));
+ sp<DataStruct> newIcc =
+ IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB, yuv420jpg_image_ptr->colorGamut);
+ JPEGR_CHECK(appendGainMap(yuv420jpg_image_ptr, gainmapjpg_image_ptr, /* exif */ nullptr,
+ newIcc->getData(), newIcc->getLength(), metadata, dest));
}
return NO_ERROR;
}
-status_t JpegR::getJPEGRInfo(jr_compressed_ptr compressed_jpegr_image, jr_info_ptr jpegr_info) {
- if (compressed_jpegr_image == nullptr || compressed_jpegr_image->data == nullptr) {
+status_t JpegR::getJPEGRInfo(jr_compressed_ptr jpegr_image_ptr, jr_info_ptr jpeg_image_info_ptr) {
+ if (jpegr_image_ptr == nullptr || jpegr_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpegr image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (jpegr_info == nullptr) {
+ if (jpeg_image_info_ptr == nullptr) {
ALOGE("received nullptr for compressed jpegr info struct");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
jpegr_compressed_struct primary_image, gainmap_image;
- status_t status =
- extractPrimaryImageAndGainMap(compressed_jpegr_image, &primary_image, &gainmap_image);
+ status_t status = extractPrimaryImageAndGainMap(jpegr_image_ptr, &primary_image, &gainmap_image);
if (status != NO_ERROR && status != ERROR_JPEGR_GAIN_MAP_IMAGE_NOT_FOUND) {
return status;
}
- JpegDecoderHelper jpeg_decoder;
- if (!jpeg_decoder.getCompressedImageParameters(primary_image.data, primary_image.length,
- &jpegr_info->width, &jpegr_info->height,
- jpegr_info->iccData, jpegr_info->exifData)) {
+ JpegDecoderHelper jpeg_dec_obj_hdr;
+ if (!jpeg_dec_obj_hdr.getCompressedImageParameters(primary_image.data, primary_image.length,
+ &jpeg_image_info_ptr->width,
+ &jpeg_image_info_ptr->height,
+ jpeg_image_info_ptr->iccData,
+ jpeg_image_info_ptr->exifData)) {
return ERROR_JPEGR_DECODE_ERROR;
}
@@ -557,41 +576,34 @@
}
/* Decode API */
-status_t JpegR::decodeJPEGR(jr_compressed_ptr compressed_jpegr_image,
- jr_uncompressed_ptr dest,
- float max_display_boost,
- jr_exif_ptr exif,
+status_t JpegR::decodeJPEGR(jr_compressed_ptr jpegr_image_ptr, jr_uncompressed_ptr dest,
+ float max_display_boost, jr_exif_ptr exif,
ultrahdr_output_format output_format,
- jr_uncompressed_ptr gain_map,
- ultrahdr_metadata_ptr metadata) {
- if (compressed_jpegr_image == nullptr || compressed_jpegr_image->data == nullptr) {
+ jr_uncompressed_ptr gainmap_image_ptr, ultrahdr_metadata_ptr metadata) {
+ if (jpegr_image_ptr == nullptr || jpegr_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpegr image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
if (dest == nullptr || dest->data == nullptr) {
ALOGE("received nullptr for dest image");
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
if (max_display_boost < 1.0f) {
ALOGE("received bad value for max_display_boost %f", max_display_boost);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
if (exif != nullptr && exif->data == nullptr) {
ALOGE("received nullptr address for exif data");
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
if (output_format <= ULTRAHDR_OUTPUT_UNSPECIFIED || output_format > ULTRAHDR_OUTPUT_MAX) {
ALOGE("received bad value for output format %d", output_format);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
- jpegr_compressed_struct primary_image, gainmap_image;
+ jpegr_compressed_struct primary_jpeg_image, gainmap_jpeg_image;
status_t status =
- extractPrimaryImageAndGainMap(compressed_jpegr_image, &primary_image, &gainmap_image);
+ extractPrimaryImageAndGainMap(jpegr_image_ptr, &primary_jpeg_image, &gainmap_jpeg_image);
if (status != NO_ERROR) {
if (output_format != ULTRAHDR_OUTPUT_SDR || status != ERROR_JPEGR_GAIN_MAP_IMAGE_NOT_FOUND) {
ALOGE("received invalid compressed jpegr image");
@@ -599,22 +611,22 @@
}
}
- JpegDecoderHelper jpeg_decoder;
- if (!jpeg_decoder.decompressImage(primary_image.data, primary_image.length,
- (output_format == ULTRAHDR_OUTPUT_SDR))) {
+ JpegDecoderHelper jpeg_dec_obj_yuv420;
+ if (!jpeg_dec_obj_yuv420.decompressImage(primary_jpeg_image.data, primary_jpeg_image.length,
+ (output_format == ULTRAHDR_OUTPUT_SDR))) {
return ERROR_JPEGR_DECODE_ERROR;
}
if (output_format == ULTRAHDR_OUTPUT_SDR) {
- if ((jpeg_decoder.getDecompressedImageWidth() *
- jpeg_decoder.getDecompressedImageHeight() * 4) >
- jpeg_decoder.getDecompressedImageSize()) {
+ if ((jpeg_dec_obj_yuv420.getDecompressedImageWidth() *
+ jpeg_dec_obj_yuv420.getDecompressedImageHeight() * 4) >
+ jpeg_dec_obj_yuv420.getDecompressedImageSize()) {
return ERROR_JPEGR_CALCULATION_ERROR;
}
} else {
- if ((jpeg_decoder.getDecompressedImageWidth() *
- jpeg_decoder.getDecompressedImageHeight() * 3 / 2) >
- jpeg_decoder.getDecompressedImageSize()) {
+ if ((jpeg_dec_obj_yuv420.getDecompressedImageWidth() *
+ jpeg_dec_obj_yuv420.getDecompressedImageHeight() * 3 / 2) >
+ jpeg_dec_obj_yuv420.getDecompressedImageSize()) {
return ERROR_JPEGR_CALCULATION_ERROR;
}
}
@@ -623,46 +635,46 @@
if (exif->data == nullptr) {
return ERROR_JPEGR_INVALID_NULL_PTR;
}
- if (exif->length < jpeg_decoder.getEXIFSize()) {
+ if (exif->length < jpeg_dec_obj_yuv420.getEXIFSize()) {
return ERROR_JPEGR_BUFFER_TOO_SMALL;
}
- memcpy(exif->data, jpeg_decoder.getEXIFPtr(), jpeg_decoder.getEXIFSize());
- exif->length = jpeg_decoder.getEXIFSize();
+ memcpy(exif->data, jpeg_dec_obj_yuv420.getEXIFPtr(), jpeg_dec_obj_yuv420.getEXIFSize());
+ exif->length = jpeg_dec_obj_yuv420.getEXIFSize();
}
if (output_format == ULTRAHDR_OUTPUT_SDR) {
- dest->width = jpeg_decoder.getDecompressedImageWidth();
- dest->height = jpeg_decoder.getDecompressedImageHeight();
- memcpy(dest->data, jpeg_decoder.getDecompressedImagePtr(), dest->width * dest->height * 4);
+ dest->width = jpeg_dec_obj_yuv420.getDecompressedImageWidth();
+ dest->height = jpeg_dec_obj_yuv420.getDecompressedImageHeight();
+ memcpy(dest->data, jpeg_dec_obj_yuv420.getDecompressedImagePtr(),
+ dest->width * dest->height * 4);
return NO_ERROR;
}
- JpegDecoderHelper gain_map_decoder;
- if (!gain_map_decoder.decompressImage(gainmap_image.data, gainmap_image.length)) {
+ JpegDecoderHelper jpeg_dec_obj_gm;
+ if (!jpeg_dec_obj_gm.decompressImage(gainmap_jpeg_image.data, gainmap_jpeg_image.length)) {
return ERROR_JPEGR_DECODE_ERROR;
}
- if ((gain_map_decoder.getDecompressedImageWidth() *
- gain_map_decoder.getDecompressedImageHeight()) >
- gain_map_decoder.getDecompressedImageSize()) {
+ if ((jpeg_dec_obj_gm.getDecompressedImageWidth() * jpeg_dec_obj_gm.getDecompressedImageHeight()) >
+ jpeg_dec_obj_gm.getDecompressedImageSize()) {
return ERROR_JPEGR_CALCULATION_ERROR;
}
- jpegr_uncompressed_struct map;
- map.data = gain_map_decoder.getDecompressedImagePtr();
- map.width = gain_map_decoder.getDecompressedImageWidth();
- map.height = gain_map_decoder.getDecompressedImageHeight();
+ jpegr_uncompressed_struct gainmap_image;
+ gainmap_image.data = jpeg_dec_obj_gm.getDecompressedImagePtr();
+ gainmap_image.width = jpeg_dec_obj_gm.getDecompressedImageWidth();
+ gainmap_image.height = jpeg_dec_obj_gm.getDecompressedImageHeight();
- if (gain_map != nullptr) {
- gain_map->width = map.width;
- gain_map->height = map.height;
- int size = gain_map->width * gain_map->height;
- gain_map->data = malloc(size);
- memcpy(gain_map->data, map.data, size);
+ if (gainmap_image_ptr != nullptr) {
+ gainmap_image_ptr->width = gainmap_image.width;
+ gainmap_image_ptr->height = gainmap_image.height;
+ int size = gainmap_image_ptr->width * gainmap_image_ptr->height;
+ gainmap_image_ptr->data = malloc(size);
+ memcpy(gainmap_image_ptr->data, gainmap_image.data, size);
}
ultrahdr_metadata_struct uhdr_metadata;
- if (!getMetadataFromXMP(static_cast<uint8_t*>(gain_map_decoder.getXMPPtr()),
- gain_map_decoder.getXMPSize(), &uhdr_metadata)) {
+ if (!getMetadataFromXMP(static_cast<uint8_t*>(jpeg_dec_obj_gm.getXMPPtr()),
+ jpeg_dec_obj_gm.getXMPSize(), &uhdr_metadata)) {
return ERROR_JPEGR_INVALID_METADATA;
}
@@ -677,32 +689,33 @@
metadata->hdrCapacityMax = uhdr_metadata.hdrCapacityMax;
}
- jpegr_uncompressed_struct uncompressed_yuv_420_image;
- uncompressed_yuv_420_image.data = jpeg_decoder.getDecompressedImagePtr();
- uncompressed_yuv_420_image.width = jpeg_decoder.getDecompressedImageWidth();
- uncompressed_yuv_420_image.height = jpeg_decoder.getDecompressedImageHeight();
- uncompressed_yuv_420_image.colorGamut = IccHelper::readIccColorGamut(
- jpeg_decoder.getICCPtr(), jpeg_decoder.getICCSize());
+ jpegr_uncompressed_struct yuv420_image;
+ yuv420_image.data = jpeg_dec_obj_yuv420.getDecompressedImagePtr();
+ yuv420_image.width = jpeg_dec_obj_yuv420.getDecompressedImageWidth();
+ yuv420_image.height = jpeg_dec_obj_yuv420.getDecompressedImageHeight();
+ yuv420_image.colorGamut = IccHelper::readIccColorGamut(jpeg_dec_obj_yuv420.getICCPtr(),
+ jpeg_dec_obj_yuv420.getICCSize());
+ yuv420_image.luma_stride = yuv420_image.width;
+ uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
+ yuv420_image.chroma_data = data + yuv420_image.luma_stride * yuv420_image.height;
+ yuv420_image.chroma_stride = yuv420_image.width >> 1;
- JPEGR_CHECK(applyGainMap(&uncompressed_yuv_420_image, &map, &uhdr_metadata, output_format,
+ JPEGR_CHECK(applyGainMap(&yuv420_image, &gainmap_image, &uhdr_metadata, output_format,
max_display_boost, dest));
return NO_ERROR;
}
-status_t JpegR::compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
- JpegEncoderHelper* jpeg_encoder) {
- if (uncompressed_gain_map == nullptr || jpeg_encoder == nullptr) {
+status_t JpegR::compressGainMap(jr_uncompressed_ptr gainmap_image_ptr,
+ JpegEncoderHelper* jpeg_enc_obj_ptr) {
+ if (gainmap_image_ptr == nullptr || jpeg_enc_obj_ptr == nullptr) {
return ERROR_JPEGR_INVALID_NULL_PTR;
}
// Don't need to convert YUV to Bt601 since single channel
- if (!jpeg_encoder->compressImage(uncompressed_gain_map->data,
- uncompressed_gain_map->width,
- uncompressed_gain_map->height,
- kMapCompressQuality,
- nullptr,
- 0,
- true /* isSingleChannel */)) {
+ if (!jpeg_enc_obj_ptr->compressImage(reinterpret_cast<uint8_t*>(gainmap_image_ptr->data), nullptr,
+ gainmap_image_ptr->width, gainmap_image_ptr->height,
+ gainmap_image_ptr->luma_stride, 0, kMapCompressQuality,
+ nullptr, 0)) {
return ERROR_JPEGR_ENCODE_ERROR;
}
@@ -714,13 +727,13 @@
"align job size to kMapDimensionScaleFactor");
class JobQueue {
- public:
+public:
bool dequeueJob(size_t& rowStart, size_t& rowEnd);
void enqueueJob(size_t rowStart, size_t rowEnd);
void markQueueForEnd();
void reset();
- private:
+private:
bool mQueuedAllJobs = false;
std::deque<std::tuple<size_t, size_t>> mJobs;
std::mutex mMutex;
@@ -767,41 +780,39 @@
mQueuedAllJobs = false;
}
-status_t JpegR::generateGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
- jr_uncompressed_ptr uncompressed_p010_image,
- ultrahdr_transfer_function hdr_tf,
- ultrahdr_metadata_ptr metadata,
- jr_uncompressed_ptr dest,
- bool sdr_is_601) {
- if (uncompressed_yuv_420_image == nullptr
- || uncompressed_p010_image == nullptr
- || metadata == nullptr
- || dest == nullptr) {
+status_t JpegR::generateGainMap(jr_uncompressed_ptr yuv420_image_ptr,
+ jr_uncompressed_ptr p010_image_ptr,
+ ultrahdr_transfer_function hdr_tf, ultrahdr_metadata_ptr metadata,
+ jr_uncompressed_ptr dest, bool sdr_is_601) {
+ if (yuv420_image_ptr == nullptr || p010_image_ptr == nullptr || metadata == nullptr ||
+ dest == nullptr || yuv420_image_ptr->data == nullptr ||
+ yuv420_image_ptr->chroma_data == nullptr || p010_image_ptr->data == nullptr ||
+ p010_image_ptr->chroma_data == nullptr) {
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (uncompressed_yuv_420_image->width != uncompressed_p010_image->width
- || uncompressed_yuv_420_image->height != uncompressed_p010_image->height) {
+ if (yuv420_image_ptr->width != p010_image_ptr->width ||
+ yuv420_image_ptr->height != p010_image_ptr->height) {
return ERROR_JPEGR_RESOLUTION_MISMATCH;
}
-
- if (uncompressed_yuv_420_image->colorGamut == ULTRAHDR_COLORGAMUT_UNSPECIFIED
- || uncompressed_p010_image->colorGamut == ULTRAHDR_COLORGAMUT_UNSPECIFIED) {
+ if (yuv420_image_ptr->colorGamut == ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
+ p010_image_ptr->colorGamut == ULTRAHDR_COLORGAMUT_UNSPECIFIED) {
return ERROR_JPEGR_INVALID_COLORGAMUT;
}
- size_t image_width = uncompressed_yuv_420_image->width;
- size_t image_height = uncompressed_yuv_420_image->height;
- size_t map_width = image_width / kMapDimensionScaleFactor;
- size_t map_height = image_height / kMapDimensionScaleFactor;
- size_t map_stride = static_cast<size_t>(
- floor((map_width + kJpegBlock - 1) / kJpegBlock)) * kJpegBlock;
- size_t map_height_aligned = ((map_height + 1) >> 1) << 1;
+ size_t image_width = yuv420_image_ptr->width;
+ size_t image_height = yuv420_image_ptr->height;
+ size_t map_width = static_cast<size_t>(
+ floor((image_width + kMapDimensionScaleFactor - 1) / kMapDimensionScaleFactor));
+ size_t map_height = static_cast<size_t>(
+ floor((image_height + kMapDimensionScaleFactor - 1) / kMapDimensionScaleFactor));
- dest->width = map_stride;
- dest->height = map_height_aligned;
+ dest->data = new uint8_t[map_width * map_height];
+ dest->width = map_width;
+ dest->height = map_height;
dest->colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
- dest->data = new uint8_t[map_stride * map_height_aligned];
+ dest->luma_stride = map_width;
+ dest->chroma_data = nullptr;
+ dest->chroma_stride = 0;
std::unique_ptr<uint8_t[]> map_data;
map_data.reset(reinterpret_cast<uint8_t*>(dest->data));
@@ -843,12 +854,12 @@
float log2MinBoost = log2(metadata->minContentBoost);
float log2MaxBoost = log2(metadata->maxContentBoost);
- ColorTransformFn hdrGamutConversionFn = getHdrConversionFn(
- uncompressed_yuv_420_image->colorGamut, uncompressed_p010_image->colorGamut);
+ ColorTransformFn hdrGamutConversionFn =
+ getHdrConversionFn(yuv420_image_ptr->colorGamut, p010_image_ptr->colorGamut);
ColorCalculationFn luminanceFn = nullptr;
ColorTransformFn sdrYuvToRgbFn = nullptr;
- switch (uncompressed_yuv_420_image->colorGamut) {
+ switch (yuv420_image_ptr->colorGamut) {
case ULTRAHDR_COLORGAMUT_BT709:
luminanceFn = srgbLuminance;
sdrYuvToRgbFn = srgbYuvToRgb;
@@ -870,7 +881,7 @@
}
ColorTransformFn hdrYuvToRgbFn = nullptr;
- switch (uncompressed_p010_image->colorGamut) {
+ switch (p010_image_ptr->colorGamut) {
case ULTRAHDR_COLORGAMUT_BT709:
hdrYuvToRgbFn = srgbYuvToRgb;
break;
@@ -890,18 +901,15 @@
size_t rowStep = threads == 1 ? image_height : kJobSzInRows;
JobQueue jobQueue;
- std::function<void()> generateMap = [uncompressed_yuv_420_image, uncompressed_p010_image,
- metadata, dest, hdrInvOetf, hdrGamutConversionFn,
- luminanceFn, sdrYuvToRgbFn, hdrYuvToRgbFn, hdr_white_nits,
- log2MinBoost, log2MaxBoost, &jobQueue]() -> void {
+ std::function<void()> generateMap = [yuv420_image_ptr, p010_image_ptr, metadata, dest, hdrInvOetf,
+ hdrGamutConversionFn, luminanceFn, sdrYuvToRgbFn,
+ hdrYuvToRgbFn, hdr_white_nits, log2MinBoost, log2MaxBoost,
+ &jobQueue]() -> void {
size_t rowStart, rowEnd;
- size_t dest_map_width = uncompressed_yuv_420_image->width / kMapDimensionScaleFactor;
- size_t dest_map_stride = dest->width;
while (jobQueue.dequeueJob(rowStart, rowEnd)) {
for (size_t y = rowStart; y < rowEnd; ++y) {
- for (size_t x = 0; x < dest_map_width; ++x) {
- Color sdr_yuv_gamma =
- sampleYuv420(uncompressed_yuv_420_image, kMapDimensionScaleFactor, x, y);
+ for (size_t x = 0; x < dest->width; ++x) {
+ Color sdr_yuv_gamma = sampleYuv420(yuv420_image_ptr, kMapDimensionScaleFactor, x, y);
Color sdr_rgb_gamma = sdrYuvToRgbFn(sdr_yuv_gamma);
// We are assuming the SDR input is always sRGB transfer.
#if USE_SRGB_INVOETF_LUT
@@ -911,15 +919,15 @@
#endif
float sdr_y_nits = luminanceFn(sdr_rgb) * kSdrWhiteNits;
- Color hdr_yuv_gamma = sampleP010(uncompressed_p010_image, kMapDimensionScaleFactor, x, y);
+ Color hdr_yuv_gamma = sampleP010(p010_image_ptr, kMapDimensionScaleFactor, x, y);
Color hdr_rgb_gamma = hdrYuvToRgbFn(hdr_yuv_gamma);
Color hdr_rgb = hdrInvOetf(hdr_rgb_gamma);
hdr_rgb = hdrGamutConversionFn(hdr_rgb);
float hdr_y_nits = luminanceFn(hdr_rgb) * hdr_white_nits;
- size_t pixel_idx = x + y * dest_map_stride;
+ size_t pixel_idx = x + y * dest->width;
reinterpret_cast<uint8_t*>(dest->data)[pixel_idx] =
- encodeGain(sdr_y_nits, hdr_y_nits, metadata, log2MinBoost, log2MaxBoost);
+ encodeGain(sdr_y_nits, hdr_y_nits, metadata, log2MinBoost, log2MaxBoost);
}
}
}
@@ -945,71 +953,66 @@
return NO_ERROR;
}
-status_t JpegR::applyGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
- jr_uncompressed_ptr uncompressed_gain_map,
- ultrahdr_metadata_ptr metadata,
- ultrahdr_output_format output_format,
- float max_display_boost,
+status_t JpegR::applyGainMap(jr_uncompressed_ptr yuv420_image_ptr,
+ jr_uncompressed_ptr gainmap_image_ptr, ultrahdr_metadata_ptr metadata,
+ ultrahdr_output_format output_format, float max_display_boost,
jr_uncompressed_ptr dest) {
- if (uncompressed_yuv_420_image == nullptr
- || uncompressed_gain_map == nullptr
- || metadata == nullptr
- || dest == nullptr) {
+ if (yuv420_image_ptr == nullptr || gainmap_image_ptr == nullptr || metadata == nullptr ||
+ dest == nullptr || yuv420_image_ptr->data == nullptr ||
+ yuv420_image_ptr->chroma_data == nullptr || gainmap_image_ptr->data == nullptr) {
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (metadata->version.compare("1.0")) {
- ALOGE("Unsupported metadata version: %s", metadata->version.c_str());
- return ERROR_JPEGR_UNSUPPORTED_METADATA;
+ if (metadata->version.compare(kJpegrVersion)) {
+ ALOGE("Unsupported metadata version: %s", metadata->version.c_str());
+ return ERROR_JPEGR_UNSUPPORTED_METADATA;
}
if (metadata->gamma != 1.0f) {
- ALOGE("Unsupported metadata gamma: %f", metadata->gamma);
- return ERROR_JPEGR_UNSUPPORTED_METADATA;
+ ALOGE("Unsupported metadata gamma: %f", metadata->gamma);
+ return ERROR_JPEGR_UNSUPPORTED_METADATA;
}
if (metadata->offsetSdr != 0.0f || metadata->offsetHdr != 0.0f) {
- ALOGE("Unsupported metadata offset sdr, hdr: %f, %f", metadata->offsetSdr,
- metadata->offsetHdr);
- return ERROR_JPEGR_UNSUPPORTED_METADATA;
+ ALOGE("Unsupported metadata offset sdr, hdr: %f, %f", metadata->offsetSdr, metadata->offsetHdr);
+ return ERROR_JPEGR_UNSUPPORTED_METADATA;
}
- if (metadata->hdrCapacityMin != metadata->minContentBoost
- || metadata->hdrCapacityMax != metadata->maxContentBoost) {
- ALOGE("Unsupported metadata hdr capacity min, max: %f, %f", metadata->hdrCapacityMin,
- metadata->hdrCapacityMax);
- return ERROR_JPEGR_UNSUPPORTED_METADATA;
+ if (metadata->hdrCapacityMin != metadata->minContentBoost ||
+ metadata->hdrCapacityMax != metadata->maxContentBoost) {
+ ALOGE("Unsupported metadata hdr capacity min, max: %f, %f", metadata->hdrCapacityMin,
+ metadata->hdrCapacityMax);
+ return ERROR_JPEGR_UNSUPPORTED_METADATA;
}
// TODO: remove once map scaling factor is computed based on actual map dims
- size_t image_width = uncompressed_yuv_420_image->width;
- size_t image_height = uncompressed_yuv_420_image->height;
- size_t map_width = image_width / kMapDimensionScaleFactor;
- size_t map_height = image_height / kMapDimensionScaleFactor;
- map_width = static_cast<size_t>(
- floor((map_width + kJpegBlock - 1) / kJpegBlock)) * kJpegBlock;
- map_height = ((map_height + 1) >> 1) << 1;
- if (map_width != uncompressed_gain_map->width
- || map_height != uncompressed_gain_map->height) {
- ALOGE("gain map dimensions and primary image dimensions are not to scale");
+ size_t image_width = yuv420_image_ptr->width;
+ size_t image_height = yuv420_image_ptr->height;
+ size_t map_width = static_cast<size_t>(
+ floor((image_width + kMapDimensionScaleFactor - 1) / kMapDimensionScaleFactor));
+ size_t map_height = static_cast<size_t>(
+ floor((image_height + kMapDimensionScaleFactor - 1) / kMapDimensionScaleFactor));
+ if (map_width != gainmap_image_ptr->width || map_height != gainmap_image_ptr->height) {
+ ALOGE("gain map dimensions and primary image dimensions are not to scale, computed gain map "
+ "resolution is %dx%d, received gain map resolution is %dx%d",
+ (int)map_width, (int)map_height, gainmap_image_ptr->width, gainmap_image_ptr->height);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
- dest->width = uncompressed_yuv_420_image->width;
- dest->height = uncompressed_yuv_420_image->height;
+ dest->width = yuv420_image_ptr->width;
+ dest->height = yuv420_image_ptr->height;
ShepardsIDW idwTable(kMapDimensionScaleFactor);
float display_boost = std::min(max_display_boost, metadata->maxContentBoost);
GainLUT gainLUT(metadata, display_boost);
JobQueue jobQueue;
- std::function<void()> applyRecMap = [uncompressed_yuv_420_image, uncompressed_gain_map,
- metadata, dest, &jobQueue, &idwTable, output_format,
- &gainLUT, display_boost]() -> void {
- size_t width = uncompressed_yuv_420_image->width;
- size_t height = uncompressed_yuv_420_image->height;
+ std::function<void()> applyRecMap = [yuv420_image_ptr, gainmap_image_ptr, metadata, dest,
+ &jobQueue, &idwTable, output_format, &gainLUT,
+ display_boost]() -> void {
+ size_t width = yuv420_image_ptr->width;
+ size_t height = yuv420_image_ptr->height;
size_t rowStart, rowEnd;
while (jobQueue.dequeueJob(rowStart, rowEnd)) {
for (size_t y = rowStart; y < rowEnd; ++y) {
for (size_t x = 0; x < width; ++x) {
- Color yuv_gamma_sdr = getYuv420Pixel(uncompressed_yuv_420_image, x, y);
+ Color yuv_gamma_sdr = getYuv420Pixel(yuv420_image_ptr, x, y);
// Assuming the sdr image is a decoded JPEG, we should always use Rec.601 YUV coefficients
Color rgb_gamma_sdr = p3YuvToRgb(yuv_gamma_sdr);
// We are assuming the SDR base image is always sRGB transfer.
@@ -1025,9 +1028,9 @@
// Currently map_scale_factor is of type size_t, but it could be changed to a float
// later.
if (map_scale_factor != floorf(map_scale_factor)) {
- gain = sampleMap(uncompressed_gain_map, map_scale_factor, x, y);
+ gain = sampleMap(gainmap_image_ptr, map_scale_factor, x, y);
} else {
- gain = sampleMap(uncompressed_gain_map, map_scale_factor, x, y, idwTable);
+ gain = sampleMap(gainmap_image_ptr, map_scale_factor, x, y, idwTable);
}
#if USE_APPLY_GAIN_LUT
@@ -1039,14 +1042,12 @@
size_t pixel_idx = x + y * width;
switch (output_format) {
- case ULTRAHDR_OUTPUT_HDR_LINEAR:
- {
+ case ULTRAHDR_OUTPUT_HDR_LINEAR: {
uint64_t rgba_f16 = colorToRgbaF16(rgb_hdr);
reinterpret_cast<uint64_t*>(dest->data)[pixel_idx] = rgba_f16;
break;
}
- case ULTRAHDR_OUTPUT_HDR_HLG:
- {
+ case ULTRAHDR_OUTPUT_HDR_HLG: {
#if USE_HLG_OETF_LUT
ColorTransformFn hdrOetf = hlgOetfLUT;
#else
@@ -1057,8 +1058,7 @@
reinterpret_cast<uint32_t*>(dest->data)[pixel_idx] = rgba_1010102;
break;
}
- case ULTRAHDR_OUTPUT_HDR_PQ:
- {
+ case ULTRAHDR_OUTPUT_HDR_PQ: {
#if USE_PQ_OETF_LUT
ColorTransformFn hdrOetf = pqOetfLUT;
#else
@@ -1069,8 +1069,8 @@
reinterpret_cast<uint32_t*>(dest->data)[pixel_idx] = rgba_1010102;
break;
}
- default:
- {}
+ default: {
+ }
// Should be impossible to hit after input validation.
}
}
@@ -1083,9 +1083,9 @@
for (int th = 0; th < threads - 1; th++) {
workers.push_back(std::thread(applyRecMap));
}
- const int rowStep = threads == 1 ? uncompressed_yuv_420_image->height : kJobSzInRows;
- for (int rowStart = 0; rowStart < uncompressed_yuv_420_image->height;) {
- int rowEnd = std::min(rowStart + rowStep, uncompressed_yuv_420_image->height);
+ const int rowStep = threads == 1 ? yuv420_image_ptr->height : kJobSzInRows;
+ for (int rowStart = 0; rowStart < yuv420_image_ptr->height;) {
+ int rowEnd = std::min(rowStart + rowStep, yuv420_image_ptr->height);
jobQueue.enqueueJob(rowStart, rowEnd);
rowStart = rowEnd;
}
@@ -1095,18 +1095,18 @@
return NO_ERROR;
}
-status_t JpegR::extractPrimaryImageAndGainMap(jr_compressed_ptr compressed_jpegr_image,
- jr_compressed_ptr primary_image,
- jr_compressed_ptr gain_map) {
- if (compressed_jpegr_image == nullptr) {
+status_t JpegR::extractPrimaryImageAndGainMap(jr_compressed_ptr jpegr_image_ptr,
+ jr_compressed_ptr primary_jpg_image_ptr,
+ jr_compressed_ptr gainmap_jpg_image_ptr) {
+ if (jpegr_image_ptr == nullptr) {
return ERROR_JPEGR_INVALID_NULL_PTR;
}
MessageHandler msg_handler;
std::shared_ptr<DataSegment> seg =
- DataSegment::Create(DataRange(0, compressed_jpegr_image->length),
- static_cast<const uint8_t*>(compressed_jpegr_image->data),
- DataSegment::BufferDispositionPolicy::kDontDelete);
+ DataSegment::Create(DataRange(0, jpegr_image_ptr->length),
+ static_cast<const uint8_t*>(jpegr_image_ptr->data),
+ DataSegment::BufferDispositionPolicy::kDontDelete);
DataSegmentDataSource data_source(seg);
JpegInfoBuilder jpeg_info_builder;
jpeg_info_builder.SetImageLimit(2);
@@ -1125,20 +1125,20 @@
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
- if (primary_image != nullptr) {
- primary_image->data = static_cast<uint8_t*>(compressed_jpegr_image->data) +
- image_ranges[0].GetBegin();
- primary_image->length = image_ranges[0].GetLength();
+ if (primary_jpg_image_ptr != nullptr) {
+ primary_jpg_image_ptr->data =
+ static_cast<uint8_t*>(jpegr_image_ptr->data) + image_ranges[0].GetBegin();
+ primary_jpg_image_ptr->length = image_ranges[0].GetLength();
}
if (image_ranges.size() == 1) {
return ERROR_JPEGR_GAIN_MAP_IMAGE_NOT_FOUND;
}
- if (gain_map != nullptr) {
- gain_map->data = static_cast<uint8_t*>(compressed_jpegr_image->data) +
- image_ranges[1].GetBegin();
- gain_map->length = image_ranges[1].GetLength();
+ if (gainmap_jpg_image_ptr != nullptr) {
+ gainmap_jpg_image_ptr->data =
+ static_cast<uint8_t*>(jpegr_image_ptr->data) + image_ranges[1].GetBegin();
+ gainmap_jpg_image_ptr->length = image_ranges[1].GetLength();
}
// TODO: choose primary image and gain map image carefully
@@ -1183,58 +1183,48 @@
// Exif 2.2 spec for EXIF marker
// Adobe XMP spec part 3 for XMP marker
// ICC v4.3 spec for ICC
-status_t JpegR::appendGainMap(jr_compressed_ptr compressed_jpeg_image,
- jr_compressed_ptr compressed_gain_map,
- jr_exif_ptr exif,
- void* icc, size_t icc_size,
- ultrahdr_metadata_ptr metadata,
+status_t JpegR::appendGainMap(jr_compressed_ptr primary_jpg_image_ptr,
+ jr_compressed_ptr gainmap_jpg_image_ptr, jr_exif_ptr exif, void* icc,
+ size_t icc_size, ultrahdr_metadata_ptr metadata,
jr_compressed_ptr dest) {
- if (compressed_jpeg_image == nullptr
- || compressed_gain_map == nullptr
- || metadata == nullptr
- || dest == nullptr) {
+ if (primary_jpg_image_ptr == nullptr || gainmap_jpg_image_ptr == nullptr || metadata == nullptr ||
+ dest == nullptr) {
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
if (metadata->version.compare("1.0")) {
ALOGE("received bad value for version: %s", metadata->version.c_str());
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
if (metadata->maxContentBoost < metadata->minContentBoost) {
ALOGE("received bad value for content boost min %f, max %f", metadata->minContentBoost,
- metadata->maxContentBoost);
+ metadata->maxContentBoost);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
if (metadata->hdrCapacityMax < metadata->hdrCapacityMin || metadata->hdrCapacityMin < 1.0f) {
ALOGE("received bad value for hdr capacity min %f, max %f", metadata->hdrCapacityMin,
- metadata->hdrCapacityMax);
+ metadata->hdrCapacityMax);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
if (metadata->offsetSdr < 0.0f || metadata->offsetHdr < 0.0f) {
- ALOGE("received bad value for offset sdr %f, hdr %f", metadata->offsetSdr,
- metadata->offsetHdr);
+ ALOGE("received bad value for offset sdr %f, hdr %f", metadata->offsetSdr, metadata->offsetHdr);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
-
if (metadata->gamma <= 0.0f) {
ALOGE("received bad value for gamma %f", metadata->gamma);
return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
const string nameSpace = "http://ns.adobe.com/xap/1.0/";
- const int nameSpaceLength = nameSpace.size() + 1; // need to count the null terminator
+ const int nameSpaceLength = nameSpace.size() + 1; // need to count the null terminator
// calculate secondary image length first, because the length will be written into the primary
// image xmp
const string xmp_secondary = generateXmpForSecondaryImage(*metadata);
const int xmp_secondary_length = 2 /* 2 bytes representing the length of the package */
- + nameSpaceLength /* 29 bytes length of name space including \0 */
- + xmp_secondary.size(); /* length of xmp packet */
+ + nameSpaceLength /* 29 bytes length of name space including \0 */
+ + xmp_secondary.size(); /* length of xmp packet */
const int secondary_image_size = 2 /* 2 bytes length of APP1 sign */
- + xmp_secondary_length
- + compressed_gain_map->length;
+ + xmp_secondary_length + gainmap_jpg_image_ptr->length;
// primary image
const string xmp_primary = generateXmpForPrimaryImage(secondary_image_size, *metadata);
// same as primary
@@ -1273,41 +1263,39 @@
// Write ICC
if (icc != nullptr && icc_size > 0) {
- const int length = icc_size + 2;
- const uint8_t lengthH = ((length >> 8) & 0xff);
- const uint8_t lengthL = (length & 0xff);
- JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos));
- JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kAPP2, 1, pos));
- JPEGR_CHECK(Write(dest, &lengthH, 1, pos));
- JPEGR_CHECK(Write(dest, &lengthL, 1, pos));
- JPEGR_CHECK(Write(dest, icc, icc_size, pos));
+ const int length = icc_size + 2;
+ const uint8_t lengthH = ((length >> 8) & 0xff);
+ const uint8_t lengthL = (length & 0xff);
+ JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos));
+ JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kAPP2, 1, pos));
+ JPEGR_CHECK(Write(dest, &lengthH, 1, pos));
+ JPEGR_CHECK(Write(dest, &lengthL, 1, pos));
+ JPEGR_CHECK(Write(dest, icc, icc_size, pos));
}
// Prepare and write MPF
{
- const int length = 2 + calculateMpfSize();
- const uint8_t lengthH = ((length >> 8) & 0xff);
- const uint8_t lengthL = (length & 0xff);
- int primary_image_size = pos + length + compressed_jpeg_image->length;
- // between APP2 + package size + signature
- // ff e2 00 58 4d 50 46 00
- // 2 + 2 + 4 = 8 (bytes)
- // and ff d8 sign of the secondary image
- int secondary_image_offset = primary_image_size - pos - 8;
- sp<DataStruct> mpf = generateMpf(primary_image_size,
- 0, /* primary_image_offset */
- secondary_image_size,
- secondary_image_offset);
- JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos));
- JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kAPP2, 1, pos));
- JPEGR_CHECK(Write(dest, &lengthH, 1, pos));
- JPEGR_CHECK(Write(dest, &lengthL, 1, pos));
- JPEGR_CHECK(Write(dest, (void*)mpf->getData(), mpf->getLength(), pos));
+ const int length = 2 + calculateMpfSize();
+ const uint8_t lengthH = ((length >> 8) & 0xff);
+ const uint8_t lengthL = (length & 0xff);
+ int primary_image_size = pos + length + primary_jpg_image_ptr->length;
+ // between APP2 + package size + signature
+ // ff e2 00 58 4d 50 46 00
+ // 2 + 2 + 4 = 8 (bytes)
+ // and ff d8 sign of the secondary image
+ int secondary_image_offset = primary_image_size - pos - 8;
+ sp<DataStruct> mpf = generateMpf(primary_image_size, 0, /* primary_image_offset */
+ secondary_image_size, secondary_image_offset);
+ JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos));
+ JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kAPP2, 1, pos));
+ JPEGR_CHECK(Write(dest, &lengthH, 1, pos));
+ JPEGR_CHECK(Write(dest, &lengthL, 1, pos));
+ JPEGR_CHECK(Write(dest, (void*)mpf->getData(), mpf->getLength(), pos));
}
// Write primary image
- JPEGR_CHECK(Write(dest,
- (uint8_t*)compressed_jpeg_image->data + 2, compressed_jpeg_image->length - 2, pos));
+ JPEGR_CHECK(Write(dest, (uint8_t*)primary_jpg_image_ptr->data + 2,
+ primary_jpg_image_ptr->length - 2, pos));
// Finish primary image
// Begin secondary image (gain map)
@@ -1329,8 +1317,8 @@
}
// Write secondary image
- JPEGR_CHECK(Write(dest,
- (uint8_t*)compressed_gain_map->data + 2, compressed_gain_map->length - 2, pos));
+ JPEGR_CHECK(Write(dest, (uint8_t*)gainmap_jpg_image_ptr->data + 2,
+ gainmap_jpg_image_ptr->length - 2, pos));
// Set back length
dest->length = pos;
@@ -1343,62 +1331,52 @@
if (src == nullptr || dest == nullptr) {
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- uint16_t* src_luma_data = reinterpret_cast<uint16_t*>(src->data);
- size_t src_luma_stride = src->luma_stride == 0 ? src->width : src->luma_stride;
-
- uint16_t* src_chroma_data;
- size_t src_chroma_stride;
- if (src->chroma_data == nullptr) {
- src_chroma_stride = src_luma_stride;
- src_chroma_data = &reinterpret_cast<uint16_t*>(src->data)[src_luma_stride * src->height];
- } else {
- src_chroma_stride = src->chroma_stride;
- src_chroma_data = reinterpret_cast<uint16_t*>(src->chroma_data);
+ if (src->width != dest->width || src->height != dest->height) {
+ return ERROR_JPEGR_INVALID_INPUT_TYPE;
}
- dest->width = src->width;
- dest->height = src->height;
-
- size_t dest_luma_pixel_count = dest->width * dest->height;
-
+ uint16_t* src_y_data = reinterpret_cast<uint16_t*>(src->data);
+ uint8_t* dst_y_data = reinterpret_cast<uint8_t*>(dest->data);
for (size_t y = 0; y < src->height; ++y) {
+ uint16_t* src_y_row = src_y_data + y * src->luma_stride;
+ uint8_t* dst_y_row = dst_y_data + y * dest->luma_stride;
for (size_t x = 0; x < src->width; ++x) {
- size_t src_y_idx = y * src_luma_stride + x;
- size_t src_u_idx = (y >> 1) * src_chroma_stride + (x & ~0x1);
- size_t src_v_idx = src_u_idx + 1;
-
- uint16_t y_uint = src_luma_data[src_y_idx] >> 6;
- uint16_t u_uint = src_chroma_data[src_u_idx] >> 6;
- uint16_t v_uint = src_chroma_data[src_v_idx] >> 6;
-
- size_t dest_y_idx = x + y * dest->width;
- size_t dest_uv_idx = x / 2 + (y / 2) * (dest->width / 2);
-
- uint8_t* y = &reinterpret_cast<uint8_t*>(dest->data)[dest_y_idx];
- uint8_t* u = &reinterpret_cast<uint8_t*>(dest->data)[dest_luma_pixel_count + dest_uv_idx];
- uint8_t* v = &reinterpret_cast<uint8_t*>(
- dest->data)[dest_luma_pixel_count * 5 / 4 + dest_uv_idx];
-
- *y = static_cast<uint8_t>((y_uint >> 2) & 0xff);
- *u = static_cast<uint8_t>((u_uint >> 2) & 0xff);
- *v = static_cast<uint8_t>((v_uint >> 2) & 0xff);
+ uint16_t y_uint = src_y_row[x] >> 6;
+ dst_y_row[x] = static_cast<uint8_t>((y_uint >> 2) & 0xff);
+ }
+ if (dest->width != dest->luma_stride) {
+ memset(dst_y_row + dest->width, 0, dest->luma_stride - dest->width);
}
}
-
+ uint16_t* src_uv_data = reinterpret_cast<uint16_t*>(src->chroma_data);
+ uint8_t* dst_u_data = reinterpret_cast<uint8_t*>(dest->chroma_data);
+ size_t dst_v_offset = (dest->chroma_stride * dest->height / 2);
+ uint8_t* dst_v_data = dst_u_data + dst_v_offset;
+ for (size_t y = 0; y < src->height / 2; ++y) {
+ uint16_t* src_uv_row = src_uv_data + y * src->chroma_stride;
+ uint8_t* dst_u_row = dst_u_data + y * dest->chroma_stride;
+ uint8_t* dst_v_row = dst_v_data + y * dest->chroma_stride;
+ for (size_t x = 0; x < src->width / 2; ++x) {
+ uint16_t u_uint = src_uv_row[x << 1] >> 6;
+ uint16_t v_uint = src_uv_row[(x << 1) + 1] >> 6;
+ dst_u_row[x] = static_cast<uint8_t>((u_uint >> 2) & 0xff);
+ dst_v_row[x] = static_cast<uint8_t>((v_uint >> 2) & 0xff);
+ }
+ if (dest->width / 2 != dest->chroma_stride) {
+ memset(dst_u_row + dest->width / 2, 0, dest->chroma_stride - dest->width / 2);
+ memset(dst_v_row + dest->width / 2, 0, dest->chroma_stride - dest->width / 2);
+ }
+ }
dest->colorGamut = src->colorGamut;
-
return NO_ERROR;
}
-status_t JpegR::convertYuv(jr_uncompressed_ptr image,
- ultrahdr_color_gamut src_encoding,
+status_t JpegR::convertYuv(jr_uncompressed_ptr image, ultrahdr_color_gamut src_encoding,
ultrahdr_color_gamut dest_encoding) {
if (image == nullptr) {
return ERROR_JPEGR_INVALID_NULL_PTR;
}
-
- if (src_encoding == ULTRAHDR_COLORGAMUT_UNSPECIFIED
- || dest_encoding == ULTRAHDR_COLORGAMUT_UNSPECIFIED) {
+ if (src_encoding == ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
+ dest_encoding == ULTRAHDR_COLORGAMUT_UNSPECIFIED) {
return ERROR_JPEGR_INVALID_COLORGAMUT;
}
diff --git a/libs/ultrahdr/tests/gainmapmath_test.cpp b/libs/ultrahdr/tests/gainmapmath_test.cpp
index af90365..7c2d076 100644
--- a/libs/ultrahdr/tests/gainmapmath_test.cpp
+++ b/libs/ultrahdr/tests/gainmapmath_test.cpp
@@ -120,7 +120,7 @@
0xB0, 0xB1,
0xB2, 0xB3,
};
- return { pixels, 4, 4, ULTRAHDR_COLORGAMUT_BT709 };
+ return { pixels, 4, 4, ULTRAHDR_COLORGAMUT_BT709, pixels + 16, 4, 2 };
}
Color (*Yuv420Colors())[4] {
@@ -153,7 +153,7 @@
0xA0 << 6, 0xB0 << 6, 0xA1 << 6, 0xB1 << 6,
0xA2 << 6, 0xB2 << 6, 0xA3 << 6, 0xB3 << 6,
};
- return { pixels, 4, 4, ULTRAHDR_COLORGAMUT_BT709 };
+ return { pixels, 4, 4, ULTRAHDR_COLORGAMUT_BT709, pixels + 16, 4, 4 };
}
Color (*P010Colors())[4] {
@@ -636,6 +636,9 @@
memcpy(out_buf.get(), input.data, out_buf_size);
jpegr_uncompressed_struct output = Yuv420Image();
output.data = out_buf.get();
+ output.chroma_data = out_buf.get() + input.width * input.height;
+ output.luma_stride = input.width;
+ output.chroma_stride = input.width / 2;
transformYuv420(&output, 1, 1, transform);
diff --git a/libs/ultrahdr/tests/jpegencoderhelper_test.cpp b/libs/ultrahdr/tests/jpegencoderhelper_test.cpp
index f0e1fa4..33cb9f6 100644
--- a/libs/ultrahdr/tests/jpegencoderhelper_test.cpp
+++ b/libs/ultrahdr/tests/jpegencoderhelper_test.cpp
@@ -42,6 +42,7 @@
};
JpegEncoderHelperTest();
~JpegEncoderHelperTest();
+
protected:
virtual void SetUp();
virtual void TearDown();
@@ -103,24 +104,32 @@
TEST_F(JpegEncoderHelperTest, encodeAlignedImage) {
JpegEncoderHelper encoder;
- EXPECT_TRUE(encoder.compressImage(mAlignedImage.buffer.get(), mAlignedImage.width,
- mAlignedImage.height, JPEG_QUALITY, NULL, 0));
+ EXPECT_TRUE(encoder.compressImage(mAlignedImage.buffer.get(),
+ mAlignedImage.buffer.get() +
+ mAlignedImage.width * mAlignedImage.height,
+ mAlignedImage.width, mAlignedImage.height,
+ mAlignedImage.width, mAlignedImage.width / 2, JPEG_QUALITY,
+ NULL, 0));
ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
}
TEST_F(JpegEncoderHelperTest, encodeUnalignedImage) {
JpegEncoderHelper encoder;
- EXPECT_TRUE(encoder.compressImage(mUnalignedImage.buffer.get(), mUnalignedImage.width,
- mUnalignedImage.height, JPEG_QUALITY, NULL, 0));
+ EXPECT_TRUE(encoder.compressImage(mUnalignedImage.buffer.get(),
+ mUnalignedImage.buffer.get() +
+ mUnalignedImage.width * mUnalignedImage.height,
+ mUnalignedImage.width, mUnalignedImage.height,
+ mUnalignedImage.width, mUnalignedImage.width / 2,
+ JPEG_QUALITY, NULL, 0));
ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
}
TEST_F(JpegEncoderHelperTest, encodeSingleChannelImage) {
JpegEncoderHelper encoder;
- EXPECT_TRUE(encoder.compressImage(mSingleChannelImage.buffer.get(), mSingleChannelImage.width,
- mSingleChannelImage.height, JPEG_QUALITY, NULL, 0, true));
+ EXPECT_TRUE(encoder.compressImage(mSingleChannelImage.buffer.get(), nullptr,
+ mSingleChannelImage.width, mSingleChannelImage.height,
+ mSingleChannelImage.width, 0, JPEG_QUALITY, NULL, 0));
ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
}
-} // namespace android::ultrahdr
-
+} // namespace android::ultrahdr
diff --git a/libs/ultrahdr/tests/jpegr_test.cpp b/libs/ultrahdr/tests/jpegr_test.cpp
index 7837bcf..a750867 100644
--- a/libs/ultrahdr/tests/jpegr_test.cpp
+++ b/libs/ultrahdr/tests/jpegr_test.cpp
@@ -728,23 +728,23 @@
rawImg420->width = kWidth;
rawImg420->height = kHeight;
- rawImg420->luma_stride = kWidth;
+ rawImg420->luma_stride = kWidth - 2;
ASSERT_NE(uHdrLib.encodeJPEGR(rawImgP010, rawImg420,
ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
jpgImg.getImageHandle(), kQuality, nullptr),
OK)
- << "fail, API allows luma stride for 420";
+ << "fail, API allows bad luma stride for 420";
rawImg420->width = kWidth;
rawImg420->height = kHeight;
rawImg420->luma_stride = 0;
rawImg420->chroma_data = rawImgP010->data;
- rawImg420->chroma_stride = kWidth;
+ rawImg420->chroma_stride = kWidth / 2 - 2;
ASSERT_NE(uHdrLib.encodeJPEGR(rawImgP010, rawImg420,
ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
jpgImg.getImageHandle(), kQuality, nullptr),
OK)
- << "fail, API allows bad chroma pointer for 420";
+ << "fail, API allows bad chroma stride for 420";
}
}
@@ -1021,23 +1021,23 @@
rawImg420->width = kWidth;
rawImg420->height = kHeight;
- rawImg420->luma_stride = kWidth;
+ rawImg420->luma_stride = kWidth - 2;
ASSERT_NE(uHdrLib.encodeJPEGR(rawImgP010, rawImg420, jpgImg.getImageHandle(),
ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
jpgImg.getImageHandle()),
OK)
- << "fail, API allows luma stride for 420";
+ << "fail, API allows bad luma stride for 420";
rawImg420->width = kWidth;
rawImg420->height = kHeight;
rawImg420->luma_stride = 0;
rawImg420->chroma_data = rawImgP010->data;
- rawImg420->chroma_stride = kWidth;
+ rawImg420->chroma_stride = kWidth / 2 - 2;
ASSERT_NE(uHdrLib.encodeJPEGR(rawImgP010, rawImg420, jpgImg.getImageHandle(),
ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
jpgImg.getImageHandle()),
OK)
- << "fail, API allows bad chroma pointer for 420";
+ << "fail, API allows bad chroma stride for 420";
}
}
@@ -1375,11 +1375,21 @@
EXPECT_FLOAT_EQ(metadata_expected.hdrCapacityMax, metadata_read.hdrCapacityMax);
}
+class JpegRAPIEncodeAndDecodeTest
+ : public ::testing::TestWithParam<std::tuple<ultrahdr_color_gamut, ultrahdr_color_gamut>> {
+public:
+ JpegRAPIEncodeAndDecodeTest()
+ : mP010ColorGamut(std::get<0>(GetParam())), mYuv420ColorGamut(std::get<1>(GetParam())){};
+
+ const ultrahdr_color_gamut mP010ColorGamut;
+ const ultrahdr_color_gamut mYuv420ColorGamut;
+};
+
/* Test Encode API-0 and Decode */
-TEST(JpegRTest, EncodeAPI0AndDecodeTest) {
+TEST_P(JpegRAPIEncodeAndDecodeTest, EncodeAPI0AndDecodeTest) {
// reference encode
UhdrUnCompressedStructWrapper rawImg(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg.allocateMemory());
ASSERT_TRUE(rawImg.loadRawResource(kYCbCrP010FileName));
UhdrCompressedStructWrapper jpgImg(kImageWidth, kImageHeight);
@@ -1392,8 +1402,8 @@
// encode with luma stride set
{
UhdrUnCompressedStructWrapper rawImg2(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
- ASSERT_TRUE(rawImg2.setImageStride(kImageWidth + 128, 0));
+ ASSERT_TRUE(rawImg2.setImageColorGamut(mP010ColorGamut));
+ ASSERT_TRUE(rawImg2.setImageStride(kImageWidth + 18, 0));
ASSERT_TRUE(rawImg2.allocateMemory());
ASSERT_TRUE(rawImg2.loadRawResource(kYCbCrP010FileName));
UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
@@ -1410,8 +1420,8 @@
// encode with luma and chroma stride set
{
UhdrUnCompressedStructWrapper rawImg2(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
- ASSERT_TRUE(rawImg2.setImageStride(kImageWidth + 128, kImageWidth + 256));
+ ASSERT_TRUE(rawImg2.setImageColorGamut(mP010ColorGamut));
+ ASSERT_TRUE(rawImg2.setImageStride(kImageWidth + 18, kImageWidth + 28));
ASSERT_TRUE(rawImg2.setChromaMode(false));
ASSERT_TRUE(rawImg2.allocateMemory());
ASSERT_TRUE(rawImg2.loadRawResource(kYCbCrP010FileName));
@@ -1429,8 +1439,8 @@
// encode with chroma stride set
{
UhdrUnCompressedStructWrapper rawImg2(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
- ASSERT_TRUE(rawImg2.setImageStride(0, kImageWidth + 64));
+ ASSERT_TRUE(rawImg2.setImageColorGamut(mP010ColorGamut));
+ ASSERT_TRUE(rawImg2.setImageStride(0, kImageWidth + 34));
ASSERT_TRUE(rawImg2.setChromaMode(false));
ASSERT_TRUE(rawImg2.allocateMemory());
ASSERT_TRUE(rawImg2.loadRawResource(kYCbCrP010FileName));
@@ -1445,6 +1455,24 @@
ASSERT_EQ(jpg1->length, jpg2->length);
ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
}
+ // encode with luma and chroma stride set but no chroma ptr
+ {
+ UhdrUnCompressedStructWrapper rawImg2(kImageWidth, kImageHeight, YCbCr_p010);
+ ASSERT_TRUE(rawImg2.setImageColorGamut(mP010ColorGamut));
+ ASSERT_TRUE(rawImg2.setImageStride(kImageWidth, kImageWidth + 38));
+ ASSERT_TRUE(rawImg2.allocateMemory());
+ ASSERT_TRUE(rawImg2.loadRawResource(kYCbCrP010FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImg2.getImageHandle(),
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle(), kQuality, nullptr),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
auto jpg1 = jpgImg.getImageHandle();
#ifdef DUMP_OUTPUT
@@ -1457,13 +1485,13 @@
}
/* Test Encode API-1 and Decode */
-TEST(JpegRTest, EncodeAPI1AndDecodeTest) {
+TEST_P(JpegRAPIEncodeAndDecodeTest, EncodeAPI1AndDecodeTest) {
UhdrUnCompressedStructWrapper rawImgP010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImgP010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImgP010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImgP010.allocateMemory());
ASSERT_TRUE(rawImgP010.loadRawResource(kYCbCrP010FileName));
UhdrUnCompressedStructWrapper rawImg420(kImageWidth, kImageHeight, YCbCr_420);
- ASSERT_TRUE(rawImg420.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT709));
+ ASSERT_TRUE(rawImg420.setImageColorGamut(mYuv420ColorGamut));
ASSERT_TRUE(rawImg420.allocateMemory());
ASSERT_TRUE(rawImg420.loadRawResource(kYCbCr420FileName));
UhdrCompressedStructWrapper jpgImg(kImageWidth, kImageHeight);
@@ -1473,10 +1501,10 @@
ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
jpgImg.getImageHandle(), kQuality, nullptr),
OK);
- // encode with luma stride set
+ // encode with luma stride set p010
{
UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2P010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg2P010.setImageStride(kImageWidth + 128, 0));
ASSERT_TRUE(rawImg2P010.allocateMemory());
ASSERT_TRUE(rawImg2P010.loadRawResource(kYCbCrP010FileName));
@@ -1491,10 +1519,10 @@
ASSERT_EQ(jpg1->length, jpg2->length);
ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
}
- // encode with luma and chroma stride set
+ // encode with luma and chroma stride set p010
{
UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2P010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg2P010.setImageStride(kImageWidth + 128, kImageWidth + 256));
ASSERT_TRUE(rawImg2P010.setChromaMode(false));
ASSERT_TRUE(rawImg2P010.allocateMemory());
@@ -1510,10 +1538,10 @@
ASSERT_EQ(jpg1->length, jpg2->length);
ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
}
- // encode with chroma stride set
+ // encode with chroma stride set p010
{
UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2P010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg2P010.setImageStride(0, kImageWidth + 64));
ASSERT_TRUE(rawImg2P010.setChromaMode(false));
ASSERT_TRUE(rawImg2P010.allocateMemory());
@@ -1529,6 +1557,98 @@
ASSERT_EQ(jpg1->length, jpg2->length);
ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
}
+ // encode with luma and chroma stride set but no chroma ptr p010
+ {
+ UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
+ ASSERT_TRUE(rawImg2P010.setImageStride(kImageWidth + 64, kImageWidth + 256));
+ ASSERT_TRUE(rawImg2P010.allocateMemory());
+ ASSERT_TRUE(rawImg2P010.loadRawResource(kYCbCrP010FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImg2P010.getImageHandle(), rawImg420.getImageHandle(),
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle(), kQuality, nullptr),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
+ // encode with luma stride set 420
+ {
+ UhdrUnCompressedStructWrapper rawImg2420(kImageWidth, kImageHeight, YCbCr_420);
+ ASSERT_TRUE(rawImg2420.setImageColorGamut(mYuv420ColorGamut));
+ ASSERT_TRUE(rawImg2420.setImageStride(kImageWidth + 14, 0));
+ ASSERT_TRUE(rawImg2420.allocateMemory());
+ ASSERT_TRUE(rawImg2420.loadRawResource(kYCbCr420FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImgP010.getImageHandle(), rawImg2420.getImageHandle(),
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle(), kQuality, nullptr),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
+ // encode with luma and chroma stride set 420
+ {
+ UhdrUnCompressedStructWrapper rawImg2420(kImageWidth, kImageHeight, YCbCr_420);
+ ASSERT_TRUE(rawImg2420.setImageColorGamut(mYuv420ColorGamut));
+ ASSERT_TRUE(rawImg2420.setImageStride(kImageWidth + 46, kImageWidth / 2 + 34));
+ ASSERT_TRUE(rawImg2420.setChromaMode(false));
+ ASSERT_TRUE(rawImg2420.allocateMemory());
+ ASSERT_TRUE(rawImg2420.loadRawResource(kYCbCr420FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImgP010.getImageHandle(), rawImg2420.getImageHandle(),
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle(), kQuality, nullptr),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
+ // encode with chroma stride set 420
+ {
+ UhdrUnCompressedStructWrapper rawImg2420(kImageWidth, kImageHeight, YCbCr_420);
+ ASSERT_TRUE(rawImg2420.setImageColorGamut(mYuv420ColorGamut));
+ ASSERT_TRUE(rawImg2420.setImageStride(0, kImageWidth / 2 + 38));
+ ASSERT_TRUE(rawImg2420.setChromaMode(false));
+ ASSERT_TRUE(rawImg2420.allocateMemory());
+ ASSERT_TRUE(rawImg2420.loadRawResource(kYCbCr420FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImgP010.getImageHandle(), rawImg2420.getImageHandle(),
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle(), kQuality, nullptr),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
+ // encode with luma and chroma stride set but no chroma ptr 420
+ {
+ UhdrUnCompressedStructWrapper rawImg2420(kImageWidth, kImageHeight, YCbCr_420);
+ ASSERT_TRUE(rawImg2420.setImageColorGamut(mYuv420ColorGamut));
+ ASSERT_TRUE(rawImg2420.setImageStride(kImageWidth + 26, kImageWidth / 2 + 44));
+ ASSERT_TRUE(rawImg2420.allocateMemory());
+ ASSERT_TRUE(rawImg2420.loadRawResource(kYCbCr420FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImgP010.getImageHandle(), rawImg2420.getImageHandle(),
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle(), kQuality, nullptr),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
auto jpg1 = jpgImg.getImageHandle();
@@ -1542,13 +1662,13 @@
}
/* Test Encode API-2 and Decode */
-TEST(JpegRTest, EncodeAPI2AndDecodeTest) {
+TEST_P(JpegRAPIEncodeAndDecodeTest, EncodeAPI2AndDecodeTest) {
UhdrUnCompressedStructWrapper rawImgP010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImgP010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImgP010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImgP010.allocateMemory());
ASSERT_TRUE(rawImgP010.loadRawResource(kYCbCrP010FileName));
UhdrUnCompressedStructWrapper rawImg420(kImageWidth, kImageHeight, YCbCr_420);
- ASSERT_TRUE(rawImg420.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT709));
+ ASSERT_TRUE(rawImg420.setImageColorGamut(mYuv420ColorGamut));
ASSERT_TRUE(rawImg420.allocateMemory());
ASSERT_TRUE(rawImg420.loadRawResource(kYCbCr420FileName));
UhdrCompressedStructWrapper jpgImg(kImageWidth, kImageHeight);
@@ -1565,7 +1685,7 @@
// encode with luma stride set
{
UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2P010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg2P010.setImageStride(kImageWidth + 128, 0));
ASSERT_TRUE(rawImg2P010.allocateMemory());
ASSERT_TRUE(rawImg2P010.loadRawResource(kYCbCrP010FileName));
@@ -1583,7 +1703,7 @@
// encode with luma and chroma stride set
{
UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2P010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg2P010.setImageStride(kImageWidth + 128, kImageWidth + 256));
ASSERT_TRUE(rawImg2P010.setChromaMode(false));
ASSERT_TRUE(rawImg2P010.allocateMemory());
@@ -1602,7 +1722,7 @@
// encode with chroma stride set
{
UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2P010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg2P010.setImageStride(0, kImageWidth + 64));
ASSERT_TRUE(rawImg2P010.setChromaMode(false));
ASSERT_TRUE(rawImg2P010.allocateMemory());
@@ -1618,6 +1738,62 @@
ASSERT_EQ(jpg1->length, jpg2->length);
ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
}
+ // encode with luma stride set
+ {
+ UhdrUnCompressedStructWrapper rawImg2420(kImageWidth, kImageHeight, YCbCr_420);
+ ASSERT_TRUE(rawImg2420.setImageColorGamut(mYuv420ColorGamut));
+ ASSERT_TRUE(rawImg2420.setImageStride(kImageWidth + 128, 0));
+ ASSERT_TRUE(rawImg2420.allocateMemory());
+ ASSERT_TRUE(rawImg2420.loadRawResource(kYCbCr420FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImgP010.getImageHandle(), rawImg2420.getImageHandle(), sdr,
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle()),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
+ // encode with luma and chroma stride set
+ {
+ UhdrUnCompressedStructWrapper rawImg2420(kImageWidth, kImageHeight, YCbCr_420);
+ ASSERT_TRUE(rawImg2420.setImageColorGamut(mYuv420ColorGamut));
+ ASSERT_TRUE(rawImg2420.setImageStride(kImageWidth + 128, kImageWidth + 256));
+ ASSERT_TRUE(rawImg2420.setChromaMode(false));
+ ASSERT_TRUE(rawImg2420.allocateMemory());
+ ASSERT_TRUE(rawImg2420.loadRawResource(kYCbCr420FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImgP010.getImageHandle(), rawImg2420.getImageHandle(), sdr,
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle()),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
+ // encode with chroma stride set
+ {
+ UhdrUnCompressedStructWrapper rawImg2420(kImageWidth, kImageHeight, YCbCr_420);
+ ASSERT_TRUE(rawImg2420.setImageColorGamut(mYuv420ColorGamut));
+ ASSERT_TRUE(rawImg2420.setImageStride(0, kImageWidth + 64));
+ ASSERT_TRUE(rawImg2420.setChromaMode(false));
+ ASSERT_TRUE(rawImg2420.allocateMemory());
+ ASSERT_TRUE(rawImg2420.loadRawResource(kYCbCr420FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImgP010.getImageHandle(), rawImg2420.getImageHandle(), sdr,
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle()),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
auto jpg1 = jpgImg.getImageHandle();
@@ -1631,9 +1807,9 @@
}
/* Test Encode API-3 and Decode */
-TEST(JpegRTest, EncodeAPI3AndDecodeTest) {
+TEST_P(JpegRAPIEncodeAndDecodeTest, EncodeAPI3AndDecodeTest) {
UhdrUnCompressedStructWrapper rawImgP010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImgP010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImgP010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImgP010.allocateMemory());
ASSERT_TRUE(rawImgP010.loadRawResource(kYCbCrP010FileName));
UhdrCompressedStructWrapper jpgImg(kImageWidth, kImageHeight);
@@ -1650,7 +1826,7 @@
// encode with luma stride set
{
UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2P010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg2P010.setImageStride(kImageWidth + 128, 0));
ASSERT_TRUE(rawImg2P010.allocateMemory());
ASSERT_TRUE(rawImg2P010.loadRawResource(kYCbCrP010FileName));
@@ -1668,7 +1844,7 @@
// encode with luma and chroma stride set
{
UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2P010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg2P010.setImageStride(kImageWidth + 128, kImageWidth + 256));
ASSERT_TRUE(rawImg2P010.setChromaMode(false));
ASSERT_TRUE(rawImg2P010.allocateMemory());
@@ -1687,7 +1863,7 @@
// encode with chroma stride set
{
UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
- ASSERT_TRUE(rawImg2P010.setImageColorGamut(ultrahdr_color_gamut::ULTRAHDR_COLORGAMUT_BT2100));
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
ASSERT_TRUE(rawImg2P010.setImageStride(0, kImageWidth + 64));
ASSERT_TRUE(rawImg2P010.setChromaMode(false));
ASSERT_TRUE(rawImg2P010.allocateMemory());
@@ -1703,6 +1879,24 @@
ASSERT_EQ(jpg1->length, jpg2->length);
ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
}
+ // encode with luma and chroma stride set and no chroma ptr
+ {
+ UhdrUnCompressedStructWrapper rawImg2P010(kImageWidth, kImageHeight, YCbCr_p010);
+ ASSERT_TRUE(rawImg2P010.setImageColorGamut(mP010ColorGamut));
+ ASSERT_TRUE(rawImg2P010.setImageStride(kImageWidth + 32, kImageWidth + 256));
+ ASSERT_TRUE(rawImg2P010.allocateMemory());
+ ASSERT_TRUE(rawImg2P010.loadRawResource(kYCbCrP010FileName));
+ UhdrCompressedStructWrapper jpgImg2(kImageWidth, kImageHeight);
+ ASSERT_TRUE(jpgImg2.allocateMemory());
+ ASSERT_EQ(uHdrLib.encodeJPEGR(rawImg2P010.getImageHandle(), sdr,
+ ultrahdr_transfer_function::ULTRAHDR_TF_HLG,
+ jpgImg2.getImageHandle()),
+ OK);
+ auto jpg1 = jpgImg.getImageHandle();
+ auto jpg2 = jpgImg2.getImageHandle();
+ ASSERT_EQ(jpg1->length, jpg2->length);
+ ASSERT_EQ(0, memcmp(jpg1->data, jpg2->data, jpg1->length));
+ }
auto jpg1 = jpgImg.getImageHandle();
@@ -1715,6 +1909,13 @@
ASSERT_NO_FATAL_FAILURE(decodeJpegRImg(jpg1, "decode_api3_output.rgb"));
}
+INSTANTIATE_TEST_SUITE_P(
+ JpegRAPIParameterizedTests, JpegRAPIEncodeAndDecodeTest,
+ ::testing::Combine(::testing::Values(ULTRAHDR_COLORGAMUT_BT709, ULTRAHDR_COLORGAMUT_P3,
+ ULTRAHDR_COLORGAMUT_BT2100),
+ ::testing::Values(ULTRAHDR_COLORGAMUT_BT709, ULTRAHDR_COLORGAMUT_P3,
+ ULTRAHDR_COLORGAMUT_BT2100)));
+
// ============================================================================
// Profiling
// ============================================================================
@@ -1796,6 +1997,25 @@
.width = 0,
.height = 0,
.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED};
+ {
+ auto rawImg = rawImgP010.getImageHandle();
+ if (rawImg->luma_stride == 0) rawImg->luma_stride = rawImg->width;
+ if (!rawImg->chroma_data) {
+ uint16_t* data = reinterpret_cast<uint16_t*>(rawImg->data);
+ rawImg->chroma_data = data + rawImg->luma_stride * rawImg->height;
+ rawImg->chroma_stride = rawImg->luma_stride;
+ }
+ }
+ {
+ auto rawImg = rawImg420.getImageHandle();
+ if (rawImg->luma_stride == 0) rawImg->luma_stride = rawImg->width;
+ if (!rawImg->chroma_data) {
+ uint8_t* data = reinterpret_cast<uint8_t*>(rawImg->data);
+ rawImg->chroma_data = data + rawImg->luma_stride * rawImg->height;
+ rawImg->chroma_stride = rawImg->luma_stride / 2;
+ }
+ }
+
JpegRBenchmark benchmark;
ASSERT_NO_FATAL_FAILURE(benchmark.BenchmarkGenerateGainMap(rawImg420.getImageHandle(),
rawImgP010.getImageHandle(), &metadata,
diff --git a/opengl/libs/EGL/Loader.cpp b/opengl/libs/EGL/Loader.cpp
index 8d0eb59..04e2fff 100644
--- a/opengl/libs/EGL/Loader.cpp
+++ b/opengl/libs/EGL/Loader.cpp
@@ -41,24 +41,44 @@
/*
* EGL userspace drivers must be provided either:
* - as a single library:
- * /vendor/lib/egl/libGLES.so
+ * /vendor/${LIB}/egl/libGLES.so
*
* - as separate libraries:
- * /vendor/lib/egl/libEGL.so
- * /vendor/lib/egl/libGLESv1_CM.so
- * /vendor/lib/egl/libGLESv2.so
+ * /vendor/${LIB}/egl/libEGL.so
+ * /vendor/${LIB}/egl/libGLESv1_CM.so
+ * /vendor/${LIB}/egl/libGLESv2.so
*
* For backward compatibility and to facilitate the transition to
* this new naming scheme, the loader will additionally look for:
*
- * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
+ * /vendor/${LIB}/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_${SUFFIX}.so
*
*/
-Loader& Loader::getInstance() {
- static Loader loader;
- return loader;
-}
+#ifndef SYSTEM_LIB_PATH
+#if defined(__LP64__)
+#define SYSTEM_LIB_PATH "/system/lib64"
+#else
+#define SYSTEM_LIB_PATH "/system/lib"
+#endif
+#endif
+
+static const char* PERSIST_DRIVER_SUFFIX_PROPERTY = "persist.graphics.egl";
+static const char* RO_DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
+static const char* RO_BOARD_PLATFORM_PROPERTY = "ro.board.platform";
+
+static const char* HAL_SUBNAME_KEY_PROPERTIES[3] = {
+ PERSIST_DRIVER_SUFFIX_PROPERTY,
+ RO_DRIVER_SUFFIX_PROPERTY,
+ RO_BOARD_PLATFORM_PROPERTY,
+};
+
+static const char* const VENDOR_LIB_EGL_DIR =
+#if defined(__LP64__)
+ "/vendor/lib64/egl";
+#else
+ "/vendor/lib/egl";
+#endif
static void* do_dlopen(const char* path, int mode) {
ATRACE_CALL();
@@ -80,6 +100,17 @@
return android_unload_sphal_library(dso);
}
+static void* load_wrapper(const char* path) {
+ void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
+ ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
+ return so;
+}
+
+Loader& Loader::getInstance() {
+ static Loader loader;
+ return loader;
+}
+
Loader::driver_t::driver_t(void* gles)
{
dso[0] = gles;
@@ -123,30 +154,6 @@
Loader::~Loader() {
}
-static void* load_wrapper(const char* path) {
- void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
- ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
- return so;
-}
-
-#ifndef EGL_WRAPPER_DIR
-#if defined(__LP64__)
-#define EGL_WRAPPER_DIR "/system/lib64"
-#else
-#define EGL_WRAPPER_DIR "/system/lib"
-#endif
-#endif
-
-static const char* PERSIST_DRIVER_SUFFIX_PROPERTY = "persist.graphics.egl";
-static const char* RO_DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
-static const char* RO_BOARD_PLATFORM_PROPERTY = "ro.board.platform";
-
-static const char* HAL_SUBNAME_KEY_PROPERTIES[3] = {
- PERSIST_DRIVER_SUFFIX_PROPERTY,
- RO_DRIVER_SUFFIX_PROPERTY,
- RO_BOARD_PLATFORM_PROPERTY,
-};
-
// Check whether the loaded system drivers should be unloaded in order to
// load ANGLE or the updatable graphics drivers.
// If ANGLE namespace is set, it means the application is identified to run on top of ANGLE.
@@ -169,6 +176,11 @@
}
}
+ // Return true if native GLES drivers should be used and ANGLE is already loaded.
+ if (android::GraphicsEnv::getInstance().shouldUseNativeDriver() && cnx->angleLoaded) {
+ return true;
+ }
+
// Return true if updated driver namespace is set.
ns = android::GraphicsEnv::getInstance().getDriverNamespace();
if (ns) {
@@ -240,16 +252,28 @@
if (!hnd) {
// Secondly, try to load from driver apk.
hnd = attempt_to_load_updated_driver(cnx);
+
+ // If updated driver apk is set but fail to load, abort here.
+ LOG_ALWAYS_FATAL_IF(android::GraphicsEnv::getInstance().getDriverNamespace(),
+ "couldn't find an OpenGL ES implementation from %s",
+ android::GraphicsEnv::getInstance().getDriverPath().c_str());
}
+ // Attempt to load native GLES drivers specified by ro.hardware.egl if native is selected.
+ // If native is selected but fail to load, abort.
+ if (!hnd && android::GraphicsEnv::getInstance().shouldUseNativeDriver()) {
+ auto driverSuffix = base::GetProperty(RO_DRIVER_SUFFIX_PROPERTY, "");
+ LOG_ALWAYS_FATAL_IF(driverSuffix.empty(),
+ "Native GLES driver is selected but not specified in %s",
+ RO_DRIVER_SUFFIX_PROPERTY);
+ hnd = attempt_to_load_system_driver(cnx, driverSuffix.c_str(), true);
+ LOG_ALWAYS_FATAL_IF(!hnd, "Native GLES driver is selected but failed to load. %s=%s",
+ RO_DRIVER_SUFFIX_PROPERTY, driverSuffix.c_str());
+ }
+
+ // Finally, try to load default driver.
bool failToLoadFromDriverSuffixProperty = false;
if (!hnd) {
- // If updated driver apk is set but fail to load, abort here.
- if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
- LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
- android::GraphicsEnv::getInstance().getDriverPath().c_str());
- }
- // Finally, try to load system driver.
// Start by searching for the library name appended by the system
// properties of the GLES userspace driver in both locations.
// i.e.:
@@ -306,13 +330,13 @@
HAL_SUBNAME_KEY_PROPERTIES[2]);
if (!cnx->libEgl) {
- cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
+ cnx->libEgl = load_wrapper(SYSTEM_LIB_PATH "/libEGL.so");
}
if (!cnx->libGles1) {
- cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
+ cnx->libGles1 = load_wrapper(SYSTEM_LIB_PATH "/libGLESv1_CM.so");
}
if (!cnx->libGles2) {
- cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
+ cnx->libGles2 = load_wrapper(SYSTEM_LIB_PATH "/libGLESv2.so");
}
if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
@@ -415,31 +439,19 @@
class MatchFile {
public:
static std::string find(const char* libraryName, const bool exact) {
- const char* const searchPaths[] = {
-#if defined(__LP64__)
- "/vendor/lib64/egl",
- "/system/lib64/egl"
-#else
- "/vendor/lib/egl",
- "/system/lib/egl"
-#endif
- };
-
- for (auto dir : searchPaths) {
- std::string absolutePath;
- if (find(absolutePath, libraryName, dir, exact)) {
- return absolutePath;
- }
+ std::string absolutePath;
+ if (findLibPath(absolutePath, libraryName, exact)) {
+ return absolutePath;
}
// Driver not found. gah.
return std::string();
}
private:
- static bool find(std::string& result,
- const std::string& pattern, const char* const search, bool exact) {
+ static bool findLibPath(std::string& result, const std::string& pattern, bool exact) {
+ const std::string vendorLibEglDirString = std::string(VENDOR_LIB_EGL_DIR);
if (exact) {
- std::string absolutePath = std::string(search) + "/" + pattern + ".so";
+ std::string absolutePath = vendorLibEglDirString + "/" + pattern + ".so";
if (!access(absolutePath.c_str(), R_OK)) {
result = absolutePath;
return true;
@@ -447,7 +459,7 @@
return false;
}
- DIR* d = opendir(search);
+ DIR* d = opendir(VENDOR_LIB_EGL_DIR);
if (d != nullptr) {
struct dirent* e;
while ((e = readdir(d)) != nullptr) {
@@ -460,7 +472,7 @@
}
if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
- result = std::string(search) + "/" + e->d_name;
+ result = vendorLibEglDirString + "/" + e->d_name;
closedir(d);
return true;
}
diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp
index 398d602..cfafc69 100644
--- a/services/sensorservice/SensorService.cpp
+++ b/services/sensorservice/SensorService.cpp
@@ -63,8 +63,10 @@
#include <sys/types.h>
#include <unistd.h>
+#include <condition_variable>
#include <ctime>
#include <future>
+#include <mutex>
#include <string>
#include <private/android_filesystem_config.h>
@@ -196,6 +198,16 @@
if (mRuntimeSensorCallbacks.find(deviceId) == mRuntimeSensorCallbacks.end()) {
mRuntimeSensorCallbacks.emplace(deviceId, callback);
}
+
+ if (mRuntimeSensorHandler == nullptr) {
+ mRuntimeSensorEventBuffer =
+ new sensors_event_t[SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT];
+ mRuntimeSensorHandler = new RuntimeSensorHandler(this);
+ // Use PRIORITY_URGENT_DISPLAY as the injected sensor events should be dispatched as soon as
+ // possible, and also for consistency within the SensorService.
+ mRuntimeSensorHandler->run("RuntimeSensorHandler", PRIORITY_URGENT_DISPLAY);
+ }
+
return handle;
}
@@ -232,8 +244,9 @@
}
status_t SensorService::sendRuntimeSensorEvent(const sensors_event_t& event) {
- Mutex::Autolock _l(mLock);
+ std::unique_lock<std::mutex> lock(mRutimeSensorThreadMutex);
mRuntimeSensorEventQueue.push(event);
+ mRuntimeSensorsCv.notify_all();
return OK;
}
@@ -458,6 +471,7 @@
const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
mSensorEventBuffer = new sensors_event_t[minBufferSize];
mSensorEventScratch = new sensors_event_t[minBufferSize];
+ mRuntimeSensorEventBuffer = nullptr;
mMapFlushEventsToConnections = new wp<const SensorEventConnection> [minBufferSize];
mCurrentOperatingMode = NORMAL;
@@ -1089,7 +1103,6 @@
recordLastValueLocked(mSensorEventBuffer, count);
// handle virtual sensors
- bool bufferNeedsSorting = false;
if (count && vcount) {
sensors_event_t const * const event = mSensorEventBuffer;
if (!mActiveVirtualSensors.empty()) {
@@ -1125,37 +1138,11 @@
// record the last synthesized values
recordLastValueLocked(&mSensorEventBuffer[count], k);
count += k;
- bufferNeedsSorting = true;
+ sortEventBuffer(mSensorEventBuffer, count);
}
}
}
- // handle runtime sensors
- {
- size_t k = 0;
- while (!mRuntimeSensorEventQueue.empty()) {
- if (count + k >= minBufferSize) {
- ALOGE("buffer too small to hold all events: count=%zd, k=%zu, size=%zu",
- count, k, minBufferSize);
- break;
- }
- mSensorEventBuffer[count + k] = mRuntimeSensorEventQueue.front();
- mRuntimeSensorEventQueue.pop();
- k++;
- }
- if (k) {
- // record the last synthesized values
- recordLastValueLocked(&mSensorEventBuffer[count], k);
- count += k;
- bufferNeedsSorting = true;
- }
- }
-
- if (bufferNeedsSorting) {
- // sort the buffer by time-stamps
- sortEventBuffer(mSensorEventBuffer, count);
- }
-
// handle backward compatibility for RotationVector sensor
if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
for (int i = 0; i < count; i++) {
@@ -1234,7 +1221,7 @@
bool needsWakeLock = false;
for (const sp<SensorEventConnection>& connection : activeConnections) {
connection->sendEvents(mSensorEventBuffer, count, mSensorEventScratch,
- mMapFlushEventsToConnections);
+ mMapFlushEventsToConnections);
needsWakeLock |= connection->needsWakeLock();
// If the connection has one-shot sensors, it may be cleaned up after first trigger.
// Early check for one-shot sensors.
@@ -1253,6 +1240,46 @@
return false;
}
+void SensorService::processRuntimeSensorEvents() {
+ size_t count = 0;
+ const size_t maxBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
+
+ {
+ std::unique_lock<std::mutex> lock(mRutimeSensorThreadMutex);
+
+ if (mRuntimeSensorEventQueue.empty()) {
+ mRuntimeSensorsCv.wait(lock, [this] { return !mRuntimeSensorEventQueue.empty(); });
+ }
+
+ // Pop the events from the queue into the buffer until it's empty or the buffer is full.
+ while (!mRuntimeSensorEventQueue.empty()) {
+ if (count >= maxBufferSize) {
+ ALOGE("buffer too small to hold all events: count=%zd, size=%zu", count,
+ maxBufferSize);
+ break;
+ }
+ mRuntimeSensorEventBuffer[count] = mRuntimeSensorEventQueue.front();
+ mRuntimeSensorEventQueue.pop();
+ count++;
+ }
+ }
+
+ if (count) {
+ ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock);
+
+ recordLastValueLocked(mRuntimeSensorEventBuffer, count);
+ sortEventBuffer(mRuntimeSensorEventBuffer, count);
+
+ for (const sp<SensorEventConnection>& connection : connLock.getActiveConnections()) {
+ connection->sendEvents(mRuntimeSensorEventBuffer, count, /* scratch= */ nullptr,
+ /* mapFlushEventsToConnections= */ nullptr);
+ if (connection->hasOneShotSensors()) {
+ cleanupAutoDisabledSensorLocked(connection, mRuntimeSensorEventBuffer, count);
+ }
+ }
+ }
+}
+
sp<Looper> SensorService::getLooper() const {
return mLooper;
}
@@ -1300,6 +1327,14 @@
return false;
}
+bool SensorService::RuntimeSensorHandler::threadLoop() {
+ ALOGD("new thread RuntimeSensorHandler");
+ do {
+ mService->processRuntimeSensorEvents();
+ } while (!Thread::exitPending());
+ return false;
+}
+
void SensorService::recordLastValueLocked(
const sensors_event_t* buffer, size_t count) {
for (size_t i = 0; i < count; i++) {
diff --git a/services/sensorservice/SensorService.h b/services/sensorservice/SensorService.h
index 545f6c2..bf43101 100644
--- a/services/sensorservice/SensorService.h
+++ b/services/sensorservice/SensorService.h
@@ -42,6 +42,8 @@
#include <stdint.h>
#include <sys/types.h>
+#include <condition_variable>
+#include <mutex>
#include <queue>
#include <unordered_map>
#include <unordered_set>
@@ -208,6 +210,7 @@
class SensorEventAckReceiver;
class SensorRecord;
class SensorRegistrationInfo;
+ class RuntimeSensorHandler;
// Promoting a SensorEventConnection or SensorDirectConnection from wp to sp must be done with
// mLock held, but destroying that sp must be done unlocked to avoid a race condition that
@@ -264,6 +267,14 @@
SortedVector< wp<SensorDirectConnection> > mDirectConnections;
};
+ class RuntimeSensorHandler : public Thread {
+ sp<SensorService> const mService;
+ public:
+ virtual bool threadLoop();
+ explicit RuntimeSensorHandler(const sp<SensorService>& service) : mService(service) {
+ }
+ };
+
// If accessing a sensor we need to make sure the UID has access to it. If
// the app UID is idle then it cannot access sensors and gets no trigger
// events, no on-change events, flush event behavior does not change, and
@@ -368,6 +379,8 @@
// Thread interface
virtual bool threadLoop();
+ void processRuntimeSensorEvents();
+
// ISensorServer interface
virtual Vector<Sensor> getSensorList(const String16& opPackageName);
virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName);
@@ -512,6 +525,10 @@
uint32_t mSocketBufferSize;
sp<Looper> mLooper;
sp<SensorEventAckReceiver> mAckReceiver;
+ sp<RuntimeSensorHandler> mRuntimeSensorHandler;
+ // Mutex and CV used to notify the mRuntimeSensorHandler thread that there are new events.
+ std::mutex mRutimeSensorThreadMutex;
+ std::condition_variable mRuntimeSensorsCv;
// protected by mLock
mutable Mutex mLock;
@@ -519,7 +536,7 @@
std::unordered_set<int> mActiveVirtualSensors;
SensorConnectionHolder mConnectionHolder;
bool mWakeLockAcquired;
- sensors_event_t *mSensorEventBuffer, *mSensorEventScratch;
+ sensors_event_t *mSensorEventBuffer, *mSensorEventScratch, *mRuntimeSensorEventBuffer;
// WARNING: these SensorEventConnection instances must not be promoted to sp, except via
// modification to add support for them in ConnectionSafeAutolock
wp<const SensorEventConnection> * mMapFlushEventsToConnections;
diff --git a/services/sensorservice/tests/sensorservicetest.cpp b/services/sensorservice/tests/sensorservicetest.cpp
index e939d51..1baf397 100644
--- a/services/sensorservice/tests/sensorservicetest.cpp
+++ b/services/sensorservice/tests/sensorservicetest.cpp
@@ -87,7 +87,7 @@
int ret = mgr.createDirectChannel(
kMemSize, ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY, resourceHandle);
- // Should print -22 (BAD_VALUE) and the device runtime shouldn't restart
+ // Should not succeed (ret != OK) and the device runtime shouldn't restart
printf("createInvalidDirectChannel=%d\n", ret);
// Secondary test: correct channel creation & destruction (should print 0)
diff --git a/services/surfaceflinger/CompositionEngine/src/Output.cpp b/services/surfaceflinger/CompositionEngine/src/Output.cpp
index d4230f5..0b11e74 100644
--- a/services/surfaceflinger/CompositionEngine/src/Output.cpp
+++ b/services/surfaceflinger/CompositionEngine/src/Output.cpp
@@ -895,13 +895,19 @@
compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
for (auto* layer : getOutputLayersOrderedByZ()) {
- auto* compState = layer->getLayerFE().getCompositionState();
+ const auto* compState = layer->getLayerFE().getCompositionState();
// If any layer has a sideband stream, we will disable blurs. In that case, we don't
// want to force client composition because of the blur.
if (compState->sidebandStream != nullptr) {
return nullptr;
}
+
+ // If RenderEngine cannot render protected content, we cannot blur.
+ if (compState->hasProtectedContent &&
+ !getCompositionEngine().getRenderEngine().supportsProtectedContent()) {
+ return nullptr;
+ }
if (compState->isOpaque) {
continue;
}
diff --git a/services/surfaceflinger/DisplayDevice.cpp b/services/surfaceflinger/DisplayDevice.cpp
index 32bd890..3c7cbe6 100644
--- a/services/surfaceflinger/DisplayDevice.cpp
+++ b/services/surfaceflinger/DisplayDevice.cpp
@@ -38,7 +38,6 @@
#include <log/log.h>
#include <system/window.h>
-#include "Display/DisplaySnapshot.h"
#include "DisplayDevice.h"
#include "FrontEnd/DisplayInfo.h"
#include "HdrSdrRatioOverlay.h"
@@ -231,10 +230,18 @@
return BAD_VALUE;
}
mUpcomingActiveMode = info;
- ATRACE_INT(mActiveModeFPSHwcTrace.c_str(), info.modeOpt->modePtr->getFps().getIntValue());
- return mHwComposer.setActiveModeWithConstraints(getPhysicalId(),
- info.modeOpt->modePtr->getHwcId(), constraints,
- outTimeline);
+ mIsModeSetPending = true;
+
+ const auto& pendingMode = *info.modeOpt->modePtr;
+ ATRACE_INT(mActiveModeFPSHwcTrace.c_str(), pendingMode.getFps().getIntValue());
+
+ return mHwComposer.setActiveModeWithConstraints(getPhysicalId(), pendingMode.getHwcId(),
+ constraints, outTimeline);
+}
+
+void DisplayDevice::finalizeModeChange(DisplayModeId modeId, Fps displayFps, Fps renderFps) {
+ setActiveMode(modeId, displayFps, renderFps);
+ mIsModeSetPending = false;
}
nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
diff --git a/services/surfaceflinger/DisplayDevice.h b/services/surfaceflinger/DisplayDevice.h
index e92125a..a3fa701 100644
--- a/services/surfaceflinger/DisplayDevice.h
+++ b/services/surfaceflinger/DisplayDevice.h
@@ -218,6 +218,8 @@
return mUpcomingActiveMode;
}
+ bool isModeSetPending() const REQUIRES(kMainThreadContext) { return mIsModeSetPending; }
+
scheduler::FrameRateMode getActiveMode() const REQUIRES(kMainThreadContext) {
return mRefreshRateSelector->getActiveMode();
}
@@ -229,6 +231,9 @@
hal::VsyncPeriodChangeTimeline* outTimeline)
REQUIRES(kMainThreadContext);
+ void finalizeModeChange(DisplayModeId, Fps displayFps, Fps renderFps)
+ REQUIRES(kMainThreadContext);
+
scheduler::RefreshRateSelector& refreshRateSelector() const { return *mRefreshRateSelector; }
// Extends the lifetime of the RefreshRateSelector, so it can outlive this DisplayDevice.
@@ -313,7 +318,9 @@
ActiveModeInfo mDesiredActiveMode GUARDED_BY(mActiveModeLock);
TracedOrdinal<bool> mDesiredActiveModeChanged GUARDED_BY(mActiveModeLock) =
{ftl::Concat("DesiredActiveModeChanged-", getId().value).c_str(), false};
+
ActiveModeInfo mUpcomingActiveMode GUARDED_BY(kMainThreadContext);
+ bool mIsModeSetPending GUARDED_BY(kMainThreadContext) = false;
};
struct DisplayDeviceState {
diff --git a/services/surfaceflinger/DisplayHardware/HWC2.cpp b/services/surfaceflinger/DisplayHardware/HWC2.cpp
index aaf2523..0c2b77d 100644
--- a/services/surfaceflinger/DisplayHardware/HWC2.cpp
+++ b/services/surfaceflinger/DisplayHardware/HWC2.cpp
@@ -311,6 +311,14 @@
}
Error Display::supportsDoze(bool* outSupport) const {
+ {
+ std::scoped_lock lock(mDisplayCapabilitiesMutex);
+ if (!mDisplayCapabilities) {
+ // The display has not turned on since boot, so DOZE support is unknown.
+ ALOGW("%s: haven't queried capabilities yet!", __func__);
+ return Error::NO_RESOURCES;
+ }
+ }
*outSupport = hasCapability(DisplayCapability::DOZE);
return Error::NONE;
}
diff --git a/services/surfaceflinger/DisplayHardware/HWComposer.cpp b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
index 3177b33..a9bb928 100644
--- a/services/surfaceflinger/DisplayHardware/HWComposer.cpp
+++ b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
@@ -616,19 +616,29 @@
ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
{
bool supportsDoze = false;
- auto error = hwcDisplay->supportsDoze(&supportsDoze);
- if (error != hal::Error::NONE) {
- LOG_HWC_ERROR("supportsDoze", error, displayId);
- }
+ const auto queryDozeError = hwcDisplay->supportsDoze(&supportsDoze);
- if (!supportsDoze) {
+ // queryDozeError might be NO_RESOURCES, in the case of a display that has never
+ // been turned on. In that case, attempt to set to DOZE anyway.
+ if (!supportsDoze && queryDozeError == hal::Error::NONE) {
mode = hal::PowerMode::ON;
}
- error = hwcDisplay->setPowerMode(mode);
+ auto error = hwcDisplay->setPowerMode(mode);
if (error != hal::Error::NONE) {
LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(), error,
displayId);
+ // If the display had never been turned on, so its doze
+ // support was unknown, it may truly not support doze. Try
+ // switching it to ON instead.
+ if (queryDozeError == hal::Error::NO_RESOURCES) {
+ ALOGD("%s: failed to set %s to %s. Trying again with ON", __func__,
+ to_string(displayId).c_str(), to_string(mode).c_str());
+ error = hwcDisplay->setPowerMode(hal::PowerMode::ON);
+ if (error != hal::Error::NONE) {
+ LOG_HWC_ERROR("setPowerMode(ON)", error, displayId);
+ }
+ }
}
}
break;
diff --git a/services/surfaceflinger/FrontEnd/LayerSnapshot.cpp b/services/surfaceflinger/FrontEnd/LayerSnapshot.cpp
index d389a79..ee589c3 100644
--- a/services/surfaceflinger/FrontEnd/LayerSnapshot.cpp
+++ b/services/surfaceflinger/FrontEnd/LayerSnapshot.cpp
@@ -345,10 +345,9 @@
clientChanges = requested.what;
changes = requested.changes;
contentDirty = requested.what & layer_state_t::CONTENT_DIRTY;
- // TODO(b/238781169) scope down the changes to only buffer updates.
- hasReadyFrame = requested.hasReadyFrame();
+ hasReadyFrame = requested.autoRefresh;
sidebandStreamHasFrame = requested.hasSidebandStreamFrame();
- updateSurfaceDamage(requested, hasReadyFrame, forceFullDamage, surfaceDamage);
+ updateSurfaceDamage(requested, requested.hasReadyFrame(), forceFullDamage, surfaceDamage);
if (forceUpdate || requested.what & layer_state_t::eTransparentRegionChanged) {
transparentRegionHint = requested.transparentRegion;
@@ -481,7 +480,7 @@
layer_state_t::eApiChanged | layer_state_t::eShadowRadiusChanged |
layer_state_t::eBlurRegionsChanged | layer_state_t::eStretchChanged)) {
forceClientComposition = isHdrY410 || shadowSettings.length > 0 ||
- requested.blurRegions.size() > 0 || stretchEffect.hasEffect();
+ stretchEffect.hasEffect();
}
if (forceUpdate ||
diff --git a/services/surfaceflinger/FrontEnd/LayerSnapshot.h b/services/surfaceflinger/FrontEnd/LayerSnapshot.h
index 1afcef9..19477fa 100644
--- a/services/surfaceflinger/FrontEnd/LayerSnapshot.h
+++ b/services/surfaceflinger/FrontEnd/LayerSnapshot.h
@@ -79,7 +79,7 @@
std::shared_ptr<renderengine::ExternalTexture> externalTexture;
gui::LayerMetadata layerMetadata;
gui::LayerMetadata relativeLayerMetadata;
- bool hasReadyFrame;
+ bool hasReadyFrame; // used in post composition to check if there is another frame ready
ui::Transform localTransformInverse;
gui::WindowInfo inputInfo;
ui::Transform localTransform;
diff --git a/services/surfaceflinger/FrontEnd/LayerSnapshotBuilder.cpp b/services/surfaceflinger/FrontEnd/LayerSnapshotBuilder.cpp
index 23cfe928..cf39187 100644
--- a/services/surfaceflinger/FrontEnd/LayerSnapshotBuilder.cpp
+++ b/services/surfaceflinger/FrontEnd/LayerSnapshotBuilder.cpp
@@ -871,7 +871,7 @@
// computed snapshot properties
snapshot.forceClientComposition = snapshot.isHdrY410 || snapshot.shadowSettings.length > 0 ||
- requested.blurRegions.size() > 0 || snapshot.stretchEffect.hasEffect();
+ snapshot.stretchEffect.hasEffect();
snapshot.contentOpaque = snapshot.isContentOpaque();
snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
snapshot.color.a == 1.f;
diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp
index 3a41f15..cfcc424 100644
--- a/services/surfaceflinger/Layer.cpp
+++ b/services/surfaceflinger/Layer.cpp
@@ -594,8 +594,8 @@
snapshot->localTransformInverse = snapshot->localTransform.inverse();
snapshot->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
snapshot->alpha = alpha;
- snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
- snapshot->blurRegions = drawingState.blurRegions;
+ snapshot->backgroundBlurRadius = getBackgroundBlurRadius();
+ snapshot->blurRegions = getBlurRegions();
snapshot->stretchEffect = getStretchEffect();
}
@@ -659,13 +659,12 @@
// Force client composition for special cases known only to the front-end.
// Rounded corners no longer force client composition, since we may use a
// hole punch so that the layer will appear to have rounded corners.
- if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
- snapshot->stretchEffect.hasEffect()) {
+ if (isHdrY410() || drawShadows() || snapshot->stretchEffect.hasEffect()) {
snapshot->forceClientComposition = true;
}
// If there are no visible region changes, we still need to update blur parameters.
- snapshot->blurRegions = drawingState.blurRegions;
- snapshot->backgroundBlurRadius = drawingState.backgroundBlurRadius;
+ snapshot->blurRegions = getBlurRegions();
+ snapshot->backgroundBlurRadius = getBackgroundBlurRadius();
// Layer framerate is used in caching decisions.
// Retrieve it from the scheduler which maintains an instance of LayerHistory, and store it in
@@ -2111,6 +2110,13 @@
}
RoundedCornerState Layer::getRoundedCornerState() const {
+ // Today's DPUs cannot do rounded corners. If RenderEngine cannot render
+ // protected content, remove rounded corners from protected content so it
+ // can be rendered by the DPU.
+ if (isProtected() && !mFlinger->getRenderEngine().supportsProtectedContent()) {
+ return {};
+ }
+
// Get parent settings
RoundedCornerState parentSettings;
const auto& parent = mDrawingParent.promote();
diff --git a/services/surfaceflinger/LayerFE.cpp b/services/surfaceflinger/LayerFE.cpp
index f855f27..4d3e048 100644
--- a/services/surfaceflinger/LayerFE.cpp
+++ b/services/surfaceflinger/LayerFE.cpp
@@ -16,7 +16,7 @@
// #define LOG_NDEBUG 0
#undef LOG_TAG
-#define LOG_TAG "LayerFE"
+#define LOG_TAG "SurfaceFlinger"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include <gui/GLConsumer.h>
diff --git a/services/surfaceflinger/Scheduler/Scheduler.cpp b/services/surfaceflinger/Scheduler/Scheduler.cpp
index d6d7725..5a19ec5 100644
--- a/services/surfaceflinger/Scheduler/Scheduler.cpp
+++ b/services/surfaceflinger/Scheduler/Scheduler.cpp
@@ -186,7 +186,17 @@
FrameTargeter& pacesetterTargeter = *pacesetterOpt->get().targeterPtr;
pacesetterTargeter.beginFrame(beginFrameArgs, *pacesetterOpt->get().schedulePtr);
- if (!compositor.commit(pacesetterTargeter.target())) return;
+ FrameTargets targets;
+ targets.try_emplace(pacesetterId, &pacesetterTargeter.target());
+
+ for (const auto& [id, display] : mDisplays) {
+ if (id == pacesetterId) continue;
+
+ const FrameTargeter& targeter = *display.targeterPtr;
+ targets.try_emplace(id, &targeter.target());
+ }
+
+ if (!compositor.commit(pacesetterId, targets)) return;
// TODO(b/256196556): Choose the frontrunner display.
FrameTargeters targeters;
diff --git a/services/surfaceflinger/Scheduler/include/scheduler/interface/ICompositor.h b/services/surfaceflinger/Scheduler/include/scheduler/interface/ICompositor.h
index 6fe813a..12ee36e 100644
--- a/services/surfaceflinger/Scheduler/include/scheduler/interface/ICompositor.h
+++ b/services/surfaceflinger/Scheduler/include/scheduler/interface/ICompositor.h
@@ -29,6 +29,7 @@
class FrameTarget;
class FrameTargeter;
+using FrameTargets = ui::PhysicalDisplayMap<PhysicalDisplayId, const scheduler::FrameTarget*>;
using FrameTargeters = ui::PhysicalDisplayMap<PhysicalDisplayId, scheduler::FrameTargeter*>;
} // namespace scheduler
@@ -39,7 +40,7 @@
// Commits transactions for layers and displays. Returns whether any state has been invalidated,
// i.e. whether a frame should be composited for each display.
- virtual bool commit(const scheduler::FrameTarget&) = 0;
+ virtual bool commit(PhysicalDisplayId pacesetterId, const scheduler::FrameTargets&) = 0;
// Composites a frame for each display. CompositionEngine performs GPU and/or HAL composition
// via RenderEngine and the Composer HAL, respectively.
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 9f24dd6..7b37407 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -1195,9 +1195,9 @@
}
void SurfaceFlinger::setDesiredActiveMode(display::DisplayModeRequest&& request, bool force) {
- ATRACE_CALL();
-
const auto displayId = request.mode.modePtr->getPhysicalDisplayId();
+ ATRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str());
+
const auto display = getDisplayDeviceLocked(displayId);
if (!display) {
ALOGW("%s: display is no longer valid", __func__);
@@ -1225,17 +1225,24 @@
// As we called to set period, we will call to onRefreshRateChangeCompleted once
// VsyncController model is locked.
mScheduler->modulateVsync(displayId, &VsyncModulator::onRefreshRateChangeInitiated);
- updatePhaseConfiguration(mode.fps);
+
+ if (displayId == mActiveDisplayId) {
+ updatePhaseConfiguration(mode.fps);
+ }
+
mScheduler->setModeChangePending(true);
break;
case DisplayDevice::DesiredActiveModeAction::InitiateRenderRateSwitch:
mScheduler->setRenderRate(displayId, mode.fps);
- updatePhaseConfiguration(mode.fps);
- mRefreshRateStats->setRefreshRate(mode.fps);
- if (display->getPhysicalId() == mActiveDisplayId && emitEvent) {
- mScheduler->onPrimaryDisplayModeChanged(mAppConnectionHandle, mode);
+
+ if (displayId == mActiveDisplayId) {
+ updatePhaseConfiguration(mode.fps);
+ mRefreshRateStats->setRefreshRate(mode.fps);
}
+ if (emitEvent) {
+ dispatchDisplayModeChangeEvent(displayId, mode);
+ }
break;
case DisplayDevice::DesiredActiveModeAction::None:
break;
@@ -1291,24 +1298,20 @@
return future.get();
}
-void SurfaceFlinger::updateInternalStateWithChangedMode() {
- ATRACE_CALL();
+void SurfaceFlinger::finalizeDisplayModeChange(DisplayDevice& display) {
+ const auto displayId = display.getPhysicalId();
+ ATRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str());
- const auto display = getDefaultDisplayDeviceLocked();
- if (!display) {
- return;
- }
-
- const auto upcomingModeInfo = display->getUpcomingActiveMode();
+ const auto upcomingModeInfo = display.getUpcomingActiveMode();
if (!upcomingModeInfo.modeOpt) {
// There is no pending mode change. This can happen if the active
// display changed and the mode change happened on a different display.
return;
}
- if (display->getActiveMode().modePtr->getResolution() !=
+ if (display.getActiveMode().modePtr->getResolution() !=
upcomingModeInfo.modeOpt->modePtr->getResolution()) {
- auto& state = mCurrentState.displays.editValueFor(display->getDisplayToken());
+ auto& state = mCurrentState.displays.editValueFor(display.getDisplayToken());
// We need to generate new sequenceId in order to recreate the display (and this
// way the framebuffer).
state.sequenceId = DisplayDeviceState{}.sequenceId;
@@ -1319,27 +1322,24 @@
return;
}
- mPhysicalDisplays.get(display->getPhysicalId())
- .transform(&PhysicalDisplay::snapshotRef)
- .transform(ftl::unit_fn([&](const display::DisplaySnapshot& snapshot) {
- FTL_FAKE_GUARD(kMainThreadContext,
- display->setActiveMode(upcomingModeInfo.modeOpt->modePtr->getId(),
- upcomingModeInfo.modeOpt->modePtr->getFps(),
- upcomingModeInfo.modeOpt->fps));
- }));
+ const auto& activeMode = *upcomingModeInfo.modeOpt;
+ display.finalizeModeChange(activeMode.modePtr->getId(), activeMode.modePtr->getFps(),
+ activeMode.fps);
- const Fps refreshRate = upcomingModeInfo.modeOpt->fps;
- mRefreshRateStats->setRefreshRate(refreshRate);
- updatePhaseConfiguration(refreshRate);
+ if (displayId == mActiveDisplayId) {
+ mRefreshRateStats->setRefreshRate(activeMode.fps);
+ updatePhaseConfiguration(activeMode.fps);
+ }
if (upcomingModeInfo.event != scheduler::DisplayModeEvent::None) {
- mScheduler->onPrimaryDisplayModeChanged(mAppConnectionHandle, *upcomingModeInfo.modeOpt);
+ dispatchDisplayModeChangeEvent(displayId, activeMode);
}
}
void SurfaceFlinger::clearDesiredActiveModeState(const sp<DisplayDevice>& display) {
display->clearDesiredActiveModeState();
if (display->getPhysicalId() == mActiveDisplayId) {
+ // TODO(b/255635711): Check for pending mode changes on other displays.
mScheduler->setModeChangePending(false);
}
}
@@ -1353,21 +1353,18 @@
clearDesiredActiveModeState(display);
mScheduler->resyncToHardwareVsync(displayId, true /* allowToEnable */, displayFps);
mScheduler->setRenderRate(displayId, renderFps);
- updatePhaseConfiguration(renderFps);
+
+ if (displayId == mActiveDisplayId) {
+ updatePhaseConfiguration(renderFps);
+ }
}
-void SurfaceFlinger::setActiveModeInHwcIfNeeded() {
+void SurfaceFlinger::initiateDisplayModeChanges() {
ATRACE_CALL();
std::optional<PhysicalDisplayId> displayToUpdateImmediately;
for (const auto& [id, physical] : mPhysicalDisplays) {
- const auto& snapshot = physical.snapshot();
-
- if (snapshot.connectionType() != ui::DisplayConnectionType::Internal) {
- continue;
- }
-
const auto display = getDisplayDeviceLocked(id);
if (!display) continue;
@@ -1378,14 +1375,14 @@
continue;
}
- if (id != mActiveDisplayId) {
- // Display is no longer the active display, so abort the mode change.
+ if (!display->isPoweredOn()) {
+ // Display is no longer powered on, so abort the mode change.
clearDesiredActiveModeState(display);
continue;
}
const auto desiredModeId = desiredActiveMode->modeOpt->modePtr->getId();
- const auto displayModePtrOpt = snapshot.displayModes().get(desiredModeId);
+ const auto displayModePtrOpt = physical.snapshot().displayModes().get(desiredModeId);
if (!displayModePtrOpt) {
ALOGW("Desired display mode is no longer supported. Mode ID = %d",
@@ -1435,19 +1432,18 @@
if (outTimeline.refreshRequired) {
scheduleComposite(FrameHint::kNone);
- mSetActiveModePending = true;
} else {
- // Updating the internal state should be done outside the loop,
- // because it can recreate a DisplayDevice and modify mDisplays
- // which will invalidate the iterator.
+ // TODO(b/255635711): Remove `displayToUpdateImmediately` to `finalizeDisplayModeChange`
+ // for all displays. This was only needed when the loop iterated over `mDisplays` rather
+ // than `mPhysicalDisplays`.
displayToUpdateImmediately = display->getPhysicalId();
}
}
if (displayToUpdateImmediately) {
- updateInternalStateWithChangedMode();
-
const auto display = getDisplayDeviceLocked(*displayToUpdateImmediately);
+ finalizeDisplayModeChange(*display);
+
const auto desiredActiveMode = display->getDesiredActiveMode();
if (desiredActiveMode && display->getActiveMode() == desiredActiveMode->modeOpt) {
desiredActiveModeChangeDone(display);
@@ -2363,20 +2359,21 @@
mLayersWithBuffersRemoved.emplace(it->second);
}
it->second->latchBufferImpl(unused, latchTime, bgColorOnly);
- mLayersWithQueuedFrames.emplace(it->second);
- }
-
- for (auto& snapshot : mLayerSnapshotBuilder.getSnapshots()) {
- updateLayerHistory(*snapshot);
- if (!snapshot->hasReadyFrame) continue;
newDataLatched = true;
- if (!snapshot->isVisible) break;
-
- Region visibleReg;
- visibleReg.set(snapshot->transformedBoundsWithoutTransparentRegion);
- invalidateLayerStack(snapshot->outputFilter, visibleReg);
+ mLayersWithQueuedFrames.emplace(it->second);
+ mLayersIdsWithQueuedFrames.emplace(it->second->sequence);
}
+ mLayerSnapshotBuilder.forEachVisibleSnapshot([&](const frontend::LayerSnapshot& snapshot) {
+ updateLayerHistory(snapshot);
+ if (mLayersIdsWithQueuedFrames.find(snapshot.path.id) ==
+ mLayersIdsWithQueuedFrames.end())
+ return;
+ Region visibleReg;
+ visibleReg.set(snapshot.transformedBoundsWithoutTransparentRegion);
+ invalidateLayerStack(snapshot.outputFilter, visibleReg);
+ });
+
for (auto& destroyedLayer : mLayerLifecycleManager.getDestroyedLayers()) {
mLegacyLayers.erase(destroyedLayer->id);
}
@@ -2398,7 +2395,10 @@
return mustComposite;
}
-bool SurfaceFlinger::commit(const scheduler::FrameTarget& pacesetterFrameTarget) {
+bool SurfaceFlinger::commit(PhysicalDisplayId pacesetterId,
+ const scheduler::FrameTargets& frameTargets) {
+ const scheduler::FrameTarget& pacesetterFrameTarget = *frameTargets.get(pacesetterId)->get();
+
const VsyncId vsyncId = pacesetterFrameTarget.vsyncId();
ATRACE_NAME(ftl::Concat(__func__, ' ', ftl::to_underlying(vsyncId)).c_str());
@@ -2411,20 +2411,35 @@
mTracingEnabledChanged = false;
}
- // If we are in the middle of a mode change and the fence hasn't
- // fired yet just wait for the next commit.
- if (mSetActiveModePending) {
- if (pacesetterFrameTarget.isFramePending()) {
- mScheduler->scheduleFrame();
- return false;
- }
+ // If a mode set is pending and the fence hasn't fired yet, wait for the next commit.
+ if (std::any_of(frameTargets.begin(), frameTargets.end(),
+ [this](const auto& pair) FTL_FAKE_GUARD(mStateLock)
+ FTL_FAKE_GUARD(kMainThreadContext) {
+ if (!pair.second->isFramePending()) return false;
- // We received the present fence from the HWC, so we assume it successfully updated
- // the mode, hence we update SF.
- mSetActiveModePending = false;
- {
- Mutex::Autolock lock(mStateLock);
- updateInternalStateWithChangedMode();
+ if (const auto display = getDisplayDeviceLocked(pair.first)) {
+ return display->isModeSetPending();
+ }
+
+ return false;
+ })) {
+ mScheduler->scheduleFrame();
+ return false;
+ }
+
+ {
+ Mutex::Autolock lock(mStateLock);
+
+ for (const auto [id, target] : frameTargets) {
+ // TODO(b/241285876): This is `nullptr` when the DisplayDevice is about to be removed in
+ // this commit, since the PhysicalDisplay has already been removed. Rather than checking
+ // for `nullptr` below, change Scheduler::onFrameSignal to filter out the FrameTarget of
+ // the removed display.
+ const auto display = getDisplayDeviceLocked(id);
+
+ if (display && display->isModeSetPending()) {
+ finalizeDisplayModeChange(*display);
+ }
}
}
@@ -2515,7 +2530,7 @@
? &mLayerHierarchyBuilder.getHierarchy()
: nullptr,
updateAttachedChoreographer);
- setActiveModeInHwcIfNeeded();
+ initiateDisplayModeChanges();
}
updateCursorAsync();
@@ -2727,6 +2742,7 @@
mScheduler->modulateVsync({}, &VsyncModulator::onDisplayRefresh, hasGpuUseOrReuse);
mLayersWithQueuedFrames.clear();
+ mLayersIdsWithQueuedFrames.clear();
if (mLayerTracingEnabled && mLayerTracing.flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
// This will block and should only be used for debugging.
addToLayerTracing(mVisibleRegionsDirty, pacesetterTarget.frameBeginTime(), vsyncId);
@@ -3322,6 +3338,16 @@
mScheduler->onHotplugReceived(mSfConnectionHandle, displayId, connected);
}
+void SurfaceFlinger::dispatchDisplayModeChangeEvent(PhysicalDisplayId displayId,
+ const scheduler::FrameRateMode& mode) {
+ // TODO(b/255635821): Merge code paths and move to Scheduler.
+ const auto onDisplayModeChanged = displayId == mActiveDisplayId
+ ? &scheduler::Scheduler::onPrimaryDisplayModeChanged
+ : &scheduler::Scheduler::onNonPrimaryDisplayModeChanged;
+
+ ((*mScheduler).*onDisplayModeChanged)(mAppConnectionHandle, mode);
+}
+
sp<DisplayDevice> SurfaceFlinger::setupNewDisplayDeviceInternal(
const wp<IBinder>& displayToken,
std::shared_ptr<compositionengine::Display> compositionDisplay,
@@ -3420,14 +3446,8 @@
RenderIntent::COLORIMETRIC});
if (const auto& physical = state.physical) {
- mPhysicalDisplays.get(physical->id)
- .transform(&PhysicalDisplay::snapshotRef)
- .transform(ftl::unit_fn([&](const display::DisplaySnapshot& snapshot) {
- FTL_FAKE_GUARD(kMainThreadContext,
- display->setActiveMode(physical->activeMode->getId(),
- physical->activeMode->getFps(),
- physical->activeMode->getFps()));
- }));
+ const auto& mode = *physical->activeMode;
+ display->setActiveMode(mode.getId(), mode.getFps(), mode.getFps());
}
display->setLayerFilter(makeLayerFilterForDisplay(display->getId(), state.layerStack));
@@ -3946,12 +3966,8 @@
if (!display) continue;
- const bool isInternalDisplay = mPhysicalDisplays.get(displayId)
- .transform(&PhysicalDisplay::isInternal)
- .value_or(false);
-
- if (isInternalDisplay && displayId != mActiveDisplayId) {
- ALOGV("%s(%s): Inactive display", __func__, to_string(displayId).c_str());
+ if (!display->isPoweredOn()) {
+ ALOGV("%s(%s): Display is powered off", __func__, to_string(displayId).c_str());
continue;
}
@@ -3959,7 +3975,7 @@
setDesiredActiveMode(std::move(request));
} else {
ALOGV("%s: Mode %d is disallowed for display %s", __func__, modePtr->getId().value(),
- to_string(display->getId()).c_str());
+ to_string(displayId).c_str());
}
}
}
@@ -6127,6 +6143,13 @@
displayProto->set_id(display->getId().value);
displayProto->set_name(display->getDisplayName());
displayProto->set_layer_stack(display->getLayerStack().id);
+
+ if (!display->isVirtual()) {
+ const auto dpi = display->refreshRateSelector().getActiveMode().modePtr->getDpi();
+ displayProto->set_dpi_x(dpi.x);
+ displayProto->set_dpi_y(dpi.y);
+ }
+
LayerProtoHelper::writeSizeToProto(display->getWidth(), display->getHeight(),
[&]() { return displayProto->mutable_size(); });
LayerProtoHelper::writeToProto(display->getLayerStackSpaceRect(), [&]() {
@@ -7311,16 +7334,27 @@
} // namespace
-status_t SurfaceFlinger::captureDisplay(const DisplayCaptureArgs& args,
- const sp<IScreenCaptureListener>& captureListener) {
+static void invokeScreenCaptureError(const status_t status,
+ const sp<IScreenCaptureListener>& captureListener) {
+ ScreenCaptureResults captureResults;
+ captureResults.fenceResult = base::unexpected(status);
+ captureListener->onScreenCaptureCompleted(captureResults);
+}
+
+void SurfaceFlinger::captureDisplay(const DisplayCaptureArgs& args,
+ const sp<IScreenCaptureListener>& captureListener) {
ATRACE_CALL();
status_t validate = validateScreenshotPermissions(args);
if (validate != OK) {
- return validate;
+ invokeScreenCaptureError(validate, captureListener);
+ return;
}
- if (!args.displayToken) return BAD_VALUE;
+ if (!args.displayToken) {
+ invokeScreenCaptureError(BAD_VALUE, captureListener);
+ return;
+ }
wp<const DisplayDevice> displayWeak;
ui::LayerStack layerStack;
@@ -7329,7 +7363,10 @@
{
Mutex::Autolock lock(mStateLock);
sp<DisplayDevice> display = getDisplayDeviceLocked(args.displayToken);
- if (!display) return NAME_NOT_FOUND;
+ if (!display) {
+ invokeScreenCaptureError(NAME_NOT_FOUND, captureListener);
+ return;
+ }
displayWeak = display;
layerStack = display->getLayerStack();
@@ -7344,7 +7381,8 @@
excludeLayerIds.emplace(excludeLayer);
} else {
ALOGW("Invalid layer handle passed as excludeLayer to captureDisplay");
- return NAME_NOT_FOUND;
+ invokeScreenCaptureError(NAME_NOT_FOUND, captureListener);
+ return;
}
}
}
@@ -7367,14 +7405,12 @@
getLayerSnapshots = RenderArea::fromTraverseLayersLambda(traverseLayers);
}
- auto future = captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, reqSize,
- args.pixelFormat, args.allowProtected, args.grayscale,
- captureListener);
- return fenceStatus(future.get());
+ captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, reqSize, args.pixelFormat,
+ args.allowProtected, args.grayscale, captureListener);
}
-status_t SurfaceFlinger::captureDisplay(DisplayId displayId,
- const sp<IScreenCaptureListener>& captureListener) {
+void SurfaceFlinger::captureDisplay(DisplayId displayId,
+ const sp<IScreenCaptureListener>& captureListener) {
ui::LayerStack layerStack;
wp<const DisplayDevice> displayWeak;
ui::Size size;
@@ -7383,7 +7419,8 @@
const auto display = getDisplayDeviceLocked(displayId);
if (!display) {
- return NAME_NOT_FOUND;
+ invokeScreenCaptureError(NAME_NOT_FOUND, captureListener);
+ return;
}
displayWeak = display;
@@ -7411,25 +7448,25 @@
if (captureListener == nullptr) {
ALOGE("capture screen must provide a capture listener callback");
- return BAD_VALUE;
+ invokeScreenCaptureError(BAD_VALUE, captureListener);
+ return;
}
constexpr bool kAllowProtected = false;
constexpr bool kGrayscale = false;
- auto future = captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, size,
- ui::PixelFormat::RGBA_8888, kAllowProtected, kGrayscale,
- captureListener);
- return fenceStatus(future.get());
+ captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, size,
+ ui::PixelFormat::RGBA_8888, kAllowProtected, kGrayscale, captureListener);
}
-status_t SurfaceFlinger::captureLayers(const LayerCaptureArgs& args,
- const sp<IScreenCaptureListener>& captureListener) {
+void SurfaceFlinger::captureLayers(const LayerCaptureArgs& args,
+ const sp<IScreenCaptureListener>& captureListener) {
ATRACE_CALL();
status_t validate = validateScreenshotPermissions(args);
if (validate != OK) {
- return validate;
+ invokeScreenCaptureError(validate, captureListener);
+ return;
}
ui::Size reqSize;
@@ -7447,13 +7484,15 @@
parent = LayerHandle::getLayer(args.layerHandle);
if (parent == nullptr) {
ALOGE("captureLayers called with an invalid or removed parent");
- return NAME_NOT_FOUND;
+ invokeScreenCaptureError(NAME_NOT_FOUND, captureListener);
+ return;
}
if (!canCaptureBlackoutContent &&
parent->getDrawingState().flags & layer_state_t::eLayerSecure) {
ALOGW("Attempting to capture secure layer: PERMISSION_DENIED");
- return PERMISSION_DENIED;
+ invokeScreenCaptureError(PERMISSION_DENIED, captureListener);
+ return;
}
Rect parentSourceBounds = parent->getCroppedBufferSize(parent->getDrawingState());
@@ -7470,7 +7509,8 @@
if (crop.isEmpty() || args.frameScaleX <= 0.0f || args.frameScaleY <= 0.0f) {
// Error out if the layer has no source bounds (i.e. they are boundless) and a source
// crop was not specified, or an invalid frame scale was provided.
- return BAD_VALUE;
+ invokeScreenCaptureError(BAD_VALUE, captureListener);
+ return;
}
reqSize = ui::Size(crop.width() * args.frameScaleX, crop.height() * args.frameScaleY);
@@ -7480,7 +7520,8 @@
excludeLayerIds.emplace(excludeLayer);
} else {
ALOGW("Invalid layer handle passed as excludeLayer to captureLayers");
- return NAME_NOT_FOUND;
+ invokeScreenCaptureError(NAME_NOT_FOUND, captureListener);
+ return;
}
}
} // mStateLock
@@ -7488,7 +7529,8 @@
// really small crop or frameScale
if (reqSize.width <= 0 || reqSize.height <= 0) {
ALOGW("Failed to captureLayes: crop or scale too small");
- return BAD_VALUE;
+ invokeScreenCaptureError(BAD_VALUE, captureListener);
+ return;
}
bool childrenOnly = args.childrenOnly;
@@ -7552,26 +7594,27 @@
if (captureListener == nullptr) {
ALOGE("capture screen must provide a capture listener callback");
- return BAD_VALUE;
+ invokeScreenCaptureError(BAD_VALUE, captureListener);
+ return;
}
- auto future = captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, reqSize,
- args.pixelFormat, args.allowProtected, args.grayscale,
- captureListener);
- return fenceStatus(future.get());
+ captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, reqSize, args.pixelFormat,
+ args.allowProtected, args.grayscale, captureListener);
}
-ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenCommon(
- RenderAreaFuture renderAreaFuture, GetLayerSnapshotsFunction getLayerSnapshots,
- ui::Size bufferSize, ui::PixelFormat reqPixelFormat, bool allowProtected, bool grayscale,
- const sp<IScreenCaptureListener>& captureListener) {
+void SurfaceFlinger::captureScreenCommon(RenderAreaFuture renderAreaFuture,
+ GetLayerSnapshotsFunction getLayerSnapshots,
+ ui::Size bufferSize, ui::PixelFormat reqPixelFormat,
+ bool allowProtected, bool grayscale,
+ const sp<IScreenCaptureListener>& captureListener) {
ATRACE_CALL();
if (exceedsMaxRenderTargetSize(bufferSize.getWidth(), bufferSize.getHeight())) {
ALOGE("Attempted to capture screen with size (%" PRId32 ", %" PRId32
") that exceeds render target size limit.",
bufferSize.getWidth(), bufferSize.getHeight());
- return ftl::yield<FenceResult>(base::unexpected(BAD_VALUE)).share();
+ invokeScreenCaptureError(BAD_VALUE, captureListener);
+ return;
}
// Loop over all visible layers to see whether there's any protected layer. A protected layer is
@@ -7611,14 +7654,16 @@
// Otherwise an irreponsible process may cause an SF crash by allocating
// too much.
ALOGE("%s: Buffer failed to allocate: %d", __func__, bufferStatus);
- return ftl::yield<FenceResult>(base::unexpected(bufferStatus)).share();
+ invokeScreenCaptureError(bufferStatus, captureListener);
+ return;
}
const std::shared_ptr<renderengine::ExternalTexture> texture = std::make_shared<
renderengine::impl::ExternalTexture>(buffer, getRenderEngine(),
renderengine::impl::ExternalTexture::Usage::
WRITEABLE);
- return captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, texture,
- false /* regionSampling */, grayscale, captureListener);
+ auto fence = captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, texture,
+ false /* regionSampling */, grayscale, captureListener);
+ fence.get();
}
ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenCommon(
@@ -7922,6 +7967,7 @@
const sp<DisplayDevice>& display,
const scheduler::RefreshRateSelector::PolicyVariant& policy) {
const auto displayId = display->getPhysicalId();
+ ATRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str());
Mutex::Autolock lock(mStateLock);
@@ -7942,13 +7988,11 @@
break;
}
- const bool isInternalDisplay = mPhysicalDisplays.get(displayId)
- .transform(&PhysicalDisplay::isInternal)
- .value_or(false);
-
- if (isInternalDisplay && displayId != mActiveDisplayId) {
- // The policy will be be applied when the display becomes active.
- ALOGV("%s(%s): Inactive display", __func__, to_string(displayId).c_str());
+ // TODO(b/255635711): Apply the policy once the display is powered on, which is currently only
+ // done for the internal display that becomes active on fold/unfold. For now, assume that DM
+ // always powers on the secondary (internal or external) display before setting its policy.
+ if (!display->isPoweredOn()) {
+ ALOGV("%s(%s): Display is powered off", __func__, to_string(displayId).c_str());
return NO_ERROR;
}
@@ -8311,7 +8355,9 @@
resetPhaseConfiguration(activeDisplay.getActiveMode().fps);
+ // TODO(b/255635711): Check for pending mode changes on other displays.
mScheduler->setModeChangePending(false);
+
mScheduler->setPacesetterDisplay(mActiveDisplayId);
onActiveDisplaySizeChanged(activeDisplay);
@@ -8842,33 +8888,35 @@
outInfo->secure = info.secure;
outInfo->installOrientation = static_cast<gui::Rotation>(info.installOrientation);
- gui::DeviceProductInfo dinfo;
- std::optional<DeviceProductInfo> dpi = info.deviceProductInfo;
- dinfo.name = std::move(dpi->name);
- dinfo.manufacturerPnpId =
- std::vector<uint8_t>(dpi->manufacturerPnpId.begin(), dpi->manufacturerPnpId.end());
- dinfo.productId = dpi->productId;
- dinfo.relativeAddress =
- std::vector<uint8_t>(dpi->relativeAddress.begin(), dpi->relativeAddress.end());
- if (const auto* model =
- std::get_if<DeviceProductInfo::ModelYear>(&dpi->manufactureOrModelDate)) {
- gui::DeviceProductInfo::ModelYear modelYear;
- modelYear.year = model->year;
- dinfo.manufactureOrModelDate.set<Tag::modelYear>(modelYear);
- } else if (const auto* manufacture = std::get_if<DeviceProductInfo::ManufactureYear>(
- &dpi->manufactureOrModelDate)) {
- gui::DeviceProductInfo::ManufactureYear date;
- date.modelYear.year = manufacture->year;
- dinfo.manufactureOrModelDate.set<Tag::manufactureYear>(date);
- } else if (const auto* manufacture = std::get_if<DeviceProductInfo::ManufactureWeekAndYear>(
- &dpi->manufactureOrModelDate)) {
- gui::DeviceProductInfo::ManufactureWeekAndYear date;
- date.manufactureYear.modelYear.year = manufacture->year;
- date.week = manufacture->week;
- dinfo.manufactureOrModelDate.set<Tag::manufactureWeekAndYear>(date);
- }
+ if (const std::optional<DeviceProductInfo> dpi = info.deviceProductInfo) {
+ gui::DeviceProductInfo dinfo;
+ dinfo.name = std::move(dpi->name);
+ dinfo.manufacturerPnpId = std::vector<uint8_t>(dpi->manufacturerPnpId.begin(),
+ dpi->manufacturerPnpId.end());
+ dinfo.productId = dpi->productId;
+ dinfo.relativeAddress =
+ std::vector<uint8_t>(dpi->relativeAddress.begin(), dpi->relativeAddress.end());
+ if (const auto* model =
+ std::get_if<DeviceProductInfo::ModelYear>(&dpi->manufactureOrModelDate)) {
+ gui::DeviceProductInfo::ModelYear modelYear;
+ modelYear.year = model->year;
+ dinfo.manufactureOrModelDate.set<Tag::modelYear>(modelYear);
+ } else if (const auto* manufacture = std::get_if<DeviceProductInfo::ManufactureYear>(
+ &dpi->manufactureOrModelDate)) {
+ gui::DeviceProductInfo::ManufactureYear date;
+ date.modelYear.year = manufacture->year;
+ dinfo.manufactureOrModelDate.set<Tag::manufactureYear>(date);
+ } else if (const auto* manufacture =
+ std::get_if<DeviceProductInfo::ManufactureWeekAndYear>(
+ &dpi->manufactureOrModelDate)) {
+ gui::DeviceProductInfo::ManufactureWeekAndYear date;
+ date.manufactureYear.modelYear.year = manufacture->year;
+ date.week = manufacture->week;
+ dinfo.manufactureOrModelDate.set<Tag::manufactureWeekAndYear>(date);
+ }
- outInfo->deviceProductInfo = dinfo;
+ outInfo->deviceProductInfo = dinfo;
+ }
}
return binderStatusFromStatusT(status);
}
@@ -9055,28 +9103,28 @@
binder::Status SurfaceComposerAIDL::captureDisplay(
const DisplayCaptureArgs& args, const sp<IScreenCaptureListener>& captureListener) {
- status_t status = mFlinger->captureDisplay(args, captureListener);
- return binderStatusFromStatusT(status);
+ mFlinger->captureDisplay(args, captureListener);
+ return binderStatusFromStatusT(NO_ERROR);
}
binder::Status SurfaceComposerAIDL::captureDisplayById(
int64_t displayId, const sp<IScreenCaptureListener>& captureListener) {
- status_t status;
+ // status_t status;
IPCThreadState* ipc = IPCThreadState::self();
const int uid = ipc->getCallingUid();
if (uid == AID_ROOT || uid == AID_GRAPHICS || uid == AID_SYSTEM || uid == AID_SHELL) {
std::optional<DisplayId> id = DisplayId::fromValue(static_cast<uint64_t>(displayId));
- status = mFlinger->captureDisplay(*id, captureListener);
+ mFlinger->captureDisplay(*id, captureListener);
} else {
- status = PERMISSION_DENIED;
+ invokeScreenCaptureError(PERMISSION_DENIED, captureListener);
}
- return binderStatusFromStatusT(status);
+ return binderStatusFromStatusT(NO_ERROR);
}
binder::Status SurfaceComposerAIDL::captureLayers(
const LayerCaptureArgs& args, const sp<IScreenCaptureListener>& captureListener) {
- status_t status = mFlinger->captureLayers(args, captureListener);
- return binderStatusFromStatusT(status);
+ mFlinger->captureLayers(args, captureListener);
+ return binderStatusFromStatusT(NO_ERROR);
}
binder::Status SurfaceComposerAIDL::overrideHdrTypes(const sp<IBinder>& display,
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index b07910d..d9c1101 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -535,9 +535,9 @@
EventRegistrationFlags eventRegistration = {},
const sp<IBinder>& layerHandle = nullptr);
- status_t captureDisplay(const DisplayCaptureArgs&, const sp<IScreenCaptureListener>&);
- status_t captureDisplay(DisplayId, const sp<IScreenCaptureListener>&);
- status_t captureLayers(const LayerCaptureArgs&, const sp<IScreenCaptureListener>&);
+ void captureDisplay(const DisplayCaptureArgs&, const sp<IScreenCaptureListener>&);
+ void captureDisplay(DisplayId, const sp<IScreenCaptureListener>&);
+ void captureLayers(const LayerCaptureArgs&, const sp<IScreenCaptureListener>&);
status_t getDisplayStats(const sp<IBinder>& displayToken, DisplayStatInfo* stats);
status_t getDisplayState(const sp<IBinder>& displayToken, ui::DisplayState*)
@@ -638,7 +638,8 @@
// ICompositor overrides:
void configure() override REQUIRES(kMainThreadContext);
- bool commit(const scheduler::FrameTarget&) override REQUIRES(kMainThreadContext);
+ bool commit(PhysicalDisplayId pacesetterId, const scheduler::FrameTargets&) override
+ REQUIRES(kMainThreadContext);
CompositeResultsPerDisplay composite(PhysicalDisplayId pacesetterId,
const scheduler::FrameTargeters&) override
REQUIRES(kMainThreadContext);
@@ -684,11 +685,10 @@
REQUIRES(mStateLock);
status_t setActiveModeFromBackdoor(const sp<display::DisplayToken>&, DisplayModeId);
- // Sets the active mode and a new refresh rate in SF.
- void updateInternalStateWithChangedMode() REQUIRES(mStateLock, kMainThreadContext);
- // Calls to setActiveMode on the main thread if there is a pending mode change
- // that needs to be applied.
- void setActiveModeInHwcIfNeeded() REQUIRES(mStateLock, kMainThreadContext);
+
+ void initiateDisplayModeChanges() REQUIRES(mStateLock, kMainThreadContext);
+ void finalizeDisplayModeChange(DisplayDevice&) REQUIRES(mStateLock, kMainThreadContext);
+
void clearDesiredActiveModeState(const sp<DisplayDevice>&) REQUIRES(mStateLock);
// Called when active mode is no longer is progress
void desiredActiveModeChangeDone(const sp<DisplayDevice>&) REQUIRES(mStateLock);
@@ -836,10 +836,9 @@
// Boot animation, on/off animations and screen capture
void startBootAnim();
- ftl::SharedFuture<FenceResult> captureScreenCommon(RenderAreaFuture, GetLayerSnapshotsFunction,
- ui::Size bufferSize, ui::PixelFormat,
- bool allowProtected, bool grayscale,
- const sp<IScreenCaptureListener>&);
+ void captureScreenCommon(RenderAreaFuture, GetLayerSnapshotsFunction, ui::Size bufferSize,
+ ui::PixelFormat, bool allowProtected, bool grayscale,
+ const sp<IScreenCaptureListener>&);
ftl::SharedFuture<FenceResult> captureScreenCommon(
RenderAreaFuture, GetLayerSnapshotsFunction,
const std::shared_ptr<renderengine::ExternalTexture>&, bool regionSampling,
@@ -1011,7 +1010,9 @@
const DisplayDeviceState& drawingState)
REQUIRES(mStateLock, kMainThreadContext);
- void dispatchDisplayHotplugEvent(PhysicalDisplayId displayId, bool connected);
+ void dispatchDisplayHotplugEvent(PhysicalDisplayId, bool connected);
+ void dispatchDisplayModeChangeEvent(PhysicalDisplayId, const scheduler::FrameRateMode&)
+ REQUIRES(mStateLock);
/*
* VSYNC
@@ -1206,6 +1207,8 @@
// latched.
std::unordered_set<sp<Layer>, SpHash<Layer>> mLayersWithQueuedFrames;
std::unordered_set<sp<Layer>, SpHash<Layer>> mLayersWithBuffersRemoved;
+ std::unordered_set<uint32_t> mLayersIdsWithQueuedFrames;
+
// Tracks layers that need to update a display's dirty region.
std::vector<sp<Layer>> mLayersPendingRefresh;
// Sorted list of layers that were composed during previous frame. This is used to
@@ -1330,9 +1333,6 @@
std::unique_ptr<scheduler::RefreshRateStats> mRefreshRateStats;
scheduler::PresentLatencyTracker mPresentLatencyTracker GUARDED_BY(kMainThreadContext);
- // below flags are set by main thread only
- bool mSetActiveModePending = false;
-
bool mLumaSampling = true;
sp<RegionSamplingThread> mRegionSamplingThread;
sp<FpsReporter> mFpsReporter;
diff --git a/services/surfaceflinger/fuzzer/surfaceflinger_fuzzers_utils.h b/services/surfaceflinger/fuzzer/surfaceflinger_fuzzers_utils.h
index 28ac664..ca1af6e 100644
--- a/services/surfaceflinger/fuzzer/surfaceflinger_fuzzers_utils.h
+++ b/services/surfaceflinger/fuzzer/surfaceflinger_fuzzers_utils.h
@@ -286,7 +286,7 @@
private:
// ICompositor overrides:
void configure() override {}
- bool commit(const scheduler::FrameTarget&) override { return false; }
+ bool commit(PhysicalDisplayId, const scheduler::FrameTargets&) override { return false; }
CompositeResultsPerDisplay composite(PhysicalDisplayId,
const scheduler::FrameTargeters&) override {
return {};
diff --git a/services/surfaceflinger/layerproto/display.proto b/services/surfaceflinger/layerproto/display.proto
index c8cd926..64de775 100644
--- a/services/surfaceflinger/layerproto/display.proto
+++ b/services/surfaceflinger/layerproto/display.proto
@@ -35,4 +35,8 @@
TransformProto transform = 6;
bool is_virtual = 7;
+
+ double dpi_x = 8;
+
+ double dpi_y = 9;
}
diff --git a/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp b/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp
index 1dcf222..9aa089f 100644
--- a/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp
+++ b/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp
@@ -35,7 +35,7 @@
struct NoOpCompositor final : ICompositor {
void configure() override {}
- bool commit(const scheduler::FrameTarget&) override { return false; }
+ bool commit(PhysicalDisplayId, const scheduler::FrameTargets&) override { return false; }
CompositeResultsPerDisplay composite(PhysicalDisplayId,
const scheduler::FrameTargeters&) override {
return {};
diff --git a/services/surfaceflinger/tests/unittests/SurfaceFlinger_DisplayModeSwitching.cpp b/services/surfaceflinger/tests/unittests/SurfaceFlinger_DisplayModeSwitching.cpp
index 703bdda..24eb318 100644
--- a/services/surfaceflinger/tests/unittests/SurfaceFlinger_DisplayModeSwitching.cpp
+++ b/services/surfaceflinger/tests/unittests/SurfaceFlinger_DisplayModeSwitching.cpp
@@ -67,10 +67,36 @@
.WillByDefault(Return(true));
}
+ static constexpr HWDisplayId kInnerDisplayHwcId = PrimaryDisplayVariant::HWC_DISPLAY_ID;
+ static constexpr HWDisplayId kOuterDisplayHwcId = kInnerDisplayHwcId + 1;
+
+ auto injectOuterDisplay() {
+ constexpr PhysicalDisplayId kOuterDisplayId = PhysicalDisplayId::fromPort(254u);
+
+ constexpr bool kIsPrimary = false;
+ TestableSurfaceFlinger::FakeHwcDisplayInjector(kOuterDisplayId, hal::DisplayType::PHYSICAL,
+ kIsPrimary)
+ .setHwcDisplayId(kOuterDisplayHwcId)
+ .setPowerMode(hal::PowerMode::OFF)
+ .inject(&mFlinger, mComposer);
+
+ mOuterDisplay = mFakeDisplayInjector.injectInternalDisplay(
+ [&](FakeDisplayDeviceInjector& injector) {
+ injector.setPowerMode(hal::PowerMode::OFF);
+ injector.setDisplayModes(mock::cloneForDisplay(kOuterDisplayId, kModes),
+ kModeId120);
+ },
+ {.displayId = kOuterDisplayId,
+ .hwcDisplayId = kOuterDisplayHwcId,
+ .isPrimary = kIsPrimary});
+
+ return std::forward_as_tuple(mDisplay, mOuterDisplay);
+ }
+
protected:
void setupScheduler(std::shared_ptr<scheduler::RefreshRateSelector>);
- sp<DisplayDevice> mDisplay;
+ sp<DisplayDevice> mDisplay, mOuterDisplay;
mock::EventThread* mAppEventThread;
static constexpr DisplayModeId kModeId60{0};
@@ -328,32 +354,16 @@
return true;
}
-TEST_F(DisplayModeSwitchingTest, multiDisplay) {
- constexpr HWDisplayId kInnerDisplayHwcId = PrimaryDisplayVariant::HWC_DISPLAY_ID;
- constexpr HWDisplayId kOuterDisplayHwcId = kInnerDisplayHwcId + 1;
+TEST_F(DisplayModeSwitchingTest, innerXorOuterDisplay) {
+ const auto [innerDisplay, outerDisplay] = injectOuterDisplay();
- constexpr PhysicalDisplayId kOuterDisplayId = PhysicalDisplayId::fromPort(254u);
-
- constexpr bool kIsPrimary = false;
- TestableSurfaceFlinger::FakeHwcDisplayInjector(kOuterDisplayId, hal::DisplayType::PHYSICAL,
- kIsPrimary)
- .setHwcDisplayId(kOuterDisplayHwcId)
- .inject(&mFlinger, mComposer);
-
- const auto outerDisplay = mFakeDisplayInjector.injectInternalDisplay(
- [&](FakeDisplayDeviceInjector& injector) {
- injector.setDisplayModes(mock::cloneForDisplay(kOuterDisplayId, kModes),
- kModeId120);
- },
- {.displayId = kOuterDisplayId,
- .hwcDisplayId = kOuterDisplayHwcId,
- .isPrimary = kIsPrimary});
-
- const auto& innerDisplay = mDisplay;
+ EXPECT_TRUE(innerDisplay->isPoweredOn());
+ EXPECT_FALSE(outerDisplay->isPoweredOn());
EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId60));
EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId120));
+ // Only the inner display is powered on.
mFlinger.onActiveDisplayChanged(nullptr, *innerDisplay);
EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId60));
@@ -388,6 +398,10 @@
EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId90));
EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId120));
+ innerDisplay->setPowerMode(hal::PowerMode::OFF);
+ outerDisplay->setPowerMode(hal::PowerMode::ON);
+
+ // Only the outer display is powered on.
mFlinger.onActiveDisplayChanged(innerDisplay.get(), *outerDisplay);
EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId90));
@@ -409,5 +423,107 @@
EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId60));
}
+TEST_F(DisplayModeSwitchingTest, innerAndOuterDisplay) {
+ const auto [innerDisplay, outerDisplay] = injectOuterDisplay();
+
+ EXPECT_TRUE(innerDisplay->isPoweredOn());
+ EXPECT_FALSE(outerDisplay->isPoweredOn());
+
+ EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId60));
+ EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId120));
+
+ outerDisplay->setPowerMode(hal::PowerMode::ON);
+
+ // Both displays are powered on.
+ mFlinger.onActiveDisplayChanged(nullptr, *innerDisplay);
+
+ EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId60));
+ EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId120));
+
+ EXPECT_EQ(NO_ERROR,
+ mFlinger.setDesiredDisplayModeSpecs(innerDisplay->getDisplayToken().promote(),
+ mock::createDisplayModeSpecs(kModeId90.value(),
+ false, 0.f, 120.f)));
+
+ EXPECT_EQ(NO_ERROR,
+ mFlinger.setDesiredDisplayModeSpecs(outerDisplay->getDisplayToken().promote(),
+ mock::createDisplayModeSpecs(kModeId60.value(),
+ false, 0.f, 120.f)));
+
+ EXPECT_THAT(innerDisplay, ModeSwitchingTo(&mFlinger, kModeId90));
+ EXPECT_THAT(outerDisplay, ModeSwitchingTo(&mFlinger, kModeId60));
+
+ const VsyncPeriodChangeTimeline timeline{.refreshRequired = true};
+ EXPECT_CALL(*mComposer,
+ setActiveConfigWithConstraints(kInnerDisplayHwcId,
+ hal::HWConfigId(kModeId90.value()), _, _))
+ .WillOnce(DoAll(SetArgPointee<3>(timeline), Return(Error::NONE)));
+
+ EXPECT_CALL(*mComposer,
+ setActiveConfigWithConstraints(kOuterDisplayHwcId,
+ hal::HWConfigId(kModeId60.value()), _, _))
+ .WillOnce(DoAll(SetArgPointee<3>(timeline), Return(Error::NONE)));
+
+ mFlinger.commit();
+
+ EXPECT_THAT(innerDisplay, ModeSwitchingTo(&mFlinger, kModeId90));
+ EXPECT_THAT(outerDisplay, ModeSwitchingTo(&mFlinger, kModeId60));
+
+ mFlinger.commit();
+
+ EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId90));
+ EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId60));
+}
+
+TEST_F(DisplayModeSwitchingTest, powerOffDuringModeSet) {
+ const auto [innerDisplay, outerDisplay] = injectOuterDisplay();
+
+ EXPECT_TRUE(innerDisplay->isPoweredOn());
+ EXPECT_FALSE(outerDisplay->isPoweredOn());
+
+ EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId60));
+ EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId120));
+
+ outerDisplay->setPowerMode(hal::PowerMode::ON);
+
+ // Both displays are powered on.
+ mFlinger.onActiveDisplayChanged(nullptr, *innerDisplay);
+
+ EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId60));
+ EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId120));
+
+ EXPECT_EQ(NO_ERROR,
+ mFlinger.setDesiredDisplayModeSpecs(innerDisplay->getDisplayToken().promote(),
+ mock::createDisplayModeSpecs(kModeId90.value(),
+ false, 0.f, 120.f)));
+
+ EXPECT_EQ(NO_ERROR,
+ mFlinger.setDesiredDisplayModeSpecs(outerDisplay->getDisplayToken().promote(),
+ mock::createDisplayModeSpecs(kModeId60.value(),
+ false, 0.f, 120.f)));
+
+ EXPECT_THAT(innerDisplay, ModeSwitchingTo(&mFlinger, kModeId90));
+ EXPECT_THAT(outerDisplay, ModeSwitchingTo(&mFlinger, kModeId60));
+
+ // Power off the outer display before the mode has been set.
+ outerDisplay->setPowerMode(hal::PowerMode::OFF);
+
+ const VsyncPeriodChangeTimeline timeline{.refreshRequired = true};
+ EXPECT_CALL(*mComposer,
+ setActiveConfigWithConstraints(kInnerDisplayHwcId,
+ hal::HWConfigId(kModeId90.value()), _, _))
+ .WillOnce(DoAll(SetArgPointee<3>(timeline), Return(Error::NONE)));
+
+ mFlinger.commit();
+
+ EXPECT_THAT(innerDisplay, ModeSwitchingTo(&mFlinger, kModeId90));
+ EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId120));
+
+ mFlinger.commit();
+
+ EXPECT_THAT(innerDisplay, ModeSettledTo(kModeId90));
+ EXPECT_THAT(outerDisplay, ModeSettledTo(kModeId120));
+}
+
} // namespace
} // namespace android
diff --git a/services/surfaceflinger/tests/unittests/TestableScheduler.h b/services/surfaceflinger/tests/unittests/TestableScheduler.h
index f3c9d0d..151b178 100644
--- a/services/surfaceflinger/tests/unittests/TestableScheduler.h
+++ b/services/surfaceflinger/tests/unittests/TestableScheduler.h
@@ -180,7 +180,7 @@
private:
// ICompositor overrides:
void configure() override {}
- bool commit(const scheduler::FrameTarget&) override { return false; }
+ bool commit(PhysicalDisplayId, const scheduler::FrameTargets&) override { return false; }
CompositeResultsPerDisplay composite(PhysicalDisplayId,
const scheduler::FrameTargeters&) override {
return {};
diff --git a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h
index 9b3a893..e59d44d 100644
--- a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h
+++ b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h
@@ -386,10 +386,19 @@
.sfWorkDuration = 10ms},
*mScheduler->getVsyncSchedule());
- mFlinger->commit(frameTargeter.target());
+ scheduler::FrameTargets targets;
+ scheduler::FrameTargeters targeters;
+
+ for (const auto& [id, display] :
+ FTL_FAKE_GUARD(mFlinger->mStateLock, mFlinger->mPhysicalDisplays)) {
+ targets.try_emplace(id, &frameTargeter.target());
+ targeters.try_emplace(id, &frameTargeter);
+ }
+
+ mFlinger->commit(displayId, targets);
if (composite) {
- mFlinger->composite(displayId, ftl::init::map(displayId, &frameTargeter));
+ mFlinger->composite(displayId, targeters);
}
}