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