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