blob: 9a4c5505fa353721faa6e33dfaafe5d4811bd1c6 [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
John Reck04fc5832014-02-05 16:38:25 -080019#include <SkColorFilter.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000020#include <SkImage.h>
John Reck04fc5832014-02-05 16:38:25 -080021#include <SkMatrix.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040022#include <android/hardware_buffer.h>
Stan Iliev5af5d302020-01-13 11:29:18 -050023#include <android/surface_texture.h>
Greg Daniel27e1fa22021-05-26 09:24:15 -040024#include <cutils/compiler.h>
25#include <utils/Errors.h>
John Reck04fc5832014-02-05 16:38:25 -080026
Greg Daniel27e1fa22021-05-26 09:24:15 -040027#include <EGL/egl.h>
28#include <EGL/eglext.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040029#include <map>
30#include <memory>
31
John Reck04fc5832014-02-05 16:38:25 -080032#include "Layer.h"
John Reck04fc5832014-02-05 16:38:25 -080033#include "Rect.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040034#include "renderstate/RenderState.h"
John Reck04fc5832014-02-05 16:38:25 -080035
36namespace android {
37namespace uirenderer {
38
Stan Ilievaaa9e832019-09-17 14:07:23 -040039class AutoBackendTextureRelease;
sergeyv3e9999b2017-01-19 15:37:02 -080040class RenderState;
41
Stan Iliev5af5d302020-01-13 11:29:18 -050042typedef std::unique_ptr<ASurfaceTexture, decltype(&ASurfaceTexture_release)> AutoTextureRelease;
Stan Ilievaaa9e832019-09-17 14:07:23 -040043
John Reck04fc5832014-02-05 16:38:25 -080044// Container to hold the properties a layer should be set to at the start
45// of a render pass
Derek Sollenberger28a4d992018-09-20 13:37:24 -040046class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback {
John Reck04fc5832014-02-05 16:38:25 -080047public:
John Recka39dd592014-02-14 16:59:37 -080048 // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
49 // and will not call incrementRef on it as a result.
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050050 explicit DeferredLayerUpdater(RenderState& renderState);
sergeyv3e9999b2017-01-19 15:37:02 -080051
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050052 ~DeferredLayerUpdater();
John Reck04fc5832014-02-05 16:38:25 -080053
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050054 bool setSize(int width, int height) {
John Reck04fc5832014-02-05 16:38:25 -080055 if (mWidth != width || mHeight != height) {
56 mWidth = width;
57 mHeight = height;
58 return true;
59 }
60 return false;
61 }
62
John Reck417ed6d2016-03-22 16:01:08 -070063 int getWidth() { return mWidth; }
64 int getHeight() { return mHeight; }
65
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050066 bool setBlend(bool blend) {
John Reck04fc5832014-02-05 16:38:25 -080067 if (blend != mBlend) {
68 mBlend = blend;
69 return true;
70 }
71 return false;
72 }
73
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050074 void setSurfaceTexture(AutoTextureRelease&& consumer);
John Reck04fc5832014-02-05 16:38:25 -080075
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050076 void updateTexImage() { mUpdateTexImage = true; }
John Reck04fc5832014-02-05 16:38:25 -080077
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050078 void setTransform(const SkMatrix* matrix) {
John Reck04fc5832014-02-05 16:38:25 -080079 delete mTransform;
Chris Craikd41c4d82015-01-05 15:51:13 -080080 mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
John Reck04fc5832014-02-05 16:38:25 -080081 }
82
John Reck1bcacfd2017-11-03 10:12:19 -070083 SkMatrix* getTransform() { return mTransform; }
John Reck417ed6d2016-03-22 16:01:08 -070084
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050085 void setPaint(const SkPaint* paint);
John Reck04fc5832014-02-05 16:38:25 -080086
Chris Craikd2dfd8f2015-12-16 14:27:20 -080087 void apply();
John Reck04fc5832014-02-05 16:38:25 -080088
John Reck1bcacfd2017-11-03 10:12:19 -070089 Layer* backingLayer() { return mLayer; }
John Reck04fc5832014-02-05 16:38:25 -080090
Chris Craikd2dfd8f2015-12-16 14:27:20 -080091 void detachSurfaceTexture();
John Reck918ad522014-06-27 14:45:25 -070092
ramindani3952ed62021-08-12 15:55:12 +000093 void updateLayer(bool forceFilter, const sk_sp<SkImage>& layerImage, const uint32_t transform,
Alec Mourid0001fe2021-11-22 10:09:22 -080094 SkRect currentCrop, float maxLuminanceNits = -1.f);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040095
sergeyv3e9999b2017-01-19 15:37:02 -080096 void destroyLayer();
97
Derek Sollenberger28a4d992018-09-20 13:37:24 -040098protected:
99 void onContextDestroyed() override;
100
John Reck04fc5832014-02-05 16:38:25 -0800101private:
Stan Ilievaaa9e832019-09-17 14:07:23 -0400102 /**
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 Daniel27e1fa22021-05-26 09:24:15 -0400109 ~ImageSlot() {}
Stan Ilievaaa9e832019-09-17 14:07:23 -0400110
111 sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400112 bool forceCreate, GrDirectContext* context);
Stan Ilievaaa9e832019-09-17 14:07:23 -0400113
Greg Daniel27e1fa22021-05-26 09:24:15 -0400114 void releaseQueueOwnership(GrDirectContext* context);
115
116 void clear(GrDirectContext* context);
117
Stan Ilievaaa9e832019-09-17 14:07:23 -0400118 private:
Stan Ilievaaa9e832019-09-17 14:07:23 -0400119
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 Daniel27e1fa22021-05-26 09:24:15 -0400132 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 Ilievaaa9e832019-09-17 14:07:23 -0400136 /**
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
sergeyv3e9999b2017-01-19 15:37:02 -0800142 RenderState& mRenderState;
143
John Reck04fc5832014-02-05 16:38:25 -0800144 // Generic properties
sergeyv3e9999b2017-01-19 15:37:02 -0800145 int mWidth = 0;
146 int mHeight = 0;
147 bool mBlend = false;
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400148 sk_sp<SkColorFilter> mColorFilter;
sergeyv3e9999b2017-01-19 15:37:02 -0800149 int mAlpha = 255;
150 SkBlendMode mMode = SkBlendMode::kSrcOver;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400151 AutoTextureRelease mSurfaceTexture;
John Reck04fc5832014-02-05 16:38:25 -0800152 SkMatrix* mTransform;
sergeyv00eb43d2017-02-13 14:34:15 -0800153 bool mGLContextAttached;
John Reck04fc5832014-02-05 16:38:25 -0800154 bool mUpdateTexImage;
Greg Daniel27e1fa22021-05-26 09:24:15 -0400155 int mCurrentSlot = -1;
John Reck04fc5832014-02-05 16:38:25 -0800156
157 Layer* mLayer;
John Reck04fc5832014-02-05 16:38:25 -0800158};
159
160} /* namespace uirenderer */
161} /* namespace android */