Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018 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 <android/native_window.h> |
| 18 | #include <android/surface_control.h> |
| 19 | |
| 20 | #include <gui/Surface.h> |
| 21 | #include <gui/SurfaceComposerClient.h> |
| 22 | #include <gui/SurfaceControl.h> |
| 23 | |
| 24 | using namespace android; |
| 25 | |
| 26 | using Transaction = SurfaceComposerClient::Transaction; |
| 27 | |
| 28 | #define CHECK_NOT_NULL(name) \ |
| 29 | LOG_ALWAYS_FATAL_IF(name == nullptr, "nullptr passed as " #name " argument"); |
| 30 | |
| 31 | #define CHECK_VALID_RECT(name) \ |
| 32 | LOG_ALWAYS_FATAL_IF(!static_cast<const Rect&>(name).isValid(), \ |
| 33 | "invalid arg passed as " #name " argument"); |
| 34 | |
| 35 | Transaction* ASurfaceTransaction_to_Transaction(ASurfaceTransaction* aSurfaceTransaction) { |
| 36 | return reinterpret_cast<Transaction*>(aSurfaceTransaction); |
| 37 | } |
| 38 | |
| 39 | SurfaceControl* ASurfaceControl_to_SurfaceControl(ASurfaceControl* aSurfaceControl) { |
| 40 | return reinterpret_cast<SurfaceControl*>(aSurfaceControl); |
| 41 | } |
| 42 | |
| 43 | void SurfaceControl_acquire(SurfaceControl* surfaceControl) { |
| 44 | // incStrong/decStrong token must be the same, doesn't matter what it is |
| 45 | surfaceControl->incStrong((void*)SurfaceControl_acquire); |
| 46 | } |
| 47 | |
| 48 | void SurfaceControl_release(SurfaceControl* surfaceControl) { |
| 49 | // incStrong/decStrong token must be the same, doesn't matter what it is |
| 50 | surfaceControl->decStrong((void*)SurfaceControl_acquire); |
| 51 | } |
| 52 | |
| 53 | ASurfaceControl* ASurfaceControl_createFromWindow(ANativeWindow* window, const char* debug_name) { |
| 54 | CHECK_NOT_NULL(window); |
| 55 | CHECK_NOT_NULL(debug_name); |
| 56 | |
| 57 | sp<SurfaceComposerClient> client = new SurfaceComposerClient(); |
| 58 | if (client->initCheck() != NO_ERROR) { |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
| 62 | uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState; |
| 63 | sp<SurfaceControl> surfaceControl = |
| 64 | client->createWithSurfaceParent(String8(debug_name), 0 /* width */, 0 /* height */, |
| 65 | // Format is only relevant for buffer queue layers. |
| 66 | PIXEL_FORMAT_UNKNOWN /* format */, flags, |
| 67 | static_cast<Surface*>(window)); |
| 68 | if (!surfaceControl) { |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | SurfaceControl_acquire(surfaceControl.get()); |
| 73 | return reinterpret_cast<ASurfaceControl*>(surfaceControl.get()); |
| 74 | } |
| 75 | |
| 76 | ASurfaceControl* ASurfaceControl_create(ASurfaceControl* parent, const char* debug_name) { |
| 77 | CHECK_NOT_NULL(parent); |
| 78 | CHECK_NOT_NULL(debug_name); |
| 79 | |
| 80 | SurfaceComposerClient* client = ASurfaceControl_to_SurfaceControl(parent)->getClient().get(); |
| 81 | |
| 82 | SurfaceControl* surfaceControlParent = ASurfaceControl_to_SurfaceControl(parent); |
| 83 | |
| 84 | uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState; |
| 85 | sp<SurfaceControl> surfaceControl = |
| 86 | client->createSurface(String8(debug_name), 0 /* width */, 0 /* height */, |
| 87 | // Format is only relevant for buffer queue layers. |
| 88 | PIXEL_FORMAT_UNKNOWN /* format */, flags, |
| 89 | surfaceControlParent); |
| 90 | if (!surfaceControl) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | SurfaceControl_acquire(surfaceControl.get()); |
| 95 | return reinterpret_cast<ASurfaceControl*>(surfaceControl.get()); |
| 96 | } |
| 97 | |
| 98 | void ASurfaceControl_destroy(ASurfaceControl* aSurfaceControl) { |
| 99 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 100 | |
| 101 | Transaction().reparent(surfaceControl, nullptr).apply(); |
| 102 | SurfaceControl_release(surfaceControl.get()); |
| 103 | } |
| 104 | |
| 105 | ASurfaceTransaction* ASurfaceTransaction_create() { |
| 106 | Transaction* transaction = new Transaction; |
| 107 | return reinterpret_cast<ASurfaceTransaction*>(transaction); |
| 108 | } |
| 109 | |
| 110 | void ASurfaceTransaction_delete(ASurfaceTransaction* aSurfaceTransaction) { |
| 111 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 112 | delete transaction; |
| 113 | } |
| 114 | |
| 115 | void ASurfaceTransaction_apply(ASurfaceTransaction* aSurfaceTransaction) { |
| 116 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 117 | |
| 118 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 119 | |
| 120 | transaction->apply(); |
| 121 | } |
| 122 | |
| 123 | void ASurfaceTransaction_setOnComplete(ASurfaceTransaction* aSurfaceTransaction, void* context, |
| 124 | ASurfaceTransaction_OnComplete func) { |
| 125 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 126 | CHECK_NOT_NULL(context); |
| 127 | CHECK_NOT_NULL(func); |
| 128 | |
| 129 | TransactionCompletedCallbackTakesContext callback = [func](void* callback_context, |
| 130 | const TransactionStats& stats) { |
| 131 | int fence = (stats.presentFence) ? stats.presentFence->dup() : -1; |
| 132 | (*func)(callback_context, fence); |
| 133 | }; |
| 134 | |
| 135 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 136 | |
| 137 | transaction->addTransactionCompletedCallback(callback, context); |
| 138 | } |
| 139 | |
| 140 | void ASurfaceTransaction_setVisibility(ASurfaceTransaction* aSurfaceTransaction, ASurfaceControl* aSurfaceControl, |
| 141 | int8_t visibility) { |
| 142 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 143 | CHECK_NOT_NULL(aSurfaceControl); |
| 144 | |
| 145 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 146 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 147 | |
| 148 | switch (visibility) { |
| 149 | case ASURFACE_TRANSACTION_VISIBILITY_SHOW: |
| 150 | transaction->show(surfaceControl); |
| 151 | break; |
| 152 | case ASURFACE_TRANSACTION_VISIBILITY_HIDE: |
| 153 | transaction->hide(surfaceControl); |
| 154 | break; |
| 155 | default: |
| 156 | LOG_ALWAYS_FATAL("invalid visibility %d", visibility); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void ASurfaceTransaction_setZOrder(ASurfaceTransaction* aSurfaceTransaction, ASurfaceControl* aSurfaceControl, |
| 161 | int32_t z_order) { |
| 162 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 163 | CHECK_NOT_NULL(aSurfaceControl); |
| 164 | |
| 165 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 166 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 167 | |
| 168 | transaction->setLayer(surfaceControl, z_order); |
| 169 | } |
| 170 | |
| 171 | void ASurfaceTransaction_setBuffer(ASurfaceTransaction* aSurfaceTransaction, ASurfaceControl* aSurfaceControl, |
| 172 | AHardwareBuffer* buffer, int fence_fd) { |
| 173 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 174 | CHECK_NOT_NULL(aSurfaceControl); |
| 175 | |
| 176 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 177 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 178 | |
| 179 | sp<GraphicBuffer> graphic_buffer(reinterpret_cast<GraphicBuffer*>(buffer)); |
| 180 | |
| 181 | transaction->setBuffer(surfaceControl, graphic_buffer); |
| 182 | if (fence_fd != -1) { |
| 183 | sp<Fence> fence = new Fence(fence_fd); |
| 184 | transaction->setAcquireFence(surfaceControl, fence); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void ASurfaceTransaction_setGeometry(ASurfaceTransaction* aSurfaceTransaction, |
| 189 | ASurfaceControl* aSurfaceControl, const ARect& source, |
| 190 | const ARect& destination, int32_t transform) { |
| 191 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 192 | CHECK_NOT_NULL(aSurfaceControl); |
| 193 | CHECK_VALID_RECT(source); |
| 194 | CHECK_VALID_RECT(destination); |
| 195 | |
| 196 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 197 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 198 | |
| 199 | transaction->setCrop(surfaceControl, static_cast<const Rect&>(source)); |
| 200 | transaction->setFrame(surfaceControl, static_cast<const Rect&>(destination)); |
| 201 | transaction->setTransform(surfaceControl, transform); |
| 202 | } |
| 203 | |
| 204 | void ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction* aSurfaceTransaction, |
| 205 | ASurfaceControl* aSurfaceControl, |
| 206 | int8_t transparency) { |
| 207 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 208 | CHECK_NOT_NULL(aSurfaceControl); |
| 209 | |
| 210 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 211 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 212 | |
| 213 | uint32_t flags = (transparency == ASURFACE_TRANSACTION_TRANSPARENCY_OPAQUE) ? |
| 214 | layer_state_t::eLayerOpaque : 0; |
| 215 | transaction->setFlags(surfaceControl, flags, layer_state_t::eLayerOpaque); |
| 216 | } |
| 217 | |
| 218 | void ASurfaceTransaction_setDamageRegion(ASurfaceTransaction* aSurfaceTransaction, ASurfaceControl* aSurfaceControl, |
| 219 | const ARect rects[], uint32_t count) { |
| 220 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 221 | CHECK_NOT_NULL(aSurfaceControl); |
| 222 | |
| 223 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 224 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 225 | |
| 226 | Region region; |
| 227 | for (uint32_t i = 0; i < count; ++i) { |
| 228 | region.merge(static_cast<const Rect&>(rects[i])); |
| 229 | } |
| 230 | |
| 231 | transaction->setSurfaceDamageRegion(surfaceControl, region); |
| 232 | } |