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 | |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 21 | #include <GLES/gl.h> |
| 22 | #include <GLES/glext.h> |
| 23 | |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 24 | #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 Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 31 | #include "DisplayDevice.h" |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 32 | |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 33 | |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 37 | LayerScreenshot::LayerScreenshot(SurfaceFlinger* flinger, |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 38 | const sp<Client>& client) |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 39 | : LayerBaseClient(flinger, client), |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 40 | mTextureName(0), mFlinger(flinger) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | LayerScreenshot::~LayerScreenshot() |
| 45 | { |
| 46 | if (mTextureName) { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 47 | mFlinger->deleteTextureAsync(mTextureName); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 51 | status_t LayerScreenshot::captureLocked(int32_t layerStack) { |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 52 | GLfloat u, v; |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 53 | status_t result = mFlinger->renderScreenToTextureLocked(layerStack, |
| 54 | &mTextureName, &u, &v); |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 55 | if (result != NO_ERROR) { |
| 56 | return result; |
| 57 | } |
| 58 | initTexture(u, v); |
| 59 | return NO_ERROR; |
| 60 | } |
| 61 | |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 62 | status_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 Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 68 | initTexture(u, v); |
| 69 | return NO_ERROR; |
| 70 | } |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 71 | |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 72 | void LayerScreenshot::initTexture(GLfloat u, GLfloat v) { |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 73 | 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 Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 76 | 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 Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 80 | } |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 81 | |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 82 | void LayerScreenshot::initStates(uint32_t w, uint32_t h, uint32_t flags) { |
| 83 | LayerBaseClient::initStates(w, h, flags); |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 84 | if (!(flags & ISurfaceComposerClient::eHidden)) { |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 85 | capture(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | uint32_t LayerScreenshot::doTransaction(uint32_t flags) |
| 90 | { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 91 | const LayerBase::State& draw(drawingState()); |
| 92 | const LayerBase::State& curr(currentState()); |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 93 | |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 94 | if (draw.flags & layer_state_t::eLayerHidden) { |
| 95 | if (!(curr.flags & layer_state_t::eLayerHidden)) { |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 96 | // we're going from hidden to visible |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 97 | status_t err = captureLocked(curr.layerStack); |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 98 | if (err != NO_ERROR) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 99 | ALOGW("createScreenshotSurface failed (%s)", strerror(-err)); |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 102 | } else if (curr.flags & layer_state_t::eLayerHidden) { |
Mathias Agopian | 4a9ac37 | 2011-11-01 14:39:06 -0700 | [diff] [blame] | 103 | // 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 Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 112 | void LayerScreenshot::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 113 | { |
| 114 | const State& s(drawingState()); |
Mathias Agopian | f74e8e0 | 2012-04-16 03:14:05 -0700 | [diff] [blame] | 115 | if (s.alpha>0) { |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 116 | const GLfloat alpha = s.alpha/255.0f; |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 117 | const uint32_t fbHeight = hw->getHeight(); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 118 | |
| 119 | if (s.alpha == 0xFF) { |
| 120 | glDisable(GL_BLEND); |
Jesse Hall | b763d5f | 2012-10-10 21:37:22 -0700 | [diff] [blame^] | 121 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 122 | } else { |
| 123 | glEnable(GL_BLEND); |
Jesse Hall | b763d5f | 2012-10-10 21:37:22 -0700 | [diff] [blame^] | 124 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 125 | glColor4f(alpha, alpha, alpha, alpha); |
| 126 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 129 | LayerMesh mesh; |
| 130 | computeGeometry(hw, &mesh); |
| 131 | |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 132 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 133 | glEnable(GL_TEXTURE_2D); |
| 134 | |
| 135 | glBindTexture(GL_TEXTURE_2D, mTextureName); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 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 Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 142 | glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices()); |
| 143 | glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount()); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 144 | |
| 145 | glDisable(GL_BLEND); |
| 146 | glDisable(GL_TEXTURE_2D); |
| 147 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // --------------------------------------------------------------------------- |
| 152 | |
| 153 | }; // namespace android |