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