blob: f2bf19d299c5a4a931e52108182538f9c66614b0 [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) {
44 mFlinger->postMessageAsync(
45 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
46 }
47}
48
Mathias Agopian4a9ac372011-11-01 14:39:06 -070049status_t LayerScreenshot::captureLocked() {
50 GLfloat u, v;
51 status_t result = mFlinger->renderScreenToTextureLocked(0, &mTextureName, &u, &v);
52 if (result != NO_ERROR) {
53 return result;
54 }
55 initTexture(u, v);
56 return NO_ERROR;
57}
58
Mathias Agopian118d0242011-10-13 16:02:48 -070059status_t LayerScreenshot::capture() {
60 GLfloat u, v;
61 status_t result = mFlinger->renderScreenToTexture(0, &mTextureName, &u, &v);
62 if (result != NO_ERROR) {
63 return result;
64 }
Mathias Agopian4a9ac372011-11-01 14:39:06 -070065 initTexture(u, v);
66 return NO_ERROR;
67}
Mathias Agopian118d0242011-10-13 16:02:48 -070068
Mathias Agopian4a9ac372011-11-01 14:39:06 -070069void LayerScreenshot::initTexture(GLfloat u, GLfloat v) {
Mathias Agopian118d0242011-10-13 16:02:48 -070070 glBindTexture(GL_TEXTURE_2D, mTextureName);
71 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
72 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
Mathias Agopian118d0242011-10-13 16:02:48 -070073 mTexCoords[0] = 0; mTexCoords[1] = v;
74 mTexCoords[2] = 0; mTexCoords[3] = 0;
75 mTexCoords[4] = u; mTexCoords[5] = 0;
76 mTexCoords[6] = u; mTexCoords[7] = v;
Mathias Agopian4a9ac372011-11-01 14:39:06 -070077}
Mathias Agopian118d0242011-10-13 16:02:48 -070078
Mathias Agopian4a9ac372011-11-01 14:39:06 -070079void LayerScreenshot::initStates(uint32_t w, uint32_t h, uint32_t flags) {
80 LayerBaseClient::initStates(w, h, flags);
81 if (!(flags & ISurfaceComposer::eHidden)) {
82 capture();
83 }
84}
85
86uint32_t LayerScreenshot::doTransaction(uint32_t flags)
87{
88 const Layer::State& draw(drawingState());
89 const Layer::State& curr(currentState());
90
91 if (draw.flags & ISurfaceComposer::eLayerHidden) {
92 if (!(curr.flags & ISurfaceComposer::eLayerHidden)) {
93 // we're going from hidden to visible
94 status_t err = captureLocked();
95 if (err != NO_ERROR) {
Steve Block32397c12012-01-05 23:22:43 +000096 ALOGW("createScreenshotSurface failed (%s)", strerror(-err));
Mathias Agopian4a9ac372011-11-01 14:39:06 -070097 }
98 }
99 } else if (curr.flags & ISurfaceComposer::eLayerHidden) {
100 // we're going from visible to hidden
101 if (mTextureName) {
102 glDeleteTextures(1, &mTextureName);
103 mTextureName = 0;
104 }
105 }
106 return LayerBaseClient::doTransaction(flags);
Mathias Agopian118d0242011-10-13 16:02:48 -0700107}
108
Mathias Agopian1b031492012-06-20 17:51:20 -0700109void LayerScreenshot::onDraw(const DisplayHardware& hw, const Region& clip) const
Mathias Agopian118d0242011-10-13 16:02:48 -0700110{
111 const State& s(drawingState());
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700112 if (s.alpha>0) {
Mathias Agopian118d0242011-10-13 16:02:48 -0700113 const GLfloat alpha = s.alpha/255.0f;
114 const uint32_t fbHeight = hw.getHeight();
115
116 if (s.alpha == 0xFF) {
117 glDisable(GL_BLEND);
118 } else {
119 glEnable(GL_BLEND);
120 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
121 }
122
Mathias Agopian4fec8732012-06-29 14:12:52 -0700123 LayerMesh mesh;
124 computeGeometry(hw, &mesh);
125
Mathias Agopian118d0242011-10-13 16:02:48 -0700126 glColor4f(0, 0, 0, alpha);
127
128 glDisable(GL_TEXTURE_EXTERNAL_OES);
129 glEnable(GL_TEXTURE_2D);
130
131 glBindTexture(GL_TEXTURE_2D, mTextureName);
132 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
133 glMatrixMode(GL_TEXTURE);
134 glLoadIdentity();
135 glMatrixMode(GL_MODELVIEW);
136
137 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
138 glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700139 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
140 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
Mathias Agopian118d0242011-10-13 16:02:48 -0700141
142 glDisable(GL_BLEND);
143 glDisable(GL_TEXTURE_2D);
144 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
145 }
146}
147
148// ---------------------------------------------------------------------------
149
150}; // namespace android