blob: f8009b330e06e03aa1342a90cf665207bfb85c54 [file] [log] [blame]
Mathias Agopian118d0242011-10-13 16:02:48 -07001/*
2 * Copyright (C) 2011 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
17#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
Mathias Agopiand3ee2312012-08-02 14:01:42 -070021#include <GLES/gl.h>
22#include <GLES/glext.h>
23
Mathias Agopian118d0242011-10-13 16:02:48 -070024#include <utils/Errors.h>
25#include <utils/Log.h>
26
27#include <ui/GraphicBuffer.h>
28
29#include "LayerScreenshot.h"
30#include "SurfaceFlinger.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070031#include "DisplayDevice.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070032
Mathias Agopian4a9ac372011-11-01 14:39:06 -070033
Mathias Agopian118d0242011-10-13 16:02:48 -070034namespace android {
35// ---------------------------------------------------------------------------
36
Mathias Agopian3ee454a2012-08-27 16:28:24 -070037LayerScreenshot::LayerScreenshot(SurfaceFlinger* flinger,
Mathias Agopian118d0242011-10-13 16:02:48 -070038 const sp<Client>& client)
Mathias Agopian3ee454a2012-08-27 16:28:24 -070039 : LayerBaseClient(flinger, client),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070040 mTextureName(0), mFlinger(flinger), mIsSecure(false)
Mathias Agopian118d0242011-10-13 16:02:48 -070041{
42}
43
44LayerScreenshot::~LayerScreenshot()
45{
46 if (mTextureName) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -070047 mFlinger->deleteTextureAsync(mTextureName);
Mathias Agopian118d0242011-10-13 16:02:48 -070048 }
49}
50
Mathias Agopian3ee454a2012-08-27 16:28:24 -070051status_t LayerScreenshot::captureLocked(int32_t layerStack) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -070052 GLfloat u, v;
Mathias Agopian3ee454a2012-08-27 16:28:24 -070053 status_t result = mFlinger->renderScreenToTextureLocked(layerStack,
54 &mTextureName, &u, &v);
Mathias Agopian4a9ac372011-11-01 14:39:06 -070055 if (result != NO_ERROR) {
56 return result;
57 }
58 initTexture(u, v);
Jamie Gennisdd3cb842012-10-19 18:19:11 -070059
60 // Currently screenshot always comes from the default display
61 mIsSecure = mFlinger->getDefaultDisplayDevice()->getSecureLayerVisible();
62
Mathias Agopian4a9ac372011-11-01 14:39:06 -070063 return NO_ERROR;
64}
65
Mathias Agopian118d0242011-10-13 16:02:48 -070066status_t LayerScreenshot::capture() {
67 GLfloat u, v;
68 status_t result = mFlinger->renderScreenToTexture(0, &mTextureName, &u, &v);
69 if (result != NO_ERROR) {
70 return result;
71 }
Mathias Agopian4a9ac372011-11-01 14:39:06 -070072 initTexture(u, v);
Jamie Gennisdd3cb842012-10-19 18:19:11 -070073
74 // Currently screenshot always comes from the default display
75 mIsSecure = mFlinger->getDefaultDisplayDevice()->getSecureLayerVisible();
76
Mathias Agopian4a9ac372011-11-01 14:39:06 -070077 return NO_ERROR;
78}
Mathias Agopian118d0242011-10-13 16:02:48 -070079
Mathias Agopian4a9ac372011-11-01 14:39:06 -070080void LayerScreenshot::initTexture(GLfloat u, GLfloat v) {
Mathias Agopian118d0242011-10-13 16:02:48 -070081 glBindTexture(GL_TEXTURE_2D, mTextureName);
82 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
83 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
Mathias Agopian118d0242011-10-13 16:02:48 -070084 mTexCoords[0] = 0; mTexCoords[1] = v;
85 mTexCoords[2] = 0; mTexCoords[3] = 0;
86 mTexCoords[4] = u; mTexCoords[5] = 0;
87 mTexCoords[6] = u; mTexCoords[7] = v;
Mathias Agopian4a9ac372011-11-01 14:39:06 -070088}
Mathias Agopian118d0242011-10-13 16:02:48 -070089
Mathias Agopian4a9ac372011-11-01 14:39:06 -070090void LayerScreenshot::initStates(uint32_t w, uint32_t h, uint32_t flags) {
91 LayerBaseClient::initStates(w, h, flags);
Mathias Agopian3165cc22012-08-08 19:42:09 -070092 if (!(flags & ISurfaceComposerClient::eHidden)) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -070093 capture();
94 }
Jamie Gennisdd3cb842012-10-19 18:19:11 -070095 if (flags & ISurfaceComposerClient::eSecure) {
96 ALOGW("ignoring surface flag eSecure - LayerScreenshot is considered "
97 "secure iff it captures the contents of a secure surface.");
98 }
Mathias Agopian4a9ac372011-11-01 14:39:06 -070099}
100
101uint32_t LayerScreenshot::doTransaction(uint32_t flags)
102{
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700103 const LayerBase::State& draw(drawingState());
104 const LayerBase::State& curr(currentState());
Mathias Agopian4a9ac372011-11-01 14:39:06 -0700105
Mathias Agopian3165cc22012-08-08 19:42:09 -0700106 if (draw.flags & layer_state_t::eLayerHidden) {
107 if (!(curr.flags & layer_state_t::eLayerHidden)) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -0700108 // we're going from hidden to visible
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700109 status_t err = captureLocked(curr.layerStack);
Mathias Agopian4a9ac372011-11-01 14:39:06 -0700110 if (err != NO_ERROR) {
Steve Block32397c12012-01-05 23:22:43 +0000111 ALOGW("createScreenshotSurface failed (%s)", strerror(-err));
Mathias Agopian4a9ac372011-11-01 14:39:06 -0700112 }
113 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700114 } else if (curr.flags & layer_state_t::eLayerHidden) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -0700115 // we're going from visible to hidden
116 if (mTextureName) {
117 glDeleteTextures(1, &mTextureName);
118 mTextureName = 0;
119 }
120 }
121 return LayerBaseClient::doTransaction(flags);
Mathias Agopian118d0242011-10-13 16:02:48 -0700122}
123
Mathias Agopian42977342012-08-05 00:40:46 -0700124void LayerScreenshot::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
Mathias Agopian118d0242011-10-13 16:02:48 -0700125{
126 const State& s(drawingState());
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700127 if (s.alpha>0) {
Mathias Agopian118d0242011-10-13 16:02:48 -0700128 const GLfloat alpha = s.alpha/255.0f;
Mathias Agopian42977342012-08-05 00:40:46 -0700129 const uint32_t fbHeight = hw->getHeight();
Mathias Agopian118d0242011-10-13 16:02:48 -0700130
131 if (s.alpha == 0xFF) {
132 glDisable(GL_BLEND);
Jesse Hallb763d5f2012-10-10 21:37:22 -0700133 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Mathias Agopian118d0242011-10-13 16:02:48 -0700134 } else {
135 glEnable(GL_BLEND);
Jesse Hallb763d5f2012-10-10 21:37:22 -0700136 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Jesse Hallb763d5f2012-10-10 21:37:22 -0700137 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Mathias Agopian118d0242011-10-13 16:02:48 -0700138 }
139
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700140 GLuint texName = mTextureName;
141 if (isSecure() && !hw->isSecure()) {
142 texName = mFlinger->getProtectedTexName();
143 }
144
Mathias Agopian4fec8732012-06-29 14:12:52 -0700145 LayerMesh mesh;
146 computeGeometry(hw, &mesh);
147
Jesse Hall837d2f92012-10-15 12:38:33 -0700148 glColor4f(alpha, alpha, alpha, alpha);
149
Mathias Agopian118d0242011-10-13 16:02:48 -0700150 glDisable(GL_TEXTURE_EXTERNAL_OES);
151 glEnable(GL_TEXTURE_2D);
152
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700153 glBindTexture(GL_TEXTURE_2D, texName);
Mathias Agopian118d0242011-10-13 16:02:48 -0700154 glMatrixMode(GL_TEXTURE);
155 glLoadIdentity();
156 glMatrixMode(GL_MODELVIEW);
157
158 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
159 glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700160 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
161 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
Mathias Agopian118d0242011-10-13 16:02:48 -0700162
163 glDisable(GL_BLEND);
164 glDisable(GL_TEXTURE_2D);
165 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
166 }
167}
168
169// ---------------------------------------------------------------------------
170
171}; // namespace android