blob: 289f65c22ad382518e3176bfa4427500d1d31c1a [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 Ilievaaa9e832019-09-17 14:07:23 -040024// TODO: Use public SurfaceTexture APIs once available and include public NDK header file instead.
25#include <gui/surfacetexture/surface_texture_platform.h>
John Reck04fc5832014-02-05 16:38:25 -080026
Stan Ilievaaa9e832019-09-17 14:07:23 -040027#include <map>
28#include <memory>
29
John Reck04fc5832014-02-05 16:38:25 -080030#include "Layer.h"
John Reck04fc5832014-02-05 16:38:25 -080031#include "Rect.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040032#include "renderstate/RenderState.h"
John Reck04fc5832014-02-05 16:38:25 -080033
34namespace android {
35namespace uirenderer {
36
Stan Ilievaaa9e832019-09-17 14:07:23 -040037class AutoBackendTextureRelease;
sergeyv3e9999b2017-01-19 15:37:02 -080038class RenderState;
39
Stan Ilievaaa9e832019-09-17 14:07:23 -040040typedef std::unique_ptr<ASurfaceTexture> AutoTextureRelease;
41
John Reck04fc5832014-02-05 16:38:25 -080042// Container to hold the properties a layer should be set to at the start
43// of a render pass
Derek Sollenberger28a4d992018-09-20 13:37:24 -040044class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback {
John Reck04fc5832014-02-05 16:38:25 -080045public:
John Recka39dd592014-02-14 16:59:37 -080046 // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
47 // and will not call incrementRef on it as a result.
Stan Iliev564ca3e2018-09-04 22:00:00 +000048 ANDROID_API explicit DeferredLayerUpdater(RenderState& renderState);
sergeyv3e9999b2017-01-19 15:37:02 -080049
John Reck04fc5832014-02-05 16:38:25 -080050 ANDROID_API ~DeferredLayerUpdater();
51
Dan Stozaa41f29c2014-11-12 12:35:42 -080052 ANDROID_API bool setSize(int width, int height) {
John Reck04fc5832014-02-05 16:38:25 -080053 if (mWidth != width || mHeight != height) {
54 mWidth = width;
55 mHeight = height;
56 return true;
57 }
58 return false;
59 }
60
John Reck417ed6d2016-03-22 16:01:08 -070061 int getWidth() { return mWidth; }
62 int getHeight() { return mHeight; }
63
John Reck04fc5832014-02-05 16:38:25 -080064 ANDROID_API bool setBlend(bool blend) {
65 if (blend != mBlend) {
66 mBlend = blend;
67 return true;
68 }
69 return false;
70 }
71
Stan Ilievaaa9e832019-09-17 14:07:23 -040072 ANDROID_API void setSurfaceTexture(AutoTextureRelease&& consumer);
John Reck04fc5832014-02-05 16:38:25 -080073
John Reck1bcacfd2017-11-03 10:12:19 -070074 ANDROID_API void updateTexImage() { mUpdateTexImage = true; }
John Reck04fc5832014-02-05 16:38:25 -080075
76 ANDROID_API void setTransform(const SkMatrix* matrix) {
77 delete mTransform;
Chris Craikd41c4d82015-01-05 15:51:13 -080078 mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
John Reck04fc5832014-02-05 16:38:25 -080079 }
80
John Reck1bcacfd2017-11-03 10:12:19 -070081 SkMatrix* getTransform() { return mTransform; }
John Reck417ed6d2016-03-22 16:01:08 -070082
Derek Sollenberger674554f2014-02-19 16:47:32 +000083 ANDROID_API void setPaint(const SkPaint* paint);
John Reck04fc5832014-02-05 16:38:25 -080084
Chris Craikd2dfd8f2015-12-16 14:27:20 -080085 void apply();
John Reck04fc5832014-02-05 16:38:25 -080086
John Reck1bcacfd2017-11-03 10:12:19 -070087 Layer* backingLayer() { return mLayer; }
John Reck04fc5832014-02-05 16:38:25 -080088
Chris Craikd2dfd8f2015-12-16 14:27:20 -080089 void detachSurfaceTexture();
John Reck918ad522014-06-27 14:45:25 -070090
Stan Iliev564ca3e2018-09-04 22:00:00 +000091 void updateLayer(bool forceFilter, const SkMatrix& textureTransform,
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040092 const sk_sp<SkImage>& layerImage);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040093
sergeyv3e9999b2017-01-19 15:37:02 -080094 void destroyLayer();
95
Derek Sollenberger28a4d992018-09-20 13:37:24 -040096protected:
97 void onContextDestroyed() override;
98
John Reck04fc5832014-02-05 16:38:25 -080099private:
Stan Ilievaaa9e832019-09-17 14:07:23 -0400100 /**
101 * ImageSlot contains the information and object references that
102 * DeferredLayerUpdater maintains about a slot. Slot id comes from
103 * ASurfaceTexture_dequeueBuffer. Usually there are at most 3 slots active at a time.
104 */
105 class ImageSlot {
106 public:
107 ~ImageSlot() { clear(); }
108
109 sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace,
110 bool forceCreate, GrContext* context);
111
112 private:
113 void clear();
114
115 // the dataspace associated with the current image
116 android_dataspace mDataspace = HAL_DATASPACE_UNKNOWN;
117
118 AHardwareBuffer* mBuffer = nullptr;
119
120 /**
121 * mTextureRelease may outlive DeferredLayerUpdater, if the last ref is held by an SkImage.
122 * DeferredLayerUpdater holds one ref to mTextureRelease, which is decremented by "clear".
123 */
124 AutoBackendTextureRelease* mTextureRelease = nullptr;
125 };
126
127 /**
128 * DeferredLayerUpdater stores the SkImages that have been allocated by the BufferQueue
129 * for each buffer slot.
130 */
131 std::map<int, ImageSlot> mImageSlots;
132
sergeyv3e9999b2017-01-19 15:37:02 -0800133 RenderState& mRenderState;
134
John Reck04fc5832014-02-05 16:38:25 -0800135 // Generic properties
sergeyv3e9999b2017-01-19 15:37:02 -0800136 int mWidth = 0;
137 int mHeight = 0;
138 bool mBlend = false;
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400139 sk_sp<SkColorFilter> mColorFilter;
sergeyv3e9999b2017-01-19 15:37:02 -0800140 int mAlpha = 255;
141 SkBlendMode mMode = SkBlendMode::kSrcOver;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400142 AutoTextureRelease mSurfaceTexture;
John Reck04fc5832014-02-05 16:38:25 -0800143 SkMatrix* mTransform;
sergeyv00eb43d2017-02-13 14:34:15 -0800144 bool mGLContextAttached;
John Reck04fc5832014-02-05 16:38:25 -0800145 bool mUpdateTexImage;
146
147 Layer* mLayer;
John Reck04fc5832014-02-05 16:38:25 -0800148};
149
150} /* namespace uirenderer */
151} /* namespace android */