DeferredLayerUpdater.h revision 3e9999bd866fac71c72e6b484a9836c87c328a08
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, bool needsAttach) {
72        if (texture.get() != mSurfaceTexture.get()) {
73            mNeedsGLContextAttach = needsAttach;
74            mSurfaceTexture = texture;
75
76            GLenum target = texture->getCurrentTextureTarget();
77            LOG_ALWAYS_FATAL_IF(target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES,
78                    "set unsupported GLConsumer with target %x", target);
79        }
80    }
81
82    ANDROID_API void updateTexImage() {
83        mUpdateTexImage = true;
84    }
85
86    ANDROID_API void setTransform(const SkMatrix* matrix) {
87        delete mTransform;
88        mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
89    }
90
91    SkMatrix* getTransform() {
92        return mTransform;
93    }
94
95    ANDROID_API void setPaint(const SkPaint* paint);
96
97    void apply();
98
99    Layer* backingLayer() {
100        return mLayer;
101    }
102
103    void detachSurfaceTexture();
104
105    void updateLayer(bool forceFilter, GLenum renderTarget, const float* textureTransform);
106
107    void destroyLayer();
108
109    Layer::Api getBackingLayerApi() {
110        return mLayerApi;
111    }
112
113private:
114    RenderState& mRenderState;
115
116    // Generic properties
117    int mWidth = 0;
118    int mHeight = 0;
119    bool mBlend = false;
120    SkColorFilter* mColorFilter = nullptr;
121    int mAlpha = 255;
122    SkBlendMode mMode = SkBlendMode::kSrcOver;
123    sp<GLConsumer> mSurfaceTexture;
124    SkMatrix* mTransform;
125    bool mNeedsGLContextAttach;
126    bool mUpdateTexImage;
127
128    Layer* mLayer;
129    Layer::Api mLayerApi;
130    CreateLayerFn mCreateLayerFn;
131
132    void doUpdateTexImage();
133    void doUpdateVkTexImage();
134};
135
136} /* namespace uirenderer */
137} /* namespace android */
138