SkiaOpenGLPipeline.cpp revision cf2c05c652190ddc66f873192c17d193478138a1
1500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev/*
2500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * Copyright (C) 2016 The Android Open Source Project
3500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev *
4500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * Licensed under the Apache License, Version 2.0 (the "License");
5500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * you may not use this file except in compliance with the License.
6500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * You may obtain a copy of the License at
7500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev *
8500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev *      http://www.apache.org/licenses/LICENSE-2.0
9500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev *
10500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * Unless required by applicable law or agreed to in writing, software
11500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * distributed under the License is distributed on an "AS IS" BASIS,
12500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * See the License for the specific language governing permissions and
14500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * limitations under the License.
15500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev */
16500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
17500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "SkiaOpenGLPipeline.h"
18500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
19500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "DeferredLayerUpdater.h"
20500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "renderthread/EglManager.h"
21500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "renderstate/RenderState.h"
22500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "Readback.h"
23cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett#include "SkiaPipeline.h"
24cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett#include "SkiaProfileRenderer.h"
25500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "utils/TraceUtils.h"
26500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
27500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <android/native_window.h>
28500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <cutils/properties.h>
29500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <strings.h>
30500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
31500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievusing namespace android::uirenderer::renderthread;
32500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
33500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace android {
34500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace uirenderer {
35500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace skiapipeline {
36500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
37500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievSkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
38500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        : SkiaPipeline(thread)
39500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        , mEglManager(thread.eglManager()) {
40500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
41500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
42500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievMakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
43500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // TODO: Figure out why this workaround is needed, see b/13913604
44500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // In the meantime this matches the behavior of GLRenderer, so it is not a regression
45500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    EGLint error = 0;
46500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (!mEglManager.makeCurrent(mEglSurface, &error)) {
47500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return MakeCurrentResult::AlreadyCurrent;
48500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
49500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
50500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
51500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
52500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievFrame SkiaOpenGLPipeline::getFrame() {
53500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
54500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev                "drawRenderNode called on a context with no surface!");
55500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return mEglManager.beginFrame(mEglSurface);
56500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
57500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
58500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty,
59500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const SkRect& dirty,
60500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const FrameBuilder::LightGeometry& lightGeometry,
61500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        LayerUpdateQueue* layerUpdateQueue,
62500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const Rect& contentDrawBounds, bool opaque,
63500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const BakedOpRenderer::LightInfo& lightInfo,
64500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const std::vector<sp<RenderNode>>& renderNodes,
65500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        FrameInfoVisualizer* profiler) {
66500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
67500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    mEglManager.damageFrame(frame, dirty);
68500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
69500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // setup surface for fbo0
70500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    GrBackendRenderTargetDesc renderTargetDesc;
71500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fWidth = frame.width();
72500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fHeight = frame.height();
73500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fConfig = kRGBA_8888_GrPixelConfig;
74500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
75500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fSampleCnt = 0;
76500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fStencilBits = STENCIL_BUFFER_SIZE;
77500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fRenderTargetHandle = 0;
78500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
79500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
80500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
81500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    SkASSERT(mRenderThread.getGrContext() != nullptr);
82500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
83500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            mRenderThread.getGrContext(), renderTargetDesc, &props));
84500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
85500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    SkiaPipeline::updateLighting(lightGeometry, lightInfo);
86500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface);
87500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    layerUpdateQueue->clear();
88cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett
89cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett    // Draw visual debugging features
90cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett    if (CC_UNLIKELY(Properties::showDirtyRegions
91cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett            || ProfileType::None == Properties::getProfileType())) {
92cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett        SkCanvas* profileCanvas = surface->getCanvas();
93cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett        SkiaProfileRenderer profileRenderer(profileCanvas);
94cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett        profiler->draw(profileRenderer);
95cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett        profileCanvas->flush();
96cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett    }
97cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett
98500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return true;
99500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
100500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
101500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew,
102500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) {
103500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
104500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    GL_CHECKPOINT(LOW);
105500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
106500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // Even if we decided to cancel the frame, from the perspective of jank
107500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // metrics the frame was swapped at this point
108500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    currentFrameInfo->markSwapBuffers();
109500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
110500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    *requireSwap = drew || mEglManager.damageRequiresSwap();
111500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
112500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
113500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return false;
114500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
115500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
116500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return *requireSwap;
117500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
118500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
119500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
120500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    layer->apply();
121500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return Readback::copyTextureLayerInto(mRenderThread, *(layer->backingLayer()), bitmap)
122500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            == CopyResult::Success;
123500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
124500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
125500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievDeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
126500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    mEglManager.initialize();
127500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    Layer* layer = new Layer(mRenderThread.renderState(), 0, 0);
128500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    layer->generateTexture();
129500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return new DeferredLayerUpdater(layer);
130500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
131500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
132500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievvoid SkiaOpenGLPipeline::onStop() {
133500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (mEglManager.isCurrent(mEglSurface)) {
134500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mEglManager.makeCurrent(EGL_NO_SURFACE);
135500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
136500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
137500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
138500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior) {
139500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
140500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (mEglSurface != EGL_NO_SURFACE) {
141500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mEglManager.destroySurface(mEglSurface);
142500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mEglSurface = EGL_NO_SURFACE;
143500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
144500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
145500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (surface) {
146500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mEglSurface = mEglManager.createSurface(surface);
147500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
148500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
149500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (mEglSurface != EGL_NO_SURFACE) {
150500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
151500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
152500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return true;
153500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
154500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
155500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return false;
156500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
157500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
158500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::isSurfaceReady() {
159500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
160500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
161500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
162500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::isContextReady() {
163500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return CC_LIKELY(mEglManager.hasEglContext());
164500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
165500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
166500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievvoid SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
167500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
168500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (thread.eglManager().hasEglContext()) {
169500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mode = DrawGlInfo::kModeProcess;
170500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
171500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
172500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    (*functor)(mode, nullptr);
173500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
174500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // If there's no context we don't need to reset as there's no gl state to save/restore
175500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (mode != DrawGlInfo::kModeProcessNoContext) {
176500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        thread.getGrContext()->resetContext();
177500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
178500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
179500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
180500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace skiapipeline */
181500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace uirenderer */
182500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace android */
183