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