blob: a6d7e78b87bf4d7c428ae978e4b7f30b0919fed5 [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
18#include "OpenGLRenderer.h"
19
20#include "LayerRenderer.h"
John Reck749906b2014-10-03 15:02:19 -070021#include "renderthread/EglManager.h"
22#include "renderthread/RenderTask.h"
John Reck04fc5832014-02-05 16:38:25 -080023
24namespace android {
25namespace uirenderer {
26
John Reck749906b2014-10-03 15:02:19 -070027class DeleteLayerTask : public renderthread::RenderTask {
28public:
29 DeleteLayerTask(renderthread::EglManager& eglManager, Layer* layer)
30 : mEglManager(eglManager)
31 , mLayer(layer)
32 {}
John Reckd72e0a32014-05-29 18:56:11 -070033
John Reck749906b2014-10-03 15:02:19 -070034 virtual void run() {
35 mEglManager.requireGlContext();
36 LayerRenderer::destroyLayer(mLayer);
37 mLayer = 0;
38 delete this;
39 }
40
41private:
42 renderthread::EglManager& mEglManager;
43 Layer* mLayer;
44};
45
46DeferredLayerUpdater::DeferredLayerUpdater(renderthread::RenderThread& thread, Layer* layer)
John Reck25fbb3f2014-06-12 13:46:45 -070047 : mSurfaceTexture(0)
John Reck04fc5832014-02-05 16:38:25 -080048 , mTransform(0)
49 , mNeedsGLContextAttach(false)
50 , mUpdateTexImage(false)
51 , mLayer(layer)
John Reckd72e0a32014-05-29 18:56:11 -070052 , mCaches(Caches::getInstance())
John Reck749906b2014-10-03 15:02:19 -070053 , mRenderThread(thread) {
John Reck04fc5832014-02-05 16:38:25 -080054 mWidth = mLayer->layer.getWidth();
55 mHeight = mLayer->layer.getHeight();
56 mBlend = mLayer->isBlend();
Derek Sollenberger674554f2014-02-19 16:47:32 +000057 mColorFilter = SkSafeRef(mLayer->getColorFilter());
John Reck04fc5832014-02-05 16:38:25 -080058 mAlpha = mLayer->getAlpha();
59 mMode = mLayer->getMode();
John Reck04fc5832014-02-05 16:38:25 -080060}
61
62DeferredLayerUpdater::~DeferredLayerUpdater() {
Derek Sollenberger674554f2014-02-19 16:47:32 +000063 SkSafeUnref(mColorFilter);
John Reck8ca3eec2014-04-10 10:28:45 -070064 setTransform(0);
John Reck749906b2014-10-03 15:02:19 -070065 mRenderThread.queue(new DeleteLayerTask(mRenderThread.eglManager(), mLayer));
66 mLayer = 0;
John Reck04fc5832014-02-05 16:38:25 -080067}
68
Derek Sollenberger674554f2014-02-19 16:47:32 +000069void DeferredLayerUpdater::setPaint(const SkPaint* paint) {
70 OpenGLRenderer::getAlphaAndModeDirect(paint, &mAlpha, &mMode);
71 SkColorFilter* colorFilter = (paint) ? paint->getColorFilter() : NULL;
John Reck04fc5832014-02-05 16:38:25 -080072 SkRefCnt_SafeAssign(mColorFilter, colorFilter);
73}
74
John Reck68bfe0a2014-06-24 15:34:58 -070075bool DeferredLayerUpdater::apply() {
John Reck04fc5832014-02-05 16:38:25 -080076 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 Reck25fbb3f2014-06-12 13:46:45 -070081 if (mSurfaceTexture.get()) {
John Reck04fc5832014-02-05 16:38:25 -080082 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
98void 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 Craik9757ac02014-02-25 18:50:17 -0800114
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 Reck04fc5832014-02-05 16:38:25 -0800123 #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 Craik9757ac02014-02-25 18:50:17 -0800131 LayerRenderer::updateTextureLayer(mLayer, mWidth, mHeight,
132 !mBlend, forceFilter, renderTarget, transform);
John Reck04fc5832014-02-05 16:38:25 -0800133 }
134}
135
John Reck918ad522014-06-27 14:45:25 -0700136void DeferredLayerUpdater::detachSurfaceTexture() {
137 if (mSurfaceTexture.get()) {
John Reck749906b2014-10-03 15:02:19 -0700138 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 Reck918ad522014-06-27 14:45:25 -0700144 mSurfaceTexture = 0;
145 mLayer->clearTexture();
146 }
147}
148
John Reck04fc5832014-02-05 16:38:25 -0800149} /* namespace uirenderer */
150} /* namespace android */