SkiaPipeline.h revision f58cc92066903b900396f640159ea3ea992fc67d
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#pragma once
18500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
19500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "renderthread/CanvasContext.h"
20500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "FrameBuilder.h"
21500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "renderthread/IRenderPipeline.h"
22500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <SkSurface.h>
23500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
24500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace android {
25500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace uirenderer {
26500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace skiapipeline {
27500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
28500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievclass SkiaPipeline : public renderthread::IRenderPipeline {
29500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievpublic:
30500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    SkiaPipeline(renderthread::RenderThread& thread);
31500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    virtual ~SkiaPipeline() {}
32500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
33500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    TaskManager* getTaskManager() override;
34500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
35500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    void onDestroyHardwareResources() override;
36500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
37b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    bool pinImages(std::vector<SkImage*>& mutableImages) override;
38b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; }
39b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    void unpinImages() override;
40b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger
41500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    void renderLayers(const FrameBuilder::LightGeometry& lightGeometry,
42500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            LayerUpdateQueue* layerUpdateQueue, bool opaque,
43500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            const BakedOpRenderer::LightInfo& lightInfo) override;
44500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
45500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    bool createOrUpdateLayer(RenderNode* node,
46500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            const DamageAccumulator& damageAccumulator) override;
47500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
48500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
49500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            const std::vector< sp<RenderNode> >& nodes, bool opaque, const Rect &contentDrawBounds,
50500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            sk_sp<SkSurface> surface);
51500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
52500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static void destroyLayer(RenderNode* node);
53500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
54500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static void prepareToDraw(const renderthread::RenderThread& thread, Bitmap* bitmap);
55500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
56500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque);
57500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
58500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static bool skpCaptureEnabled() { return false; }
59500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
60500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static float getLightRadius() {
61500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        if (CC_UNLIKELY(Properties::overrideLightRadius > 0)) {
62500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            return Properties::overrideLightRadius;
63500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        }
64500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return mLightRadius;
65500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
66500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
67500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static uint8_t getAmbientShadowAlpha() {
68500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
69500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            return Properties::overrideAmbientShadowStrength;
70500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        }
71500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return mAmbientShadowAlpha;
72500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
73500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
74500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static uint8_t getSpotShadowAlpha() {
75500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
76500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            return Properties::overrideSpotShadowStrength;
77500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        }
78500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return mSpotShadowAlpha;
79500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
80500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
81500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static Vector3 getLightCenter() {
82500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        if (CC_UNLIKELY(Properties::overrideLightPosY > 0 || Properties::overrideLightPosZ > 0)) {
83500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            Vector3 adjustedLightCenter = mLightCenter;
84500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            if (CC_UNLIKELY(Properties::overrideLightPosY > 0)) {
85500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev                // negated since this shifts up
86500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev                adjustedLightCenter.y = - Properties::overrideLightPosY;
87500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            }
88500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            if (CC_UNLIKELY(Properties::overrideLightPosZ > 0)) {
89500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev                adjustedLightCenter.z = Properties::overrideLightPosZ;
90500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            }
91500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            return adjustedLightCenter;
92500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        }
93500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        return mLightCenter;
94500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
95500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
96500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static void updateLighting(const FrameBuilder::LightGeometry& lightGeometry,
97500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev            const BakedOpRenderer::LightInfo& lightInfo) {
98500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mLightRadius = lightGeometry.radius;
99500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mAmbientShadowAlpha = lightInfo.ambientShadowAlpha;
100500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mSpotShadowAlpha = lightInfo.spotShadowAlpha;
101500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev        mLightCenter = lightGeometry.center;
102500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    }
1034bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett
104500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievprotected:
1054bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett    void dumpResourceCacheUsage() const;
1064bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett
107500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    renderthread::RenderThread& mRenderThread;
108500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
109500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievprivate:
110f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett    void renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
111f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett            const std::vector< sp<RenderNode> >& nodes, bool opaque, const Rect &contentDrawBounds,
112f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett            SkCanvas* canvas);
113f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett
114f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett    /**
115f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett     *  Debugging feature.  Draws a semi-transparent overlay on each pixel, indicating
116f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett     *  how many times it has been drawn.
117f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett     */
118f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett    void renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip,
119f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett            const std::vector< sp<RenderNode> >& nodes, const Rect &contentDrawBounds,
120f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett            sk_sp<SkSurface>);
121f58cc92066903b900396f640159ea3ea992fc67dMatt Sarett
122500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    TaskManager mTaskManager;
123b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    std::vector<sk_sp<SkImage>> mPinnedImages;
124500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static float mLightRadius;
125500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static uint8_t mAmbientShadowAlpha;
126500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static uint8_t mSpotShadowAlpha;
127500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev    static Vector3 mLightCenter;
128500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev};
129500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev
130500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace skiapipeline */
131500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace uirenderer */
132500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace android */
133