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 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 18 | #include "renderstate/RenderState.h" |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 19 | #include "utils/PaintUtils.h" |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 24 | DeferredLayerUpdater::DeferredLayerUpdater(RenderState& renderState) |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 25 | : mRenderState(renderState) |
| 26 | , mBlend(false) |
| 27 | , mSurfaceTexture(nullptr) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 28 | , mTransform(nullptr) |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 29 | , mGLContextAttached(false) |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 30 | , mUpdateTexImage(false) |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 31 | , mLayer(nullptr) { |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 32 | renderState.registerContextCallback(this); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | DeferredLayerUpdater::~DeferredLayerUpdater() { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 36 | setTransform(nullptr); |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 37 | mRenderState.removeContextCallback(this); |
| 38 | destroyLayer(); |
| 39 | } |
| 40 | |
| 41 | void DeferredLayerUpdater::onContextDestroyed() { |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 42 | destroyLayer(); |
| 43 | } |
| 44 | |
| 45 | void DeferredLayerUpdater::destroyLayer() { |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 46 | if (!mLayer) { |
| 47 | return; |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 48 | } |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 49 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 50 | if (mSurfaceTexture.get() && mGLContextAttached) { |
| 51 | mSurfaceTexture->detachFromView(); |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 52 | mGLContextAttached = false; |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | mLayer->postDecStrong(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 56 | |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 57 | mLayer = nullptr; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 60 | void DeferredLayerUpdater::setPaint(const SkPaint* paint) { |
Chris Craik | bf6f0f2 | 2015-10-01 12:36:07 -0700 | [diff] [blame] | 61 | mAlpha = PaintUtils::getAlphaDirect(paint); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 62 | mMode = PaintUtils::getBlendModeDirect(paint); |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 63 | if (paint) { |
| 64 | mColorFilter = paint->refColorFilter(); |
| 65 | } else { |
| 66 | mColorFilter.reset(); |
| 67 | } |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 70 | void DeferredLayerUpdater::apply() { |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 71 | if (!mLayer) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 72 | mLayer = new Layer(mRenderState, mColorFilter, mAlpha, mMode); |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 73 | } |
| 74 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 75 | mLayer->setColorFilter(mColorFilter); |
| 76 | mLayer->setAlpha(mAlpha, mMode); |
| 77 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 78 | if (mSurfaceTexture.get()) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 79 | if (!mGLContextAttached) { |
| 80 | mGLContextAttached = true; |
| 81 | mUpdateTexImage = true; |
| 82 | mSurfaceTexture->attachToView(); |
Stan Iliev | 85f9096 | 2018-08-31 18:35:06 +0000 | [diff] [blame] | 83 | } |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 84 | if (mUpdateTexImage) { |
| 85 | mUpdateTexImage = false; |
| 86 | sk_sp<SkImage> layerImage; |
| 87 | SkMatrix textureTransform; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 88 | 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 Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 93 | layerImage = mSurfaceTexture->dequeueImage(textureTransform, &queueEmpty, |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 94 | 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 Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 99 | updateLayer(forceFilter, textureTransform, layerImage); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 103 | if (mTransform) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 104 | mLayer->getTransform() = *mTransform; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 105 | setTransform(nullptr); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 110 | void DeferredLayerUpdater::updateLayer(bool forceFilter, const SkMatrix& textureTransform, |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 111 | const sk_sp<SkImage>& layerImage) { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 112 | mLayer->setBlend(mBlend); |
| 113 | mLayer->setForceFilter(forceFilter); |
| 114 | mLayer->setSize(mWidth, mHeight); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 115 | mLayer->getTexTransform() = textureTransform; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 116 | mLayer->setImage(layerImage); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 117 | } |
| 118 | |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 119 | void DeferredLayerUpdater::detachSurfaceTexture() { |
| 120 | if (mSurfaceTexture.get()) { |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 121 | destroyLayer(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 122 | mSurfaceTexture = nullptr; |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 126 | } /* namespace uirenderer */ |
| 127 | } /* namespace android */ |