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 | |
| 18 | #include "OpenGLRenderer.h" |
| 19 | |
| 20 | #include "LayerRenderer.h" |
John Reck | 749906b | 2014-10-03 15:02:19 -0700 | [diff] [blame^] | 21 | #include "renderthread/EglManager.h" |
| 22 | #include "renderthread/RenderTask.h" |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
John Reck | 749906b | 2014-10-03 15:02:19 -0700 | [diff] [blame^] | 27 | class DeleteLayerTask : public renderthread::RenderTask { |
| 28 | public: |
| 29 | DeleteLayerTask(renderthread::EglManager& eglManager, Layer* layer) |
| 30 | : mEglManager(eglManager) |
| 31 | , mLayer(layer) |
| 32 | {} |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 33 | |
John Reck | 749906b | 2014-10-03 15:02:19 -0700 | [diff] [blame^] | 34 | virtual void run() { |
| 35 | mEglManager.requireGlContext(); |
| 36 | LayerRenderer::destroyLayer(mLayer); |
| 37 | mLayer = 0; |
| 38 | delete this; |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | renderthread::EglManager& mEglManager; |
| 43 | Layer* mLayer; |
| 44 | }; |
| 45 | |
| 46 | DeferredLayerUpdater::DeferredLayerUpdater(renderthread::RenderThread& thread, Layer* layer) |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 47 | : mSurfaceTexture(0) |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 48 | , mTransform(0) |
| 49 | , mNeedsGLContextAttach(false) |
| 50 | , mUpdateTexImage(false) |
| 51 | , mLayer(layer) |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 52 | , mCaches(Caches::getInstance()) |
John Reck | 749906b | 2014-10-03 15:02:19 -0700 | [diff] [blame^] | 53 | , mRenderThread(thread) { |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 54 | mWidth = mLayer->layer.getWidth(); |
| 55 | mHeight = mLayer->layer.getHeight(); |
| 56 | mBlend = mLayer->isBlend(); |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 57 | mColorFilter = SkSafeRef(mLayer->getColorFilter()); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 58 | mAlpha = mLayer->getAlpha(); |
| 59 | mMode = mLayer->getMode(); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | DeferredLayerUpdater::~DeferredLayerUpdater() { |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 63 | SkSafeUnref(mColorFilter); |
John Reck | 8ca3eec | 2014-04-10 10:28:45 -0700 | [diff] [blame] | 64 | setTransform(0); |
John Reck | 749906b | 2014-10-03 15:02:19 -0700 | [diff] [blame^] | 65 | mRenderThread.queue(new DeleteLayerTask(mRenderThread.eglManager(), mLayer)); |
| 66 | mLayer = 0; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 69 | void DeferredLayerUpdater::setPaint(const SkPaint* paint) { |
| 70 | OpenGLRenderer::getAlphaAndModeDirect(paint, &mAlpha, &mMode); |
| 71 | SkColorFilter* colorFilter = (paint) ? paint->getColorFilter() : NULL; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 72 | SkRefCnt_SafeAssign(mColorFilter, colorFilter); |
| 73 | } |
| 74 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 75 | bool DeferredLayerUpdater::apply() { |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 76 | bool success = true; |
| 77 | // These properties are applied the same to both layer types |
| 78 | mLayer->setColorFilter(mColorFilter); |
| 79 | mLayer->setAlpha(mAlpha, mMode); |
| 80 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 81 | if (mSurfaceTexture.get()) { |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 82 | if (mNeedsGLContextAttach) { |
| 83 | mNeedsGLContextAttach = false; |
| 84 | mSurfaceTexture->attachToContext(mLayer->getTexture()); |
| 85 | } |
| 86 | if (mUpdateTexImage) { |
| 87 | mUpdateTexImage = false; |
| 88 | doUpdateTexImage(); |
| 89 | } |
| 90 | if (mTransform) { |
| 91 | mLayer->getTransform().load(*mTransform); |
| 92 | setTransform(0); |
| 93 | } |
| 94 | } |
| 95 | return success; |
| 96 | } |
| 97 | |
| 98 | void DeferredLayerUpdater::doUpdateTexImage() { |
| 99 | if (mSurfaceTexture->updateTexImage() == NO_ERROR) { |
| 100 | float transform[16]; |
| 101 | |
| 102 | int64_t frameNumber = mSurfaceTexture->getFrameNumber(); |
| 103 | // If the GLConsumer queue is in synchronous mode, need to discard all |
| 104 | // but latest frame, using the frame number to tell when we no longer |
| 105 | // have newer frames to target. Since we can't tell which mode it is in, |
| 106 | // do this unconditionally. |
| 107 | int dropCounter = 0; |
| 108 | while (mSurfaceTexture->updateTexImage() == NO_ERROR) { |
| 109 | int64_t newFrameNumber = mSurfaceTexture->getFrameNumber(); |
| 110 | if (newFrameNumber == frameNumber) break; |
| 111 | frameNumber = newFrameNumber; |
| 112 | dropCounter++; |
| 113 | } |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 114 | |
| 115 | bool forceFilter = false; |
| 116 | sp<GraphicBuffer> buffer = mSurfaceTexture->getCurrentBuffer(); |
| 117 | if (buffer != NULL) { |
| 118 | // force filtration if buffer size != layer size |
| 119 | forceFilter = mWidth != buffer->getWidth() |
| 120 | || mHeight != buffer->getHeight(); |
| 121 | } |
| 122 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 123 | #if DEBUG_RENDERER |
| 124 | if (dropCounter > 0) { |
| 125 | RENDERER_LOGD("Dropped %d frames on texture layer update", dropCounter); |
| 126 | } |
| 127 | #endif |
| 128 | mSurfaceTexture->getTransformMatrix(transform); |
| 129 | GLenum renderTarget = mSurfaceTexture->getCurrentTextureTarget(); |
| 130 | |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 131 | LayerRenderer::updateTextureLayer(mLayer, mWidth, mHeight, |
| 132 | !mBlend, forceFilter, renderTarget, transform); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 136 | void DeferredLayerUpdater::detachSurfaceTexture() { |
| 137 | if (mSurfaceTexture.get()) { |
John Reck | 749906b | 2014-10-03 15:02:19 -0700 | [diff] [blame^] | 138 | mRenderThread.eglManager().requireGlContext(); |
| 139 | status_t err = mSurfaceTexture->detachFromContext(); |
| 140 | if (err != 0) { |
| 141 | // TODO: Elevate to fatal exception |
| 142 | ALOGE("Failed to detach SurfaceTexture from context %d", err); |
| 143 | } |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 144 | mSurfaceTexture = 0; |
| 145 | mLayer->clearTexture(); |
| 146 | } |
| 147 | } |
| 148 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 149 | } /* namespace uirenderer */ |
| 150 | } /* namespace android */ |