blob: 3e8e8a1a1ca322254567e743e61d499a155cb9d8 [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 */
16#include "DeferredLayerUpdater.h"
17
Greg Daniel8cd3edf2017-01-09 14:15:41 -050018#include "GlLayer.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050019#include "VkLayer.h"
John Reck749906b2014-10-03 15:02:19 -070020#include "renderthread/EglManager.h"
21#include "renderthread/RenderTask.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070022#include "utils/PaintUtils.h"
John Reck04fc5832014-02-05 16:38:25 -080023
24namespace android {
25namespace uirenderer {
26
John Reckc36df952015-07-29 10:09:36 -070027DeferredLayerUpdater::DeferredLayerUpdater(Layer* layer)
Chris Craikd41c4d82015-01-05 15:51:13 -080028 : mSurfaceTexture(nullptr)
29 , mTransform(nullptr)
John Reck04fc5832014-02-05 16:38:25 -080030 , mNeedsGLContextAttach(false)
31 , mUpdateTexImage(false)
Chris Craik5e00c7c2016-07-06 16:10:09 -070032 , mLayer(layer) {
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040033 mWidth = mLayer->getWidth();
34 mHeight = mLayer->getHeight();
John Reck04fc5832014-02-05 16:38:25 -080035 mBlend = mLayer->isBlend();
Derek Sollenberger674554f2014-02-19 16:47:32 +000036 mColorFilter = SkSafeRef(mLayer->getColorFilter());
John Reck04fc5832014-02-05 16:38:25 -080037 mAlpha = mLayer->getAlpha();
38 mMode = mLayer->getMode();
John Reck04fc5832014-02-05 16:38:25 -080039}
40
41DeferredLayerUpdater::~DeferredLayerUpdater() {
Derek Sollenberger674554f2014-02-19 16:47:32 +000042 SkSafeUnref(mColorFilter);
Chris Craikd41c4d82015-01-05 15:51:13 -080043 setTransform(nullptr);
John Reck0e89e2b2014-10-31 14:49:06 -070044 mLayer->postDecStrong();
Chris Craikd41c4d82015-01-05 15:51:13 -080045 mLayer = nullptr;
John Reck04fc5832014-02-05 16:38:25 -080046}
47
Derek Sollenberger674554f2014-02-19 16:47:32 +000048void DeferredLayerUpdater::setPaint(const SkPaint* paint) {
Chris Craikbf6f0f22015-10-01 12:36:07 -070049 mAlpha = PaintUtils::getAlphaDirect(paint);
Mike Reed260ab722016-10-07 15:59:20 -040050 mMode = PaintUtils::getBlendModeDirect(paint);
Chris Craikd41c4d82015-01-05 15:51:13 -080051 SkColorFilter* colorFilter = (paint) ? paint->getColorFilter() : nullptr;
John Reck04fc5832014-02-05 16:38:25 -080052 SkRefCnt_SafeAssign(mColorFilter, colorFilter);
53}
54
Chris Craikd2dfd8f2015-12-16 14:27:20 -080055void DeferredLayerUpdater::apply() {
John Reck04fc5832014-02-05 16:38:25 -080056 mLayer->setColorFilter(mColorFilter);
57 mLayer->setAlpha(mAlpha, mMode);
58
John Reck25fbb3f2014-06-12 13:46:45 -070059 if (mSurfaceTexture.get()) {
Greg Daniel45ec62b2017-01-04 14:27:00 -050060 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 Reck04fc5832014-02-05 16:38:25 -080077 }
78 if (mTransform) {
79 mLayer->getTransform().load(*mTransform);
Chris Craikd41c4d82015-01-05 15:51:13 -080080 setTransform(nullptr);
John Reck04fc5832014-02-05 16:38:25 -080081 }
82 }
John Reck04fc5832014-02-05 16:38:25 -080083}
84
85void DeferredLayerUpdater::doUpdateTexImage() {
Greg Daniel8cd3edf2017-01-09 14:15:41 -050086 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 Reck04fc5832014-02-05 16:38:25 -080089 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 Craik9757ac02014-02-25 18:50:17 -0800104
105 bool forceFilter = false;
106 sp<GraphicBuffer> buffer = mSurfaceTexture->getCurrentBuffer();
Chris Craikd41c4d82015-01-05 15:51:13 -0800107 if (buffer != nullptr) {
Chris Craik9757ac02014-02-25 18:50:17 -0800108 // force filtration if buffer size != layer size
Dan Stoza32d2e762014-11-18 14:47:11 -0800109 forceFilter = mWidth != static_cast<int>(buffer->getWidth())
110 || mHeight != static_cast<int>(buffer->getHeight());
Chris Craik9757ac02014-02-25 18:50:17 -0800111 }
112
John Reck04fc5832014-02-05 16:38:25 -0800113 #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 Craik48f650c2015-03-10 11:03:39 -0700121 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 Sollenberger56ad6ec2016-07-22 12:13:32 -0400124 updateLayer(forceFilter, renderTarget, transform);
125 }
126}
127
Greg Daniel45ec62b2017-01-04 14:27:00 -0500128void 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 Sollenberger56ad6ec2016-07-22 12:13:32 -0400140void DeferredLayerUpdater::updateLayer(bool forceFilter, GLenum renderTarget,
141 const float* textureTransform) {
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500142 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 Daniel45ec62b2017-01-04 14:27:00 -0500146 updateLayer(forceFilter, textureTransform);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400147
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500148 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 Reck04fc5832014-02-05 16:38:25 -0800154 }
155}
156
Greg Daniel45ec62b2017-01-04 14:27:00 -0500157void 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 Reck918ad522014-06-27 14:45:25 -0700164void DeferredLayerUpdater::detachSurfaceTexture() {
165 if (mSurfaceTexture.get()) {
Greg Daniel45ec62b2017-01-04 14:27:00 -0500166 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 Reck749906b2014-10-03 15:02:19 -0700173 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800174 mSurfaceTexture = nullptr;
John Reck918ad522014-06-27 14:45:25 -0700175 }
176}
177
John Reck04fc5832014-02-05 16:38:25 -0800178} /* namespace uirenderer */
179} /* namespace android */