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

Commit 77153538 authored by Angel Aguayo's avatar Angel Aguayo Committed by Android (Google) Code Review
Browse files

Merge "Optimize drawMesh APIs by utilizing a cached GPU based mesh"

parents 34969b42 15fdab56
Loading
Loading
Loading
Loading
+35 −3
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@
#include "SkVertices.h"
#include "VectorDrawable.h"
#include "include/gpu/GpuTypes.h" // from Skia
#include "include/gpu/GrDirectContext.h"
#include "pipeline/skia/AnimatedDrawables.h"
#include "pipeline/skia/FunctorDrawable.h"

@@ -466,12 +467,43 @@ struct DrawVertices final : Op {
struct DrawMesh final : Op {
    static const auto kType = Type::DrawMesh;
    DrawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint)
            : mesh(mesh), blender(std::move(blender)), paint(paint) {}
            : cpuMesh(mesh), blender(std::move(blender)), paint(paint) {
        isGpuBased = false;
    }

    SkMesh mesh;
    SkMesh cpuMesh;
    mutable SkMesh gpuMesh;
    sk_sp<SkBlender> blender;
    SkPaint paint;
    void draw(SkCanvas* c, const SkMatrix&) const { c->drawMesh(mesh, blender, paint); }
    mutable bool isGpuBased;
    mutable GrDirectContext::DirectContextID contextId;
    void draw(SkCanvas* c, const SkMatrix&) const {
        GrDirectContext* directContext = c->recordingContext()->asDirectContext();
        GrDirectContext::DirectContextID id = directContext->directContextID();
        if (!isGpuBased || contextId != id) {
            sk_sp<SkMesh::VertexBuffer> vb =
                    SkMesh::CopyVertexBuffer(directContext, cpuMesh.refVertexBuffer());
            if (!cpuMesh.indexBuffer()) {
                gpuMesh = SkMesh::Make(cpuMesh.refSpec(), cpuMesh.mode(), vb, cpuMesh.vertexCount(),
                                       cpuMesh.vertexOffset(), cpuMesh.refUniforms(),
                                       cpuMesh.bounds())
                                  .mesh;
            } else {
                sk_sp<SkMesh::IndexBuffer> ib =
                        SkMesh::CopyIndexBuffer(directContext, cpuMesh.refIndexBuffer());
                gpuMesh = SkMesh::MakeIndexed(cpuMesh.refSpec(), cpuMesh.mode(), vb,
                                              cpuMesh.vertexCount(), cpuMesh.vertexOffset(), ib,
                                              cpuMesh.indexCount(), cpuMesh.indexOffset(),
                                              cpuMesh.refUniforms(), cpuMesh.bounds())
                                  .mesh;
            }

            isGpuBased = true;
            contextId = id;
        }

        c->drawMesh(gpuMesh, blender, paint);
    }
};
struct DrawAtlas final : Op {
    static const auto kType = Type::DrawAtlas;