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" |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 28 | #include "DisplayHardware.h" |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 29 | |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 30 | |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | // --------------------------------------------------------------------------- |
| 33 | |
| 34 | LayerScreenshot::LayerScreenshot(SurfaceFlinger* flinger, DisplayID display, |
| 35 | const sp<Client>& client) |
| 36 | : LayerBaseClient(flinger, display, client), |
| 37 | mTextureName(0), mFlinger(flinger) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | LayerScreenshot::~LayerScreenshot() |
| 42 | { |
| 43 | if (mTextureName) { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 44 | mFlinger->deleteTextureAsync(mTextureName); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 48 | status_t LayerScreenshot::captureLocked() { |
| 49 | GLfloat u, v; |
| 50 | status_t result = mFlinger->renderScreenToTextureLocked(0, &mTextureName, &u, &v); |
| 51 | if (result != NO_ERROR) { |
| 52 | return result; |
| 53 | } |
| 54 | initTexture(u, v); |
| 55 | return NO_ERROR; |
| 56 | } |
| 57 | |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 58 | status_t LayerScreenshot::capture() { |
| 59 | GLfloat u, v; |
| 60 | status_t result = mFlinger->renderScreenToTexture(0, &mTextureName, &u, &v); |
| 61 | if (result != NO_ERROR) { |
| 62 | return result; |
| 63 | } |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 64 | initTexture(u, v); |
| 65 | return NO_ERROR; |
| 66 | } |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 67 | |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 68 | void LayerScreenshot::initTexture(GLfloat u, GLfloat v) { |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 69 | glBindTexture(GL_TEXTURE_2D, mTextureName); |
| 70 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 71 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 72 | mTexCoords[0] = 0; mTexCoords[1] = v; |
| 73 | mTexCoords[2] = 0; mTexCoords[3] = 0; |
| 74 | mTexCoords[4] = u; mTexCoords[5] = 0; |
| 75 | mTexCoords[6] = u; mTexCoords[7] = v; |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 76 | } |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 77 | |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 78 | void LayerScreenshot::initStates(uint32_t w, uint32_t h, uint32_t flags) { |
| 79 | LayerBaseClient::initStates(w, h, flags); |
| 80 | if (!(flags & ISurfaceComposer::eHidden)) { |
| 81 | capture(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | uint32_t LayerScreenshot::doTransaction(uint32_t flags) |
| 86 | { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 87 | const LayerBase::State& draw(drawingState()); |
| 88 | const LayerBase::State& curr(currentState()); |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 89 | |
| 90 | if (draw.flags & ISurfaceComposer::eLayerHidden) { |
| 91 | if (!(curr.flags & ISurfaceComposer::eLayerHidden)) { |
| 92 | // we're going from hidden to visible |
| 93 | status_t err = captureLocked(); |
| 94 | if (err != NO_ERROR) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 95 | ALOGW("createScreenshotSurface failed (%s)", strerror(-err)); |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } else if (curr.flags & ISurfaceComposer::eLayerHidden) { |
| 99 | // we're going from visible to hidden |
| 100 | if (mTextureName) { |
| 101 | glDeleteTextures(1, &mTextureName); |
| 102 | mTextureName = 0; |
| 103 | } |
| 104 | } |
| 105 | return LayerBaseClient::doTransaction(flags); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 108 | void LayerScreenshot::onDraw(const DisplayHardware& hw, const Region& clip) const |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 109 | { |
| 110 | const State& s(drawingState()); |
Mathias Agopian | f74e8e0 | 2012-04-16 03:14:05 -0700 | [diff] [blame] | 111 | if (s.alpha>0) { |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 112 | const GLfloat alpha = s.alpha/255.0f; |
| 113 | const uint32_t fbHeight = hw.getHeight(); |
| 114 | |
| 115 | if (s.alpha == 0xFF) { |
| 116 | glDisable(GL_BLEND); |
| 117 | } else { |
| 118 | glEnable(GL_BLEND); |
| 119 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 120 | } |
| 121 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 122 | LayerMesh mesh; |
| 123 | computeGeometry(hw, &mesh); |
| 124 | |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 125 | glColor4f(0, 0, 0, alpha); |
| 126 | |
| 127 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 128 | glEnable(GL_TEXTURE_2D); |
| 129 | |
| 130 | glBindTexture(GL_TEXTURE_2D, mTextureName); |
| 131 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 132 | glMatrixMode(GL_TEXTURE); |
| 133 | glLoadIdentity(); |
| 134 | glMatrixMode(GL_MODELVIEW); |
| 135 | |
| 136 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 137 | glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 138 | glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices()); |
| 139 | glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount()); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 140 | |
| 141 | glDisable(GL_BLEND); |
| 142 | glDisable(GL_TEXTURE_2D); |
| 143 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // --------------------------------------------------------------------------- |
| 148 | |
| 149 | }; // namespace android |