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 | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 43 | mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE) |
| 44 | { |
| 45 | int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule); |
| 46 | LOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID); |
| 47 | if (err == 0) { |
| 48 | err = hwc_open(mModule, &mHwc); |
| 49 | LOGE_IF(err, "%s device failed to initialize (%s)", |
| 50 | HWC_HARDWARE_COMPOSER, strerror(-err)); |
Mathias Agopian | a0bd0d8 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 51 | if (err == 0) { |
| 52 | if (mHwc->registerProcs) { |
| 53 | mCBContext.hwc = this; |
| 54 | mCBContext.procs.invalidate = &hook_invalidate; |
| 55 | mHwc->registerProcs(mHwc, &mCBContext.procs); |
| 56 | } |
| 57 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | HWComposer::~HWComposer() { |
| 62 | free(mList); |
| 63 | if (mHwc) { |
| 64 | hwc_close(mHwc); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | status_t HWComposer::initCheck() const { |
| 69 | return mHwc ? NO_ERROR : NO_INIT; |
| 70 | } |
| 71 | |
Mathias Agopian | a0bd0d8 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 72 | void HWComposer::hook_invalidate(struct hwc_procs* procs) { |
| 73 | reinterpret_cast<cb_context *>(procs)->hwc->invalidate(); |
| 74 | } |
| 75 | |
| 76 | void HWComposer::invalidate() { |
| 77 | mFlinger->signalEvent(); |
| 78 | } |
| 79 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 80 | void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) { |
| 81 | mDpy = (hwc_display_t)dpy; |
| 82 | mSur = (hwc_surface_t)sur; |
| 83 | } |
| 84 | |
| 85 | status_t HWComposer::createWorkList(size_t numLayers) { |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 86 | if (mHwc) { |
| 87 | if (!mList || mCapacity < numLayers) { |
| 88 | free(mList); |
| 89 | size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t); |
| 90 | mList = (hwc_layer_list_t*)malloc(size); |
| 91 | mCapacity = numLayers; |
| 92 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 93 | mList->flags = HWC_GEOMETRY_CHANGED; |
| 94 | mList->numHwLayers = numLayers; |
| 95 | } |
| 96 | return NO_ERROR; |
| 97 | } |
| 98 | |
| 99 | status_t HWComposer::prepare() const { |
| 100 | int err = mHwc->prepare(mHwc, mList); |
| 101 | return (status_t)err; |
| 102 | } |
| 103 | |
| 104 | status_t HWComposer::commit() const { |
| 105 | int err = mHwc->set(mHwc, mDpy, mSur, mList); |
Mathias Agopian | 3e1a18a | 2010-10-07 14:57:04 -0700 | [diff] [blame] | 106 | if (mList) { |
| 107 | mList->flags &= ~HWC_GEOMETRY_CHANGED; |
| 108 | } |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 109 | return (status_t)err; |
| 110 | } |
| 111 | |
Antti Hatala | 11042a4 | 2010-09-09 02:33:05 -0700 | [diff] [blame] | 112 | status_t HWComposer::release() const { |
Mathias Agopian | 7f97258 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 113 | if (mHwc) { |
| 114 | int err = mHwc->set(mHwc, NULL, NULL, NULL); |
| 115 | return (status_t)err; |
| 116 | } |
| 117 | return NO_ERROR; |
| 118 | } |
| 119 | |
| 120 | status_t HWComposer::disable() { |
| 121 | if (mHwc) { |
| 122 | free(mList); |
| 123 | mList = NULL; |
| 124 | int err = mHwc->prepare(mHwc, NULL); |
| 125 | return (status_t)err; |
| 126 | } |
| 127 | return NO_ERROR; |
Antti Hatala | 11042a4 | 2010-09-09 02:33:05 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 130 | size_t HWComposer::getNumLayers() const { |
| 131 | return mList ? mList->numHwLayers : 0; |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Mathias Agopian | f8e705d | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 134 | hwc_layer_t* HWComposer::getLayers() const { |
| 135 | return mList ? mList->hwLayers : 0; |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Mathias Agopian | 5bd1b27 | 2011-09-09 00:49:11 -0700 | [diff] [blame^] | 138 | void HWComposer::dump(String8& result, char* buffer, size_t SIZE, |
| 139 | const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const { |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 140 | if (mHwc && mList) { |
| 141 | result.append("Hardware Composer state:\n"); |
| 142 | |
| 143 | snprintf(buffer, SIZE, " numHwLayers=%u, flags=%08x\n", |
| 144 | mList->numHwLayers, mList->flags); |
| 145 | result.append(buffer); |
| 146 | |
| 147 | for (size_t i=0 ; i<mList->numHwLayers ; i++) { |
| 148 | const hwc_layer_t& l(mList->hwLayers[i]); |
Mathias Agopian | 5bd1b27 | 2011-09-09 00:49:11 -0700 | [diff] [blame^] | 149 | snprintf(buffer, SIZE, " %8s | %08x | %08x | %02x | %04x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n", |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 150 | l.compositionType ? "OVERLAY" : "FB", |
| 151 | l.hints, l.flags, l.transform, l.blending, |
| 152 | l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom, |
Mathias Agopian | 5bd1b27 | 2011-09-09 00:49:11 -0700 | [diff] [blame^] | 153 | l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom, |
| 154 | visibleLayersSortedByZ[i]->getName().string()); |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 155 | result.append(buffer); |
| 156 | } |
Erik Gilling | 94720d7 | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 157 | |
| 158 | } |
| 159 | if (mHwc && mHwc->common.version >= 1 && mHwc->dump) { |
| 160 | mHwc->dump(mHwc, buffer, SIZE); |
| 161 | result.append(buffer); |
Mathias Agopian | 90da4dd | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Mathias Agopian | e0d5f5b | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 165 | // --------------------------------------------------------------------------- |
| 166 | }; // namespace android |