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