DeferredLayerUpdater.h revision 45ec62ba72c5017fae7d8baab20bfb0d4c99c627
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
35// Container to hold the properties a layer should be set to at the start
36// of a render pass
37class DeferredLayerUpdater : public VirtualLightRefBase {
38public:
39    // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
40    // and will not call incrementRef on it as a result.
41    ANDROID_API explicit DeferredLayerUpdater(Layer* layer);
42    ANDROID_API ~DeferredLayerUpdater();
43
44    ANDROID_API bool setSize(int width, int height) {
45        if (mWidth != width || mHeight != height) {
46            mWidth = width;
47            mHeight = height;
48            return true;
49        }
50        return false;
51    }
52
53    int getWidth() { return mWidth; }
54    int getHeight() { return mHeight; }
55
56    ANDROID_API bool setBlend(bool blend) {
57        if (blend != mBlend) {
58            mBlend = blend;
59            return true;
60        }
61        return false;
62    }
63
64    ANDROID_API void setSurfaceTexture(const sp<GLConsumer>& texture, bool needsAttach) {
65        if (texture.get() != mSurfaceTexture.get()) {
66            mNeedsGLContextAttach = needsAttach;
67            mSurfaceTexture = texture;
68
69            GLenum target = texture->getCurrentTextureTarget();
70            LOG_ALWAYS_FATAL_IF(target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES,
71                    "set unsupported GLConsumer with target %x", target);
72        }
73    }
74
75    ANDROID_API void updateTexImage() {
76        mUpdateTexImage = true;
77    }
78
79    ANDROID_API void setTransform(const SkMatrix* matrix) {
80        delete mTransform;
81        mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
82    }
83
84    SkMatrix* getTransform() {
85        return mTransform;
86    }
87
88    ANDROID_API void setPaint(const SkPaint* paint);
89
90    void apply();
91
92    Layer* backingLayer() {
93        return mLayer;
94    }
95
96    void detachSurfaceTexture();
97
98    void updateLayer(bool forceFilter, GLenum renderTarget, const float* textureTransform);
99
100private:
101    // Generic properties
102    int mWidth;
103    int mHeight;
104    bool mBlend;
105    SkColorFilter* mColorFilter;
106    int mAlpha;
107    SkBlendMode mMode;
108    sp<GLConsumer> mSurfaceTexture;
109    SkMatrix* mTransform;
110    bool mNeedsGLContextAttach;
111    bool mUpdateTexImage;
112
113    Layer* mLayer;
114
115    void doUpdateTexImage();
116    void doUpdateVkTexImage();
117    void updateLayer(bool forceFilter, const float* textureTransform);
118};
119
120} /* namespace uirenderer */
121} /* namespace android */
122