blob: a7f8f6189a8e6da3afa0efa7c1249b62e986ab1c [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
Kevin Lubick4e8ce462022-12-01 20:29:16 +000019#include <SkBlendMode.h>
John Reck04fc5832014-02-05 16:38:25 -080020#include <SkColorFilter.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000021#include <SkImage.h>
John Reck04fc5832014-02-05 16:38:25 -080022#include <SkMatrix.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040023#include <android/hardware_buffer.h>
Stan Iliev5af5d302020-01-13 11:29:18 -050024#include <android/surface_texture.h>
Greg Daniel27e1fa22021-05-26 09:24:15 -040025#include <cutils/compiler.h>
26#include <utils/Errors.h>
John Reck04fc5832014-02-05 16:38:25 -080027
Greg Daniel27e1fa22021-05-26 09:24:15 -040028#include <EGL/egl.h>
29#include <EGL/eglext.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040030#include <map>
31#include <memory>
32
John Reck04fc5832014-02-05 16:38:25 -080033#include "Layer.h"
John Reck04fc5832014-02-05 16:38:25 -080034#include "Rect.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040035#include "renderstate/RenderState.h"
John Reck04fc5832014-02-05 16:38:25 -080036
37namespace android {
38namespace uirenderer {
39
Stan Ilievaaa9e832019-09-17 14:07:23 -040040class AutoBackendTextureRelease;
sergeyv3e9999b2017-01-19 15:37:02 -080041class RenderState;
42
Stan Iliev5af5d302020-01-13 11:29:18 -050043typedef std::unique_ptr<ASurfaceTexture, decltype(&ASurfaceTexture_release)> AutoTextureRelease;
Stan Ilievaaa9e832019-09-17 14:07:23 -040044
John Reck04fc5832014-02-05 16:38:25 -080045// Container to hold the properties a layer should be set to at the start
46// of a render pass
Derek Sollenberger28a4d992018-09-20 13:37:24 -040047class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback {
John Reck04fc5832014-02-05 16:38:25 -080048public:
John Recka39dd592014-02-14 16:59:37 -080049 // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
50 // and will not call incrementRef on it as a result.
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050051 explicit DeferredLayerUpdater(RenderState& renderState);
sergeyv3e9999b2017-01-19 15:37:02 -080052
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050053 ~DeferredLayerUpdater();
John Reck04fc5832014-02-05 16:38:25 -080054
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050055 bool setSize(int width, int height) {
John Reck04fc5832014-02-05 16:38:25 -080056 if (mWidth != width || mHeight != height) {
57 mWidth = width;
58 mHeight = height;
59 return true;
60 }
61 return false;
62 }
63
John Reck417ed6d2016-03-22 16:01:08 -070064 int getWidth() { return mWidth; }
65 int getHeight() { return mHeight; }
66
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050067 bool setBlend(bool blend) {
John Reck04fc5832014-02-05 16:38:25 -080068 if (blend != mBlend) {
69 mBlend = blend;
70 return true;
71 }
72 return false;
73 }
74
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050075 void setSurfaceTexture(AutoTextureRelease&& consumer);
John Reck04fc5832014-02-05 16:38:25 -080076
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050077 void updateTexImage() { mUpdateTexImage = true; }
John Reck04fc5832014-02-05 16:38:25 -080078
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050079 void setTransform(const SkMatrix* matrix) {
John Reck04fc5832014-02-05 16:38:25 -080080 delete mTransform;
Chris Craikd41c4d82015-01-05 15:51:13 -080081 mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
John Reck04fc5832014-02-05 16:38:25 -080082 }
83
John Reck1bcacfd2017-11-03 10:12:19 -070084 SkMatrix* getTransform() { return mTransform; }
John Reck417ed6d2016-03-22 16:01:08 -070085
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050086 void setPaint(const SkPaint* paint);
John Reck04fc5832014-02-05 16:38:25 -080087
Chris Craikd2dfd8f2015-12-16 14:27:20 -080088 void apply();
John Reck04fc5832014-02-05 16:38:25 -080089
John Reck1bcacfd2017-11-03 10:12:19 -070090 Layer* backingLayer() { return mLayer; }
John Reck04fc5832014-02-05 16:38:25 -080091
Chris Craikd2dfd8f2015-12-16 14:27:20 -080092 void detachSurfaceTexture();
John Reck918ad522014-06-27 14:45:25 -070093
ramindani3952ed62021-08-12 15:55:12 +000094 void updateLayer(bool forceFilter, const sk_sp<SkImage>& layerImage, const uint32_t transform,
Alec Mourid0001fe2021-11-22 10:09:22 -080095 SkRect currentCrop, float maxLuminanceNits = -1.f);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040096
sergeyv3e9999b2017-01-19 15:37:02 -080097 void destroyLayer();
98
Derek Sollenberger28a4d992018-09-20 13:37:24 -040099protected:
100 void onContextDestroyed() override;
101
John Reck04fc5832014-02-05 16:38:25 -0800102private:
Stan Ilievaaa9e832019-09-17 14:07:23 -0400103 /**
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 Daniel27e1fa22021-05-26 09:24:15 -0400110 ~ImageSlot() {}
Stan Ilievaaa9e832019-09-17 14:07:23 -0400111
112 sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400113 bool forceCreate, GrDirectContext* context);
Stan Ilievaaa9e832019-09-17 14:07:23 -0400114
Greg Daniel27e1fa22021-05-26 09:24:15 -0400115 void releaseQueueOwnership(GrDirectContext* context);
116
117 void clear(GrDirectContext* context);
118
Stan Ilievaaa9e832019-09-17 14:07:23 -0400119 private:
Stan Ilievaaa9e832019-09-17 14:07:23 -0400120
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 Daniel27e1fa22021-05-26 09:24:15 -0400133 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 Ilievaaa9e832019-09-17 14:07:23 -0400137 /**
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
sergeyv3e9999b2017-01-19 15:37:02 -0800143 RenderState& mRenderState;
144
John Reck04fc5832014-02-05 16:38:25 -0800145 // Generic properties
sergeyv3e9999b2017-01-19 15:37:02 -0800146 int mWidth = 0;
147 int mHeight = 0;
148 bool mBlend = false;
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400149 sk_sp<SkColorFilter> mColorFilter;
sergeyv3e9999b2017-01-19 15:37:02 -0800150 int mAlpha = 255;
151 SkBlendMode mMode = SkBlendMode::kSrcOver;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400152 AutoTextureRelease mSurfaceTexture;
John Reck04fc5832014-02-05 16:38:25 -0800153 SkMatrix* mTransform;
sergeyv00eb43db2017-02-13 14:34:15 -0800154 bool mGLContextAttached;
John Reck04fc5832014-02-05 16:38:25 -0800155 bool mUpdateTexImage;
Greg Daniel27e1fa22021-05-26 09:24:15 -0400156 int mCurrentSlot = -1;
John Reck04fc5832014-02-05 16:38:25 -0800157
158 Layer* mLayer;
John Reck04fc5832014-02-05 16:38:25 -0800159};
160
161} /* namespace uirenderer */
162} /* namespace android */