blob: 5c18ebd53056f7fadb1fb9d2bea5680e25243832 [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),
Mathias Agopian118d0242011-10-13 16:02:48 -070040 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 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);
59 return NO_ERROR;
60}
61
Mathias Agopian118d0242011-10-13 16:02:48 -070062status_t LayerScreenshot::capture() {
63 GLfloat u, v;
64 status_t result = mFlinger->renderScreenToTexture(0, &mTextureName, &u, &v);
65 if (result != NO_ERROR) {
66 return result;
67 }
Mathias Agopian4a9ac372011-11-01 14:39:06 -070068 initTexture(u, v);
69 return NO_ERROR;
70}
Mathias Agopian118d0242011-10-13 16:02:48 -070071
Mathias Agopian4a9ac372011-11-01 14:39:06 -070072void LayerScreenshot::initTexture(GLfloat u, GLfloat v) {
Mathias Agopian118d0242011-10-13 16:02:48 -070073 glBindTexture(GL_TEXTURE_2D, mTextureName);
74 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
75 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
Mathias Agopian118d0242011-10-13 16:02:48 -070076 mTexCoords[0] = 0; mTexCoords[1] = v;
77 mTexCoords[2] = 0; mTexCoords[3] = 0;
78 mTexCoords[4] = u; mTexCoords[5] = 0;
79 mTexCoords[6] = u; mTexCoords[7] = v;
Mathias Agopian4a9ac372011-11-01 14:39:06 -070080}
Mathias Agopian118d0242011-10-13 16:02:48 -070081
Mathias Agopian4a9ac372011-11-01 14:39:06 -070082void LayerScreenshot::initStates(uint32_t w, uint32_t h, uint32_t flags) {
83 LayerBaseClient::initStates(w, h, flags);
Mathias Agopian3165cc22012-08-08 19:42:09 -070084 if (!(flags & ISurfaceComposerClient::eHidden)) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -070085 capture();
86 }
87}
88
89uint32_t LayerScreenshot::doTransaction(uint32_t flags)
90{
Mathias Agopian921e6ac2012-07-23 23:11:29 -070091 const LayerBase::State& draw(drawingState());
92 const LayerBase::State& curr(currentState());
Mathias Agopian4a9ac372011-11-01 14:39:06 -070093
Mathias Agopian3165cc22012-08-08 19:42:09 -070094 if (draw.flags & layer_state_t::eLayerHidden) {
95 if (!(curr.flags & layer_state_t::eLayerHidden)) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -070096 // we're going from hidden to visible
Mathias Agopian3ee454a2012-08-27 16:28:24 -070097 status_t err = captureLocked(curr.layerStack);
Mathias Agopian4a9ac372011-11-01 14:39:06 -070098 if (err != NO_ERROR) {
Steve Block32397c12012-01-05 23:22:43 +000099 ALOGW("createScreenshotSurface failed (%s)", strerror(-err));
Mathias Agopian4a9ac372011-11-01 14:39:06 -0700100 }
101 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700102 } else if (curr.flags & layer_state_t::eLayerHidden) {
Mathias Agopian4a9ac372011-11-01 14:39:06 -0700103 // we're going from visible to hidden
104 if (mTextureName) {
105 glDeleteTextures(1, &mTextureName);
106 mTextureName = 0;
107 }
108 }
109 return LayerBaseClient::doTransaction(flags);
Mathias Agopian118d0242011-10-13 16:02:48 -0700110}
111
Mathias Agopian42977342012-08-05 00:40:46 -0700112void LayerScreenshot::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
Mathias Agopian118d0242011-10-13 16:02:48 -0700113{
114 const State& s(drawingState());
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700115 if (s.alpha>0) {
Mathias Agopian118d0242011-10-13 16:02:48 -0700116 const GLfloat alpha = s.alpha/255.0f;
Mathias Agopian42977342012-08-05 00:40:46 -0700117 const uint32_t fbHeight = hw->getHeight();
Mathias Agopian118d0242011-10-13 16:02:48 -0700118
119 if (s.alpha == 0xFF) {
120 glDisable(GL_BLEND);
121 } else {
122 glEnable(GL_BLEND);
123 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
124 }
125
Mathias Agopian4fec8732012-06-29 14:12:52 -0700126 LayerMesh mesh;
127 computeGeometry(hw, &mesh);
128
Mathias Agopian118d0242011-10-13 16:02:48 -0700129 glColor4f(0, 0, 0, alpha);
130
131 glDisable(GL_TEXTURE_EXTERNAL_OES);
132 glEnable(GL_TEXTURE_2D);
133
134 glBindTexture(GL_TEXTURE_2D, mTextureName);
135 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
136 glMatrixMode(GL_TEXTURE);
137 glLoadIdentity();
138 glMatrixMode(GL_MODELVIEW);
139
140 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
141 glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700142 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
143 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
Mathias Agopian118d0242011-10-13 16:02:48 -0700144
145 glDisable(GL_BLEND);
146 glDisable(GL_TEXTURE_2D);
147 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
148 }
149}
150
151// ---------------------------------------------------------------------------
152
153}; // namespace android