Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 <stdint.h> |
Mathias Agopian | b59beb5 | 2010-08-11 17:31:33 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <utils/Errors.h> |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 24 | #include <utils/String8.h> |
Mathias Agopian | 5bd1b27 | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 25 | #include <utils/Vector.h> |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 26 | |
| 27 | #include <hardware/hardware.h> |
| 28 | |
| 29 | #include <cutils/log.h> |
| 30 | |
| 31 | #include <EGL/egl.h> |
| 32 | |
Mathias Agopian | 5bd1b27 | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 33 | #include "LayerBase.h" |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 34 | #include "HWComposer.h" |
Mathias Agopian | a0bd0d8 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 35 | #include "SurfaceFlinger.h" |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | // --------------------------------------------------------------------------- |
| 39 | |
Mathias Agopian | a0bd0d8 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 40 | HWComposer::HWComposer(const sp<SurfaceFlinger>& flinger) |
| 41 | : mFlinger(flinger), |
| 42 | mModule(0), mHwc(0), mList(0), mCapacity(0), |
Mathias Agopian | 88cde07 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 43 | mNumOVLayers(0), mNumFBLayers(0), |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 44 | mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE) |
| 45 | { |
| 46 | int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule); |
| 47 | LOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID); |
| 48 | if (err == 0) { |
| 49 | err = hwc_open(mModule, &mHwc); |
| 50 | LOGE_IF(err, "%s device failed to initialize (%s)", |
| 51 | HWC_HARDWARE_COMPOSER, strerror(-err)); |
Mathias Agopian | a0bd0d8 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 52 | if (err == 0) { |
| 53 | if (mHwc->registerProcs) { |
| 54 | mCBContext.hwc = this; |
| 55 | mCBContext.procs.invalidate = &hook_invalidate; |
| 56 | mHwc->registerProcs(mHwc, &mCBContext.procs); |
| 57 | } |
| 58 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | HWComposer::~HWComposer() { |
| 63 | free(mList); |
| 64 | if (mHwc) { |
| 65 | hwc_close(mHwc); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | status_t HWComposer::initCheck() const { |
| 70 | return mHwc ? NO_ERROR : NO_INIT; |
| 71 | } |
| 72 | |
Mathias Agopian | a0bd0d8 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 73 | void HWComposer::hook_invalidate(struct hwc_procs* procs) { |
| 74 | reinterpret_cast<cb_context *>(procs)->hwc->invalidate(); |
| 75 | } |
| 76 | |
| 77 | void HWComposer::invalidate() { |
Mathias Agopian | edaab94 | 2011-10-05 15:00:22 -0700 | [diff] [blame] | 78 | mFlinger->repaintEverything(); |
Mathias Agopian | a0bd0d8 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 81 | void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) { |
| 82 | mDpy = (hwc_display_t)dpy; |
| 83 | mSur = (hwc_surface_t)sur; |
| 84 | } |
| 85 | |
| 86 | status_t HWComposer::createWorkList(size_t numLayers) { |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 87 | if (mHwc) { |
| 88 | if (!mList || mCapacity < numLayers) { |
| 89 | free(mList); |
| 90 | size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t); |
| 91 | mList = (hwc_layer_list_t*)malloc(size); |
| 92 | mCapacity = numLayers; |
| 93 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 94 | mList->flags = HWC_GEOMETRY_CHANGED; |
| 95 | mList->numHwLayers = numLayers; |
| 96 | } |
| 97 | return NO_ERROR; |
| 98 | } |
| 99 | |
| 100 | status_t HWComposer::prepare() const { |
| 101 | int err = mHwc->prepare(mHwc, mList); |
Mathias Agopian | 88cde07 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 102 | if (err == NO_ERROR) { |
| 103 | size_t numOVLayers = 0; |
| 104 | size_t numFBLayers = 0; |
| 105 | size_t count = mList->numHwLayers; |
| 106 | for (size_t i=0 ; i<count ; i++) { |
| 107 | hwc_layer& l(mList->hwLayers[i]); |
| 108 | if (l.flags & HWC_SKIP_LAYER) { |
| 109 | l.compositionType = HWC_FRAMEBUFFER; |
| 110 | } |
| 111 | switch (l.compositionType) { |
| 112 | case HWC_OVERLAY: |
| 113 | numOVLayers++; |
| 114 | break; |
| 115 | case HWC_FRAMEBUFFER: |
| 116 | numFBLayers++; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | mNumOVLayers = numOVLayers; |
| 121 | mNumFBLayers = numFBLayers; |
| 122 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 123 | return (status_t)err; |
| 124 | } |
| 125 | |
Mathias Agopian | 88cde07 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 126 | size_t HWComposer::getLayerCount(int type) const { |
| 127 | switch (type) { |
| 128 | case HWC_OVERLAY: |
| 129 | return mNumOVLayers; |
| 130 | case HWC_FRAMEBUFFER: |
| 131 | return mNumFBLayers; |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 136 | status_t HWComposer::commit() const { |
| 137 | int err = mHwc->set(mHwc, mDpy, mSur, mList); |
Mathias Agopian | 3e1a18a | 2010-10-07 14:57:04 -0700 | [diff] [blame] | 138 | if (mList) { |
| 139 | mList->flags &= ~HWC_GEOMETRY_CHANGED; |
| 140 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 141 | return (status_t)err; |
| 142 | } |
| 143 | |
Antti Hatala | 11042a4 | 2010-09-09 02:33:05 -0700 | [diff] [blame] | 144 | status_t HWComposer::release() const { |
Mathias Agopian | 7f97258 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 145 | if (mHwc) { |
| 146 | int err = mHwc->set(mHwc, NULL, NULL, NULL); |
| 147 | return (status_t)err; |
| 148 | } |
| 149 | return NO_ERROR; |
| 150 | } |
| 151 | |
| 152 | status_t HWComposer::disable() { |
| 153 | if (mHwc) { |
| 154 | free(mList); |
| 155 | mList = NULL; |
| 156 | int err = mHwc->prepare(mHwc, NULL); |
| 157 | return (status_t)err; |
| 158 | } |
| 159 | return NO_ERROR; |
Antti Hatala | 11042a4 | 2010-09-09 02:33:05 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 162 | size_t HWComposer::getNumLayers() const { |
| 163 | return mList ? mList->numHwLayers : 0; |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 166 | hwc_layer_t* HWComposer::getLayers() const { |
| 167 | return mList ? mList->hwLayers : 0; |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Mathias Agopian | 5bd1b27 | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 170 | void HWComposer::dump(String8& result, char* buffer, size_t SIZE, |
| 171 | const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const { |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 172 | if (mHwc && mList) { |
| 173 | result.append("Hardware Composer state:\n"); |
| 174 | |
| 175 | snprintf(buffer, SIZE, " numHwLayers=%u, flags=%08x\n", |
| 176 | mList->numHwLayers, mList->flags); |
| 177 | result.append(buffer); |
Mathias Agopian | 880ac7a | 2011-09-20 15:13:14 -0700 | [diff] [blame] | 178 | result.append( |
Mathias Agopian | 1f29cbe | 2011-09-29 18:07:08 -0700 | [diff] [blame] | 179 | " type | handle | hints | flags | tr | blend | format | source crop | frame name \n" |
| 180 | "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n"); |
| 181 | // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____] |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 182 | for (size_t i=0 ; i<mList->numHwLayers ; i++) { |
| 183 | const hwc_layer_t& l(mList->hwLayers[i]); |
Mathias Agopian | 880ac7a | 2011-09-20 15:13:14 -0700 | [diff] [blame] | 184 | const sp<LayerBase> layer(visibleLayersSortedByZ[i]); |
| 185 | int32_t format = -1; |
| 186 | if (layer->getLayer() != NULL) { |
| 187 | const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer()); |
| 188 | if (buffer != NULL) { |
| 189 | format = buffer->getPixelFormat(); |
| 190 | } |
| 191 | } |
| 192 | snprintf(buffer, SIZE, |
Mathias Agopian | 1f29cbe | 2011-09-29 18:07:08 -0700 | [diff] [blame] | 193 | " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n", |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 194 | l.compositionType ? "OVERLAY" : "FB", |
Mathias Agopian | 1f29cbe | 2011-09-29 18:07:08 -0700 | [diff] [blame] | 195 | intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format, |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 196 | l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom, |
Mathias Agopian | 5bd1b27 | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 197 | l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom, |
Mathias Agopian | 880ac7a | 2011-09-20 15:13:14 -0700 | [diff] [blame] | 198 | layer->getName().string()); |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 199 | result.append(buffer); |
| 200 | } |
Erik Gilling | 94720d7 | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 201 | } |
| 202 | if (mHwc && mHwc->common.version >= 1 && mHwc->dump) { |
| 203 | mHwc->dump(mHwc, buffer, SIZE); |
| 204 | result.append(buffer); |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 208 | // --------------------------------------------------------------------------- |
| 209 | }; // namespace android |