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