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