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