Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "SkiaOpenGLPipeline.h" |
| 18 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 19 | #include "hwui/Bitmap.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 20 | #include "DeferredLayerUpdater.h" |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 21 | #include "GlLayer.h" |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 22 | #include "LayerDrawable.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 23 | #include "renderthread/EglManager.h" |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 24 | #include "renderthread/Frame.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 25 | #include "renderstate/RenderState.h" |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 26 | #include "SkiaPipeline.h" |
| 27 | #include "SkiaProfileRenderer.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 28 | #include "utils/TraceUtils.h" |
| 29 | |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 30 | #include <GrBackendSurface.h> |
| 31 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 32 | #include <cutils/properties.h> |
| 33 | #include <strings.h> |
| 34 | |
| 35 | using namespace android::uirenderer::renderthread; |
| 36 | |
| 37 | namespace android { |
| 38 | namespace uirenderer { |
| 39 | namespace skiapipeline { |
| 40 | |
| 41 | SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread) |
| 42 | : SkiaPipeline(thread) |
| 43 | , mEglManager(thread.eglManager()) { |
| 44 | } |
| 45 | |
| 46 | MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() { |
| 47 | // TODO: Figure out why this workaround is needed, see b/13913604 |
| 48 | // In the meantime this matches the behavior of GLRenderer, so it is not a regression |
| 49 | EGLint error = 0; |
| 50 | if (!mEglManager.makeCurrent(mEglSurface, &error)) { |
| 51 | return MakeCurrentResult::AlreadyCurrent; |
| 52 | } |
| 53 | return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded; |
| 54 | } |
| 55 | |
| 56 | Frame SkiaOpenGLPipeline::getFrame() { |
| 57 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 58 | "drawRenderNode called on a context with no surface!"); |
| 59 | return mEglManager.beginFrame(mEglSurface); |
| 60 | } |
| 61 | |
| 62 | bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, |
| 63 | const SkRect& dirty, |
| 64 | const FrameBuilder::LightGeometry& lightGeometry, |
| 65 | LayerUpdateQueue* layerUpdateQueue, |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 66 | const Rect& contentDrawBounds, bool opaque, bool wideColorGamut, |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 67 | const BakedOpRenderer::LightInfo& lightInfo, |
| 68 | const std::vector<sp<RenderNode>>& renderNodes, |
| 69 | FrameInfoVisualizer* profiler) { |
| 70 | |
| 71 | mEglManager.damageFrame(frame, dirty); |
| 72 | |
| 73 | // setup surface for fbo0 |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 74 | GrGLFramebufferInfo fboInfo; |
| 75 | fboInfo.fFBOID = 0; |
| 76 | |
| 77 | GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, |
| 78 | kRGBA_8888_GrPixelConfig, fboInfo); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 79 | |
| 80 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 81 | |
| 82 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
| 83 | sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget( |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 84 | mRenderThread.getGrContext(), backendRT, kBottomLeft_GrSurfaceOrigin, nullptr, &props)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 85 | |
| 86 | SkiaPipeline::updateLighting(lightGeometry, lightInfo); |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 87 | renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, wideColorGamut, |
| 88 | contentDrawBounds, surface); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 89 | layerUpdateQueue->clear(); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 90 | |
| 91 | // Draw visual debugging features |
| 92 | if (CC_UNLIKELY(Properties::showDirtyRegions |
Matt Sarett | 4c9bbf4 | 2016-11-07 14:23:12 -0500 | [diff] [blame] | 93 | || ProfileType::None != Properties::getProfileType())) { |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 94 | SkCanvas* profileCanvas = surface->getCanvas(); |
| 95 | SkiaProfileRenderer profileRenderer(profileCanvas); |
| 96 | profiler->draw(profileRenderer); |
| 97 | profileCanvas->flush(); |
| 98 | } |
| 99 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 100 | // Log memory statistics |
| 101 | if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) { |
| 102 | dumpResourceCacheUsage(); |
| 103 | } |
| 104 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, |
| 109 | const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) { |
| 110 | |
| 111 | GL_CHECKPOINT(LOW); |
| 112 | |
| 113 | // Even if we decided to cancel the frame, from the perspective of jank |
| 114 | // metrics the frame was swapped at this point |
| 115 | currentFrameInfo->markSwapBuffers(); |
| 116 | |
| 117 | *requireSwap = drew || mEglManager.damageRequiresSwap(); |
| 118 | |
| 119 | if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | return *requireSwap; |
| 124 | } |
| 125 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 126 | bool SkiaOpenGLPipeline::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) { |
| 127 | if (!mRenderThread.getGrContext()) { |
| 128 | return false; |
| 129 | } |
| 130 | |
Chris Craik | 2f1aaf7 | 2017-02-14 13:01:42 -0800 | [diff] [blame] | 131 | // acquire most recent buffer for drawing |
| 132 | deferredLayer->updateTexImage(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 133 | deferredLayer->apply(); |
| 134 | |
| 135 | SkCanvas canvas(*bitmap); |
| 136 | Layer* layer = deferredLayer->backingLayer(); |
| 137 | return LayerDrawable::DrawLayer(mRenderThread.getGrContext(), &canvas, layer); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 138 | } |
| 139 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 140 | static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight, |
| 141 | SkColorFilter* colorFilter, int alpha, SkBlendMode mode, bool blend) { |
| 142 | GlLayer* layer = new GlLayer(renderState, layerWidth, layerHeight, colorFilter, alpha, |
| 143 | mode, blend); |
| 144 | layer->generateTexture(); |
| 145 | return layer; |
| 146 | } |
| 147 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 148 | DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() { |
| 149 | mEglManager.initialize(); |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 150 | return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::OpenGL); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void SkiaOpenGLPipeline::onStop() { |
| 154 | if (mEglManager.isCurrent(mEglSurface)) { |
| 155 | mEglManager.makeCurrent(EGL_NO_SURFACE); |
| 156 | } |
| 157 | } |
| 158 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 159 | bool SkiaOpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior, |
| 160 | ColorMode colorMode) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 161 | |
| 162 | if (mEglSurface != EGL_NO_SURFACE) { |
| 163 | mEglManager.destroySurface(mEglSurface); |
| 164 | mEglSurface = EGL_NO_SURFACE; |
| 165 | } |
| 166 | |
| 167 | if (surface) { |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 168 | const bool wideColorGamut = colorMode == ColorMode::WideColorGamut; |
| 169 | mEglSurface = mEglManager.createSurface(surface, wideColorGamut); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | if (mEglSurface != EGL_NO_SURFACE) { |
| 173 | const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer); |
| 174 | mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | bool SkiaOpenGLPipeline::isSurfaceReady() { |
| 182 | return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE); |
| 183 | } |
| 184 | |
| 185 | bool SkiaOpenGLPipeline::isContextReady() { |
| 186 | return CC_LIKELY(mEglManager.hasEglContext()); |
| 187 | } |
| 188 | |
| 189 | void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { |
| 190 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 191 | if (thread.eglManager().hasEglContext()) { |
| 192 | mode = DrawGlInfo::kModeProcess; |
| 193 | } |
| 194 | |
| 195 | (*functor)(mode, nullptr); |
| 196 | |
| 197 | // If there's no context we don't need to reset as there's no gl state to save/restore |
| 198 | if (mode != DrawGlInfo::kModeProcessNoContext) { |
| 199 | thread.getGrContext()->resetContext(); |
| 200 | } |
| 201 | } |
| 202 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 203 | #define FENCE_TIMEOUT 2000000000 |
| 204 | |
| 205 | class AutoEglFence { |
| 206 | public: |
| 207 | AutoEglFence(EGLDisplay display) |
| 208 | : mDisplay(display) { |
| 209 | fence = eglCreateSyncKHR(mDisplay, EGL_SYNC_FENCE_KHR, NULL); |
| 210 | } |
| 211 | |
| 212 | ~AutoEglFence() { |
| 213 | if (fence != EGL_NO_SYNC_KHR) { |
| 214 | eglDestroySyncKHR(mDisplay, fence); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | EGLSyncKHR fence = EGL_NO_SYNC_KHR; |
| 219 | private: |
| 220 | EGLDisplay mDisplay = EGL_NO_DISPLAY; |
| 221 | }; |
| 222 | |
| 223 | class AutoEglImage { |
| 224 | public: |
| 225 | AutoEglImage(EGLDisplay display, EGLClientBuffer clientBuffer) |
| 226 | : mDisplay(display) { |
| 227 | EGLint imageAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE }; |
| 228 | image = eglCreateImageKHR(display, EGL_NO_CONTEXT, |
| 229 | EGL_NATIVE_BUFFER_ANDROID, clientBuffer, imageAttrs); |
| 230 | } |
| 231 | |
| 232 | ~AutoEglImage() { |
| 233 | if (image != EGL_NO_IMAGE_KHR) { |
| 234 | eglDestroyImageKHR(mDisplay, image); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | EGLImageKHR image = EGL_NO_IMAGE_KHR; |
| 239 | private: |
| 240 | EGLDisplay mDisplay = EGL_NO_DISPLAY; |
| 241 | }; |
| 242 | |
| 243 | class AutoSkiaGlTexture { |
| 244 | public: |
| 245 | AutoSkiaGlTexture() { |
| 246 | glGenTextures(1, &mTexture); |
| 247 | glBindTexture(GL_TEXTURE_2D, mTexture); |
| 248 | } |
| 249 | |
| 250 | ~AutoSkiaGlTexture() { |
| 251 | glDeleteTextures(1, &mTexture); |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | GLuint mTexture = 0; |
| 256 | }; |
| 257 | |
| 258 | sk_sp<Bitmap> SkiaOpenGLPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread, |
| 259 | SkBitmap& skBitmap) { |
| 260 | renderThread.eglManager().initialize(); |
| 261 | |
| 262 | sk_sp<GrContext> grContext = sk_ref_sp(renderThread.getGrContext()); |
| 263 | const SkImageInfo& info = skBitmap.info(); |
| 264 | PixelFormat pixelFormat; |
| 265 | GLint format, type; |
| 266 | bool isSupported = false; |
| 267 | |
| 268 | //TODO: add support for linear blending (when ANDROID_ENABLE_LINEAR_BLENDING is defined) |
| 269 | switch (info.colorType()) { |
| 270 | case kRGBA_8888_SkColorType: |
| 271 | isSupported = true; |
Leon Scroggins III | f51a80d | 2017-07-12 10:46:35 -0400 | [diff] [blame] | 272 | // ARGB_4444 is upconverted to RGBA_8888 |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 273 | case kARGB_4444_SkColorType: |
| 274 | pixelFormat = PIXEL_FORMAT_RGBA_8888; |
| 275 | format = GL_RGBA; |
| 276 | type = GL_UNSIGNED_BYTE; |
| 277 | break; |
| 278 | case kRGBA_F16_SkColorType: |
| 279 | isSupported = grContext->caps()->isConfigTexturable(kRGBA_half_GrPixelConfig); |
| 280 | if (isSupported) { |
| 281 | type = GL_HALF_FLOAT; |
| 282 | pixelFormat = PIXEL_FORMAT_RGBA_FP16; |
| 283 | } else { |
| 284 | type = GL_UNSIGNED_BYTE; |
| 285 | pixelFormat = PIXEL_FORMAT_RGBA_8888; |
| 286 | } |
| 287 | format = GL_RGBA; |
| 288 | break; |
| 289 | case kRGB_565_SkColorType: |
| 290 | isSupported = true; |
| 291 | pixelFormat = PIXEL_FORMAT_RGB_565; |
| 292 | format = GL_RGB; |
| 293 | type = GL_UNSIGNED_SHORT_5_6_5; |
| 294 | break; |
| 295 | case kGray_8_SkColorType: |
| 296 | isSupported = true; |
| 297 | pixelFormat = PIXEL_FORMAT_RGBA_8888; |
| 298 | format = GL_LUMINANCE; |
| 299 | type = GL_UNSIGNED_BYTE; |
| 300 | break; |
| 301 | default: |
| 302 | ALOGW("unable to create hardware bitmap of colortype: %d", info.colorType()); |
| 303 | return nullptr; |
| 304 | } |
| 305 | |
| 306 | SkBitmap bitmap; |
| 307 | if (isSupported) { |
| 308 | bitmap = skBitmap; |
| 309 | } else { |
| 310 | bitmap.allocPixels(SkImageInfo::MakeN32(info.width(), info.height(), info.alphaType(), |
| 311 | nullptr)); |
| 312 | bitmap.eraseColor(0); |
| 313 | if (info.colorType() == kRGBA_F16_SkColorType) { |
| 314 | // Drawing RGBA_F16 onto ARGB_8888 is not supported |
| 315 | skBitmap.readPixels(bitmap.info().makeColorSpace(SkColorSpace::MakeSRGB()), |
| 316 | bitmap.getPixels(), bitmap.rowBytes(), 0, 0); |
| 317 | } else { |
| 318 | SkCanvas canvas(bitmap); |
| 319 | canvas.drawBitmap(skBitmap, 0.0f, 0.0f, nullptr); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | sp<GraphicBuffer> buffer = new GraphicBuffer(info.width(), info.height(), pixelFormat, |
| 324 | GraphicBuffer::USAGE_HW_TEXTURE | |
| 325 | GraphicBuffer::USAGE_SW_WRITE_NEVER | |
| 326 | GraphicBuffer::USAGE_SW_READ_NEVER, |
| 327 | std::string("Bitmap::allocateSkiaHardwareBitmap pid [") + std::to_string(getpid()) + "]"); |
| 328 | |
| 329 | status_t error = buffer->initCheck(); |
| 330 | if (error < 0) { |
| 331 | ALOGW("createGraphicBuffer() failed in GraphicBuffer.create()"); |
| 332 | return nullptr; |
| 333 | } |
| 334 | |
| 335 | //upload the bitmap into a texture |
| 336 | EGLDisplay display = eglGetCurrentDisplay(); |
| 337 | LOG_ALWAYS_FATAL_IF(display == EGL_NO_DISPLAY, |
| 338 | "Failed to get EGL_DEFAULT_DISPLAY! err=%s", |
| 339 | uirenderer::renderthread::EglManager::eglErrorString()); |
| 340 | // We use an EGLImage to access the content of the GraphicBuffer |
| 341 | // The EGL image is later bound to a 2D texture |
| 342 | EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer(); |
| 343 | AutoEglImage autoImage(display, clientBuffer); |
| 344 | if (autoImage.image == EGL_NO_IMAGE_KHR) { |
| 345 | ALOGW("Could not create EGL image, err =%s", |
| 346 | uirenderer::renderthread::EglManager::eglErrorString()); |
| 347 | return nullptr; |
| 348 | } |
| 349 | AutoSkiaGlTexture glTexture; |
| 350 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, autoImage.image); |
| 351 | GL_CHECKPOINT(MODERATE); |
| 352 | |
| 353 | // glTexSubImage2D is synchronous in sense that it memcpy() from pointer that we provide. |
| 354 | // But asynchronous in sense that driver may upload texture onto hardware buffer when we first |
| 355 | // use it in drawing |
| 356 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, info.width(), info.height(), format, type, |
| 357 | bitmap.getPixels()); |
| 358 | GL_CHECKPOINT(MODERATE); |
| 359 | |
| 360 | // The fence is used to wait for the texture upload to finish |
| 361 | // properly. We cannot rely on glFlush() and glFinish() as |
| 362 | // some drivers completely ignore these API calls |
| 363 | AutoEglFence autoFence(display); |
| 364 | if (autoFence.fence == EGL_NO_SYNC_KHR) { |
| 365 | LOG_ALWAYS_FATAL("Could not create sync fence %#x", eglGetError()); |
| 366 | return nullptr; |
| 367 | } |
| 368 | // The flag EGL_SYNC_FLUSH_COMMANDS_BIT_KHR will trigger a |
| 369 | // pipeline flush (similar to what a glFlush() would do.) |
| 370 | EGLint waitStatus = eglClientWaitSyncKHR(display, autoFence.fence, |
| 371 | EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, FENCE_TIMEOUT); |
| 372 | if (waitStatus != EGL_CONDITION_SATISFIED_KHR) { |
| 373 | LOG_ALWAYS_FATAL("Failed to wait for the fence %#x", eglGetError()); |
| 374 | return nullptr; |
| 375 | } |
| 376 | |
| 377 | grContext->resetContext(kTextureBinding_GrGLBackendState); |
| 378 | |
| 379 | return sk_sp<Bitmap>(new Bitmap(buffer.get(), bitmap.info())); |
| 380 | } |
| 381 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 382 | } /* namespace skiapipeline */ |
| 383 | } /* namespace uirenderer */ |
| 384 | } /* namespace android */ |