| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 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 |  */ | 
| Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 16 |  | 
 | 17 | #pragma once | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 18 |  | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 19 | #include <SkColorFilter.h> | 
| Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 20 | #include <SkImage.h> | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 21 | #include <SkMatrix.h> | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 22 | #include <android/hardware_buffer.h> | 
| Stan Iliev | 5af5d30 | 2020-01-13 11:29:18 -0500 | [diff] [blame] | 23 | #include <android/surface_texture.h> | 
| Greg Daniel | 27e1fa2 | 2021-05-26 09:24:15 -0400 | [diff] [blame] | 24 | #include <cutils/compiler.h> | 
 | 25 | #include <utils/Errors.h> | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 26 |  | 
| Greg Daniel | 27e1fa2 | 2021-05-26 09:24:15 -0400 | [diff] [blame] | 27 | #include <EGL/egl.h> | 
 | 28 | #include <EGL/eglext.h> | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 29 | #include <map> | 
 | 30 | #include <memory> | 
 | 31 |  | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 32 | #include "Layer.h" | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 33 | #include "Rect.h" | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 34 | #include "renderstate/RenderState.h" | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 35 |  | 
 | 36 | namespace android { | 
 | 37 | namespace uirenderer { | 
 | 38 |  | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 39 | class AutoBackendTextureRelease; | 
| sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 40 | class RenderState; | 
 | 41 |  | 
| Stan Iliev | 5af5d30 | 2020-01-13 11:29:18 -0500 | [diff] [blame] | 42 | typedef std::unique_ptr<ASurfaceTexture, decltype(&ASurfaceTexture_release)> AutoTextureRelease; | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 43 |  | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 44 | // Container to hold the properties a layer should be set to at the start | 
 | 45 | // of a render pass | 
| Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 46 | class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback { | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 47 | public: | 
| John Reck | a39dd59 | 2014-02-14 16:59:37 -0800 | [diff] [blame] | 48 |     // Note that DeferredLayerUpdater assumes it is taking ownership of the layer | 
 | 49 |     // and will not call incrementRef on it as a result. | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 50 |     explicit DeferredLayerUpdater(RenderState& renderState); | 
| sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 51 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 52 |     ~DeferredLayerUpdater(); | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 53 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 54 |     bool setSize(int width, int height) { | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 55 |         if (mWidth != width || mHeight != height) { | 
 | 56 |             mWidth = width; | 
 | 57 |             mHeight = height; | 
 | 58 |             return true; | 
 | 59 |         } | 
 | 60 |         return false; | 
 | 61 |     } | 
 | 62 |  | 
| John Reck | 417ed6d | 2016-03-22 16:01:08 -0700 | [diff] [blame] | 63 |     int getWidth() { return mWidth; } | 
 | 64 |     int getHeight() { return mHeight; } | 
 | 65 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 66 |     bool setBlend(bool blend) { | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 67 |         if (blend != mBlend) { | 
 | 68 |             mBlend = blend; | 
 | 69 |             return true; | 
 | 70 |         } | 
 | 71 |         return false; | 
 | 72 |     } | 
 | 73 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 74 |     void setSurfaceTexture(AutoTextureRelease&& consumer); | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 75 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 76 |     void updateTexImage() { mUpdateTexImage = true; } | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 77 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 78 |     void setTransform(const SkMatrix* matrix) { | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 79 |         delete mTransform; | 
| Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 80 |         mTransform = matrix ? new SkMatrix(*matrix) : nullptr; | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 81 |     } | 
 | 82 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 83 |     SkMatrix* getTransform() { return mTransform; } | 
| John Reck | 417ed6d | 2016-03-22 16:01:08 -0700 | [diff] [blame] | 84 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 85 |     void setPaint(const SkPaint* paint); | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 86 |  | 
| Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 87 |     void apply(); | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 88 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 89 |     Layer* backingLayer() { return mLayer; } | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 90 |  | 
| Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 91 |     void detachSurfaceTexture(); | 
| John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 92 |  | 
| ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 93 |     void updateLayer(bool forceFilter, const sk_sp<SkImage>& layerImage, const uint32_t transform, | 
| Alec Mouri | d0001fe | 2021-11-22 10:09:22 -0800 | [diff] [blame] | 94 |                      SkRect currentCrop, float maxLuminanceNits = -1.f); | 
| Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 95 |  | 
| sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 96 |     void destroyLayer(); | 
 | 97 |  | 
| Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 98 | protected: | 
 | 99 |     void onContextDestroyed() override; | 
 | 100 |  | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 101 | private: | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 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: | 
| Greg Daniel | 27e1fa2 | 2021-05-26 09:24:15 -0400 | [diff] [blame] | 109 |         ~ImageSlot() {} | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 110 |  | 
 | 111 |         sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace, | 
| Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 112 |                                       bool forceCreate, GrDirectContext* context); | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 113 |  | 
| Greg Daniel | 27e1fa2 | 2021-05-26 09:24:15 -0400 | [diff] [blame] | 114 |         void releaseQueueOwnership(GrDirectContext* context); | 
 | 115 |  | 
 | 116 |         void clear(GrDirectContext* context); | 
 | 117 |  | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 118 |     private: | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 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 |  | 
| Greg Daniel | 27e1fa2 | 2021-05-26 09:24:15 -0400 | [diff] [blame] | 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 |  | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 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 |  | 
| sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 142 |     RenderState& mRenderState; | 
 | 143 |  | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 144 |     // Generic properties | 
| sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 145 |     int mWidth = 0; | 
 | 146 |     int mHeight = 0; | 
 | 147 |     bool mBlend = false; | 
| Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 148 |     sk_sp<SkColorFilter> mColorFilter; | 
| sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 149 |     int mAlpha = 255; | 
 | 150 |     SkBlendMode mMode = SkBlendMode::kSrcOver; | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 151 |     AutoTextureRelease mSurfaceTexture; | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 152 |     SkMatrix* mTransform; | 
| sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 153 |     bool mGLContextAttached; | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 154 |     bool mUpdateTexImage; | 
| Greg Daniel | 27e1fa2 | 2021-05-26 09:24:15 -0400 | [diff] [blame] | 155 |     int mCurrentSlot = -1; | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 156 |  | 
 | 157 |     Layer* mLayer; | 
| John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 158 | }; | 
 | 159 |  | 
 | 160 | } /* namespace uirenderer */ | 
 | 161 | } /* namespace android */ |