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