1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include "renderthread/CanvasContext.h"
20#include "FrameBuilder.h"
21#include "renderthread/IRenderPipeline.h"
22#include <SkSurface.h>
23
24namespace android {
25namespace uirenderer {
26namespace skiapipeline {
27
28class SkiaPipeline : public renderthread::IRenderPipeline {
29public:
30    SkiaPipeline(renderthread::RenderThread& thread);
31    virtual ~SkiaPipeline() {}
32
33    TaskManager* getTaskManager() override;
34
35    void onDestroyHardwareResources() override;
36
37    bool pinImages(std::vector<SkImage*>& mutableImages) override;
38    bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; }
39    void unpinImages() override;
40
41    void renderLayers(const FrameBuilder::LightGeometry& lightGeometry,
42            LayerUpdateQueue* layerUpdateQueue, bool opaque, bool wideColorGamut,
43            const BakedOpRenderer::LightInfo& lightInfo) override;
44
45    bool createOrUpdateLayer(RenderNode* node,
46            const DamageAccumulator& damageAccumulator, bool wideColorGamut) override;
47
48    void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
49            const std::vector< sp<RenderNode> >& nodes, bool opaque, bool wideColorGamut,
50            const Rect &contentDrawBounds, sk_sp<SkSurface> surface);
51
52    std::vector<VectorDrawableRoot*>* getVectorDrawables() { return &mVectorDrawables; }
53
54    static void destroyLayer(RenderNode* node);
55
56    static void prepareToDraw(const renderthread::RenderThread& thread, Bitmap* bitmap);
57
58    static void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque, bool wideColorGamut);
59
60    static bool skpCaptureEnabled() { return false; }
61
62    static float getLightRadius() {
63        if (CC_UNLIKELY(Properties::overrideLightRadius > 0)) {
64            return Properties::overrideLightRadius;
65        }
66        return mLightRadius;
67    }
68
69    static uint8_t getAmbientShadowAlpha() {
70        if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
71            return Properties::overrideAmbientShadowStrength;
72        }
73        return mAmbientShadowAlpha;
74    }
75
76    static uint8_t getSpotShadowAlpha() {
77        if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
78            return Properties::overrideSpotShadowStrength;
79        }
80        return mSpotShadowAlpha;
81    }
82
83    static Vector3 getLightCenter() {
84        if (CC_UNLIKELY(Properties::overrideLightPosY > 0 || Properties::overrideLightPosZ > 0)) {
85            Vector3 adjustedLightCenter = mLightCenter;
86            if (CC_UNLIKELY(Properties::overrideLightPosY > 0)) {
87                // negated since this shifts up
88                adjustedLightCenter.y = - Properties::overrideLightPosY;
89            }
90            if (CC_UNLIKELY(Properties::overrideLightPosZ > 0)) {
91                adjustedLightCenter.z = Properties::overrideLightPosZ;
92            }
93            return adjustedLightCenter;
94        }
95        return mLightCenter;
96    }
97
98    static void updateLighting(const FrameBuilder::LightGeometry& lightGeometry,
99            const BakedOpRenderer::LightInfo& lightInfo) {
100        mLightRadius = lightGeometry.radius;
101        mAmbientShadowAlpha = lightInfo.ambientShadowAlpha;
102        mSpotShadowAlpha = lightInfo.spotShadowAlpha;
103        mLightCenter = lightGeometry.center;
104    }
105
106protected:
107    void dumpResourceCacheUsage() const;
108
109    renderthread::RenderThread& mRenderThread;
110
111private:
112    void renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
113            const std::vector< sp<RenderNode> >& nodes, bool opaque, bool wideColorGamut,
114            const Rect &contentDrawBounds, SkCanvas* canvas);
115
116    /**
117     *  Debugging feature.  Draws a semi-transparent overlay on each pixel, indicating
118     *  how many times it has been drawn.
119     */
120    void renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip,
121            const std::vector< sp<RenderNode> >& nodes, const Rect &contentDrawBounds,
122            sk_sp<SkSurface>);
123
124    /**
125     *  Render mVectorDrawables into offscreen buffers.
126     */
127    void renderVectorDrawableCache();
128
129    TaskManager mTaskManager;
130    std::vector<sk_sp<SkImage>> mPinnedImages;
131
132    /**
133     *  populated by prepareTree with dirty VDs
134     */
135    std::vector<VectorDrawableRoot*> mVectorDrawables;
136    static float mLightRadius;
137    static uint8_t mAmbientShadowAlpha;
138    static uint8_t mSpotShadowAlpha;
139    static Vector3 mLightCenter;
140};
141
142} /* namespace skiapipeline */
143} /* namespace uirenderer */
144} /* namespace android */
145