| Leon Scroggins III | 8fcfa66 | 2021-10-12 11:33:30 -0400 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2021 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <RenderEngineBench.h> | 
|  | 18 | #include <android/bitmap.h> | 
|  | 19 | #include <android/data_space.h> | 
|  | 20 | #include <android/imagedecoder.h> | 
|  | 21 | #include <log/log.h> | 
|  | 22 | #include <renderengine/ExternalTexture.h> | 
|  | 23 | #include <renderengine/RenderEngine.h> | 
|  | 24 | #include <sys/types.h> | 
|  | 25 |  | 
|  | 26 | using namespace android; | 
|  | 27 | using namespace android::renderengine; | 
|  | 28 |  | 
|  | 29 | namespace { | 
|  | 30 | struct DecoderDeleter { | 
|  | 31 | void operator()(AImageDecoder* decoder) { AImageDecoder_delete(decoder); } | 
|  | 32 | }; | 
|  | 33 |  | 
|  | 34 | using AutoDecoderDeleter = std::unique_ptr<AImageDecoder, DecoderDeleter>; | 
|  | 35 |  | 
|  | 36 | bool ok(int aImageDecoderResult, const char* path, const char* method) { | 
|  | 37 | if (aImageDecoderResult == ANDROID_IMAGE_DECODER_SUCCESS) { | 
|  | 38 | return true; | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | ALOGE("Failed AImageDecoder_%s on '%s' with error '%s'", method, path, | 
|  | 42 | AImageDecoder_resultToString(aImageDecoderResult)); | 
|  | 43 | return false; | 
|  | 44 | } | 
|  | 45 | } // namespace | 
|  | 46 |  | 
|  | 47 | namespace renderenginebench { | 
|  | 48 |  | 
|  | 49 | void decode(const char* path, const sp<GraphicBuffer>& buffer) { | 
|  | 50 | base::unique_fd fd{open(path, O_RDONLY)}; | 
|  | 51 | if (fd.get() < 0) { | 
|  | 52 | ALOGE("Failed to open %s", path); | 
|  | 53 | return; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | AImageDecoder* decoder{nullptr}; | 
|  | 57 | auto result = AImageDecoder_createFromFd(fd.get(), &decoder); | 
|  | 58 | if (!ok(result, path, "createFromFd")) { | 
|  | 59 | return; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | AutoDecoderDeleter deleter(decoder); | 
|  | 63 |  | 
|  | 64 | LOG_ALWAYS_FATAL_IF(buffer->getWidth() <= 0 || buffer->getHeight() <= 0, | 
|  | 65 | "Impossible buffer size!"); | 
|  | 66 | auto width = static_cast<int32_t>(buffer->getWidth()); | 
|  | 67 | auto height = static_cast<int32_t>(buffer->getHeight()); | 
|  | 68 | result = AImageDecoder_setTargetSize(decoder, width, height); | 
|  | 69 | if (!ok(result, path, "setTargetSize")) { | 
|  | 70 | return; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | void* pixels{nullptr}; | 
|  | 74 | int32_t stride{0}; | 
|  | 75 | if (auto status = buffer->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &pixels, | 
|  | 76 | nullptr /*outBytesPerPixel*/, &stride); | 
|  | 77 | status < 0) { | 
|  | 78 | ALOGE("Failed to lock pixels!"); | 
|  | 79 | return; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | result = AImageDecoder_decodeImage(decoder, pixels, static_cast<size_t>(stride), | 
|  | 83 | static_cast<size_t>(stride * height)); | 
|  | 84 | if (auto status = buffer->unlock(); status < 0) { | 
|  | 85 | ALOGE("Failed to unlock pixels!"); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | // For the side effect of logging. | 
|  | 89 | (void)ok(result, path, "decodeImage"); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | void encodeToJpeg(const char* path, const sp<GraphicBuffer>& buffer) { | 
|  | 93 | base::unique_fd fd{open(path, O_WRONLY | O_CREAT, S_IWUSR)}; | 
|  | 94 | if (fd.get() < 0) { | 
|  | 95 | ALOGE("Failed to open %s", path); | 
|  | 96 | return; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | void* pixels{nullptr}; | 
|  | 100 | int32_t stride{0}; | 
|  | 101 | if (auto status = buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, &pixels, | 
|  | 102 | nullptr /*outBytesPerPixel*/, &stride); | 
|  | 103 | status < 0) { | 
|  | 104 | ALOGE("Failed to lock pixels!"); | 
|  | 105 | return; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | AndroidBitmapInfo info{ | 
|  | 109 | .width = buffer->getWidth(), | 
|  | 110 | .height = buffer->getHeight(), | 
|  | 111 | .stride = static_cast<uint32_t>(stride), | 
|  | 112 | .format = ANDROID_BITMAP_FORMAT_RGBA_8888, | 
|  | 113 | .flags = ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE, | 
|  | 114 | }; | 
|  | 115 | int result = AndroidBitmap_compress(&info, ADATASPACE_SRGB, pixels, | 
|  | 116 | ANDROID_BITMAP_COMPRESS_FORMAT_JPEG, 80, &fd, | 
|  | 117 | [](void* fdPtr, const void* data, size_t size) -> bool { | 
|  | 118 | const ssize_t bytesWritten = | 
|  | 119 | write(reinterpret_cast<base::unique_fd*>(fdPtr) | 
|  | 120 | ->get(), | 
|  | 121 | data, size); | 
|  | 122 | return bytesWritten > 0 && | 
|  | 123 | static_cast<size_t>(bytesWritten) == size; | 
|  | 124 | }); | 
|  | 125 | if (result == ANDROID_BITMAP_RESULT_SUCCESS) { | 
|  | 126 | ALOGD("Successfully encoded to '%s'", path); | 
|  | 127 | } else { | 
|  | 128 | ALOGE("Failed to encode to %s with error %d", path, result); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | if (auto status = buffer->unlock(); status < 0) { | 
|  | 132 | ALOGE("Failed to unlock pixels!"); | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | } // namespace renderenginebench |