John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | #include "DeferredLayerUpdater.h" |
| 17 | |
Fedor Kudasov | d501e10 | 2019-06-21 10:22:53 +0100 | [diff] [blame] | 18 | #include <GLES2/gl2.h> |
| 19 | #include <GLES2/gl2ext.h> |
| 20 | |
Stan Iliev | 5af5d30 | 2020-01-13 11:29:18 -0500 | [diff] [blame] | 21 | // TODO: Use public SurfaceTexture APIs once available and include public NDK header file instead. |
| 22 | #include <surfacetexture/surface_texture_platform.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 23 | #include "AutoBackendTextureRelease.h" |
| 24 | #include "Matrix.h" |
| 25 | #include "Properties.h" |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 26 | #include "renderstate/RenderState.h" |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 27 | #include "renderthread/EglManager.h" |
| 28 | #include "renderthread/RenderThread.h" |
| 29 | #include "renderthread/VulkanManager.h" |
| 30 | |
| 31 | using namespace android::uirenderer::renderthread; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 36 | DeferredLayerUpdater::DeferredLayerUpdater(RenderState& renderState) |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 37 | : mRenderState(renderState) |
| 38 | , mBlend(false) |
Stan Iliev | 5af5d30 | 2020-01-13 11:29:18 -0500 | [diff] [blame] | 39 | , mSurfaceTexture(nullptr, [](ASurfaceTexture*) {}) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 40 | , mTransform(nullptr) |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 41 | , mGLContextAttached(false) |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 42 | , mUpdateTexImage(false) |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 43 | , mLayer(nullptr) { |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 44 | renderState.registerContextCallback(this); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | DeferredLayerUpdater::~DeferredLayerUpdater() { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 48 | setTransform(nullptr); |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 49 | mRenderState.removeContextCallback(this); |
| 50 | destroyLayer(); |
| 51 | } |
| 52 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 53 | void DeferredLayerUpdater::setSurfaceTexture(AutoTextureRelease&& consumer) { |
| 54 | mSurfaceTexture = std::move(consumer); |
Fedor Kudasov | d501e10 | 2019-06-21 10:22:53 +0100 | [diff] [blame] | 55 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 56 | GLenum target = ASurfaceTexture_getCurrentTextureTarget(mSurfaceTexture.get()); |
| 57 | LOG_ALWAYS_FATAL_IF(target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES, |
| 58 | "set unsupported SurfaceTexture with target %x", target); |
Fedor Kudasov | d501e10 | 2019-06-21 10:22:53 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 61 | void DeferredLayerUpdater::onContextDestroyed() { |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 62 | destroyLayer(); |
| 63 | } |
| 64 | |
| 65 | void DeferredLayerUpdater::destroyLayer() { |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 66 | if (!mLayer) { |
| 67 | return; |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 68 | } |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 69 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 70 | if (mSurfaceTexture.get() && mGLContextAttached) { |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 71 | ASurfaceTexture_releaseConsumerOwnership(mSurfaceTexture.get()); |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 72 | mGLContextAttached = false; |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | mLayer->postDecStrong(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 76 | |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 77 | mLayer = nullptr; |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 78 | |
| 79 | mImageSlots.clear(); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 82 | void DeferredLayerUpdater::setPaint(const SkPaint* paint) { |
Chris Craik | bf6f0f2 | 2015-10-01 12:36:07 -0700 | [diff] [blame] | 83 | mAlpha = PaintUtils::getAlphaDirect(paint); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 84 | mMode = PaintUtils::getBlendModeDirect(paint); |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 85 | if (paint) { |
| 86 | mColorFilter = paint->refColorFilter(); |
| 87 | } else { |
| 88 | mColorFilter.reset(); |
| 89 | } |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 92 | static status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, EGLDisplay* display, |
| 93 | int* releaseFence, void* handle) { |
| 94 | *display = EGL_NO_DISPLAY; |
| 95 | RenderState* renderState = (RenderState*)handle; |
| 96 | status_t err; |
| 97 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 98 | EglManager& eglManager = renderState->getRenderThread().eglManager(); |
| 99 | *display = eglManager.eglDisplay(); |
| 100 | err = eglManager.createReleaseFence(useFenceSync, eglFence, releaseFence); |
| 101 | } else { |
| 102 | err = renderState->getRenderThread().vulkanManager().createReleaseFence( |
| 103 | releaseFence, renderState->getRenderThread().getGrContext()); |
| 104 | } |
| 105 | return err; |
| 106 | } |
| 107 | |
| 108 | static status_t fenceWait(int fence, void* handle) { |
| 109 | // Wait on the producer fence for the buffer to be ready. |
| 110 | status_t err; |
| 111 | RenderState* renderState = (RenderState*)handle; |
| 112 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 113 | err = renderState->getRenderThread().eglManager().fenceWait(fence); |
| 114 | } else { |
| 115 | err = renderState->getRenderThread().vulkanManager().fenceWait( |
| 116 | fence, renderState->getRenderThread().getGrContext()); |
| 117 | } |
| 118 | return err; |
| 119 | } |
| 120 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 121 | void DeferredLayerUpdater::apply() { |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 122 | if (!mLayer) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 123 | mLayer = new Layer(mRenderState, mColorFilter, mAlpha, mMode); |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 124 | } |
| 125 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 126 | mLayer->setColorFilter(mColorFilter); |
| 127 | mLayer->setAlpha(mAlpha, mMode); |
| 128 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 129 | if (mSurfaceTexture.get()) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 130 | if (!mGLContextAttached) { |
| 131 | mGLContextAttached = true; |
| 132 | mUpdateTexImage = true; |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 133 | ASurfaceTexture_takeConsumerOwnership(mSurfaceTexture.get()); |
Stan Iliev | 85f9096 | 2018-08-31 18:35:06 +0000 | [diff] [blame] | 134 | } |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 135 | if (mUpdateTexImage) { |
| 136 | mUpdateTexImage = false; |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 137 | float transformMatrix[16]; |
| 138 | android_dataspace dataspace; |
| 139 | int slot; |
| 140 | bool newContent = false; |
| 141 | // Note: ASurfaceTexture_dequeueBuffer discards all but the last frame. This |
| 142 | // is necessary if the SurfaceTexture queue is in synchronous mode, and we |
| 143 | // cannot tell which mode it is in. |
| 144 | AHardwareBuffer* hardwareBuffer = ASurfaceTexture_dequeueBuffer( |
| 145 | mSurfaceTexture.get(), &slot, &dataspace, transformMatrix, &newContent, |
| 146 | createReleaseFence, fenceWait, &mRenderState); |
| 147 | |
| 148 | if (hardwareBuffer) { |
| 149 | sk_sp<SkImage> layerImage = mImageSlots[slot].createIfNeeded( |
| 150 | hardwareBuffer, dataspace, newContent, |
| 151 | mRenderState.getRenderThread().getGrContext()); |
| 152 | if (layerImage.get()) { |
| 153 | SkMatrix textureTransform; |
| 154 | mat4(transformMatrix).copyTo(textureTransform); |
| 155 | // force filtration if buffer size != layer size |
| 156 | bool forceFilter = |
| 157 | mWidth != layerImage->width() || mHeight != layerImage->height(); |
| 158 | updateLayer(forceFilter, textureTransform, layerImage); |
| 159 | } |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 163 | if (mTransform) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 164 | mLayer->getTransform() = *mTransform; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 165 | setTransform(nullptr); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 170 | void DeferredLayerUpdater::updateLayer(bool forceFilter, const SkMatrix& textureTransform, |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 171 | const sk_sp<SkImage>& layerImage) { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 172 | mLayer->setBlend(mBlend); |
| 173 | mLayer->setForceFilter(forceFilter); |
| 174 | mLayer->setSize(mWidth, mHeight); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 175 | mLayer->getTexTransform() = textureTransform; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 176 | mLayer->setImage(layerImage); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 177 | } |
| 178 | |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 179 | void DeferredLayerUpdater::detachSurfaceTexture() { |
| 180 | if (mSurfaceTexture.get()) { |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 181 | destroyLayer(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 182 | mSurfaceTexture = nullptr; |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 186 | sk_sp<SkImage> DeferredLayerUpdater::ImageSlot::createIfNeeded(AHardwareBuffer* buffer, |
| 187 | android_dataspace dataspace, |
| 188 | bool forceCreate, |
| 189 | GrContext* context) { |
| 190 | if (!mTextureRelease || !mTextureRelease->getImage().get() || dataspace != mDataspace || |
| 191 | forceCreate || mBuffer != buffer) { |
| 192 | if (buffer != mBuffer) { |
| 193 | clear(); |
| 194 | } |
| 195 | |
| 196 | if (!buffer) { |
| 197 | return nullptr; |
| 198 | } |
| 199 | |
| 200 | if (!mTextureRelease) { |
| 201 | mTextureRelease = new AutoBackendTextureRelease(context, buffer); |
| 202 | } else { |
| 203 | mTextureRelease->newBufferContent(context); |
| 204 | } |
| 205 | |
| 206 | mDataspace = dataspace; |
| 207 | mBuffer = buffer; |
| 208 | mTextureRelease->makeImage(buffer, dataspace, context); |
| 209 | } |
| 210 | return mTextureRelease ? mTextureRelease->getImage() : nullptr; |
| 211 | } |
| 212 | |
| 213 | void DeferredLayerUpdater::ImageSlot::clear() { |
| 214 | if (mTextureRelease) { |
| 215 | // The following unref counteracts the initial mUsageCount of 1, set by default initializer. |
| 216 | mTextureRelease->unref(true); |
| 217 | mTextureRelease = nullptr; |
| 218 | } |
| 219 | |
| 220 | mBuffer = nullptr; |
| 221 | } |
| 222 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 223 | } /* namespace uirenderer */ |
| 224 | } /* namespace android */ |