Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | namespace android { |
| 31 | // --------------------------------------------------------------------------- |
| 32 | |
| 33 | LayerScreenshot::LayerScreenshot(SurfaceFlinger* flinger, DisplayID display, |
| 34 | const sp<Client>& client) |
| 35 | : LayerBaseClient(flinger, display, client), |
| 36 | mTextureName(0), mFlinger(flinger) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | LayerScreenshot::~LayerScreenshot() |
| 41 | { |
| 42 | if (mTextureName) { |
| 43 | mFlinger->postMessageAsync( |
| 44 | new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | status_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 | |
| 67 | void 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 | |
| 77 | if (s.alpha == 0xFF) { |
| 78 | glDisable(GL_BLEND); |
| 79 | } else { |
| 80 | glEnable(GL_BLEND); |
| 81 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 82 | } |
| 83 | |
| 84 | glColor4f(0, 0, 0, alpha); |
| 85 | |
| 86 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 87 | glEnable(GL_TEXTURE_2D); |
| 88 | |
| 89 | glBindTexture(GL_TEXTURE_2D, mTextureName); |
| 90 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 91 | glMatrixMode(GL_TEXTURE); |
| 92 | glLoadIdentity(); |
| 93 | glMatrixMode(GL_MODELVIEW); |
| 94 | |
| 95 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 96 | glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords); |
| 97 | glVertexPointer(2, GL_FLOAT, 0, mVertices); |
| 98 | |
| 99 | while (it != end) { |
| 100 | const Rect& r = *it++; |
| 101 | const GLint sy = fbHeight - (r.top + r.height()); |
| 102 | glScissor(r.left, sy, r.width(), r.height()); |
| 103 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 104 | } |
| 105 | |
| 106 | glDisable(GL_BLEND); |
| 107 | glDisable(GL_TEXTURE_2D); |
| 108 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // --------------------------------------------------------------------------- |
| 113 | |
| 114 | }; // namespace android |