LayerScreenshot.cpp revision a2f4e56fec0fb36c4a370eb23d6e9dc57f250b59
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 31namespace android { 32// --------------------------------------------------------------------------- 33 34LayerScreenshot::LayerScreenshot(SurfaceFlinger* flinger, DisplayID display, 35 const sp<Client>& client) 36 : LayerBaseClient(flinger, display, client), 37 mTextureName(0), mFlinger(flinger) 38{ 39} 40 41LayerScreenshot::~LayerScreenshot() 42{ 43 if (mTextureName) { 44 mFlinger->postMessageAsync( 45 new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) ); 46 } 47} 48 49status_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 59status_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 } 65 initTexture(u, v); 66 return NO_ERROR; 67} 68 69void LayerScreenshot::initTexture(GLfloat u, GLfloat v) { 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); 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; 77} 78 79void 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 86uint32_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) { 96 ALOGW("createScreenshotSurface failed (%s)", strerror(-err)); 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); 107} 108 109void LayerScreenshot::onDraw(const Region& clip) const 110{ 111 const State& s(drawingState()); 112 Region::const_iterator it = clip.begin(); 113 Region::const_iterator const end = clip.end(); 114 if (s.alpha>0 && (it != end)) { 115 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 116 const GLfloat alpha = s.alpha/255.0f; 117 const uint32_t fbHeight = hw.getHeight(); 118 119 if (s.alpha == 0xFF) { 120 glDisable(GL_BLEND); 121 } else { 122 glEnable(GL_BLEND); 123 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 124 } 125 126 glColor4f(0, 0, 0, alpha); 127 128 glDisable(GL_TEXTURE_EXTERNAL_OES); 129 glEnable(GL_TEXTURE_2D); 130 131 glBindTexture(GL_TEXTURE_2D, mTextureName); 132 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 133 glMatrixMode(GL_TEXTURE); 134 glLoadIdentity(); 135 glMatrixMode(GL_MODELVIEW); 136 137 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 138 glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords); 139 glVertexPointer(2, GL_FLOAT, 0, mVertices); 140 141 while (it != end) { 142 const Rect& r = *it++; 143 const GLint sy = fbHeight - (r.top + r.height()); 144 glScissor(r.left, sy, r.width(), r.height()); 145 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 146 } 147 148 glDisable(GL_BLEND); 149 glDisable(GL_TEXTURE_2D); 150 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 151 } 152} 153 154// --------------------------------------------------------------------------- 155 156}; // namespace android 157