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