Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bea67ce5 authored by Jerome Gaillard's avatar Jerome Gaillard
Browse files

Create a new RenderPipeline for CPU only rendering

This takes out of SkiaPipeline what relies on GPU, and puts it into
SkiaGpuPipeline, making SkiaOpenGLPipeline and SkiaVulkanPipeline
inherit from SkiaGpuPipeline.
From the new restricted SkiaPipeline, this creates SkiaCpuPipeline that
only relies on CPU for rendering.
In addition, this moves references to the GrContext out of
IRenderPipeline and into SkGpuPipeline as the only usage is in classes
inheriting from SkGpuPipeline.

Bug: 322360037
Test: build libhwui on host
Change-Id: Ifadacfc593ff54377d6a8a65b0505e294c8d1b29
parent 3a79b1d7
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -78,13 +78,13 @@ cc_defaults {
    include_dirs: [
        "external/skia/include/private",
        "external/skia/src/core",
        "external/skia/src/utils",
    ],

    target: {
        android: {
            include_dirs: [
                "external/skia/src/image",
                "external/skia/src/utils",
                "external/skia/src/gpu",
                "external/skia/src/shaders",
            ],
@@ -529,7 +529,9 @@ cc_defaults {
        "effects/GainmapRenderer.cpp",
        "pipeline/skia/BackdropFilterDrawable.cpp",
        "pipeline/skia/HolePunch.cpp",
        "pipeline/skia/SkiaCpuPipeline.cpp",
        "pipeline/skia/SkiaDisplayList.cpp",
        "pipeline/skia/SkiaPipeline.cpp",
        "pipeline/skia/SkiaRecordingCanvas.cpp",
        "pipeline/skia/StretchMask.cpp",
        "pipeline/skia/RenderNodeDrawable.cpp",
@@ -568,6 +570,7 @@ cc_defaults {
        "HWUIProperties.sysprop",
        "Interpolator.cpp",
        "JankTracker.cpp",
        "LayerUpdateQueue.cpp",
        "LightingInfo.cpp",
        "Matrix.cpp",
        "Mesh.cpp",
@@ -604,9 +607,9 @@ cc_defaults {
                "pipeline/skia/GLFunctorDrawable.cpp",
                "pipeline/skia/LayerDrawable.cpp",
                "pipeline/skia/ShaderCache.cpp",
                "pipeline/skia/SkiaGpuPipeline.cpp",
                "pipeline/skia/SkiaMemoryTracer.cpp",
                "pipeline/skia/SkiaOpenGLPipeline.cpp",
                "pipeline/skia/SkiaPipeline.cpp",
                "pipeline/skia/SkiaProfileRenderer.cpp",
                "pipeline/skia/SkiaVulkanPipeline.cpp",
                "pipeline/skia/VkFunctorDrawable.cpp",
@@ -630,7 +633,6 @@ cc_defaults {
                "DeferredLayerUpdater.cpp",
                "HardwareBitmapUploader.cpp",
                "Layer.cpp",
                "LayerUpdateQueue.cpp",
                "ProfileDataContainer.cpp",
                "Readback.cpp",
                "TreeInfo.cpp",
+1 −1
Original line number Diff line number Diff line
@@ -242,7 +242,7 @@ enum class ProfileType { None, Console, Bars };

enum class OverdrawColorSet { Default = 0, Deuteranomaly };

enum class RenderPipelineType { SkiaGL, SkiaVulkan, NotInitialized = 128 };
enum class RenderPipelineType { SkiaGL, SkiaVulkan, SkiaCpu, NotInitialized = 128 };

enum class StretchEffectBehavior {
    ShaderHWUI,   // Stretch shader in HWUI only, matrix scale in SF
+132 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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 "pipeline/skia/SkiaCpuPipeline.h"

#include <system/window.h>

#include "DeviceInfo.h"
#include "LightingInfo.h"
#include "renderthread/Frame.h"
#include "utils/Color.h"

using namespace android::uirenderer::renderthread;

namespace android {
namespace uirenderer {
namespace skiapipeline {

void SkiaCpuPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) {
    // Render all layers that need to be updated, in order.
    for (size_t i = 0; i < layers.entries().size(); i++) {
        RenderNode* layerNode = layers.entries()[i].renderNode.get();
        // only schedule repaint if node still on layer - possible it may have been
        // removed during a dropped frame, but layers may still remain scheduled so
        // as not to lose info on what portion is damaged
        if (CC_UNLIKELY(layerNode->getLayerSurface() == nullptr)) {
            continue;
        }
        bool rendered = renderLayerImpl(layerNode, layers.entries()[i].damage);
        if (!rendered) {
            return;
        }
    }
}

// If the given node didn't have a layer surface, or had one of the wrong size, this method
// creates a new one and returns true. Otherwise does nothing and returns false.
bool SkiaCpuPipeline::createOrUpdateLayer(RenderNode* node,
                                          const DamageAccumulator& damageAccumulator,
                                          ErrorHandler* errorHandler) {
    // compute the size of the surface (i.e. texture) to be allocated for this layer
    const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE;
    const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE;

    SkSurface* layer = node->getLayerSurface();
    if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) {
        SkImageInfo info;
        info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(),
                                 kPremul_SkAlphaType, getSurfaceColorSpace());
        SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
        node->setLayerSurface(SkSurfaces::Raster(info, &props));
        if (node->getLayerSurface()) {
            // update the transform in window of the layer to reset its origin wrt light source
            // position
            Matrix4 windowTransform;
            damageAccumulator.computeCurrentTransform(&windowTransform);
            node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform);
        } else {
            String8 cachesOutput;
            mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput,
                                                         &mRenderThread.renderState());
            ALOGE("%s", cachesOutput.c_str());
            if (errorHandler) {
                std::ostringstream err;
                err << "Unable to create layer for " << node->getName();
                const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
                err << ", size " << info.width() << "x" << info.height() << " max size "
                    << maxTextureSize << " color type " << (int)info.colorType() << " has context "
                    << (int)(mRenderThread.getGrContext() != nullptr);
                errorHandler->onError(err.str());
            }
        }
        return true;
    }
    return false;
}

MakeCurrentResult SkiaCpuPipeline::makeCurrent() {
    return MakeCurrentResult::AlreadyCurrent;
}

Frame SkiaCpuPipeline::getFrame() {
    return Frame(mSurface->width(), mSurface->height(), 0);
}

IRenderPipeline::DrawResult SkiaCpuPipeline::draw(
        const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
        const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
        const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
        const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler,
        const HardwareBufferRenderParams& bufferParams, std::mutex& profilerLock) {
    LightingInfo::updateLighting(lightGeometry, lightInfo);
    renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, mSurface,
                SkMatrix::I());
    return {true, IRenderPipeline::DrawResult::kUnknownTime, android::base::unique_fd{}};
}

bool SkiaCpuPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
    if (surface) {
        ANativeWindowBuffer* buffer;
        surface->dequeueBuffer(surface, &buffer, nullptr);
        int width, height;
        surface->query(surface, NATIVE_WINDOW_WIDTH, &width);
        surface->query(surface, NATIVE_WINDOW_HEIGHT, &height);
        SkImageInfo imageInfo =
                SkImageInfo::Make(width, height, mSurfaceColorType,
                                  SkAlphaType::kPremul_SkAlphaType, mSurfaceColorSpace);
        size_t widthBytes = width * imageInfo.bytesPerPixel();
        void* pixels = buffer->reserved[0];
        mSurface = SkSurfaces::WrapPixels(imageInfo, pixels, widthBytes);
    } else {
        mSurface = sk_sp<SkSurface>();
    }
    return true;
}

} /* namespace skiapipeline */
} /* namespace uirenderer */
} /* namespace android */
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.
 */

#pragma once

#include "pipeline/skia/SkiaPipeline.h"

namespace android {

namespace uirenderer {
namespace skiapipeline {

class SkiaCpuPipeline : public SkiaPipeline {
public:
    SkiaCpuPipeline(renderthread::RenderThread& thread) : SkiaPipeline(thread) {}
    ~SkiaCpuPipeline() {}

    bool pinImages(std::vector<SkImage*>& mutableImages) override { return false; }
    bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; }
    void unpinImages() override {}

    // If the given node didn't have a layer surface, or had one of the wrong size, this method
    // creates a new one and returns true. Otherwise does nothing and returns false.
    bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
                             ErrorHandler* errorHandler) override;
    void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) override;
    void setHardwareBuffer(AHardwareBuffer* hardwareBuffer) override {}
    bool hasHardwareBuffer() override { return false; }

    renderthread::MakeCurrentResult makeCurrent() override;
    renderthread::Frame getFrame() override;
    renderthread::IRenderPipeline::DrawResult draw(
            const renderthread::Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
            const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
            const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
            const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler,
            const renderthread::HardwareBufferRenderParams& bufferParams,
            std::mutex& profilerLock) override;
    bool swapBuffers(const renderthread::Frame& frame, IRenderPipeline::DrawResult& drawResult,
                     const SkRect& screenDirty, FrameInfo* currentFrameInfo,
                     bool* requireSwap) override {
        return false;
    }
    DeferredLayerUpdater* createTextureLayer() override { return nullptr; }
    bool setSurface(ANativeWindow* surface, renderthread::SwapBehavior swapBehavior) override;
    [[nodiscard]] android::base::unique_fd flush() override {
        return android::base::unique_fd(-1);
    };
    void onStop() override {}
    bool isSurfaceReady() override { return mSurface.get() != nullptr; }
    bool isContextReady() override { return true; }

    const SkM44& getPixelSnapMatrix() const override {
        static const SkM44 sSnapMatrix = SkM44();
        return sSnapMatrix;
    }

private:
    sk_sp<SkSurface> mSurface;
};

} /* namespace skiapipeline */
} /* namespace uirenderer */
} /* namespace android */
+194 −0
Original line number Diff line number Diff line
/*
 * 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 "pipeline/skia/SkiaGpuPipeline.h"

#include <SkImageAndroid.h>
#include <gui/TraceUtils.h>
#include <include/android/SkSurfaceAndroid.h>
#include <include/gpu/ganesh/SkSurfaceGanesh.h>

using namespace android::uirenderer::renderthread;

namespace android {
namespace uirenderer {
namespace skiapipeline {

SkiaGpuPipeline::SkiaGpuPipeline(RenderThread& thread) : SkiaPipeline(thread) {}

SkiaGpuPipeline::~SkiaGpuPipeline() {
    unpinImages();
}

void SkiaGpuPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) {
    sk_sp<GrDirectContext> cachedContext;

    // Render all layers that need to be updated, in order.
    for (size_t i = 0; i < layers.entries().size(); i++) {
        RenderNode* layerNode = layers.entries()[i].renderNode.get();
        // only schedule repaint if node still on layer - possible it may have been
        // removed during a dropped frame, but layers may still remain scheduled so
        // as not to lose info on what portion is damaged
        if (CC_UNLIKELY(layerNode->getLayerSurface() == nullptr)) {
            continue;
        }
        bool rendered = renderLayerImpl(layerNode, layers.entries()[i].damage);
        if (!rendered) {
            return;
        }
        // cache the current context so that we can defer flushing it until
        // either all the layers have been rendered or the context changes
        GrDirectContext* currentContext =
                GrAsDirectContext(layerNode->getLayerSurface()->getCanvas()->recordingContext());
        if (cachedContext.get() != currentContext) {
            if (cachedContext.get()) {
                ATRACE_NAME("flush layers (context changed)");
                cachedContext->flushAndSubmit();
            }
            cachedContext.reset(SkSafeRef(currentContext));
        }
    }
    if (cachedContext.get()) {
        ATRACE_NAME("flush layers");
        cachedContext->flushAndSubmit();
    }
}

// If the given node didn't have a layer surface, or had one of the wrong size, this method
// creates a new one and returns true. Otherwise does nothing and returns false.
bool SkiaGpuPipeline::createOrUpdateLayer(RenderNode* node,
                                          const DamageAccumulator& damageAccumulator,
                                          ErrorHandler* errorHandler) {
    // compute the size of the surface (i.e. texture) to be allocated for this layer
    const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE;
    const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE;

    SkSurface* layer = node->getLayerSurface();
    if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) {
        SkImageInfo info;
        info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(),
                                 kPremul_SkAlphaType, getSurfaceColorSpace());
        SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
        SkASSERT(mRenderThread.getGrContext() != nullptr);
        node->setLayerSurface(SkSurfaces::RenderTarget(mRenderThread.getGrContext(),
                                                       skgpu::Budgeted::kYes, info, 0,
                                                       this->getSurfaceOrigin(), &props));
        if (node->getLayerSurface()) {
            // update the transform in window of the layer to reset its origin wrt light source
            // position
            Matrix4 windowTransform;
            damageAccumulator.computeCurrentTransform(&windowTransform);
            node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform);
        } else {
            String8 cachesOutput;
            mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput,
                                                         &mRenderThread.renderState());
            ALOGE("%s", cachesOutput.c_str());
            if (errorHandler) {
                std::ostringstream err;
                err << "Unable to create layer for " << node->getName();
                const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
                err << ", size " << info.width() << "x" << info.height() << " max size "
                    << maxTextureSize << " color type " << (int)info.colorType() << " has context "
                    << (int)(mRenderThread.getGrContext() != nullptr);
                errorHandler->onError(err.str());
            }
        }
        return true;
    }
    return false;
}

bool SkiaGpuPipeline::pinImages(std::vector<SkImage*>& mutableImages) {
    if (!mRenderThread.getGrContext()) {
        ALOGD("Trying to pin an image with an invalid GrContext");
        return false;
    }
    for (SkImage* image : mutableImages) {
        if (skgpu::ganesh::PinAsTexture(mRenderThread.getGrContext(), image)) {
            mPinnedImages.emplace_back(sk_ref_sp(image));
        } else {
            return false;
        }
    }
    return true;
}

void SkiaGpuPipeline::unpinImages() {
    for (auto& image : mPinnedImages) {
        skgpu::ganesh::UnpinTexture(mRenderThread.getGrContext(), image.get());
    }
    mPinnedImages.clear();
}

void SkiaGpuPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
    GrDirectContext* context = thread.getGrContext();
    if (context && !bitmap->isHardware()) {
        ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
        auto image = bitmap->makeImage();
        if (image.get()) {
            skgpu::ganesh::PinAsTexture(context, image.get());
            skgpu::ganesh::UnpinTexture(context, image.get());
            // A submit is necessary as there may not be a frame coming soon, so without a call
            // to submit these texture uploads can just sit in the queue building up until
            // we run out of RAM
            context->flushAndSubmit();
        }
    }
}

sk_sp<SkSurface> SkiaGpuPipeline::getBufferSkSurface(
        const renderthread::HardwareBufferRenderParams& bufferParams) {
    auto bufferColorSpace = bufferParams.getColorSpace();
    if (mBufferSurface == nullptr || mBufferColorSpace == nullptr ||
        !SkColorSpace::Equals(mBufferColorSpace.get(), bufferColorSpace.get())) {
        mBufferSurface = SkSurfaces::WrapAndroidHardwareBuffer(
                mRenderThread.getGrContext(), mHardwareBuffer, kTopLeft_GrSurfaceOrigin,
                bufferColorSpace, nullptr, true);
        mBufferColorSpace = bufferColorSpace;
    }
    return mBufferSurface;
}

void SkiaGpuPipeline::dumpResourceCacheUsage() const {
    int resources;
    size_t bytes;
    mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes);
    size_t maxBytes = mRenderThread.getGrContext()->getResourceCacheLimit();

    SkString log("Resource Cache Usage:\n");
    log.appendf("%8d items\n", resources);
    log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes,
                bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f)));

    ALOGD("%s", log.c_str());
}

void SkiaGpuPipeline::setHardwareBuffer(AHardwareBuffer* buffer) {
    if (mHardwareBuffer) {
        AHardwareBuffer_release(mHardwareBuffer);
        mHardwareBuffer = nullptr;
    }

    if (buffer) {
        AHardwareBuffer_acquire(buffer);
        mHardwareBuffer = buffer;
    }
}

} /* namespace skiapipeline */
} /* namespace uirenderer */
} /* namespace android */
Loading