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 | |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 290 | bool HardwareBitmapUploader::hasFP16Support() { |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 291 | static std::once_flag sOnce; |
| 292 | static bool hasFP16Support = false; |
| 293 | |
| 294 | // Gralloc shouldn't let us create a USAGE_HW_TEXTURE if GLES is unable to consume it, so |
| 295 | // we don't need to double-check the GLES version/extension. |
| 296 | std::call_once(sOnce, []() { |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 297 | AHardwareBuffer_Desc desc = { |
| 298 | .width = 1, |
| 299 | .height = 1, |
| 300 | .layers = 1, |
| 301 | .format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT, |
| 302 | .usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER | |
| 303 | AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER | |
| 304 | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE, |
| 305 | }; |
| 306 | UniqueAHardwareBuffer buffer = allocateAHardwareBuffer(desc); |
| 307 | hasFP16Support = buffer != nullptr; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 308 | }); |
| 309 | |
| 310 | return hasFP16Support; |
| 311 | } |
| 312 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 313 | static FormatInfo determineFormat(const SkBitmap& skBitmap, bool usingGL) { |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 314 | FormatInfo formatInfo; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 315 | switch (skBitmap.info().colorType()) { |
| 316 | case kRGBA_8888_SkColorType: |
| 317 | formatInfo.isSupported = true; |
Colin Cross | f4e74b8 | 2019-10-31 13:47:42 -0700 | [diff] [blame] | 318 | [[fallthrough]]; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 319 | // ARGB_4444 is upconverted to RGBA_8888 |
| 320 | case kARGB_4444_SkColorType: |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 321 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 322 | formatInfo.format = GL_RGBA; |
| 323 | formatInfo.type = GL_UNSIGNED_BYTE; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 324 | formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 325 | break; |
| 326 | case kRGBA_F16_SkColorType: |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 327 | formatInfo.isSupported = HardwareBitmapUploader::hasFP16Support(); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 328 | if (formatInfo.isSupported) { |
| 329 | formatInfo.type = GL_HALF_FLOAT; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 330 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 331 | formatInfo.vkFormat = VK_FORMAT_R16G16B16A16_SFLOAT; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 332 | } else { |
| 333 | formatInfo.type = GL_UNSIGNED_BYTE; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 334 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 335 | formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 336 | } |
| 337 | formatInfo.format = GL_RGBA; |
| 338 | break; |
| 339 | case kRGB_565_SkColorType: |
| 340 | formatInfo.isSupported = true; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 341 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 342 | formatInfo.format = GL_RGB; |
| 343 | formatInfo.type = GL_UNSIGNED_SHORT_5_6_5; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 344 | formatInfo.vkFormat = VK_FORMAT_R5G6B5_UNORM_PACK16; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 345 | break; |
| 346 | case kGray_8_SkColorType: |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 347 | formatInfo.isSupported = usingGL; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 348 | formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 349 | formatInfo.format = GL_LUMINANCE; |
| 350 | formatInfo.type = GL_UNSIGNED_BYTE; |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 351 | formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 352 | break; |
| 353 | default: |
| 354 | ALOGW("unable to create hardware bitmap of colortype: %d", skBitmap.info().colorType()); |
| 355 | formatInfo.valid = false; |
| 356 | } |
| 357 | return formatInfo; |
| 358 | } |
| 359 | |
| 360 | static SkBitmap makeHwCompatible(const FormatInfo& format, const SkBitmap& source) { |
| 361 | if (format.isSupported) { |
| 362 | return source; |
| 363 | } else { |
| 364 | SkBitmap bitmap; |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 365 | bitmap.allocPixels(source.info().makeColorType(kN32_SkColorType)); |
| 366 | bitmap.writePixels(source.pixmap()); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 367 | return bitmap; |
| 368 | } |
| 369 | } |
| 370 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 371 | |
| 372 | static void createUploader(bool usingGL) { |
| 373 | static std::mutex lock; |
| 374 | std::lock_guard _lock{lock}; |
| 375 | if (!sUploader.get()) { |
| 376 | if (usingGL) { |
| 377 | sUploader = new EGLUploader(); |
| 378 | } else { |
| 379 | sUploader = new VkUploader(); |
| 380 | } |
| 381 | } |
| 382 | } |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 383 | |
| 384 | sk_sp<Bitmap> HardwareBitmapUploader::allocateHardwareBitmap(const SkBitmap& sourceBitmap) { |
| 385 | ATRACE_CALL(); |
| 386 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 387 | bool usingGL = uirenderer::Properties::getRenderPipelineType() == |
| 388 | uirenderer::RenderPipelineType::SkiaGL; |
| 389 | |
| 390 | FormatInfo format = determineFormat(sourceBitmap, usingGL); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 391 | if (!format.valid) { |
| 392 | return nullptr; |
| 393 | } |
| 394 | |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 395 | SkBitmap bitmap = makeHwCompatible(format, sourceBitmap); |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 396 | AHardwareBuffer_Desc desc = { |
| 397 | .width = static_cast<uint32_t>(bitmap.width()), |
| 398 | .height = static_cast<uint32_t>(bitmap.height()), |
| 399 | .layers = 1, |
| 400 | .format = format.bufferFormat, |
| 401 | .usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER | AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER | |
| 402 | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE, |
| 403 | }; |
| 404 | UniqueAHardwareBuffer ahb = allocateAHardwareBuffer(desc); |
| 405 | if (!ahb) { |
| 406 | ALOGW("allocateHardwareBitmap() failed in AHardwareBuffer_allocate()"); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 407 | return nullptr; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 408 | }; |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 409 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 410 | createUploader(usingGL); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 411 | |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 412 | if (!sUploader->uploadHardwareBitmap(bitmap, format, ahb.get())) { |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 413 | return nullptr; |
| 414 | } |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 415 | return Bitmap::createFrom(ahb.get(), bitmap.colorType(), bitmap.refColorSpace(), |
| 416 | bitmap.alphaType(), Bitmap::computePalette(bitmap)); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 419 | void HardwareBitmapUploader::initialize() { |
| 420 | bool usingGL = uirenderer::Properties::getRenderPipelineType() == |
| 421 | uirenderer::RenderPipelineType::SkiaGL; |
| 422 | createUploader(usingGL); |
Greg Daniel | c073252 | 2019-02-20 08:31:03 -0500 | [diff] [blame] | 423 | } |
| 424 | |
John Reck | 6104cea | 2019-01-10 14:37:17 -0800 | [diff] [blame] | 425 | void HardwareBitmapUploader::terminate() { |
Greg Daniel | 78b7ddc | 2019-03-27 12:52:43 -0400 | [diff] [blame] | 426 | if (sUploader) { |
| 427 | sUploader->destroy(); |
| 428 | } |
John Reck | 6104cea | 2019-01-10 14:37:17 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 431 | } // namespace android::uirenderer |