blob: 3abb47ca92d162a85d65289d73b74acfc2cbdc18 [file] [log] [blame]
John Reck04fc5832014-02-05 16:38:25 -08001/*
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 Craik5e00c7c2016-07-06 16:10:09 -070016
17#pragma once
John Reck04fc5832014-02-05 16:38:25 -080018
Alec Mourief3ecd52024-09-24 21:00:32 +000019#include <EGL/egl.h>
20#include <EGL/eglext.h>
Kevin Lubick4e8ce462022-12-01 20:29:16 +000021#include <SkBlendMode.h>
John Reck04fc5832014-02-05 16:38:25 -080022#include <SkColorFilter.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000023#include <SkImage.h>
John Reck04fc5832014-02-05 16:38:25 -080024#include <SkMatrix.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040025#include <android/hardware_buffer.h>
Stan Iliev5af5d302020-01-13 11:29:18 -050026#include <android/surface_texture.h>
Greg Daniel27e1fa22021-05-26 09:24:15 -040027#include <cutils/compiler.h>
28#include <utils/Errors.h>
Alec Mourief3ecd52024-09-24 21:00:32 +000029#include <utils/Timers.h>
John Reck04fc5832014-02-05 16:38:25 -080030
Alec Mourief3ecd52024-09-24 21:00:32 +000031#include <chrono>
Stan Ilievaaa9e832019-09-17 14:07:23 -040032#include <map>
33#include <memory>
34
John Reck04fc5832014-02-05 16:38:25 -080035#include "Layer.h"
John Reck04fc5832014-02-05 16:38:25 -080036#include "Rect.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040037#include "renderstate/RenderState.h"
John Reck04fc5832014-02-05 16:38:25 -080038
39namespace android {
40namespace uirenderer {
41
Stan Ilievaaa9e832019-09-17 14:07:23 -040042class AutoBackendTextureRelease;
sergeyv3e9999b2017-01-19 15:37:02 -080043class RenderState;
44
Stan Iliev5af5d302020-01-13 11:29:18 -050045typedef std::unique_ptr<ASurfaceTexture, decltype(&ASurfaceTexture_release)> AutoTextureRelease;
Stan Ilievaaa9e832019-09-17 14:07:23 -040046
John Reck04fc5832014-02-05 16:38:25 -080047// Container to hold the properties a layer should be set to at the start
48// of a render pass
Derek Sollenberger28a4d992018-09-20 13:37:24 -040049class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback {
John Reck04fc5832014-02-05 16:38:25 -080050public:
John Recka39dd592014-02-14 16:59:37 -080051 // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
52 // and will not call incrementRef on it as a result.
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050053 explicit DeferredLayerUpdater(RenderState& renderState);
sergeyv3e9999b2017-01-19 15:37:02 -080054
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050055 ~DeferredLayerUpdater();
John Reck04fc5832014-02-05 16:38:25 -080056
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050057 bool setSize(int width, int height) {
John Reck04fc5832014-02-05 16:38:25 -080058 if (mWidth != width || mHeight != height) {
59 mWidth = width;
60 mHeight = height;
61 return true;
62 }
63 return false;
64 }
65
John Reck417ed6d2016-03-22 16:01:08 -070066 int getWidth() { return mWidth; }
67 int getHeight() { return mHeight; }
68
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050069 bool setBlend(bool blend) {
John Reck04fc5832014-02-05 16:38:25 -080070 if (blend != mBlend) {
71 mBlend = blend;
72 return true;
73 }
74 return false;
75 }
76
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050077 void setSurfaceTexture(AutoTextureRelease&& consumer);
John Reck04fc5832014-02-05 16:38:25 -080078
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050079 void updateTexImage() { mUpdateTexImage = true; }
John Reck04fc5832014-02-05 16:38:25 -080080
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050081 void setTransform(const SkMatrix* matrix) {
John Reck04fc5832014-02-05 16:38:25 -080082 delete mTransform;
Chris Craikd41c4d82015-01-05 15:51:13 -080083 mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
John Reck04fc5832014-02-05 16:38:25 -080084 }
85
John Reck1bcacfd2017-11-03 10:12:19 -070086 SkMatrix* getTransform() { return mTransform; }
John Reck417ed6d2016-03-22 16:01:08 -070087
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050088 void setPaint(const SkPaint* paint);
John Reck04fc5832014-02-05 16:38:25 -080089
Chris Craikd2dfd8f2015-12-16 14:27:20 -080090 void apply();
John Reck04fc5832014-02-05 16:38:25 -080091
John Reck1bcacfd2017-11-03 10:12:19 -070092 Layer* backingLayer() { return mLayer; }
John Reck04fc5832014-02-05 16:38:25 -080093
Chris Craikd2dfd8f2015-12-16 14:27:20 -080094 void detachSurfaceTexture();
John Reck918ad522014-06-27 14:45:25 -070095
ramindani3952ed62021-08-12 15:55:12 +000096 void updateLayer(bool forceFilter, const sk_sp<SkImage>& layerImage, const uint32_t transform,
Alec Mourid0001fe2021-11-22 10:09:22 -080097 SkRect currentCrop, float maxLuminanceNits = -1.f);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040098
sergeyv3e9999b2017-01-19 15:37:02 -080099 void destroyLayer();
100
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400101protected:
102 void onContextDestroyed() override;
103
John Reck04fc5832014-02-05 16:38:25 -0800104private:
Stan Ilievaaa9e832019-09-17 14:07:23 -0400105 /**
106 * ImageSlot contains the information and object references that
107 * DeferredLayerUpdater maintains about a slot. Slot id comes from
108 * ASurfaceTexture_dequeueBuffer. Usually there are at most 3 slots active at a time.
109 */
110 class ImageSlot {
111 public:
Greg Daniel27e1fa22021-05-26 09:24:15 -0400112 ~ImageSlot() {}
Stan Ilievaaa9e832019-09-17 14:07:23 -0400113
114 sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400115 bool forceCreate, GrDirectContext* context);
Stan Ilievaaa9e832019-09-17 14:07:23 -0400116
Greg Daniel27e1fa22021-05-26 09:24:15 -0400117 void releaseQueueOwnership(GrDirectContext* context);
118
119 void clear(GrDirectContext* context);
120
Stan Ilievaaa9e832019-09-17 14:07:23 -0400121 private:
Stan Ilievaaa9e832019-09-17 14:07:23 -0400122
123 // the dataspace associated with the current image
124 android_dataspace mDataspace = HAL_DATASPACE_UNKNOWN;
125
126 AHardwareBuffer* mBuffer = nullptr;
127
128 /**
129 * mTextureRelease may outlive DeferredLayerUpdater, if the last ref is held by an SkImage.
130 * DeferredLayerUpdater holds one ref to mTextureRelease, which is decremented by "clear".
131 */
132 AutoBackendTextureRelease* mTextureRelease = nullptr;
133 };
134
Greg Daniel27e1fa22021-05-26 09:24:15 -0400135 static status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, EGLDisplay* display,
136 int* releaseFence, void* handle);
137 static status_t fenceWait(int fence, void* handle);
138
Stan Ilievaaa9e832019-09-17 14:07:23 -0400139 /**
140 * DeferredLayerUpdater stores the SkImages that have been allocated by the BufferQueue
141 * for each buffer slot.
142 */
143 std::map<int, ImageSlot> mImageSlots;
144
sergeyv3e9999b2017-01-19 15:37:02 -0800145 RenderState& mRenderState;
146
John Reck04fc5832014-02-05 16:38:25 -0800147 // Generic properties
sergeyv3e9999b2017-01-19 15:37:02 -0800148 int mWidth = 0;
149 int mHeight = 0;
150 bool mBlend = false;
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400151 sk_sp<SkColorFilter> mColorFilter;
sergeyv3e9999b2017-01-19 15:37:02 -0800152 int mAlpha = 255;
153 SkBlendMode mMode = SkBlendMode::kSrcOver;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400154 AutoTextureRelease mSurfaceTexture;
John Reck04fc5832014-02-05 16:38:25 -0800155 SkMatrix* mTransform;
sergeyv00eb43d2017-02-13 14:34:15 -0800156 bool mGLContextAttached;
John Reck04fc5832014-02-05 16:38:25 -0800157 bool mUpdateTexImage;
Greg Daniel27e1fa22021-05-26 09:24:15 -0400158 int mCurrentSlot = -1;
Alec Mourief3ecd52024-09-24 21:00:32 +0000159 android_dataspace mDataspace = HAL_DATASPACE_UNKNOWN;
160 std::chrono::steady_clock::time_point mFirstTimeForDataspace =
161 std::chrono::steady_clock::time_point::min();
John Reck04fc5832014-02-05 16:38:25 -0800162
163 Layer* mLayer;
John Reck04fc5832014-02-05 16:38:25 -0800164};
165
166} /* namespace uirenderer */
167} /* namespace android */