blob: 00916559a9c2336f9ccb24dabc254a7e5915add5 [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) {
sergeyv3e9999b2017-01-19 15:37:02 -080032 renderState.registerDeferredLayerUpdater(this);
John Reck04fc5832014-02-05 16:38:25 -080033}
34
35DeferredLayerUpdater::~DeferredLayerUpdater() {
Chris Craikd41c4d82015-01-05 15:51:13 -080036 setTransform(nullptr);
sergeyv3e9999b2017-01-19 15:37:02 -080037 mRenderState.unregisterDeferredLayerUpdater(this);
38 destroyLayer();
39}
40
41void DeferredLayerUpdater::destroyLayer() {
sergeyv00eb43d2017-02-13 14:34:15 -080042 if (!mLayer) {
43 return;
sergeyv3e9999b2017-01-19 15:37:02 -080044 }
sergeyv00eb43d2017-02-13 14:34:15 -080045
Stan Iliev564ca3e2018-09-04 22:00:00 +000046 if (mSurfaceTexture.get() && mGLContextAttached) {
47 mSurfaceTexture->detachFromView();
sergeyv00eb43d2017-02-13 14:34:15 -080048 mGLContextAttached = false;
sergeyv00eb43d2017-02-13 14:34:15 -080049 }
50
51 mLayer->postDecStrong();
52 mLayer = nullptr;
John Reck04fc5832014-02-05 16:38:25 -080053}
54
Derek Sollenberger674554f2014-02-19 16:47:32 +000055void DeferredLayerUpdater::setPaint(const SkPaint* paint) {
Chris Craikbf6f0f22015-10-01 12:36:07 -070056 mAlpha = PaintUtils::getAlphaDirect(paint);
Mike Reed260ab722016-10-07 15:59:20 -040057 mMode = PaintUtils::getBlendModeDirect(paint);
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -040058 if (paint) {
59 mColorFilter = paint->refColorFilter();
60 } else {
61 mColorFilter.reset();
62 }
John Reck04fc5832014-02-05 16:38:25 -080063}
64
Chris Craikd2dfd8f2015-12-16 14:27:20 -080065void DeferredLayerUpdater::apply() {
sergeyv3e9999b2017-01-19 15:37:02 -080066 if (!mLayer) {
Stan Iliev564ca3e2018-09-04 22:00:00 +000067 mLayer = new Layer(mRenderState, mColorFilter, mAlpha, mMode);
sergeyv3e9999b2017-01-19 15:37:02 -080068 }
69
John Reck04fc5832014-02-05 16:38:25 -080070 mLayer->setColorFilter(mColorFilter);
71 mLayer->setAlpha(mAlpha, mMode);
72
John Reck25fbb3f2014-06-12 13:46:45 -070073 if (mSurfaceTexture.get()) {
Stan Iliev564ca3e2018-09-04 22:00:00 +000074 if (!mGLContextAttached) {
75 mGLContextAttached = true;
76 mUpdateTexImage = true;
77 mSurfaceTexture->attachToView();
Stan Iliev85f90962018-08-31 18:35:06 +000078 }
Stan Iliev564ca3e2018-09-04 22:00:00 +000079 if (mUpdateTexImage) {
80 mUpdateTexImage = false;
81 sk_sp<SkImage> layerImage;
82 SkMatrix textureTransform;
83 android_dataspace dataSpace;
84 bool queueEmpty = true;
85 // If the SurfaceTexture queue is in synchronous mode, need to discard all
86 // but latest frame. Since we can't tell which mode it is in,
87 // do this unconditionally.
88 do {
89 layerImage = mSurfaceTexture->dequeueImage(textureTransform, dataSpace, &queueEmpty,
90 mRenderState);
91 } while (layerImage.get() && (!queueEmpty));
92 if (layerImage.get()) {
93 // force filtration if buffer size != layer size
94 bool forceFilter = mWidth != layerImage->width() || mHeight != layerImage->height();
95 updateLayer(forceFilter, textureTransform, dataSpace, layerImage);
96 }
97 }
98
John Reck04fc5832014-02-05 16:38:25 -080099 if (mTransform) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000100 mLayer->getTransform() = *mTransform;
Chris Craikd41c4d82015-01-05 15:51:13 -0800101 setTransform(nullptr);
John Reck04fc5832014-02-05 16:38:25 -0800102 }
103 }
John Reck04fc5832014-02-05 16:38:25 -0800104}
105
Stan Iliev564ca3e2018-09-04 22:00:00 +0000106void DeferredLayerUpdater::updateLayer(bool forceFilter, const SkMatrix& textureTransform,
107 android_dataspace dataspace, const sk_sp<SkImage>& layerImage) {
Greg Daniel45ec62b2017-01-04 14:27:00 -0500108 mLayer->setBlend(mBlend);
109 mLayer->setForceFilter(forceFilter);
110 mLayer->setSize(mWidth, mHeight);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000111 mLayer->getTexTransform() = textureTransform;
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400112 mLayer->setDataSpace(dataspace);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000113 mLayer->setImage(layerImage);
Greg Daniel45ec62b2017-01-04 14:27:00 -0500114}
115
John Reck918ad522014-06-27 14:45:25 -0700116void DeferredLayerUpdater::detachSurfaceTexture() {
117 if (mSurfaceTexture.get()) {
sergeyv00eb43d2017-02-13 14:34:15 -0800118 destroyLayer();
Chris Craikd41c4d82015-01-05 15:51:13 -0800119 mSurfaceTexture = nullptr;
John Reck918ad522014-06-27 14:45:25 -0700120 }
121}
122
John Reck04fc5832014-02-05 16:38:25 -0800123} /* namespace uirenderer */
124} /* namespace android */