Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "SurfaceInterceptor" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
| 21 | #include "Layer.h" |
| 22 | #include "SurfaceFlinger.h" |
| 23 | #include "SurfaceInterceptor.h" |
| 24 | |
| 25 | #include <cutils/log.h> |
| 26 | |
| 27 | #include <utils/Trace.h> |
| 28 | |
| 29 | #include <fstream> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | void SurfaceInterceptor::enable(const SortedVector<sp<Layer>>& layers) { |
| 36 | ATRACE_CALL(); |
| 37 | if (mEnabled) { |
| 38 | return; |
| 39 | } |
| 40 | mEnabled = true; |
| 41 | saveExistingLayers(layers); |
| 42 | } |
| 43 | |
| 44 | void SurfaceInterceptor::disable() { |
| 45 | ATRACE_CALL(); |
| 46 | if (!mEnabled) { |
| 47 | return; |
| 48 | } |
| 49 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 50 | mEnabled = false; |
| 51 | status_t err(writeProtoFileLocked()); |
| 52 | ALOGE_IF(err == PERMISSION_DENIED, "Could not save the proto file! Permission denied"); |
| 53 | ALOGE_IF(err == NOT_ENOUGH_DATA, "Could not save the proto file! There are missing fields"); |
| 54 | mTrace.Clear(); |
| 55 | } |
| 56 | |
| 57 | void SurfaceInterceptor::saveExistingLayers(const SortedVector<sp<Layer>>& layers) { |
| 58 | ATRACE_CALL(); |
| 59 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 60 | for (const auto& layer : layers) { |
| 61 | saveLayerCreateLocked(layer); |
| 62 | saveInitialLayerStateLocked(layer); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void SurfaceInterceptor::saveInitialLayerStateLocked(const sp<const Layer>& layer) { |
| 67 | ATRACE_CALL(); |
| 68 | if (layer == nullptr) { |
| 69 | return; |
| 70 | } |
| 71 | Increment* increment(addTraceIncrementLocked()); |
| 72 | Transaction* transaction(increment->mutable_transaction()); |
| 73 | transaction->set_synchronous(layer->mTransactionFlags & BnSurfaceComposer::eSynchronous); |
| 74 | transaction->set_animation(layer->mTransactionFlags & BnSurfaceComposer::eAnimation); |
| 75 | |
| 76 | const int32_t layerId(getLayerId(layer)); |
| 77 | addPositionLocked(transaction, layerId, layer->mCurrentState.active.transform.tx(), |
| 78 | layer->mCurrentState.active.transform.ty()); |
| 79 | addDepthLocked(transaction, layerId, layer->mCurrentState.z); |
| 80 | addSizeLocked(transaction, layerId, layer->mCurrentState.active.w, |
| 81 | layer->mCurrentState.active.h); |
| 82 | addAlphaLocked(transaction, layerId, layer->mCurrentState.alpha); |
| 83 | addTransparentRegionLocked(transaction, layerId, layer->mCurrentState.activeTransparentRegion); |
| 84 | addLayerStackLocked(transaction, layerId, layer->mCurrentState.layerStack); |
| 85 | addCropLocked(transaction, layerId, layer->mCurrentState.crop); |
| 86 | if (layer->mCurrentState.handle != NULL) { |
| 87 | addDeferTransactionLocked(transaction, layerId, layer->mCurrentState.handle, |
| 88 | layer->mCurrentState.frameNumber); |
| 89 | } |
| 90 | addFinalCropLocked(transaction, layerId, layer->mCurrentState.finalCrop); |
| 91 | addOverrideScalingModeLocked(transaction, layerId, layer->getEffectiveScalingMode()); |
| 92 | addFlagsLocked(transaction, layerId, layer->mCurrentState.flags); |
| 93 | } |
| 94 | |
| 95 | status_t SurfaceInterceptor::writeProtoFileLocked() { |
| 96 | ATRACE_CALL(); |
| 97 | std::ofstream output(mOutputFileName, std::ios::out | std::ios::trunc | std::ios::binary); |
| 98 | // SerializeToOstream returns false when it's missing required data or when it could not write |
| 99 | if (!mTrace.IsInitialized()) { |
| 100 | return NOT_ENOUGH_DATA; |
| 101 | } |
| 102 | if (!mTrace.SerializeToOstream(&output)) { |
| 103 | return PERMISSION_DENIED; |
| 104 | } |
| 105 | return NO_ERROR; |
| 106 | } |
| 107 | |
| 108 | void SurfaceInterceptor::setOutputFileName(const std::string& outputFileName) { |
| 109 | mOutputFileName = outputFileName; |
| 110 | } |
| 111 | |
| 112 | const sp<const Layer> SurfaceInterceptor::getLayer(const sp<const IBinder>& handle) { |
| 113 | const auto layerHandle(static_cast<const Layer::Handle*>(handle.get())); |
| 114 | const sp<const Layer> layer(layerHandle->owner.promote()); |
| 115 | // layer could be a nullptr at this point |
| 116 | return layer; |
| 117 | } |
| 118 | |
| 119 | const std::string SurfaceInterceptor::getLayerName(const sp<const Layer>& layer) { |
| 120 | return layer->getName().string(); |
| 121 | } |
| 122 | |
| 123 | int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) { |
| 124 | return layer->sequence; |
| 125 | } |
| 126 | |
| 127 | Increment* SurfaceInterceptor::addTraceIncrementLocked() { |
| 128 | Increment* increment(mTrace.add_increment()); |
| 129 | increment->set_time_stamp(systemTime()); |
| 130 | return increment; |
| 131 | } |
| 132 | |
| 133 | Change* SurfaceInterceptor::addChangeLocked(Transaction* transaction, int32_t layerId) { |
| 134 | Change* change(transaction->add_change()); |
| 135 | change->set_id(layerId); |
| 136 | return change; |
| 137 | } |
| 138 | |
| 139 | void SurfaceInterceptor::setProtoRectLocked(Rectangle* protoRect, const Rect& rect) { |
| 140 | protoRect->set_left(rect.left); |
| 141 | protoRect->set_top(rect.top); |
| 142 | protoRect->set_right(rect.right); |
| 143 | protoRect->set_bottom(rect.bottom); |
| 144 | } |
| 145 | |
| 146 | void SurfaceInterceptor::addPositionLocked(Transaction* transaction, int32_t layerId, float x, |
| 147 | float y) |
| 148 | { |
| 149 | Change* change(addChangeLocked(transaction, layerId)); |
| 150 | PositionChange* posChange(change->mutable_position()); |
| 151 | posChange->set_x(x); |
| 152 | posChange->set_y(y); |
| 153 | } |
| 154 | |
| 155 | void SurfaceInterceptor::addDepthLocked(Transaction* transaction, int32_t layerId, uint32_t z) { |
| 156 | Change* change(addChangeLocked(transaction, layerId)); |
| 157 | LayerChange* depthChange(change->mutable_layer()); |
| 158 | depthChange->set_layer(z); |
| 159 | } |
| 160 | |
| 161 | void SurfaceInterceptor::addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w, |
| 162 | uint32_t h) |
| 163 | { |
| 164 | Change* change(addChangeLocked(transaction, layerId)); |
| 165 | SizeChange* sizeChange(change->mutable_size()); |
| 166 | sizeChange->set_w(w); |
| 167 | sizeChange->set_h(h); |
| 168 | } |
| 169 | |
| 170 | void SurfaceInterceptor::addAlphaLocked(Transaction* transaction, int32_t layerId, float alpha) { |
| 171 | Change* change(addChangeLocked(transaction, layerId)); |
| 172 | AlphaChange* alphaChange(change->mutable_alpha()); |
| 173 | alphaChange->set_alpha(alpha); |
| 174 | } |
| 175 | |
| 176 | void SurfaceInterceptor::addMatrixLocked(Transaction* transaction, int32_t layerId, |
| 177 | const layer_state_t::matrix22_t& matrix) |
| 178 | { |
| 179 | Change* change(addChangeLocked(transaction, layerId)); |
| 180 | MatrixChange* matrixChange(change->mutable_matrix()); |
| 181 | matrixChange->set_dsdx(matrix.dsdx); |
| 182 | matrixChange->set_dtdx(matrix.dtdx); |
| 183 | matrixChange->set_dsdy(matrix.dsdy); |
| 184 | matrixChange->set_dtdy(matrix.dtdy); |
| 185 | } |
| 186 | |
| 187 | void SurfaceInterceptor::addTransparentRegionLocked(Transaction* transaction, int32_t layerId, |
| 188 | const Region& transRegion) |
| 189 | { |
| 190 | Change* change(addChangeLocked(transaction, layerId)); |
| 191 | TransparentRegionHintChange* transparentChange(change->mutable_transparent_region_hint()); |
| 192 | |
| 193 | for (const auto& rect : transRegion) { |
| 194 | Rectangle* protoRect(transparentChange->add_region()); |
| 195 | setProtoRectLocked(protoRect, rect); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags) { |
| 200 | // There can be multiple flags changed |
| 201 | if (flags & layer_state_t::eLayerHidden) { |
| 202 | Change* change(addChangeLocked(transaction, layerId)); |
| 203 | HiddenFlagChange* flagChange(change->mutable_hidden_flag()); |
| 204 | flagChange->set_hidden_flag(true); |
| 205 | } |
| 206 | if (flags & layer_state_t::eLayerOpaque) { |
| 207 | Change* change(addChangeLocked(transaction, layerId)); |
| 208 | OpaqueFlagChange* flagChange(change->mutable_opaque_flag()); |
| 209 | flagChange->set_opaque_flag(true); |
| 210 | } |
| 211 | if (flags & layer_state_t::eLayerSecure) { |
| 212 | Change* change(addChangeLocked(transaction, layerId)); |
| 213 | SecureFlagChange* flagChange(change->mutable_secure_flag()); |
| 214 | flagChange->set_secure_flag(true); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void SurfaceInterceptor::addLayerStackLocked(Transaction* transaction, int32_t layerId, |
| 219 | uint32_t layerStack) |
| 220 | { |
| 221 | Change* change(addChangeLocked(transaction, layerId)); |
| 222 | LayerStackChange* layerStackChange(change->mutable_layer_stack()); |
| 223 | layerStackChange->set_layer_stack(layerStack); |
| 224 | } |
| 225 | |
| 226 | void SurfaceInterceptor::addCropLocked(Transaction* transaction, int32_t layerId, |
| 227 | const Rect& rect) |
| 228 | { |
| 229 | Change* change(addChangeLocked(transaction, layerId)); |
| 230 | CropChange* cropChange(change->mutable_crop()); |
| 231 | Rectangle* protoRect(cropChange->mutable_rectangle()); |
| 232 | setProtoRectLocked(protoRect, rect); |
| 233 | } |
| 234 | |
| 235 | void SurfaceInterceptor::addFinalCropLocked(Transaction* transaction, int32_t layerId, |
| 236 | const Rect& rect) |
| 237 | { |
| 238 | Change* change(addChangeLocked(transaction, layerId)); |
| 239 | FinalCropChange* finalCropChange(change->mutable_final_crop()); |
| 240 | Rectangle* protoRect(finalCropChange->mutable_rectangle()); |
| 241 | setProtoRectLocked(protoRect, rect); |
| 242 | } |
| 243 | |
| 244 | void SurfaceInterceptor::addDeferTransactionLocked(Transaction* transaction, int32_t layerId, |
| 245 | const sp<const IBinder>& handle, uint64_t frameNumber) |
| 246 | { |
| 247 | Change* change(addChangeLocked(transaction, layerId)); |
| 248 | const sp<const Layer> layer(getLayer(handle)); |
| 249 | if (layer == nullptr) { |
| 250 | ALOGE("An existing layer could not be retrieved with the handle" |
| 251 | " for the deferred transaction"); |
| 252 | return; |
| 253 | } |
| 254 | DeferredTransactionChange* deferTransaction(change->mutable_deferred_transaction()); |
| 255 | deferTransaction->set_layer_id(getLayerId(layer)); |
| 256 | deferTransaction->set_frame_number(frameNumber); |
| 257 | } |
| 258 | |
| 259 | void SurfaceInterceptor::addOverrideScalingModeLocked(Transaction* transaction, int32_t layerId, |
| 260 | int32_t overrideScalingMode) |
| 261 | { |
| 262 | Change* change(addChangeLocked(transaction, layerId)); |
| 263 | OverrideScalingModeChange* overrideChange(change->mutable_override_scaling_mode()); |
| 264 | overrideChange->set_override_scaling_mode(overrideScalingMode); |
| 265 | } |
| 266 | |
| 267 | void SurfaceInterceptor::addChangedPropertiesLocked(Transaction* transaction, |
| 268 | const layer_state_t& state) |
| 269 | { |
| 270 | const sp<const Layer> layer(getLayer(state.surface)); |
| 271 | if (layer == nullptr) { |
| 272 | ALOGE("An existing layer could not be retrieved with the surface " |
| 273 | "from the layer_state_t surface in the update transaction"); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | const int32_t layerId(getLayerId(layer)); |
| 278 | |
| 279 | if (state.what & layer_state_t::ePositionChanged) { |
| 280 | addPositionLocked(transaction, layerId, state.x, state.y); |
| 281 | } |
| 282 | if (state.what & layer_state_t::eLayerChanged) { |
| 283 | addDepthLocked(transaction, layerId, state.z); |
| 284 | } |
| 285 | if (state.what & layer_state_t::eSizeChanged) { |
| 286 | addSizeLocked(transaction, layerId, state.w, state.h); |
| 287 | } |
| 288 | if (state.what & layer_state_t::eAlphaChanged) { |
| 289 | addAlphaLocked(transaction, layerId, state.alpha); |
| 290 | } |
| 291 | if (state.what & layer_state_t::eMatrixChanged) { |
| 292 | addMatrixLocked(transaction, layerId, state.matrix); |
| 293 | } |
| 294 | if (state.what & layer_state_t::eTransparentRegionChanged) { |
| 295 | addTransparentRegionLocked(transaction, layerId, state.transparentRegion); |
| 296 | } |
| 297 | if (state.what & layer_state_t::eFlagsChanged) { |
| 298 | addFlagsLocked(transaction, layerId, state.flags); |
| 299 | } |
| 300 | if (state.what & layer_state_t::eLayerStackChanged) { |
| 301 | addLayerStackLocked(transaction, layerId, state.layerStack); |
| 302 | } |
| 303 | if (state.what & layer_state_t::eCropChanged) { |
| 304 | addCropLocked(transaction, layerId, state.crop); |
| 305 | } |
| 306 | if (state.what & layer_state_t::eDeferTransaction) { |
| 307 | addDeferTransactionLocked(transaction, layerId, state.handle, state.frameNumber); |
| 308 | } |
| 309 | if (state.what & layer_state_t::eFinalCropChanged) { |
| 310 | addFinalCropLocked(transaction, layerId, state.finalCrop); |
| 311 | } |
| 312 | if (state.what & layer_state_t::eOverrideScalingModeChanged) { |
| 313 | addOverrideScalingModeLocked(transaction, layerId, state.overrideScalingMode); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void SurfaceInterceptor::addUpdatedLayersLocked(Increment* increment, uint32_t flags, |
| 318 | const Vector<ComposerState>& stateUpdates) |
| 319 | { |
| 320 | Transaction* transaction(increment->mutable_transaction()); |
| 321 | transaction->set_synchronous(flags & BnSurfaceComposer::eSynchronous); |
| 322 | transaction->set_animation(flags & BnSurfaceComposer::eAnimation); |
| 323 | for (const auto& compState: stateUpdates) { |
| 324 | addChangedPropertiesLocked(transaction, compState.state); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void SurfaceInterceptor::addCreatedLayerLocked(Increment* increment, const sp<const Layer>& layer) { |
| 329 | Create* create(increment->mutable_create()); |
| 330 | create->set_id(getLayerId(layer)); |
| 331 | create->set_name(getLayerName(layer)); |
| 332 | create->set_w(layer->mCurrentState.active.w); |
| 333 | create->set_h(layer->mCurrentState.active.h); |
| 334 | } |
| 335 | |
| 336 | void SurfaceInterceptor::addDeletedLayerLocked(Increment* increment, const sp<const Layer>& layer) { |
| 337 | Delete* deleteLayer(increment->mutable_delete_()); |
| 338 | deleteLayer->set_id(getLayerId(layer)); |
| 339 | } |
| 340 | |
| 341 | void SurfaceInterceptor::addUpdatedBufferLocked(Increment* increment, const sp<const Layer>& layer, |
| 342 | uint32_t width, uint32_t height, uint64_t frameNumber) |
| 343 | { |
| 344 | BufferUpdate* update(increment->mutable_buffer_update()); |
| 345 | update->set_id(getLayerId(layer)); |
| 346 | update->set_w(width); |
| 347 | update->set_h(height); |
| 348 | update->set_frame_number(frameNumber); |
| 349 | } |
| 350 | |
| 351 | void SurfaceInterceptor::addUpdatedVsyncLocked(Increment* increment, nsecs_t timestamp) { |
| 352 | VSyncEvent* event(increment->mutable_vsync_event()); |
| 353 | event->set_when(timestamp); |
| 354 | } |
| 355 | |
| 356 | void SurfaceInterceptor::saveLayerUpdates(const Vector<ComposerState>& stateUpdates, |
| 357 | uint32_t flags) |
| 358 | { |
| 359 | ATRACE_CALL(); |
| 360 | if (!mEnabled || stateUpdates.size() <= 0) { |
| 361 | return; |
| 362 | } |
| 363 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 364 | addUpdatedLayersLocked(addTraceIncrementLocked(), flags, stateUpdates); |
| 365 | } |
| 366 | |
| 367 | void SurfaceInterceptor::saveLayerCreate(const sp<const Layer>& layer) { |
| 368 | ATRACE_CALL(); |
| 369 | if (!mEnabled || layer == nullptr) { |
| 370 | return; |
| 371 | } |
| 372 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 373 | addCreatedLayerLocked(addTraceIncrementLocked(), layer); |
| 374 | } |
| 375 | |
| 376 | void SurfaceInterceptor::saveLayerCreateLocked(const sp<const Layer>& layer) { |
| 377 | if (!mEnabled || layer == nullptr) { |
| 378 | return; |
| 379 | } |
| 380 | addCreatedLayerLocked(addTraceIncrementLocked(), layer); |
| 381 | } |
| 382 | |
| 383 | void SurfaceInterceptor::saveLayerDelete(const sp<const Layer>& layer) { |
| 384 | ATRACE_CALL(); |
| 385 | if (!mEnabled || layer == nullptr) { |
| 386 | return; |
| 387 | } |
| 388 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 389 | addDeletedLayerLocked(addTraceIncrementLocked(), layer); |
| 390 | } |
| 391 | |
| 392 | void SurfaceInterceptor::saveBufferUpdate(const sp<const Layer>& layer, uint32_t width, |
| 393 | uint32_t height, uint64_t frameNumber) |
| 394 | { |
| 395 | ATRACE_CALL(); |
| 396 | if (!mEnabled || layer == nullptr) { |
| 397 | return; |
| 398 | } |
| 399 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 400 | addUpdatedBufferLocked(addTraceIncrementLocked(), layer, width, height, frameNumber); |
| 401 | } |
| 402 | |
| 403 | void SurfaceInterceptor::saveVSyncEvent(nsecs_t timestamp) { |
| 404 | if (!mEnabled) { |
| 405 | return; |
| 406 | } |
| 407 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 408 | addUpdatedVsyncLocked(addTraceIncrementLocked(), timestamp); |
| 409 | } |
| 410 | |
| 411 | } // namespace android |