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