blob: c44c0d537fa763b164a6545e11137eff03cb4f12 [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>
John Reck1bcacfd2017-11-03 10:12:19 -070023#include <cutils/compiler.h>
Stan Iliev5af5d302020-01-13 11:29:18 -050024#include <android/surface_texture.h>
John Reck04fc5832014-02-05 16:38:25 -080025
Stan Ilievaaa9e832019-09-17 14:07:23 -040026#include <map>
27#include <memory>
28
John Reck04fc5832014-02-05 16:38:25 -080029#include "Layer.h"
John Reck04fc5832014-02-05 16:38:25 -080030#include "Rect.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040031#include "renderstate/RenderState.h"
John Reck04fc5832014-02-05 16:38:25 -080032
33namespace android {
34namespace uirenderer {
35
Stan Ilievaaa9e832019-09-17 14:07:23 -040036class AutoBackendTextureRelease;
sergeyv3e9999b2017-01-19 15:37:02 -080037class RenderState;
38
Stan Iliev5af5d302020-01-13 11:29:18 -050039typedef std::unique_ptr<ASurfaceTexture, decltype(&ASurfaceTexture_release)> AutoTextureRelease;
Stan Ilievaaa9e832019-09-17 14:07:23 -040040
John Reck04fc5832014-02-05 16:38:25 -080041// Container to hold the properties a layer should be set to at the start
42// of a render pass
Derek Sollenberger28a4d992018-09-20 13:37:24 -040043class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback {
John Reck04fc5832014-02-05 16:38:25 -080044public:
John Recka39dd592014-02-14 16:59:37 -080045 // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
46 // and will not call incrementRef on it as a result.
Stan Iliev564ca3e2018-09-04 22:00:00 +000047 ANDROID_API explicit DeferredLayerUpdater(RenderState& renderState);
sergeyv3e9999b2017-01-19 15:37:02 -080048
John Reck04fc5832014-02-05 16:38:25 -080049 ANDROID_API ~DeferredLayerUpdater();
50
Dan Stozaa41f29c2014-11-12 12:35:42 -080051 ANDROID_API bool setSize(int width, int height) {
John Reck04fc5832014-02-05 16:38:25 -080052 if (mWidth != width || mHeight != height) {
53 mWidth = width;
54 mHeight = height;
55 return true;
56 }
57 return false;
58 }
59
John Reck417ed6d2016-03-22 16:01:08 -070060 int getWidth() { return mWidth; }
61 int getHeight() { return mHeight; }
62
John Reck04fc5832014-02-05 16:38:25 -080063 ANDROID_API bool setBlend(bool blend) {
64 if (blend != mBlend) {
65 mBlend = blend;
66 return true;
67 }
68 return false;
69 }
70
Stan Ilievaaa9e832019-09-17 14:07:23 -040071 ANDROID_API void setSurfaceTexture(AutoTextureRelease&& consumer);
John Reck04fc5832014-02-05 16:38:25 -080072
John Reck1bcacfd2017-11-03 10:12:19 -070073 ANDROID_API void updateTexImage() { mUpdateTexImage = true; }
John Reck04fc5832014-02-05 16:38:25 -080074
75 ANDROID_API void setTransform(const SkMatrix* matrix) {
76 delete mTransform;
Chris Craikd41c4d82015-01-05 15:51:13 -080077 mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
John Reck04fc5832014-02-05 16:38:25 -080078 }
79
John Reck1bcacfd2017-11-03 10:12:19 -070080 SkMatrix* getTransform() { return mTransform; }
John Reck417ed6d2016-03-22 16:01:08 -070081
Derek Sollenberger674554f2014-02-19 16:47:32 +000082 ANDROID_API void setPaint(const SkPaint* paint);
John Reck04fc5832014-02-05 16:38:25 -080083
Chris Craikd2dfd8f2015-12-16 14:27:20 -080084 void apply();
John Reck04fc5832014-02-05 16:38:25 -080085
John Reck1bcacfd2017-11-03 10:12:19 -070086 Layer* backingLayer() { return mLayer; }
John Reck04fc5832014-02-05 16:38:25 -080087
Chris Craikd2dfd8f2015-12-16 14:27:20 -080088 void detachSurfaceTexture();
John Reck918ad522014-06-27 14:45:25 -070089
Stan Iliev564ca3e2018-09-04 22:00:00 +000090 void updateLayer(bool forceFilter, const SkMatrix& textureTransform,
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040091 const sk_sp<SkImage>& layerImage);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040092
sergeyv3e9999b2017-01-19 15:37:02 -080093 void destroyLayer();
94
Derek Sollenberger28a4d992018-09-20 13:37:24 -040095protected:
96 void onContextDestroyed() override;
97
John Reck04fc5832014-02-05 16:38:25 -080098private:
Stan Ilievaaa9e832019-09-17 14:07:23 -040099 /**
100 * ImageSlot contains the information and object references that
101 * DeferredLayerUpdater maintains about a slot. Slot id comes from
102 * ASurfaceTexture_dequeueBuffer. Usually there are at most 3 slots active at a time.
103 */
104 class ImageSlot {
105 public:
106 ~ImageSlot() { clear(); }
107
108 sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace,
109 bool forceCreate, GrContext* context);
110
111 private:
112 void clear();
113
114 // the dataspace associated with the current image
115 android_dataspace mDataspace = HAL_DATASPACE_UNKNOWN;
116
117 AHardwareBuffer* mBuffer = nullptr;
118
119 /**
120 * mTextureRelease may outlive DeferredLayerUpdater, if the last ref is held by an SkImage.
121 * DeferredLayerUpdater holds one ref to mTextureRelease, which is decremented by "clear".
122 */
123 AutoBackendTextureRelease* mTextureRelease = nullptr;
124 };
125
126 /**
127 * DeferredLayerUpdater stores the SkImages that have been allocated by the BufferQueue
128 * for each buffer slot.
129 */
130 std::map<int, ImageSlot> mImageSlots;
131
sergeyv3e9999b2017-01-19 15:37:02 -0800132 RenderState& mRenderState;
133
John Reck04fc5832014-02-05 16:38:25 -0800134 // Generic properties
sergeyv3e9999b2017-01-19 15:37:02 -0800135 int mWidth = 0;
136 int mHeight = 0;
137 bool mBlend = false;
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400138 sk_sp<SkColorFilter> mColorFilter;
sergeyv3e9999b2017-01-19 15:37:02 -0800139 int mAlpha = 255;
140 SkBlendMode mMode = SkBlendMode::kSrcOver;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400141 AutoTextureRelease mSurfaceTexture;
John Reck04fc5832014-02-05 16:38:25 -0800142 SkMatrix* mTransform;
sergeyv00eb43d2017-02-13 14:34:15 -0800143 bool mGLContextAttached;
John Reck04fc5832014-02-05 16:38:25 -0800144 bool mUpdateTexImage;
145
146 Layer* mLayer;
John Reck04fc5832014-02-05 16:38:25 -0800147};
148
149} /* namespace uirenderer */
150} /* namespace android */