1/*
2 * Copyright (C) 2014 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 <cutils/compiler.h>
20#include <gui/GLConsumer.h>
21#include <SkColorFilter.h>
22#include <SkMatrix.h>
23#include <utils/StrongPointer.h>
24
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include "Layer.h"
29#include "Rect.h"
30#include "renderthread/RenderThread.h"
31
32namespace android {
33namespace uirenderer {
34
35class RenderState;
36
37// Container to hold the properties a layer should be set to at the start
38// of a render pass
39class DeferredLayerUpdater : public VirtualLightRefBase {
40public:
41    // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
42    // and will not call incrementRef on it as a result.
43    typedef std::function<Layer*(RenderState& renderState, uint32_t layerWidth,
44            uint32_t layerHeight, SkColorFilter* colorFilter, int alpha,
45            SkBlendMode mode, bool blend)> CreateLayerFn;
46    ANDROID_API explicit DeferredLayerUpdater(RenderState& renderState,
47            CreateLayerFn createLayerFn, Layer::Api layerApi);
48
49    ANDROID_API ~DeferredLayerUpdater();
50
51    ANDROID_API bool setSize(int width, int height) {
52        if (mWidth != width || mHeight != height) {
53            mWidth = width;
54            mHeight = height;
55            return true;
56        }
57        return false;
58    }
59
60    int getWidth() { return mWidth; }
61    int getHeight() { return mHeight; }
62
63    ANDROID_API bool setBlend(bool blend) {
64        if (blend != mBlend) {
65            mBlend = blend;
66            return true;
67        }
68        return false;
69    }
70
71    ANDROID_API void setSurfaceTexture(const sp<GLConsumer>& texture) {
72        if (texture.get() != mSurfaceTexture.get()) {
73            mSurfaceTexture = texture;
74
75            GLenum target = texture->getCurrentTextureTarget();
76            LOG_ALWAYS_FATAL_IF(target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES,
77                    "set unsupported GLConsumer with target %x", target);
78        }
79    }
80
81    ANDROID_API void updateTexImage() {
82        mUpdateTexImage = true;
83    }
84
85    ANDROID_API void setTransform(const SkMatrix* matrix) {
86        delete mTransform;
87        mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
88    }
89
90    SkMatrix* getTransform() {
91        return mTransform;
92    }
93
94    ANDROID_API void setPaint(const SkPaint* paint);
95
96    void apply();
97
98    Layer* backingLayer() {
99        return mLayer;
100    }
101
102    void detachSurfaceTexture();
103
104    void updateLayer(bool forceFilter, const float* textureTransform);
105
106    void destroyLayer();
107
108    Layer::Api getBackingLayerApi() {
109        return mLayerApi;
110    }
111
112private:
113    RenderState& mRenderState;
114
115    // Generic properties
116    int mWidth = 0;
117    int mHeight = 0;
118    bool mBlend = false;
119    SkColorFilter* mColorFilter = nullptr;
120    int mAlpha = 255;
121    SkBlendMode mMode = SkBlendMode::kSrcOver;
122    sp<GLConsumer> mSurfaceTexture;
123    SkMatrix* mTransform;
124    bool mGLContextAttached;
125    bool mUpdateTexImage;
126
127    Layer* mLayer;
128    Layer::Api mLayerApi;
129    CreateLayerFn mCreateLayerFn;
130
131    void doUpdateTexImage();
132    void doUpdateVkTexImage();
133};
134
135} /* namespace uirenderer */
136} /* namespace android */
137