blob: bed820686a99a817645acdb3c9e1b35487c440e8 [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
21#include <utils/Errors.h>
22#include <utils/Log.h>
23
24#include <ui/GraphicBuffer.h>
25
26#include "LayerScreenshot.h"
27#include "SurfaceFlinger.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070028#include "DisplayHardware.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070029
Mathias Agopian4a9ac372011-11-01 14:39:06 -070030
Mathias Agopian118d0242011-10-13 16:02:48 -070031namespace android {
32// ---------------------------------------------------------------------------
33
34LayerScreenshot::LayerScreenshot(SurfaceFlinger* flinger, DisplayID display,
35 const sp<Client>& client)
36 : LayerBaseClient(flinger, display, client),
37 mTextureName(0), mFlinger(flinger)
38{
39}
40
41LayerScreenshot::~LayerScreenshot()
42{
43 if (mTextureName) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044 mFlinger->deleteTextureAsync(mTextureName);
Mathias Agopian118d0242011-10-13 16:02:48 -070045 }
46}
47
Mathias Agopian4a9ac372011-11-01 14:39:06 -070048status_t LayerScreenshot::captureLocked() {
49 GLfloat u, v;
50 status_t result = mFlinger->renderScreenToTextureLocked(0, &mTextureName, &u, &v);
51 if (result != NO_ERROR) {
52 return result;
53 }
54 initTexture(u, v);
55 return NO_ERROR;
56}
57
Mathias Agopian118d0242011-10-13 16:02:48 -070058status_t LayerScreenshot::capture() {
59 GLfloat u, v;
60 status_t result = mFlinger->renderScreenToTexture(0, &mTextureName, &u, &v);
61 if (result != NO_ERROR) {
62 return result;
63 }
Mathias Agopian4a9ac372011-11-01 14:39:06 -070064 initTexture(u, v);
65 return NO_ERROR;
66}
Mathias Agopian118d0242011-10-13 16:02:48 -070067
Mathias Agopian4a9ac372011-11-01 14:39:06 -070068void LayerScreenshot::initTexture(GLfloat u, GLfloat v) {
Mathias Agopian118d0242011-10-13 16:02:48 -070069 glBindTexture(GL_TEXTURE_2D, mTextureName);
70 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
71 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
Mathias Agopian118d0242011-10-13 16:02:48 -070072 mTexCoords[0] = 0; mTexCoords[1] = v;
73 mTexCoords[2] = 0; mTexCoords[3] = 0;
74 mTexCoords[4] = u; mTexCoords[5] = 0;
75 mTexCoords[6] = u; mTexCoords[7] = v;
Mathias Agopian4a9ac372011-11-01 14:39:06 -070076}
Mathias Agopian118d0242011-10-13 16:02:48 -070077
Mathias Agopian4a9ac372011-11-01 14:39:06 -070078void LayerScreenshot::initStates(uint32_t w, uint32_t h, uint32_t flags) {
79 LayerBaseClient::initStates(w, h, flags);
80 if (!(flags & ISurfaceComposer::eHidden)) {
81 capture();
82 }
83}
84
85uint32_t LayerScreenshot::doTransaction(uint32_t flags)
86{
Mathias Agopian921e6ac2012-07-23 23:11:29 -070087 const LayerBase::State& draw(drawingState());
88 const LayerBase::State& curr(currentState());
Mathias Agopian4a9ac372011-11-01 14:39:06 -070089
90 if (draw.flags & ISurfaceComposer::eLayerHidden) {
91 if (!(curr.flags & ISurfaceComposer::eLayerHidden)) {
92 // we're going from hidden to visible
93 status_t err = captureLocked();
94 if (err != NO_ERROR) {
Steve Block32397c12012-01-05 23:22:43 +000095 ALOGW("createScreenshotSurface failed (%s)", strerror(-err));
Mathias Agopian4a9ac372011-11-01 14:39:06 -070096 }
97 }
98 } else if (curr.flags & ISurfaceComposer::eLayerHidden) {
99 // we're going from visible to hidden
100 if (mTextureName) {
101 glDeleteTextures(1, &mTextureName);
102 mTextureName = 0;
103 }
104 }
105 return LayerBaseClient::doTransaction(flags);
Mathias Agopian118d0242011-10-13 16:02:48 -0700106}
107
Mathias Agopian1b031492012-06-20 17:51:20 -0700108void LayerScreenshot::onDraw(const DisplayHardware& hw, const Region& clip) const
Mathias Agopian118d0242011-10-13 16:02:48 -0700109{
110 const State& s(drawingState());
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700111 if (s.alpha>0) {
Mathias Agopian118d0242011-10-13 16:02:48 -0700112 const GLfloat alpha = s.alpha/255.0f;
113 const uint32_t fbHeight = hw.getHeight();
114
115 if (s.alpha == 0xFF) {
116 glDisable(GL_BLEND);
117 } else {
118 glEnable(GL_BLEND);
119 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
120 }
121
Mathias Agopian4fec8732012-06-29 14:12:52 -0700122 LayerMesh mesh;
123 computeGeometry(hw, &mesh);
124
Mathias Agopian118d0242011-10-13 16:02:48 -0700125 glColor4f(0, 0, 0, alpha);
126
127 glDisable(GL_TEXTURE_EXTERNAL_OES);
128 glEnable(GL_TEXTURE_2D);
129
130 glBindTexture(GL_TEXTURE_2D, mTextureName);
131 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
132 glMatrixMode(GL_TEXTURE);
133 glLoadIdentity();
134 glMatrixMode(GL_MODELVIEW);
135
136 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
137 glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700138 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
139 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
Mathias Agopian118d0242011-10-13 16:02:48 -0700140
141 glDisable(GL_BLEND);
142 glDisable(GL_TEXTURE_2D);
143 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
144 }
145}
146
147// ---------------------------------------------------------------------------
148
149}; // namespace android