• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <SkImage.h>
21 #include <SkMatrix.h>
22 #include <android/hardware_buffer.h>
23 #include <android/surface_texture.h>
24 #include <cutils/compiler.h>
25 #include <utils/Errors.h>
26 
27 #include <EGL/egl.h>
28 #include <EGL/eglext.h>
29 #include <map>
30 #include <memory>
31 
32 #include "Layer.h"
33 #include "Rect.h"
34 #include "renderstate/RenderState.h"
35 
36 namespace android {
37 namespace uirenderer {
38 
39 class AutoBackendTextureRelease;
40 class RenderState;
41 
42 typedef std::unique_ptr<ASurfaceTexture, decltype(&ASurfaceTexture_release)> AutoTextureRelease;
43 
44 // Container to hold the properties a layer should be set to at the start
45 // of a render pass
46 class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback {
47 public:
48     // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
49     // and will not call incrementRef on it as a result.
50     explicit DeferredLayerUpdater(RenderState& renderState);
51 
52     ~DeferredLayerUpdater();
53 
setSize(int width,int height)54     bool setSize(int width, int height) {
55         if (mWidth != width || mHeight != height) {
56             mWidth = width;
57             mHeight = height;
58             return true;
59         }
60         return false;
61     }
62 
getWidth()63     int getWidth() { return mWidth; }
getHeight()64     int getHeight() { return mHeight; }
65 
setBlend(bool blend)66     bool setBlend(bool blend) {
67         if (blend != mBlend) {
68             mBlend = blend;
69             return true;
70         }
71         return false;
72     }
73 
74     void setSurfaceTexture(AutoTextureRelease&& consumer);
75 
updateTexImage()76     void updateTexImage() { mUpdateTexImage = true; }
77 
setTransform(const SkMatrix * matrix)78     void setTransform(const SkMatrix* matrix) {
79         delete mTransform;
80         mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
81     }
82 
getTransform()83     SkMatrix* getTransform() { return mTransform; }
84 
85     void setPaint(const SkPaint* paint);
86 
87     void apply();
88 
backingLayer()89     Layer* backingLayer() { return mLayer; }
90 
91     void detachSurfaceTexture();
92 
93     void updateLayer(bool forceFilter, const sk_sp<SkImage>& layerImage, const uint32_t transform,
94                      SkRect currentCrop, float maxLuminanceNits = -1.f);
95 
96     void destroyLayer();
97 
98 protected:
99     void onContextDestroyed() override;
100 
101 private:
102     /**
103      * ImageSlot contains the information and object references that
104      * DeferredLayerUpdater maintains about a slot. Slot id comes from
105      * ASurfaceTexture_dequeueBuffer. Usually there are at most 3 slots active at a time.
106      */
107     class ImageSlot {
108     public:
~ImageSlot()109         ~ImageSlot() {}
110 
111         sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace,
112                                       bool forceCreate, GrDirectContext* context);
113 
114         void releaseQueueOwnership(GrDirectContext* context);
115 
116         void clear(GrDirectContext* context);
117 
118     private:
119 
120         // the dataspace associated with the current image
121         android_dataspace mDataspace = HAL_DATASPACE_UNKNOWN;
122 
123         AHardwareBuffer* mBuffer = nullptr;
124 
125         /**
126          * mTextureRelease may outlive DeferredLayerUpdater, if the last ref is held by an SkImage.
127          * DeferredLayerUpdater holds one ref to mTextureRelease, which is decremented by "clear".
128          */
129         AutoBackendTextureRelease* mTextureRelease = nullptr;
130     };
131 
132     static status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, EGLDisplay* display,
133                                        int* releaseFence, void* handle);
134     static status_t fenceWait(int fence, void* handle);
135 
136     /**
137      * DeferredLayerUpdater stores the SkImages that have been allocated by the BufferQueue
138      * for each buffer slot.
139      */
140     std::map<int, ImageSlot> mImageSlots;
141 
142     RenderState& mRenderState;
143 
144     // Generic properties
145     int mWidth = 0;
146     int mHeight = 0;
147     bool mBlend = false;
148     sk_sp<SkColorFilter> mColorFilter;
149     int mAlpha = 255;
150     SkBlendMode mMode = SkBlendMode::kSrcOver;
151     AutoTextureRelease mSurfaceTexture;
152     SkMatrix* mTransform;
153     bool mGLContextAttached;
154     bool mUpdateTexImage;
155     int mCurrentSlot = -1;
156 
157     Layer* mLayer;
158 };
159 
160 } /* namespace uirenderer */
161 } /* namespace android */
162