blob: 3bee3018d36e2a99537b0716482d1a48f04d5e22 [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
sergeyv3e9999b2017-01-19 15:37:02 -080018#include "renderstate/RenderState.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070019#include "utils/PaintUtils.h"
John Reck04fc5832014-02-05 16:38:25 -080020
21namespace android {
22namespace uirenderer {
23
Stan Iliev564ca3e2018-09-04 22:00:00 +000024DeferredLayerUpdater::DeferredLayerUpdater(RenderState& renderState)
sergeyv3e9999b2017-01-19 15:37:02 -080025 : mRenderState(renderState)
26 , mBlend(false)
27 , mSurfaceTexture(nullptr)
Chris Craikd41c4d82015-01-05 15:51:13 -080028 , mTransform(nullptr)
sergeyv00eb43d2017-02-13 14:34:15 -080029 , mGLContextAttached(false)
John Reck04fc5832014-02-05 16:38:25 -080030 , mUpdateTexImage(false)
Stan Iliev564ca3e2018-09-04 22:00:00 +000031 , mLayer(nullptr) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -040032 renderState.registerContextCallback(this);
John Reck04fc5832014-02-05 16:38:25 -080033}
34
35DeferredLayerUpdater::~DeferredLayerUpdater() {
Chris Craikd41c4d82015-01-05 15:51:13 -080036 setTransform(nullptr);
Derek Sollenberger28a4d992018-09-20 13:37:24 -040037 mRenderState.removeContextCallback(this);
38 destroyLayer();
39}
40
41void DeferredLayerUpdater::onContextDestroyed() {
sergeyv3e9999b2017-01-19 15:37:02 -080042 destroyLayer();
43}
44
45void DeferredLayerUpdater::destroyLayer() {
sergeyv00eb43d2017-02-13 14:34:15 -080046 if (!mLayer) {
47 return;
sergeyv3e9999b2017-01-19 15:37:02 -080048 }
sergeyv00eb43d2017-02-13 14:34:15 -080049
Stan Iliev564ca3e2018-09-04 22:00:00 +000050 if (mSurfaceTexture.get() && mGLContextAttached) {
51 mSurfaceTexture->detachFromView();
sergeyv00eb43d2017-02-13 14:34:15 -080052 mGLContextAttached = false;
sergeyv00eb43d2017-02-13 14:34:15 -080053 }
54
55 mLayer->postDecStrong();
Stan Iliev1a025a72018-09-05 16:35:11 -040056
sergeyv00eb43d2017-02-13 14:34:15 -080057 mLayer = nullptr;
John Reck04fc5832014-02-05 16:38:25 -080058}
59
Derek Sollenberger674554f2014-02-19 16:47:32 +000060void DeferredLayerUpdater::setPaint(const SkPaint* paint) {
Chris Craikbf6f0f22015-10-01 12:36:07 -070061 mAlpha = PaintUtils::getAlphaDirect(paint);
Mike Reed260ab722016-10-07 15:59:20 -040062 mMode = PaintUtils::getBlendModeDirect(paint);
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -040063 if (paint) {
64 mColorFilter = paint->refColorFilter();
65 } else {
66 mColorFilter.reset();
67 }
John Reck04fc5832014-02-05 16:38:25 -080068}
69
Chris Craikd2dfd8f2015-12-16 14:27:20 -080070void DeferredLayerUpdater::apply() {
sergeyv3e9999b2017-01-19 15:37:02 -080071 if (!mLayer) {
Stan Iliev564ca3e2018-09-04 22:00:00 +000072 mLayer = new Layer(mRenderState, mColorFilter, mAlpha, mMode);
sergeyv3e9999b2017-01-19 15:37:02 -080073 }
74
John Reck04fc5832014-02-05 16:38:25 -080075 mLayer->setColorFilter(mColorFilter);
76 mLayer->setAlpha(mAlpha, mMode);
77
John Reck25fbb3f2014-06-12 13:46:45 -070078 if (mSurfaceTexture.get()) {
Stan Iliev564ca3e2018-09-04 22:00:00 +000079 if (!mGLContextAttached) {
80 mGLContextAttached = true;
81 mUpdateTexImage = true;
82 mSurfaceTexture->attachToView();
Stan Iliev85f90962018-08-31 18:35:06 +000083 }
Stan Iliev564ca3e2018-09-04 22:00:00 +000084 if (mUpdateTexImage) {
85 mUpdateTexImage = false;
86 sk_sp<SkImage> layerImage;
87 SkMatrix textureTransform;
Stan Iliev564ca3e2018-09-04 22:00:00 +000088 bool queueEmpty = true;
89 // If the SurfaceTexture queue is in synchronous mode, need to discard all
90 // but latest frame. Since we can't tell which mode it is in,
91 // do this unconditionally.
92 do {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040093 layerImage = mSurfaceTexture->dequeueImage(textureTransform, &queueEmpty,
Stan Iliev564ca3e2018-09-04 22:00:00 +000094 mRenderState);
95 } while (layerImage.get() && (!queueEmpty));
96 if (layerImage.get()) {
97 // force filtration if buffer size != layer size
98 bool forceFilter = mWidth != layerImage->width() || mHeight != layerImage->height();
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040099 updateLayer(forceFilter, textureTransform, layerImage);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000100 }
101 }
102
John Reck04fc5832014-02-05 16:38:25 -0800103 if (mTransform) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000104 mLayer->getTransform() = *mTransform;
Chris Craikd41c4d82015-01-05 15:51:13 -0800105 setTransform(nullptr);
John Reck04fc5832014-02-05 16:38:25 -0800106 }
107 }
John Reck04fc5832014-02-05 16:38:25 -0800108}
109
Stan Iliev564ca3e2018-09-04 22:00:00 +0000110void DeferredLayerUpdater::updateLayer(bool forceFilter, const SkMatrix& textureTransform,
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400111 const sk_sp<SkImage>& layerImage) {
Greg Daniel45ec62b2017-01-04 14:27:00 -0500112 mLayer->setBlend(mBlend);
113 mLayer->setForceFilter(forceFilter);
114 mLayer->setSize(mWidth, mHeight);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000115 mLayer->getTexTransform() = textureTransform;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000116 mLayer->setImage(layerImage);
Greg Daniel45ec62b2017-01-04 14:27:00 -0500117}
118
John Reck918ad522014-06-27 14:45:25 -0700119void DeferredLayerUpdater::detachSurfaceTexture() {
120 if (mSurfaceTexture.get()) {
sergeyv00eb43d2017-02-13 14:34:15 -0800121 destroyLayer();
Chris Craikd41c4d82015-01-05 15:51:13 -0800122 mSurfaceTexture = nullptr;
John Reck918ad522014-06-27 14:45:25 -0700123 }
124}
125
John Reck04fc5832014-02-05 16:38:25 -0800126} /* namespace uirenderer */
127} /* namespace android */