John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "HardwareBitmapUploader.h" |
| 18 | |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 19 | #include <EGL/egl.h> |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 20 | #include <EGL/eglext.h> |
| 21 | #include <GLES2/gl2.h> |
| 22 | #include <GLES2/gl2ext.h> |
| 23 | #include <GLES3/gl3.h> |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 24 | #include <GrDirectContext.h> |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 25 | #include <SkCanvas.h> |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 26 | #include <SkImage.h> |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 27 | #include <gui/TraceUtils.h> |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 28 | #include <utils/GLUtils.h> |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 29 | #include <utils/NdkUtils.h> |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 30 | #include <utils/Trace.h> |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 31 | |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 32 | #include <thread> |
| 33 | |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 34 | #include "hwui/Bitmap.h" |
| 35 | #include "renderthread/EglManager.h" |
| 36 | #include "renderthread/VulkanManager.h" |
| 37 | #include "thread/ThreadBase.h" |
| 38 | #include "utils/TimeUtils.h" |
| 39 | |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 40 | namespace android::uirenderer { |
| 41 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 42 | class AHBUploader; |
| 43 | // This helper uploader classes allows us to upload using either EGL or Vulkan using the same |
| 44 | // interface. |
| 45 | static sp<AHBUploader> sUploader = nullptr; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 46 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 47 | struct FormatInfo { |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 48 | AHardwareBuffer_Format bufferFormat; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 49 | GLint format, type; |
| 50 | VkFormat vkFormat; |
| 51 | bool isSupported = false; |
| 52 | bool valid = true; |
| 53 | }; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 54 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 55 | class AHBUploader : public RefBase { |
| 56 | public: |
| 57 | virtual ~AHBUploader() {} |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 58 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 59 | void destroy() { |
| 60 | std::lock_guard _lock{mLock}; |
| 61 | LOG_ALWAYS_FATAL_IF(mPendingUploads, "terminate called while uploads in progress"); |
| 62 | if (mUploadThread) { |
| 63 | mUploadThread->requestExit(); |
| 64 | mUploadThread->join(); |
| 65 | mUploadThread = nullptr; |
| 66 | } |
| 67 | onDestroy(); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 70 | bool uploadHardwareBitmap(const SkBitmap& bitmap, const FormatInfo& format, |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 71 | AHardwareBuffer* ahb) { |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 72 | ATRACE_CALL(); |
| 73 | beginUpload(); |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 74 | bool result = onUploadHardwareBitmap(bitmap, format, ahb); |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 75 | endUpload(); |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | void postIdleTimeoutCheck() { |
| 80 | mUploadThread->queue().postDelayed(5000_ms, [this](){ this->idleTimeoutCheck(); }); |
| 81 | } |
| 82 | |
| 83 | protected: |
| 84 | std::mutex mLock; |
| 85 | sp<ThreadBase> mUploadThread = nullptr; |
| 86 | |
| 87 | private: |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 88 | virtual void onIdle() = 0; |
| 89 | virtual void onDestroy() = 0; |
| 90 | |
| 91 | virtual bool onUploadHardwareBitmap(const SkBitmap& bitmap, const FormatInfo& format, |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 92 | AHardwareBuffer* ahb) = 0; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 93 | virtual void onBeginUpload() = 0; |
| 94 | |
| 95 | bool shouldTimeOutLocked() { |
| 96 | nsecs_t durationSince = systemTime() - mLastUpload; |
| 97 | return durationSince > 2000_ms; |
| 98 | } |
| 99 | |
| 100 | void idleTimeoutCheck() { |
| 101 | std::lock_guard _lock{mLock}; |
| 102 | if (mPendingUploads == 0 && shouldTimeOutLocked()) { |
| 103 | onIdle(); |
| 104 | } else { |
| 105 | this->postIdleTimeoutCheck(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void beginUpload() { |
| 110 | std::lock_guard _lock{mLock}; |
| 111 | mPendingUploads++; |
| 112 | |
| 113 | if (!mUploadThread) { |
| 114 | mUploadThread = new ThreadBase{}; |
| 115 | } |
| 116 | if (!mUploadThread->isRunning()) { |
| 117 | mUploadThread->start("GrallocUploadThread"); |
| 118 | } |
| 119 | |
| 120 | onBeginUpload(); |
| 121 | } |
| 122 | |
| 123 | void endUpload() { |
| 124 | std::lock_guard _lock{mLock}; |
| 125 | mPendingUploads--; |
| 126 | mLastUpload = systemTime(); |
| 127 | } |
| 128 | |
| 129 | int mPendingUploads = 0; |
| 130 | nsecs_t mLastUpload = 0; |
| 131 | }; |
| 132 | |
| 133 | #define FENCE_TIMEOUT 2000000000 |
| 134 | |
| 135 | class EGLUploader : public AHBUploader { |
| 136 | private: |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 137 | void onDestroy() override { |
| 138 | mEglManager.destroy(); |
| 139 | } |
| 140 | void onIdle() override { |
| 141 | mEglManager.destroy(); |
| 142 | } |
| 143 | |
| 144 | void onBeginUpload() override { |
| 145 | if (!mEglManager.hasEglContext()) { |
| 146 | mUploadThread->queue().runSync([this]() { |
| 147 | this->mEglManager.initialize(); |
| 148 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 149 | }); |
| 150 | |
| 151 | this->postIdleTimeoutCheck(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | EGLDisplay getUploadEglDisplay() { |
| 157 | std::lock_guard _lock{mLock}; |
| 158 | LOG_ALWAYS_FATAL_IF(!mEglManager.hasEglContext(), "Forgot to begin an upload?"); |
| 159 | return mEglManager.eglDisplay(); |
| 160 | } |
| 161 | |
| 162 | bool onUploadHardwareBitmap(const SkBitmap& bitmap, const FormatInfo& format, |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 163 | AHardwareBuffer* ahb) override { |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 164 | ATRACE_CALL(); |
| 165 | |
| 166 | EGLDisplay display = getUploadEglDisplay(); |
| 167 | |
| 168 | LOG_ALWAYS_FATAL_IF(display == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s", |
| 169 | uirenderer::renderthread::EglManager::eglErrorString()); |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 170 | // We use an EGLImage to access the content of the buffer |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 171 | // The EGL image is later bound to a 2D texture |
Alec Mouri | 70463a6 | 2020-03-30 15:10:17 -0700 | [diff] [blame] | 172 | const EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(ahb); |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 173 | AutoEglImage autoImage(display, clientBuffer); |
| 174 | if (autoImage.image == EGL_NO_IMAGE_KHR) { |
| 175 | ALOGW("Could not create EGL image, err =%s", |
| 176 | uirenderer::renderthread::EglManager::eglErrorString()); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | { |
| 181 | ATRACE_FORMAT("CPU -> gralloc transfer (%dx%d)", bitmap.width(), bitmap.height()); |
| 182 | EGLSyncKHR fence = mUploadThread->queue().runSync([&]() -> EGLSyncKHR { |
| 183 | AutoSkiaGlTexture glTexture; |
| 184 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, autoImage.image); |
John Reck | 996773a | 2020-02-03 16:30:56 -0800 | [diff] [blame] | 185 | if (GLUtils::dumpGLErrors()) { |
| 186 | return EGL_NO_SYNC_KHR; |
| 187 | } |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 188 | |
| 189 | // glTexSubImage2D is synchronous in sense that it memcpy() from pointer that we |
| 190 | // provide. |
| 191 | // But asynchronous in sense that driver may upload texture onto hardware buffer |
| 192 | // when we first use it in drawing |
| 193 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap.width(), bitmap.height(), |
| 194 | format.format, format.type, bitmap.getPixels()); |
John Reck | 996773a | 2020-02-03 16:30:56 -0800 | [diff] [blame] | 195 | if (GLUtils::dumpGLErrors()) { |
| 196 | return EGL_NO_SYNC_KHR; |
| 197 | } |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 198 | |
| 199 | EGLSyncKHR uploadFence = |
| 200 | eglCreateSyncKHR(eglGetCurrentDisplay(), EGL_SYNC_FENCE_KHR, NULL); |
John Reck | 996773a | 2020-02-03 16:30:56 -0800 | [diff] [blame] | 201 | if (uploadFence == EGL_NO_SYNC_KHR) { |
| 202 | ALOGW("Could not create sync fence %#x", eglGetError()); |
| 203 | }; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 204 | glFlush(); |
John Reck | 996773a | 2020-02-03 16:30:56 -0800 | [diff] [blame] | 205 | GLUtils::dumpGLErrors(); |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 206 | return uploadFence; |
| 207 | }); |
| 208 | |
John Reck | 996773a | 2020-02-03 16:30:56 -0800 | [diff] [blame] | 209 | if (fence == EGL_NO_SYNC_KHR) { |
| 210 | return false; |
| 211 | } |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 212 | EGLint waitStatus = eglClientWaitSyncKHR(display, fence, 0, FENCE_TIMEOUT); |
John Reck | 996773a | 2020-02-03 16:30:56 -0800 | [diff] [blame] | 213 | ALOGE_IF(waitStatus != EGL_CONDITION_SATISFIED_KHR, |
| 214 | "Failed to wait for the fence %#x", eglGetError()); |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 215 | |
| 216 | eglDestroySyncKHR(display, fence); |
| 217 | } |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | renderthread::EglManager mEglManager; |
| 222 | }; |
| 223 | |
| 224 | class VkUploader : public AHBUploader { |
| 225 | private: |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 226 | void onDestroy() override { |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 227 | std::lock_guard _lock{mVkLock}; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 228 | mGrContext.reset(); |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 229 | mVulkanManagerStrong.clear(); |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 230 | } |
| 231 | void onIdle() override { |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 232 | onDestroy(); |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 233 | } |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 234 | |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 235 | void onBeginUpload() override {} |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 236 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 237 | bool onUploadHardwareBitmap(const SkBitmap& bitmap, const FormatInfo& format, |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 238 | AHardwareBuffer* ahb) override { |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 239 | bool uploadSucceeded = false; |
| 240 | mUploadThread->queue().runSync([this, &uploadSucceeded, bitmap, ahb]() { |
| 241 | ATRACE_CALL(); |
| 242 | std::lock_guard _lock{mVkLock}; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 243 | |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 244 | renderthread::VulkanManager* vkManager = getVulkanManager(); |
| 245 | if (!vkManager->hasVkContext()) { |
| 246 | LOG_ALWAYS_FATAL_IF(mGrContext, |
| 247 | "GrContext exists with no VulkanManager for vulkan uploads"); |
| 248 | vkManager->initialize(); |
| 249 | } |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 250 | |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 251 | if (!mGrContext) { |
| 252 | GrContextOptions options; |
| 253 | mGrContext = vkManager->createContext(options, |
| 254 | renderthread::VulkanManager::ContextType::kUploadThread); |
| 255 | LOG_ALWAYS_FATAL_IF(!mGrContext, "failed to create GrContext for vulkan uploads"); |
| 256 | this->postIdleTimeoutCheck(); |
| 257 | } |
| 258 | |
| 259 | sk_sp<SkImage> image = |
| 260 | SkImage::MakeFromAHardwareBufferWithData(mGrContext.get(), bitmap.pixmap(), ahb); |
| 261 | mGrContext->submit(true); |
| 262 | |
| 263 | uploadSucceeded = (image.get() != nullptr); |
| 264 | }); |
| 265 | return uploadSucceeded; |
| 266 | } |
| 267 | |
| 268 | /* must be called on the upload thread after the vkLock has been acquired */ |
| 269 | renderthread::VulkanManager* getVulkanManager() { |
| 270 | if (!mVulkanManagerStrong) { |
| 271 | mVulkanManagerStrong = mVulkanManagerWeak.promote(); |
| 272 | |
| 273 | // create a new manager if we couldn't promote the weak ref |
| 274 | if (!mVulkanManagerStrong) { |
| 275 | mVulkanManagerStrong = renderthread::VulkanManager::getInstance(); |
| 276 | mGrContext.reset(); |
| 277 | mVulkanManagerWeak = mVulkanManagerStrong; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return mVulkanManagerStrong.get(); |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 284 | sk_sp<GrDirectContext> mGrContext; |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 285 | sp<renderthread::VulkanManager> mVulkanManagerStrong; |
| 286 | wp<renderthread::VulkanManager> mVulkanManagerWeak; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 287 | std::mutex mVkLock; |
| 288 | }; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 289 | |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 290 | static bool checkSupport(AHardwareBuffer_Format format) { |
| 291 | AHardwareBuffer_Desc desc = { |
| 292 | .width = 1, |
| 293 | .height = 1, |
| 294 | .layers = 1, |
| 295 | .format = format, |
| 296 | .usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER | AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER | |
| 297 | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE, |
| 298 | }; |
| 299 | UniqueAHardwareBuffer buffer = allocateAHardwareBuffer(desc); |
| 300 | return buffer != nullptr; |
| 301 | } |
| 302 | |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 303 | bool HardwareBitmapUploader::hasFP16Support() { |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 304 | static bool hasFP16Support = checkSupport(AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 305 | return hasFP16Support; |
| 306 | } |
| 307 | |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 308 | bool HardwareBitmapUploader::has1010102Support() { |
| 309 | static bool has101012Support = checkSupport(AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM); |
| 310 | return has101012Support; |
| 311 | } |
| 312 | |
Leon Scroggins III | a008aa9 | 2022-03-10 16:25:30 -0500 | [diff] [blame] | 313 | bool HardwareBitmapUploader::hasAlpha8Support() { |
| 314 | static bool hasAlpha8Support = checkSupport(AHARDWAREBUFFER_FORMAT_R8_UNORM); |
| 315 | return hasAlpha8Support; |
| 316 | } |
| 317 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 318 | static FormatInfo determineFormat(const SkBitmap& skBitmap, bool usingGL) { |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 319 | FormatInfo formatInfo; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 320 | switch (skBitmap.info().colorType()) { |
| 321 | case kRGBA_8888_SkColorType: |
| 322 | formatInfo.isSupported = true; |
Colin Cross | f4e74b8 | 2019-10-31 13:47:42 -0700 | [diff] [blame] | 323 | [[fallthrough]]; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 324 | // ARGB_4444 is upconverted to RGBA_8888 |
| 325 | case kARGB_4444_SkColorType: |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 326 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 327 | formatInfo.format = GL_RGBA; |
| 328 | formatInfo.type = GL_UNSIGNED_BYTE; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 329 | formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 330 | break; |
| 331 | case kRGBA_F16_SkColorType: |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 332 | formatInfo.isSupported = HardwareBitmapUploader::hasFP16Support(); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 333 | if (formatInfo.isSupported) { |
| 334 | formatInfo.type = GL_HALF_FLOAT; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 335 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 336 | formatInfo.vkFormat = VK_FORMAT_R16G16B16A16_SFLOAT; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 337 | } else { |
| 338 | formatInfo.type = GL_UNSIGNED_BYTE; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 339 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 340 | formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 341 | } |
| 342 | formatInfo.format = GL_RGBA; |
| 343 | break; |
| 344 | case kRGB_565_SkColorType: |
| 345 | formatInfo.isSupported = true; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 346 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 347 | formatInfo.format = GL_RGB; |
| 348 | formatInfo.type = GL_UNSIGNED_SHORT_5_6_5; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 349 | formatInfo.vkFormat = VK_FORMAT_R5G6B5_UNORM_PACK16; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 350 | break; |
| 351 | case kGray_8_SkColorType: |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 352 | formatInfo.isSupported = usingGL; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 353 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 354 | formatInfo.format = GL_LUMINANCE; |
| 355 | formatInfo.type = GL_UNSIGNED_BYTE; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 356 | formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 357 | break; |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 358 | case kRGBA_1010102_SkColorType: |
| 359 | formatInfo.isSupported = HardwareBitmapUploader::has1010102Support(); |
| 360 | if (formatInfo.isSupported) { |
| 361 | formatInfo.type = GL_UNSIGNED_INT_2_10_10_10_REV; |
| 362 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM; |
| 363 | formatInfo.vkFormat = VK_FORMAT_A2B10G10R10_UNORM_PACK32; |
| 364 | } else { |
| 365 | formatInfo.type = GL_UNSIGNED_BYTE; |
| 366 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
| 367 | formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM; |
| 368 | } |
| 369 | formatInfo.format = GL_RGBA; |
| 370 | break; |
Leon Scroggins III | a008aa9 | 2022-03-10 16:25:30 -0500 | [diff] [blame] | 371 | case kAlpha_8_SkColorType: |
| 372 | formatInfo.isSupported = HardwareBitmapUploader::hasAlpha8Support(); |
| 373 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8_UNORM; |
| 374 | formatInfo.format = GL_R8; |
| 375 | formatInfo.type = GL_UNSIGNED_BYTE; |
| 376 | formatInfo.vkFormat = VK_FORMAT_R8_UNORM; |
| 377 | break; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 378 | default: |
| 379 | ALOGW("unable to create hardware bitmap of colortype: %d", skBitmap.info().colorType()); |
| 380 | formatInfo.valid = false; |
| 381 | } |
| 382 | return formatInfo; |
| 383 | } |
| 384 | |
| 385 | static SkBitmap makeHwCompatible(const FormatInfo& format, const SkBitmap& source) { |
| 386 | if (format.isSupported) { |
| 387 | return source; |
| 388 | } else { |
| 389 | SkBitmap bitmap; |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 390 | bitmap.allocPixels(source.info().makeColorType(kN32_SkColorType)); |
| 391 | bitmap.writePixels(source.pixmap()); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 392 | return bitmap; |
| 393 | } |
| 394 | } |
| 395 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 396 | |
| 397 | static void createUploader(bool usingGL) { |
| 398 | static std::mutex lock; |
| 399 | std::lock_guard _lock{lock}; |
| 400 | if (!sUploader.get()) { |
| 401 | if (usingGL) { |
| 402 | sUploader = new EGLUploader(); |
| 403 | } else { |
| 404 | sUploader = new VkUploader(); |
| 405 | } |
| 406 | } |
| 407 | } |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 408 | |
| 409 | sk_sp<Bitmap> HardwareBitmapUploader::allocateHardwareBitmap(const SkBitmap& sourceBitmap) { |
| 410 | ATRACE_CALL(); |
| 411 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 412 | bool usingGL = uirenderer::Properties::getRenderPipelineType() == |
| 413 | uirenderer::RenderPipelineType::SkiaGL; |
| 414 | |
| 415 | FormatInfo format = determineFormat(sourceBitmap, usingGL); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 416 | if (!format.valid) { |
| 417 | return nullptr; |
| 418 | } |
| 419 | |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 420 | SkBitmap bitmap = makeHwCompatible(format, sourceBitmap); |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 421 | AHardwareBuffer_Desc desc = { |
| 422 | .width = static_cast<uint32_t>(bitmap.width()), |
| 423 | .height = static_cast<uint32_t>(bitmap.height()), |
| 424 | .layers = 1, |
| 425 | .format = format.bufferFormat, |
| 426 | .usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER | AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER | |
| 427 | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE, |
| 428 | }; |
| 429 | UniqueAHardwareBuffer ahb = allocateAHardwareBuffer(desc); |
| 430 | if (!ahb) { |
| 431 | ALOGW("allocateHardwareBitmap() failed in AHardwareBuffer_allocate()"); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 432 | return nullptr; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 433 | }; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 434 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 435 | createUploader(usingGL); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 436 | |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 437 | if (!sUploader->uploadHardwareBitmap(bitmap, format, ahb.get())) { |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 438 | return nullptr; |
| 439 | } |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 440 | return Bitmap::createFrom(ahb.get(), bitmap.colorType(), bitmap.refColorSpace(), |
| 441 | bitmap.alphaType(), Bitmap::computePalette(bitmap)); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 444 | void HardwareBitmapUploader::initialize() { |
| 445 | bool usingGL = uirenderer::Properties::getRenderPipelineType() == |
| 446 | uirenderer::RenderPipelineType::SkiaGL; |
| 447 | createUploader(usingGL); |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 448 | } |
| 449 | |
John Reck | 6104cea | 2019-01-10 14:37:17 -0800 | [diff] [blame] | 450 | void HardwareBitmapUploader::terminate() { |
Greg Daniel | 78b7ddc | 2019-03-27 12:52:43 -0400 | [diff] [blame] | 451 | if (sUploader) { |
| 452 | sUploader->destroy(); |
| 453 | } |
John Reck | 6104cea | 2019-01-10 14:37:17 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 456 | } // namespace android::uirenderer |