blob: 8945e466d38dc422030fc5b0b993259fd6846300 [file] [log] [blame]
Mathias Agopianf171ab62011-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"
28#include "DisplayHardware/DisplayHardware.h"
29
30namespace android {
31// ---------------------------------------------------------------------------
32
33LayerScreenshot::LayerScreenshot(SurfaceFlinger* flinger, DisplayID display,
34 const sp<Client>& client)
35 : LayerBaseClient(flinger, display, client),
36 mTextureName(0), mFlinger(flinger)
37{
38}
39
40LayerScreenshot::~LayerScreenshot()
41{
42 if (mTextureName) {
43 mFlinger->postMessageAsync(
44 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
45 }
46}
47
48status_t LayerScreenshot::capture() {
49 GLfloat u, v;
50 status_t result = mFlinger->renderScreenToTexture(0, &mTextureName, &u, &v);
51 if (result != NO_ERROR) {
52 return result;
53 }
54
55 glBindTexture(GL_TEXTURE_2D, mTextureName);
56 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
57 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
58
59 mTexCoords[0] = 0; mTexCoords[1] = v;
60 mTexCoords[2] = 0; mTexCoords[3] = 0;
61 mTexCoords[4] = u; mTexCoords[5] = 0;
62 mTexCoords[6] = u; mTexCoords[7] = v;
63
64 return NO_ERROR;
65}
66
67void LayerScreenshot::onDraw(const Region& clip) const
68{
69 const State& s(drawingState());
70 Region::const_iterator it = clip.begin();
71 Region::const_iterator const end = clip.end();
72 if (s.alpha>0 && (it != end)) {
73 const DisplayHardware& hw(graphicPlane(0).displayHardware());
74 const GLfloat alpha = s.alpha/255.0f;
75 const uint32_t fbHeight = hw.getHeight();
76 glDisable(GL_DITHER);
77
78 if (s.alpha == 0xFF) {
79 glDisable(GL_BLEND);
80 } else {
81 glEnable(GL_BLEND);
82 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
83 }
84
85 glColor4f(0, 0, 0, alpha);
86
87#if defined(GL_OES_EGL_image_external)
88 if (GLExtensions::getInstance().haveTextureExternal()) {
89 glDisable(GL_TEXTURE_EXTERNAL_OES);
90 }
91#endif
92
93 glBindTexture(GL_TEXTURE_2D, mTextureName);
94 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
95 glEnable(GL_TEXTURE_2D);
96 glMatrixMode(GL_TEXTURE);
97 glLoadIdentity();
98 glMatrixMode(GL_MODELVIEW);
99
100 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
101 glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords);
102 glVertexPointer(2, GL_FLOAT, 0, mVertices);
103
104 while (it != end) {
105 const Rect& r = *it++;
106 const GLint sy = fbHeight - (r.top + r.height());
107 glScissor(r.left, sy, r.width(), r.height());
108 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
109 }
110 }
111 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
112}
113
114// ---------------------------------------------------------------------------
115
116}; // namespace android