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 | |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 18 | #include "GlLayer.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 19 | #include "VkLayer.h" |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 20 | #include "renderstate/RenderState.h" |
John Reck | 749906b | 2014-10-03 15:02:19 -0700 | [diff] [blame] | 21 | #include "renderthread/EglManager.h" |
| 22 | #include "renderthread/RenderTask.h" |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 23 | #include "utils/PaintUtils.h" |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 28 | DeferredLayerUpdater::DeferredLayerUpdater(RenderState& renderState, CreateLayerFn createLayerFn, |
| 29 | Layer::Api layerApi) |
| 30 | : mRenderState(renderState) |
| 31 | , mBlend(false) |
| 32 | , mSurfaceTexture(nullptr) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 33 | , mTransform(nullptr) |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 34 | , mGLContextAttached(false) |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 35 | , mUpdateTexImage(false) |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 36 | , mLayer(nullptr) |
| 37 | , mLayerApi(layerApi) |
| 38 | , mCreateLayerFn(createLayerFn) { |
| 39 | renderState.registerDeferredLayerUpdater(this); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | DeferredLayerUpdater::~DeferredLayerUpdater() { |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 43 | SkSafeUnref(mColorFilter); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 44 | setTransform(nullptr); |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 45 | mRenderState.unregisterDeferredLayerUpdater(this); |
| 46 | destroyLayer(); |
| 47 | } |
| 48 | |
| 49 | void DeferredLayerUpdater::destroyLayer() { |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 50 | if (!mLayer) { |
| 51 | return; |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 52 | } |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 53 | |
| 54 | if (mSurfaceTexture.get() && mLayerApi == Layer::Api::OpenGL && mGLContextAttached) { |
| 55 | status_t err = mSurfaceTexture->detachFromContext(); |
| 56 | mGLContextAttached = false; |
| 57 | if (err != 0) { |
| 58 | // TODO: Elevate to fatal exception |
| 59 | ALOGE("Failed to detach SurfaceTexture from context %d", err); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | mLayer->postDecStrong(); |
| 64 | mLayer = nullptr; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 67 | void DeferredLayerUpdater::setPaint(const SkPaint* paint) { |
Chris Craik | bf6f0f2 | 2015-10-01 12:36:07 -0700 | [diff] [blame] | 68 | mAlpha = PaintUtils::getAlphaDirect(paint); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 69 | mMode = PaintUtils::getBlendModeDirect(paint); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 70 | SkColorFilter* colorFilter = (paint) ? paint->getColorFilter() : nullptr; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 71 | SkRefCnt_SafeAssign(mColorFilter, colorFilter); |
| 72 | } |
| 73 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 74 | void DeferredLayerUpdater::apply() { |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 75 | if (!mLayer) { |
| 76 | mLayer = mCreateLayerFn(mRenderState, mWidth, mHeight, mColorFilter, mAlpha, mMode, mBlend); |
| 77 | } |
| 78 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 79 | mLayer->setColorFilter(mColorFilter); |
| 80 | mLayer->setAlpha(mAlpha, mMode); |
| 81 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 82 | if (mSurfaceTexture.get()) { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 83 | if (mLayer->getApi() == Layer::Api::Vulkan) { |
| 84 | if (mUpdateTexImage) { |
| 85 | mUpdateTexImage = false; |
| 86 | doUpdateVkTexImage(); |
| 87 | } |
| 88 | } else { |
| 89 | LOG_ALWAYS_FATAL_IF(mLayer->getApi() != Layer::Api::OpenGL, |
| 90 | "apply surfaceTexture with non GL backend %x, GL %x, VK %x", |
| 91 | mLayer->getApi(), Layer::Api::OpenGL, Layer::Api::Vulkan); |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 92 | if (!mGLContextAttached) { |
| 93 | mGLContextAttached = true; |
| 94 | mUpdateTexImage = true; |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 95 | mSurfaceTexture->attachToContext(static_cast<GlLayer*>(mLayer)->getTextureId()); |
| 96 | } |
| 97 | if (mUpdateTexImage) { |
| 98 | mUpdateTexImage = false; |
| 99 | doUpdateTexImage(); |
| 100 | } |
Chris Craik | 09df887 | 2017-02-14 12:37:49 -0800 | [diff] [blame] | 101 | GLenum renderTarget = mSurfaceTexture->getCurrentTextureTarget(); |
| 102 | static_cast<GlLayer*>(mLayer)->setRenderTarget(renderTarget); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 103 | } |
| 104 | if (mTransform) { |
| 105 | mLayer->getTransform().load(*mTransform); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 106 | setTransform(nullptr); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void DeferredLayerUpdater::doUpdateTexImage() { |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 112 | LOG_ALWAYS_FATAL_IF(mLayer->getApi() != Layer::Api::OpenGL, |
| 113 | "doUpdateTexImage non GL backend %x, GL %x, VK %x", |
| 114 | mLayer->getApi(), Layer::Api::OpenGL, Layer::Api::Vulkan); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 115 | if (mSurfaceTexture->updateTexImage() == NO_ERROR) { |
| 116 | float transform[16]; |
| 117 | |
| 118 | int64_t frameNumber = mSurfaceTexture->getFrameNumber(); |
| 119 | // If the GLConsumer queue is in synchronous mode, need to discard all |
| 120 | // but latest frame, using the frame number to tell when we no longer |
| 121 | // have newer frames to target. Since we can't tell which mode it is in, |
| 122 | // do this unconditionally. |
| 123 | int dropCounter = 0; |
| 124 | while (mSurfaceTexture->updateTexImage() == NO_ERROR) { |
| 125 | int64_t newFrameNumber = mSurfaceTexture->getFrameNumber(); |
| 126 | if (newFrameNumber == frameNumber) break; |
| 127 | frameNumber = newFrameNumber; |
| 128 | dropCounter++; |
| 129 | } |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 130 | |
| 131 | bool forceFilter = false; |
| 132 | sp<GraphicBuffer> buffer = mSurfaceTexture->getCurrentBuffer(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 133 | if (buffer != nullptr) { |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 134 | // force filtration if buffer size != layer size |
Dan Stoza | 32d2e76 | 2014-11-18 14:47:11 -0800 | [diff] [blame] | 135 | forceFilter = mWidth != static_cast<int>(buffer->getWidth()) |
| 136 | || mHeight != static_cast<int>(buffer->getHeight()); |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 137 | } |
| 138 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 139 | #if DEBUG_RENDERER |
| 140 | if (dropCounter > 0) { |
| 141 | RENDERER_LOGD("Dropped %d frames on texture layer update", dropCounter); |
| 142 | } |
| 143 | #endif |
| 144 | mSurfaceTexture->getTransformMatrix(transform); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 145 | |
Chris Craik | 09df887 | 2017-02-14 12:37:49 -0800 | [diff] [blame] | 146 | updateLayer(forceFilter, transform); |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 150 | void DeferredLayerUpdater::doUpdateVkTexImage() { |
| 151 | LOG_ALWAYS_FATAL_IF(mLayer->getApi() != Layer::Api::Vulkan, |
| 152 | "updateLayer non Vulkan backend %x, GL %x, VK %x", |
| 153 | mLayer->getApi(), Layer::Api::OpenGL, Layer::Api::Vulkan); |
| 154 | |
| 155 | static const mat4 identityMatrix; |
Chris Craik | 09df887 | 2017-02-14 12:37:49 -0800 | [diff] [blame] | 156 | updateLayer(false, identityMatrix.data); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 157 | |
| 158 | VkLayer* vkLayer = static_cast<VkLayer*>(mLayer); |
| 159 | vkLayer->updateTexture(); |
| 160 | } |
| 161 | |
Chris Craik | 09df887 | 2017-02-14 12:37:49 -0800 | [diff] [blame] | 162 | void DeferredLayerUpdater::updateLayer(bool forceFilter, const float* textureTransform) { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 163 | mLayer->setBlend(mBlend); |
| 164 | mLayer->setForceFilter(forceFilter); |
| 165 | mLayer->setSize(mWidth, mHeight); |
| 166 | mLayer->getTexTransform().load(textureTransform); |
| 167 | } |
| 168 | |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 169 | void DeferredLayerUpdater::detachSurfaceTexture() { |
| 170 | if (mSurfaceTexture.get()) { |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 171 | destroyLayer(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 172 | mSurfaceTexture = nullptr; |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 176 | } /* namespace uirenderer */ |
| 177 | } /* namespace android */ |