blob: 799ecdcb75d211474e00ddb46fa8c5aebfebe823 [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
37LayerScreenshot::LayerScreenshot(SurfaceFlinger* flinger, DisplayID display,
38 const sp<Client>& client)
39 : LayerBaseClient(flinger, display, client),
40 mTextureName(0), mFlinger(flinger)
41{
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 Agopian4a9ac372011-11-01 14:39:06 -070051status_t LayerScreenshot::captureLocked() {
52 GLfloat u, v;
53 status_t result = mFlinger->renderScreenToTextureLocked(0, &mTextureName, &u, &v);
54 if (result != NO_ERROR) {
55 return result;
56 }
57 initTexture(u, v);
58 return NO_ERROR;
59}
60
Mathias Agopian118d0242011-10-13 16:02:48 -070061status_t LayerScreenshot::capture() {
62 GLfloat u, v;
63 status_t result = mFlinger->renderScreenToTexture(0, &mTextureName, &u, &v);
64 if (result != NO_ERROR) {
65 return result;
66 }
Mathias Agopian4a9ac372011-11-01 14:39:06 -070067 initTexture(u, v);
68 return NO_ERROR;
69}
Mathias Agopian118d0242011-10-13 16:02:48 -070070
Mathias Agopian4a9ac372011-11-01 14:39:06 -070071void LayerScreenshot::initTexture(GLfloat u, GLfloat v) {
Mathias Agopian118d0242011-10-13 16:02:48 -070072 glBindTexture(GL_TEXTURE_2D, mTextureName);
73 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
74 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
Mathias Agopian118d0242011-10-13 16:02:48 -070075 mTexCoords[0] = 0; mTexCoords[1] = v;
76 mTexCoords[2] = 0; mTexCoords[3] = 0;
77 mTexCoords[4] = u; mTexCoords[5] = 0;
78 mTexCoords[6] = u; mTexCoords[7] = v;
Mathias Agopian4a9ac372011-11-01 14:39:06 -070079}
Mathias Agopian118d0242011-10-13 16:02:48 -070080
Mathias Agopian4a9ac372011-11-01 14:39:06 -070081void LayerScreenshot::initStates(uint32_t w, uint32_t h, uint32_t flags) {
82 LayerBaseClient::initStates(w, h, flags);
Mathias Agopian3165cc22012-08-08 19:42:09 -070083 if (!(flags & ISurfaceComposerClient::eHidden)) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -070084 capture();
85 }
86}
87
88uint32_t LayerScreenshot::doTransaction(uint32_t flags)
89{
Mathias Agopian921e6ac2012-07-23 23:11:29 -070090 const LayerBase::State& draw(drawingState());
91 const LayerBase::State& curr(currentState());
Mathias Agopian4a9ac372011-11-01 14:39:06 -070092
Mathias Agopian3165cc22012-08-08 19:42:09 -070093 if (draw.flags & layer_state_t::eLayerHidden) {
94 if (!(curr.flags & layer_state_t::eLayerHidden)) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -070095 // we're going from hidden to visible
96 status_t err = captureLocked();
97 if (err != NO_ERROR) {
Steve Block32397c12012-01-05 23:22:43 +000098 ALOGW("createScreenshotSurface failed (%s)", strerror(-err));
Mathias Agopian4a9ac372011-11-01 14:39:06 -070099 }
100 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700101 } else if (curr.flags & layer_state_t::eLayerHidden) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -0700102 // we're going from visible to hidden
103 if (mTextureName) {
104 glDeleteTextures(1, &mTextureName);
105 mTextureName = 0;
106 }
107 }
108 return LayerBaseClient::doTransaction(flags);
Mathias Agopian118d0242011-10-13 16:02:48 -0700109}
110
Mathias Agopian42977342012-08-05 00:40:46 -0700111void LayerScreenshot::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
Mathias Agopian118d0242011-10-13 16:02:48 -0700112{
113 const State& s(drawingState());
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700114 if (s.alpha>0) {
Mathias Agopian118d0242011-10-13 16:02:48 -0700115 const GLfloat alpha = s.alpha/255.0f;
Mathias Agopian42977342012-08-05 00:40:46 -0700116 const uint32_t fbHeight = hw->getHeight();
Mathias Agopian118d0242011-10-13 16:02:48 -0700117
118 if (s.alpha == 0xFF) {
119 glDisable(GL_BLEND);
120 } else {
121 glEnable(GL_BLEND);
122 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
123 }
124
Mathias Agopian4fec8732012-06-29 14:12:52 -0700125 LayerMesh mesh;
126 computeGeometry(hw, &mesh);
127
Mathias Agopian118d0242011-10-13 16:02:48 -0700128 glColor4f(0, 0, 0, alpha);
129
130 glDisable(GL_TEXTURE_EXTERNAL_OES);
131 glEnable(GL_TEXTURE_2D);
132
133 glBindTexture(GL_TEXTURE_2D, mTextureName);
134 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
135 glMatrixMode(GL_TEXTURE);
136 glLoadIdentity();
137 glMatrixMode(GL_MODELVIEW);
138
139 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
140 glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700141 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
142 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
Mathias Agopian118d0242011-10-13 16:02:48 -0700143
144 glDisable(GL_BLEND);
145 glDisable(GL_TEXTURE_2D);
146 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
147 }
148}
149
150// ---------------------------------------------------------------------------
151
152}; // namespace android