2016-10-26 10:30:09 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkiaOpenGLPipeline.h"
|
|
|
|
|
2021-05-21 15:40:53 -07:00
|
|
|
#include <gui/TraceUtils.h>
|
2016-10-26 10:30:09 -04:00
|
|
|
#include "DeferredLayerUpdater.h"
|
2016-11-07 16:05:41 -05:00
|
|
|
#include "LayerDrawable.h"
|
2019-06-19 11:41:34 +01:00
|
|
|
#include "LightingInfo.h"
|
2016-10-26 11:03:23 -04:00
|
|
|
#include "SkiaPipeline.h"
|
|
|
|
#include "SkiaProfileRenderer.h"
|
2017-11-03 10:12:19 -07:00
|
|
|
#include "hwui/Bitmap.h"
|
2018-09-20 13:37:24 -04:00
|
|
|
#include "private/hwui/DrawGlInfo.h"
|
2017-11-03 10:12:19 -07:00
|
|
|
#include "renderstate/RenderState.h"
|
|
|
|
#include "renderthread/EglManager.h"
|
|
|
|
#include "renderthread/Frame.h"
|
2018-05-03 14:40:56 -07:00
|
|
|
#include "utils/GLUtils.h"
|
2016-10-26 10:30:09 -04:00
|
|
|
|
2018-09-14 15:22:35 -07:00
|
|
|
#include <GLES3/gl3.h>
|
|
|
|
|
2017-07-12 11:30:15 -04:00
|
|
|
#include <GrBackendSurface.h>
|
2018-05-03 16:12:18 -04:00
|
|
|
#include <SkBlendMode.h>
|
2018-05-07 08:14:14 -07:00
|
|
|
#include <SkImageInfo.h>
|
2017-07-12 11:30:15 -04:00
|
|
|
|
2016-10-26 10:30:09 -04:00
|
|
|
#include <cutils/properties.h>
|
|
|
|
#include <strings.h>
|
|
|
|
|
|
|
|
using namespace android::uirenderer::renderthread;
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
namespace skiapipeline {
|
|
|
|
|
|
|
|
SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
|
2018-09-19 13:52:13 -04:00
|
|
|
: SkiaPipeline(thread), mEglManager(thread.eglManager()) {
|
|
|
|
thread.renderState().registerContextCallback(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkiaOpenGLPipeline::~SkiaOpenGLPipeline() {
|
|
|
|
mRenderThread.renderState().removeContextCallback(this);
|
|
|
|
}
|
2016-10-26 10:30:09 -04:00
|
|
|
|
|
|
|
MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
|
|
|
|
// TODO: Figure out why this workaround is needed, see b/13913604
|
|
|
|
// In the meantime this matches the behavior of GLRenderer, so it is not a regression
|
|
|
|
EGLint error = 0;
|
|
|
|
if (!mEglManager.makeCurrent(mEglSurface, &error)) {
|
|
|
|
return MakeCurrentResult::AlreadyCurrent;
|
|
|
|
}
|
|
|
|
return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
Frame SkiaOpenGLPipeline::getFrame() {
|
|
|
|
LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
|
2017-11-03 10:12:19 -07:00
|
|
|
"drawRenderNode called on a context with no surface!");
|
2016-10-26 10:30:09 -04:00
|
|
|
return mEglManager.beginFrame(mEglSurface);
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
|
2018-05-03 14:40:56 -07:00
|
|
|
const LightGeometry& lightGeometry,
|
2017-11-03 10:12:19 -07:00
|
|
|
LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
|
2018-09-10 16:28:08 -07:00
|
|
|
bool opaque, const LightInfo& lightInfo,
|
2017-11-03 10:12:19 -07:00
|
|
|
const std::vector<sp<RenderNode>>& renderNodes,
|
|
|
|
FrameInfoVisualizer* profiler) {
|
2021-06-09 22:43:05 -04:00
|
|
|
if (!isCapturingSkp()) {
|
|
|
|
mEglManager.damageFrame(frame, dirty);
|
|
|
|
}
|
2016-10-26 10:30:09 -04:00
|
|
|
|
2018-09-10 16:28:08 -07:00
|
|
|
SkColorType colorType = getSurfaceColorType();
|
2016-10-26 10:30:09 -04:00
|
|
|
// setup surface for fbo0
|
2017-07-12 11:30:15 -04:00
|
|
|
GrGLFramebufferInfo fboInfo;
|
|
|
|
fboInfo.fFBOID = 0;
|
2018-09-10 16:28:08 -07:00
|
|
|
if (colorType == kRGBA_F16_SkColorType) {
|
2018-03-21 10:50:24 -04:00
|
|
|
fboInfo.fFormat = GL_RGBA16F;
|
2018-09-10 16:28:08 -07:00
|
|
|
} else if (colorType == kN32_SkColorType) {
|
|
|
|
// Note: The default preference of pixel format is RGBA_8888, when other
|
|
|
|
// pixel format is available, we should branch out and do more check.
|
2018-03-21 10:50:24 -04:00
|
|
|
fboInfo.fFormat = GL_RGBA8;
|
2020-07-23 13:47:49 -07:00
|
|
|
} else if (colorType == kRGBA_1010102_SkColorType) {
|
|
|
|
fboInfo.fFormat = GL_RGB10_A2;
|
2018-09-10 16:28:08 -07:00
|
|
|
} else {
|
|
|
|
LOG_ALWAYS_FATAL("Unsupported color type.");
|
2018-03-21 10:50:24 -04:00
|
|
|
}
|
2017-07-12 11:30:15 -04:00
|
|
|
|
2018-03-21 10:50:24 -04:00
|
|
|
GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
|
2016-10-26 10:30:09 -04:00
|
|
|
|
|
|
|
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
|
|
|
|
|
|
|
|
SkASSERT(mRenderThread.getGrContext() != nullptr);
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
|
2019-01-14 13:55:55 -05:00
|
|
|
mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
|
2018-10-19 15:55:33 -04:00
|
|
|
mSurfaceColorSpace, &props));
|
2016-10-26 10:30:09 -04:00
|
|
|
|
2019-06-19 11:41:34 +01:00
|
|
|
LightingInfo::updateLighting(lightGeometry, lightInfo);
|
2019-01-08 16:01:18 -05:00
|
|
|
renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
|
2019-04-05 16:57:46 -07:00
|
|
|
SkMatrix::I());
|
2016-10-26 11:03:23 -04:00
|
|
|
|
|
|
|
// Draw visual debugging features
|
2017-11-03 10:12:19 -07:00
|
|
|
if (CC_UNLIKELY(Properties::showDirtyRegions ||
|
|
|
|
ProfileType::None != Properties::getProfileType())) {
|
2016-10-26 11:03:23 -04:00
|
|
|
SkCanvas* profileCanvas = surface->getCanvas();
|
|
|
|
SkiaProfileRenderer profileRenderer(profileCanvas);
|
|
|
|
profiler->draw(profileRenderer);
|
|
|
|
}
|
|
|
|
|
2021-02-19 18:32:16 -05:00
|
|
|
{
|
|
|
|
ATRACE_NAME("flush commands");
|
|
|
|
surface->flushAndSubmit();
|
|
|
|
}
|
|
|
|
layerUpdateQueue->clear();
|
|
|
|
|
2016-11-07 15:43:41 -05:00
|
|
|
// Log memory statistics
|
|
|
|
if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
|
|
|
|
dumpResourceCacheUsage();
|
|
|
|
}
|
|
|
|
|
2016-10-26 10:30:09 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
|
|
|
|
FrameInfo* currentFrameInfo, bool* requireSwap) {
|
2016-10-26 10:30:09 -04:00
|
|
|
GL_CHECKPOINT(LOW);
|
|
|
|
|
|
|
|
// Even if we decided to cancel the frame, from the perspective of jank
|
|
|
|
// metrics the frame was swapped at this point
|
|
|
|
currentFrameInfo->markSwapBuffers();
|
|
|
|
|
|
|
|
*requireSwap = drew || mEglManager.damageRequiresSwap();
|
|
|
|
|
|
|
|
if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *requireSwap;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
|
2018-04-23 08:15:03 -07:00
|
|
|
mRenderThread.requireGlContext();
|
2018-09-04 22:00:00 +00:00
|
|
|
return new DeferredLayerUpdater(mRenderThread.renderState());
|
2016-10-26 10:30:09 -04:00
|
|
|
}
|
|
|
|
|
2018-09-19 13:52:13 -04:00
|
|
|
void SkiaOpenGLPipeline::onContextDestroyed() {
|
|
|
|
if (mEglSurface != EGL_NO_SURFACE) {
|
|
|
|
mEglManager.destroySurface(mEglSurface);
|
|
|
|
mEglSurface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 10:30:09 -04:00
|
|
|
void SkiaOpenGLPipeline::onStop() {
|
|
|
|
if (mEglManager.isCurrent(mEglSurface)) {
|
|
|
|
mEglManager.makeCurrent(EGL_NO_SURFACE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 16:11:18 -07:00
|
|
|
bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
|
2016-10-26 10:30:09 -04:00
|
|
|
if (mEglSurface != EGL_NO_SURFACE) {
|
|
|
|
mEglManager.destroySurface(mEglSurface);
|
|
|
|
mEglSurface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (surface) {
|
2018-04-23 08:15:03 -07:00
|
|
|
mRenderThread.requireGlContext();
|
2020-02-05 15:41:51 -05:00
|
|
|
auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
|
2018-11-15 15:21:29 -08:00
|
|
|
if (!newSurface) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mEglSurface = newSurface.unwrap();
|
2016-10-26 10:30:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mEglSurface != EGL_NO_SURFACE) {
|
|
|
|
const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
|
|
|
|
mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkiaOpenGLPipeline::isSurfaceReady() {
|
|
|
|
return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkiaOpenGLPipeline::isContextReady() {
|
|
|
|
return CC_LIKELY(mEglManager.hasEglContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
|
|
|
|
DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
|
|
|
|
if (thread.eglManager().hasEglContext()) {
|
|
|
|
mode = DrawGlInfo::kModeProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*functor)(mode, nullptr);
|
|
|
|
|
|
|
|
// If there's no context we don't need to reset as there's no gl state to save/restore
|
|
|
|
if (mode != DrawGlInfo::kModeProcessNoContext) {
|
|
|
|
thread.getGrContext()->resetContext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace skiapipeline */
|
|
|
|
} /* namespace uirenderer */
|
|
|
|
} /* namespace android */
|