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