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"
208cd3edfa15cc9cdbffa935d19ab894426b08d174Greg Daniel#include "GlLayer.h"
21c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger#include "LayerDrawable.h"
22500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "renderthread/EglManager.h"
23cd55852fcd840f7f4c4d7a0a7253a2995c77afa2Greg Daniel#include "renderthread/Frame.h"
24500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "renderstate/RenderState.h"
25cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett#include "SkiaPipeline.h"
26cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett#include "SkiaProfileRenderer.h"
27500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "utils/TraceUtils.h"
28500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
29500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <cutils/properties.h>
30500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <strings.h>
31500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
32500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievusing namespace android::uirenderer::renderthread;
33500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
34500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace android {
35500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace uirenderer {
36500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace skiapipeline {
37500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
38500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievSkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
39500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        : SkiaPipeline(thread)
40500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        , mEglManager(thread.eglManager()) {
41500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
42500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
43500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievMakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
44500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // TODO: Figure out why this workaround is needed, see b/13913604
45500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // In the meantime this matches the behavior of GLRenderer, so it is not a regression
46500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    EGLint error = 0;
47500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (!mEglManager.makeCurrent(mEglSurface, &error)) {
48500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return MakeCurrentResult::AlreadyCurrent;
49500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
50500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
51500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
52500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
53500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievFrame SkiaOpenGLPipeline::getFrame() {
54500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
55500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev                "drawRenderNode called on a context with no surface!");
56500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return mEglManager.beginFrame(mEglSurface);
57500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
58500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
59500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty,
60500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const SkRect& dirty,
61500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const FrameBuilder::LightGeometry& lightGeometry,
62500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        LayerUpdateQueue* layerUpdateQueue,
63500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const Rect& contentDrawBounds, bool opaque,
64500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const BakedOpRenderer::LightInfo& lightInfo,
65500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const std::vector<sp<RenderNode>>& renderNodes,
66500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        FrameInfoVisualizer* profiler) {
67500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
68500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    mEglManager.damageFrame(frame, dirty);
69500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
70500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // setup surface for fbo0
71500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    GrBackendRenderTargetDesc renderTargetDesc;
72500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fWidth = frame.width();
73500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fHeight = frame.height();
74500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fConfig = kRGBA_8888_GrPixelConfig;
75500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
76500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fSampleCnt = 0;
77500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fStencilBits = STENCIL_BUFFER_SIZE;
78500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderTargetDesc.fRenderTargetHandle = 0;
79500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
80500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
81500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
82500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    SkASSERT(mRenderThread.getGrContext() != nullptr);
83500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
84500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            mRenderThread.getGrContext(), renderTargetDesc, &props));
85500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
86500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    SkiaPipeline::updateLighting(lightGeometry, lightInfo);
87500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface);
88500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    layerUpdateQueue->clear();
89cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett
90cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett    // Draw visual debugging features
91cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett    if (CC_UNLIKELY(Properties::showDirtyRegions
924c9bbf4eef4c8eef44c16d908cb84c343d4a1a81Matt Sarett            || ProfileType::None != Properties::getProfileType())) {
93cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett        SkCanvas* profileCanvas = surface->getCanvas();
94cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett        SkiaProfileRenderer profileRenderer(profileCanvas);
95cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett        profiler->draw(profileRenderer);
96cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett        profileCanvas->flush();
97cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett    }
98cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett
994bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett    // Log memory statistics
1004bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett    if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
1014bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett        dumpResourceCacheUsage();
1024bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett    }
1034bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett
104500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return true;
105500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
106500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
107500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew,
108500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) {
109500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
110500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    GL_CHECKPOINT(LOW);
111500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
112500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // Even if we decided to cancel the frame, from the perspective of jank
113500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // metrics the frame was swapped at this point
114500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    currentFrameInfo->markSwapBuffers();
115500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
116500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    *requireSwap = drew || mEglManager.damageRequiresSwap();
117500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
118500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
119500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return false;
120500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
121500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
122500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return *requireSwap;
123500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
124500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
125c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenbergerbool SkiaOpenGLPipeline::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) {
126c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    if (!mRenderThread.getGrContext()) {
127c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger        return false;
128c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    }
129c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger
1302f1aaf7e24590e3b1dde62e0cb6d8d2978c9d5daChris Craik    // acquire most recent buffer for drawing
1312f1aaf7e24590e3b1dde62e0cb6d8d2978c9d5daChris Craik    deferredLayer->updateTexImage();
132c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    deferredLayer->apply();
133c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger
134c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    SkCanvas canvas(*bitmap);
135c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    Layer* layer = deferredLayer->backingLayer();
136c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    return LayerDrawable::DrawLayer(mRenderThread.getGrContext(), &canvas, layer);
137500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
138500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
1393e9999bd866fac71c72e6b484a9836c87c328a08sergeyvstatic Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
1403e9999bd866fac71c72e6b484a9836c87c328a08sergeyv        SkColorFilter* colorFilter, int alpha, SkBlendMode mode, bool blend) {
1413e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    GlLayer* layer = new GlLayer(renderState, layerWidth, layerHeight, colorFilter, alpha,
1423e9999bd866fac71c72e6b484a9836c87c328a08sergeyv            mode, blend);
1433e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    layer->generateTexture();
1443e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    return layer;
1453e9999bd866fac71c72e6b484a9836c87c328a08sergeyv}
1463e9999bd866fac71c72e6b484a9836c87c328a08sergeyv
147500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievDeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
148500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    mEglManager.initialize();
1493e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::OpenGL);
150500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
151500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
152500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievvoid SkiaOpenGLPipeline::onStop() {
153500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (mEglManager.isCurrent(mEglSurface)) {
154500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mEglManager.makeCurrent(EGL_NO_SURFACE);
155500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
156500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
157500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
158500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior) {
159500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
160500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (mEglSurface != EGL_NO_SURFACE) {
161500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mEglManager.destroySurface(mEglSurface);
162500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mEglSurface = EGL_NO_SURFACE;
163500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
164500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
165500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (surface) {
166500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mEglSurface = mEglManager.createSurface(surface);
167500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
168500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
169500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (mEglSurface != EGL_NO_SURFACE) {
170500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
171500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
172500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return true;
173500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
174500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
175500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return false;
176500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
177500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
178500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::isSurfaceReady() {
179500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
180500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
181500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
182500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaOpenGLPipeline::isContextReady() {
183500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    return CC_LIKELY(mEglManager.hasEglContext());
184500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
185500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
186500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievvoid SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
187500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
188500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (thread.eglManager().hasEglContext()) {
189500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mode = DrawGlInfo::kModeProcess;
190500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
191500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
192500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    (*functor)(mode, nullptr);
193500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
194500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    // If there's no context we don't need to reset as there's no gl state to save/restore
195500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    if (mode != DrawGlInfo::kModeProcessNoContext) {
196500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        thread.getGrContext()->resetContext();
197500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
198500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev}
199500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
200500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace skiapipeline */
201500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace uirenderer */
202500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace android */
203