| 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 <GrBackendSurface.h> |
| 20 | #include <SkBlendMode.h> |
| 21 | #include <SkImageInfo.h> |
| 22 | #include <cutils/properties.h> |
| 23 | #include <gui/TraceUtils.h> |
| 24 | #include <strings.h> |
| 25 | |
| 26 | #include "DeferredLayerUpdater.h" |
| 27 | #include "FrameInfo.h" |
| 28 | #include "LayerDrawable.h" |
| 29 | #include "LightingInfo.h" |
| 30 | #include "SkiaPipeline.h" |
| 31 | #include "SkiaProfileRenderer.h" |
| 32 | #include "hwui/Bitmap.h" |
| 33 | #include "private/hwui/DrawGlInfo.h" |
| 34 | #include "renderstate/RenderState.h" |
| 35 | #include "renderthread/EglManager.h" |
| 36 | #include "renderthread/Frame.h" |
| 37 | #include "renderthread/IRenderPipeline.h" |
| 38 | #include "utils/GLUtils.h" |
| 39 | |
| 40 | using namespace android::uirenderer::renderthread; |
| 41 | |
| 42 | namespace android { |
| 43 | namespace uirenderer { |
| 44 | namespace skiapipeline { |
| 45 | |
| 46 | SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread) |
| 47 | : SkiaPipeline(thread), mEglManager(thread.eglManager()) { |
| 48 | thread.renderState().registerContextCallback(this); |
| 49 | } |
| 50 | |
| 51 | SkiaOpenGLPipeline::~SkiaOpenGLPipeline() { |
| 52 | mRenderThread.renderState().removeContextCallback(this); |
| 53 | } |
| 54 | |
| 55 | MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() { |
| 56 | bool wasSurfaceless = mEglManager.isCurrent(EGL_NO_SURFACE); |
| 57 | |
| 58 | // In case the surface was destroyed (e.g. a previous trimMemory call) we |
| 59 | // need to recreate it here. |
| 60 | if (!isSurfaceReady() && mNativeWindow) { |
| 61 | setSurface(mNativeWindow.get(), mSwapBehavior); |
| 62 | } |
| 63 | |
| 64 | EGLint error = 0; |
| 65 | if (!mEglManager.makeCurrent(mEglSurface, &error)) { |
| 66 | return MakeCurrentResult::AlreadyCurrent; |
| 67 | } |
| 68 | |
| 69 | // Make sure read/draw buffer state of default framebuffer is GL_BACK. Vendor implementations |
| 70 | // disagree on the draw/read buffer state if the default framebuffer transitions from a surface |
| 71 | // to EGL_NO_SURFACE and vice-versa. There was a related discussion within Khronos on this topic. |
| 72 | // See https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13534. |
| 73 | // The discussion was not resolved with a clear consensus |
| 74 | if (error == 0 && wasSurfaceless && mEglSurface != EGL_NO_SURFACE) { |
| 75 | GLint curReadFB = 0; |
| 76 | GLint curDrawFB = 0; |
| 77 | glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &curReadFB); |
| 78 | glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &curDrawFB); |
| 79 | |
| 80 | GLint buffer = GL_NONE; |
| 81 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 82 | glGetIntegerv(GL_DRAW_BUFFER0, &buffer); |
| 83 | if (buffer == GL_NONE) { |
| 84 | const GLenum drawBuffer = GL_BACK; |
| 85 | glDrawBuffers(1, &drawBuffer); |
| 86 | } |
| 87 | |
| 88 | glGetIntegerv(GL_READ_BUFFER, &buffer); |
| 89 | if (buffer == GL_NONE) { |
| 90 | glReadBuffer(GL_BACK); |
| 91 | } |
| 92 | |
| 93 | glBindFramebuffer(GL_READ_FRAMEBUFFER, curReadFB); |
| 94 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, curDrawFB); |
| 95 | |
| 96 | GL_CHECKPOINT(LOW); |
| 97 | } |
| 98 | |
| 99 | return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded; |
| 100 | } |
| 101 | |
| 102 | Frame SkiaOpenGLPipeline::getFrame() { |
| 103 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 104 | "drawRenderNode called on a context with no surface!"); |
| 105 | return mEglManager.beginFrame(mEglSurface); |
| 106 | } |
| 107 | |
| 108 | IRenderPipeline::DrawResult SkiaOpenGLPipeline::draw( |
| 109 | const Frame& frame, const SkRect& screenDirty, const SkRect& dirty, |
| 110 | const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue, |
| 111 | const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo, |
| 112 | const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler) { |
| 113 | if (!isCapturingSkp()) { |
| 114 | mEglManager.damageFrame(frame, dirty); |
| 115 | } |
| 116 | |
| 117 | SkColorType colorType = getSurfaceColorType(); |
| 118 | // setup surface for fbo0 |
| 119 | GrGLFramebufferInfo fboInfo; |
| 120 | fboInfo.fFBOID = 0; |
| 121 | if (colorType == kRGBA_F16_SkColorType) { |
| 122 | fboInfo.fFormat = GL_RGBA16F; |
| 123 | } else if (colorType == kN32_SkColorType) { |
| 124 | // Note: The default preference of pixel format is RGBA_8888, when other |
| 125 | // pixel format is available, we should branch out and do more check. |
| 126 | fboInfo.fFormat = GL_RGBA8; |
| 127 | } else if (colorType == kRGBA_1010102_SkColorType) { |
| 128 | fboInfo.fFormat = GL_RGB10_A2; |
| 129 | } else if (colorType == kAlpha_8_SkColorType) { |
| 130 | fboInfo.fFormat = GL_R8; |
| 131 | } else { |
| 132 | LOG_ALWAYS_FATAL("Unsupported color type."); |
| 133 | } |
| 134 | |
| 135 | GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo); |
| 136 | |
| 137 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 138 | |
| 139 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
| 140 | sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget( |
| 141 | mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType, |
| 142 | mSurfaceColorSpace, &props)); |
| 143 | |
| 144 | LightingInfo::updateLighting(lightGeometry, lightInfo); |
| 145 | renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 146 | SkMatrix::I()); |
| 147 | |
| 148 | // Draw visual debugging features |
| 149 | if (CC_UNLIKELY(Properties::showDirtyRegions || |
| 150 | ProfileType::None != Properties::getProfileType())) { |
| 151 | SkCanvas* profileCanvas = surface->getCanvas(); |
| 152 | SkiaProfileRenderer profileRenderer(profileCanvas); |
| 153 | profiler->draw(profileRenderer); |
| 154 | } |
| 155 | |
| 156 | { |
| 157 | ATRACE_NAME("flush commands"); |
| 158 | surface->flushAndSubmit(); |
| 159 | } |
| 160 | layerUpdateQueue->clear(); |
| 161 | |
| 162 | // Log memory statistics |
| 163 | if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) { |
| 164 | dumpResourceCacheUsage(); |
| 165 | } |
| 166 | |
| 167 | return {true, IRenderPipeline::DrawResult::kUnknownTime}; |
| 168 | } |
| 169 | |
| 170 | bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty, |
| 171 | FrameInfo* currentFrameInfo, bool* requireSwap) { |
| 172 | GL_CHECKPOINT(LOW); |
| 173 | |
| 174 | // Even if we decided to cancel the frame, from the perspective of jank |
| 175 | // metrics the frame was swapped at this point |
| 176 | currentFrameInfo->markSwapBuffers(); |
| 177 | |
| 178 | *requireSwap = drew || mEglManager.damageRequiresSwap(); |
| 179 | |
| 180 | if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | return *requireSwap; |
| 185 | } |
| 186 | |
| 187 | DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() { |
| 188 | mRenderThread.requireGlContext(); |
| 189 | return new DeferredLayerUpdater(mRenderThread.renderState()); |
| 190 | } |
| 191 | |
| 192 | void SkiaOpenGLPipeline::onContextDestroyed() { |
| 193 | if (mEglSurface != EGL_NO_SURFACE) { |
| 194 | mEglManager.destroySurface(mEglSurface); |
| 195 | mEglSurface = EGL_NO_SURFACE; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void SkiaOpenGLPipeline::onStop() { |
| 200 | if (mEglManager.isCurrent(mEglSurface)) { |
| 201 | mEglManager.makeCurrent(EGL_NO_SURFACE); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) { |
| 206 | mNativeWindow = surface; |
| 207 | mSwapBehavior = swapBehavior; |
| 208 | |
| 209 | if (mEglSurface != EGL_NO_SURFACE) { |
| 210 | mEglManager.destroySurface(mEglSurface); |
| 211 | mEglSurface = EGL_NO_SURFACE; |
| 212 | } |
| 213 | |
| 214 | if (surface) { |
| 215 | mRenderThread.requireGlContext(); |
| 216 | auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace); |
| 217 | if (!newSurface) { |
| 218 | return false; |
| 219 | } |
| 220 | mEglSurface = newSurface.unwrap(); |
| 221 | } |
| 222 | |
| 223 | if (mEglSurface != EGL_NO_SURFACE) { |
| 224 | const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer); |
| 225 | const bool isPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer); |
| 226 | ALOGE_IF(preserveBuffer != isPreserved, "Unable to match the desired swap behavior."); |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | bool SkiaOpenGLPipeline::isSurfaceReady() { |
| 234 | return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE); |
| 235 | } |
| 236 | |
| 237 | bool SkiaOpenGLPipeline::isContextReady() { |
| 238 | return CC_LIKELY(mEglManager.hasEglContext()); |
| 239 | } |
| 240 | |
| 241 | void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { |
| 242 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 243 | if (thread.eglManager().hasEglContext()) { |
| 244 | mode = DrawGlInfo::kModeProcess; |
| 245 | } |
| 246 | |
| 247 | (*functor)(mode, nullptr); |
| 248 | |
| 249 | // If there's no context we don't need to reset as there's no gl state to save/restore |
| 250 | if (mode != DrawGlInfo::kModeProcessNoContext) { |
| 251 | thread.getGrContext()->resetContext(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | } /* namespace skiapipeline */ |
| 256 | } /* namespace uirenderer */ |
| 257 | } /* namespace android */ |