2 * Copyright 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HARDWARE_GRAPHICS_COMPOSER_COMMAND_BUFFER_H
18 #define ANDROID_HARDWARE_GRAPHICS_COMPOSER_COMMAND_BUFFER_H
21 #warn "IComposerCommandBuffer.h included without LOG_TAG"
35 #include <android/hardware/graphics/composer/2.1/IComposer.h>
37 #include <sync/sync.h>
38 #include <fmq/MessageQueue.h>
46 using android::hardware::graphics::common::V1_0::ColorTransform;
47 using android::hardware::graphics::common::V1_0::Dataspace;
48 using android::hardware::graphics::common::V1_0::Transform;
49 using android::hardware::MessageQueue;
51 using CommandQueueType = MessageQueue<uint32_t, kSynchronizedReadWrite>;
53 // This class helps build a command queue. Note that all sizes/lengths are in
54 // units of uint32_t's.
55 class CommandWriterBase {
57 CommandWriterBase(uint32_t initialMaxSize)
58 : mDataMaxSize(initialMaxSize)
60 mData = std::make_unique<uint32_t[]>(mDataMaxSize);
64 virtual ~CommandWriterBase()
74 // handles in mDataHandles are owned by the caller
77 // handles in mTemporaryHandles are owned by the writer
78 for (auto handle : mTemporaryHandles) {
79 native_handle_close(handle);
80 native_handle_delete(handle);
82 mTemporaryHandles.clear();
85 IComposerClient::Command getCommand(uint32_t offset)
87 uint32_t val = (offset < mDataWritten) ? mData[offset] : 0;
88 return static_cast<IComposerClient::Command>(val &
89 static_cast<uint32_t>(IComposerClient::Command::OPCODE_MASK));
92 bool writeQueue(bool* outQueueChanged, uint32_t* outCommandLength,
93 hidl_vec<hidl_handle>* outCommandHandles)
95 // After data are written to the queue, it may not be read by the
98 // - the writer does not send them (because of other errors)
99 // - the hwbinder transaction fails
100 // - the reader does not read them (because of other errors)
102 // Discard the stale data here.
103 size_t staleDataSize = mQueue ? mQueue->availableToRead() : 0;
104 if (staleDataSize > 0) {
105 ALOGW("discarding stale data from message queue");
106 CommandQueueType::MemTransaction tx;
107 if (mQueue->beginRead(staleDataSize, &tx)) {
108 mQueue->commitRead(staleDataSize);
112 // write data to queue, optionally resizing it
113 if (mQueue && (mDataMaxSize <= mQueue->getQuantumCount())) {
114 if (!mQueue->write(mData.get(), mDataWritten)) {
115 ALOGE("failed to write commands to message queue");
119 *outQueueChanged = false;
121 auto newQueue = std::make_unique<CommandQueueType>(mDataMaxSize);
122 if (!newQueue->isValid() ||
123 !newQueue->write(mData.get(), mDataWritten)) {
124 ALOGE("failed to prepare a new message queue ");
128 mQueue = std::move(newQueue);
129 *outQueueChanged = true;
132 *outCommandLength = mDataWritten;
133 outCommandHandles->setToExternal(
134 const_cast<hidl_handle*>(mDataHandles.data()),
135 mDataHandles.size());
140 const MQDescriptorSync<uint32_t>* getMQDescriptor() const
142 return (mQueue) ? mQueue->getDesc() : nullptr;
145 static constexpr uint16_t kSelectDisplayLength = 2;
146 void selectDisplay(Display display)
148 beginCommand(IComposerClient::Command::SELECT_DISPLAY,
149 kSelectDisplayLength);
154 static constexpr uint16_t kSelectLayerLength = 2;
155 void selectLayer(Layer layer)
157 beginCommand(IComposerClient::Command::SELECT_LAYER,
163 static constexpr uint16_t kSetErrorLength = 2;
164 void setError(uint32_t location, Error error)
166 beginCommand(IComposerClient::Command::SET_ERROR, kSetErrorLength);
168 writeSigned(static_cast<int32_t>(error));
172 static constexpr uint32_t kPresentOrValidateDisplayResultLength = 1;
173 void setPresentOrValidateResult(uint32_t state) {
174 beginCommand(IComposerClient::Command::SET_PRESENT_OR_VALIDATE_DISPLAY_RESULT, kPresentOrValidateDisplayResultLength);
179 void setChangedCompositionTypes(const std::vector<Layer>& layers,
180 const std::vector<IComposerClient::Composition>& types)
182 size_t totalLayers = std::min(layers.size(), types.size());
183 size_t currentLayer = 0;
185 while (currentLayer < totalLayers) {
186 size_t count = std::min(totalLayers - currentLayer,
187 static_cast<size_t>(kMaxLength) / 3);
190 IComposerClient::Command::SET_CHANGED_COMPOSITION_TYPES,
192 for (size_t i = 0; i < count; i++) {
193 write64(layers[currentLayer + i]);
194 writeSigned(static_cast<int32_t>(types[currentLayer + i]));
198 currentLayer += count;
202 void setDisplayRequests(uint32_t displayRequestMask,
203 const std::vector<Layer>& layers,
204 const std::vector<uint32_t>& layerRequestMasks)
206 size_t totalLayers = std::min(layers.size(),
207 layerRequestMasks.size());
208 size_t currentLayer = 0;
210 while (currentLayer < totalLayers) {
211 size_t count = std::min(totalLayers - currentLayer,
212 static_cast<size_t>(kMaxLength - 1) / 3);
214 beginCommand(IComposerClient::Command::SET_DISPLAY_REQUESTS,
216 write(displayRequestMask);
217 for (size_t i = 0; i < count; i++) {
218 write64(layers[currentLayer + i]);
219 write(static_cast<int32_t>(layerRequestMasks[currentLayer + i]));
223 currentLayer += count;
227 static constexpr uint16_t kSetPresentFenceLength = 1;
228 void setPresentFence(int presentFence)
230 beginCommand(IComposerClient::Command::SET_PRESENT_FENCE,
231 kSetPresentFenceLength);
232 writeFence(presentFence);
236 void setReleaseFences(const std::vector<Layer>& layers,
237 const std::vector<int>& releaseFences)
239 size_t totalLayers = std::min(layers.size(), releaseFences.size());
240 size_t currentLayer = 0;
242 while (currentLayer < totalLayers) {
243 size_t count = std::min(totalLayers - currentLayer,
244 static_cast<size_t>(kMaxLength) / 3);
246 beginCommand(IComposerClient::Command::SET_RELEASE_FENCES,
248 for (size_t i = 0; i < count; i++) {
249 write64(layers[currentLayer + i]);
250 writeFence(releaseFences[currentLayer + i]);
254 currentLayer += count;
258 static constexpr uint16_t kSetColorTransformLength = 17;
259 void setColorTransform(const float* matrix, ColorTransform hint)
261 beginCommand(IComposerClient::Command::SET_COLOR_TRANSFORM,
262 kSetColorTransformLength);
263 for (int i = 0; i < 16; i++) {
264 writeFloat(matrix[i]);
266 writeSigned(static_cast<int32_t>(hint));
270 void setClientTarget(uint32_t slot, const native_handle_t* target,
271 int acquireFence, Dataspace dataspace,
272 const std::vector<IComposerClient::Rect>& damage)
274 bool doWrite = (damage.size() <= (kMaxLength - 4) / 4);
275 size_t length = 4 + ((doWrite) ? damage.size() * 4 : 0);
277 beginCommand(IComposerClient::Command::SET_CLIENT_TARGET, length);
279 writeHandle(target, true);
280 writeFence(acquireFence);
281 writeSigned(static_cast<int32_t>(dataspace));
282 // When there are too many rectangles in the damage region and doWrite
283 // is false, we write no rectangle at all which means the entire
284 // client target is damaged.
291 static constexpr uint16_t kSetOutputBufferLength = 3;
292 void setOutputBuffer(uint32_t slot, const native_handle_t* buffer,
295 beginCommand(IComposerClient::Command::SET_OUTPUT_BUFFER,
296 kSetOutputBufferLength);
298 writeHandle(buffer, true);
299 writeFence(releaseFence);
303 static constexpr uint16_t kValidateDisplayLength = 0;
304 void validateDisplay()
306 beginCommand(IComposerClient::Command::VALIDATE_DISPLAY,
307 kValidateDisplayLength);
311 static constexpr uint16_t kPresentOrValidateDisplayLength = 0;
312 void presentOrvalidateDisplay()
314 beginCommand(IComposerClient::Command::PRESENT_OR_VALIDATE_DISPLAY,
315 kPresentOrValidateDisplayLength);
319 static constexpr uint16_t kAcceptDisplayChangesLength = 0;
320 void acceptDisplayChanges()
322 beginCommand(IComposerClient::Command::ACCEPT_DISPLAY_CHANGES,
323 kAcceptDisplayChangesLength);
327 static constexpr uint16_t kPresentDisplayLength = 0;
328 void presentDisplay()
330 beginCommand(IComposerClient::Command::PRESENT_DISPLAY,
331 kPresentDisplayLength);
335 static constexpr uint16_t kSetLayerCursorPositionLength = 2;
336 void setLayerCursorPosition(int32_t x, int32_t y)
338 beginCommand(IComposerClient::Command::SET_LAYER_CURSOR_POSITION,
339 kSetLayerCursorPositionLength);
345 static constexpr uint16_t kSetLayerBufferLength = 3;
346 void setLayerBuffer(uint32_t slot, const native_handle_t* buffer,
349 beginCommand(IComposerClient::Command::SET_LAYER_BUFFER,
350 kSetLayerBufferLength);
352 writeHandle(buffer, true);
353 writeFence(acquireFence);
357 void setLayerSurfaceDamage(
358 const std::vector<IComposerClient::Rect>& damage)
360 bool doWrite = (damage.size() <= kMaxLength / 4);
361 size_t length = (doWrite) ? damage.size() * 4 : 0;
363 beginCommand(IComposerClient::Command::SET_LAYER_SURFACE_DAMAGE,
365 // When there are too many rectangles in the damage region and doWrite
366 // is false, we write no rectangle at all which means the entire
374 static constexpr uint16_t kSetLayerBlendModeLength = 1;
375 void setLayerBlendMode(IComposerClient::BlendMode mode)
377 beginCommand(IComposerClient::Command::SET_LAYER_BLEND_MODE,
378 kSetLayerBlendModeLength);
379 writeSigned(static_cast<int32_t>(mode));
383 static constexpr uint16_t kSetLayerColorLength = 1;
384 void setLayerColor(IComposerClient::Color color)
386 beginCommand(IComposerClient::Command::SET_LAYER_COLOR,
387 kSetLayerColorLength);
392 static constexpr uint16_t kSetLayerCompositionTypeLength = 1;
393 void setLayerCompositionType(IComposerClient::Composition type)
395 beginCommand(IComposerClient::Command::SET_LAYER_COMPOSITION_TYPE,
396 kSetLayerCompositionTypeLength);
397 writeSigned(static_cast<int32_t>(type));
401 static constexpr uint16_t kSetLayerDataspaceLength = 1;
402 void setLayerDataspace(Dataspace dataspace)
404 beginCommand(IComposerClient::Command::SET_LAYER_DATASPACE,
405 kSetLayerDataspaceLength);
406 writeSigned(static_cast<int32_t>(dataspace));
410 static constexpr uint16_t kSetLayerDisplayFrameLength = 4;
411 void setLayerDisplayFrame(const IComposerClient::Rect& frame)
413 beginCommand(IComposerClient::Command::SET_LAYER_DISPLAY_FRAME,
414 kSetLayerDisplayFrameLength);
419 static constexpr uint16_t kSetLayerPlaneAlphaLength = 1;
420 void setLayerPlaneAlpha(float alpha)
422 beginCommand(IComposerClient::Command::SET_LAYER_PLANE_ALPHA,
423 kSetLayerPlaneAlphaLength);
428 static constexpr uint16_t kSetLayerSidebandStreamLength = 1;
429 void setLayerSidebandStream(const native_handle_t* stream)
431 beginCommand(IComposerClient::Command::SET_LAYER_SIDEBAND_STREAM,
432 kSetLayerSidebandStreamLength);
437 static constexpr uint16_t kSetLayerSourceCropLength = 4;
438 void setLayerSourceCrop(const IComposerClient::FRect& crop)
440 beginCommand(IComposerClient::Command::SET_LAYER_SOURCE_CROP,
441 kSetLayerSourceCropLength);
446 static constexpr uint16_t kSetLayerTransformLength = 1;
447 void setLayerTransform(Transform transform)
449 beginCommand(IComposerClient::Command::SET_LAYER_TRANSFORM,
450 kSetLayerTransformLength);
451 writeSigned(static_cast<int32_t>(transform));
455 void setLayerVisibleRegion(
456 const std::vector<IComposerClient::Rect>& visible)
458 bool doWrite = (visible.size() <= kMaxLength / 4);
459 size_t length = (doWrite) ? visible.size() * 4 : 0;
461 beginCommand(IComposerClient::Command::SET_LAYER_VISIBLE_REGION,
463 // When there are too many rectangles in the visible region and
464 // doWrite is false, we write no rectangle at all which means the
465 // entire layer is visible.
467 writeRegion(visible);
472 static constexpr uint16_t kSetLayerZOrderLength = 1;
473 void setLayerZOrder(uint32_t z)
475 beginCommand(IComposerClient::Command::SET_LAYER_Z_ORDER,
476 kSetLayerZOrderLength);
482 void beginCommand(IComposerClient::Command command, uint16_t length)
485 LOG_FATAL("endCommand was not called before command 0x%x",
489 growData(1 + length);
490 write(static_cast<uint32_t>(command) | length);
492 mCommandEnd = mDataWritten + length;
498 LOG_FATAL("beginCommand was not called");
499 } else if (mDataWritten > mCommandEnd) {
500 LOG_FATAL("too much data written");
501 mDataWritten = mCommandEnd;
502 } else if (mDataWritten < mCommandEnd) {
503 LOG_FATAL("too little data written");
504 while (mDataWritten < mCommandEnd) {
512 void write(uint32_t val)
514 mData[mDataWritten++] = val;
517 void writeSigned(int32_t val)
519 memcpy(&mData[mDataWritten++], &val, sizeof(val));
522 void writeFloat(float val)
524 memcpy(&mData[mDataWritten++], &val, sizeof(val));
527 void write64(uint64_t val)
529 uint32_t lo = static_cast<uint32_t>(val & 0xffffffff);
530 uint32_t hi = static_cast<uint32_t>(val >> 32);
535 void writeRect(const IComposerClient::Rect& rect)
537 writeSigned(rect.left);
538 writeSigned(rect.top);
539 writeSigned(rect.right);
540 writeSigned(rect.bottom);
543 void writeRegion(const std::vector<IComposerClient::Rect>& region)
545 for (const auto& rect : region) {
550 void writeFRect(const IComposerClient::FRect& rect)
552 writeFloat(rect.left);
553 writeFloat(rect.top);
554 writeFloat(rect.right);
555 writeFloat(rect.bottom);
558 void writeColor(const IComposerClient::Color& color)
560 write((color.r << 0) |
566 // ownership of handle is not transferred
567 void writeHandle(const native_handle_t* handle, bool useCache)
570 writeSigned(static_cast<int32_t>((useCache) ?
571 IComposerClient::HandleIndex::CACHED :
572 IComposerClient::HandleIndex::EMPTY));
576 mDataHandles.push_back(handle);
577 writeSigned(mDataHandles.size() - 1);
580 void writeHandle(const native_handle_t* handle)
582 writeHandle(handle, false);
585 // ownership of fence is transferred
586 void writeFence(int fence)
588 native_handle_t* handle = nullptr;
590 handle = getTemporaryHandle(1, 0);
592 handle->data[0] = fence;
594 ALOGW("failed to get temporary handle for fence %d", fence);
595 sync_wait(fence, -1);
603 native_handle_t* getTemporaryHandle(int numFds, int numInts)
605 native_handle_t* handle = native_handle_create(numFds, numInts);
607 mTemporaryHandles.push_back(handle);
612 static constexpr uint16_t kMaxLength =
613 std::numeric_limits<uint16_t>::max();
616 void growData(uint32_t grow)
618 uint32_t newWritten = mDataWritten + grow;
619 if (newWritten < mDataWritten) {
620 LOG_ALWAYS_FATAL("buffer overflowed; data written %" PRIu32
621 ", growing by %" PRIu32, mDataWritten, grow);
624 if (newWritten <= mDataMaxSize) {
628 uint32_t newMaxSize = mDataMaxSize << 1;
629 if (newMaxSize < newWritten) {
630 newMaxSize = newWritten;
633 auto newData = std::make_unique<uint32_t[]>(newMaxSize);
634 std::copy_n(mData.get(), mDataWritten, newData.get());
635 mDataMaxSize = newMaxSize;
636 mData = std::move(newData);
639 uint32_t mDataMaxSize;
640 std::unique_ptr<uint32_t[]> mData;
642 uint32_t mDataWritten;
643 // end offset of the current command
644 uint32_t mCommandEnd;
646 std::vector<hidl_handle> mDataHandles;
647 std::vector<native_handle_t *> mTemporaryHandles;
649 std::unique_ptr<CommandQueueType> mQueue;
652 // This class helps parse a command queue. Note that all sizes/lengths are in
653 // units of uint32_t's.
654 class CommandReaderBase {
656 CommandReaderBase() : mDataMaxSize(0)
661 bool setMQDescriptor(const MQDescriptorSync<uint32_t>& descriptor)
663 mQueue = std::make_unique<CommandQueueType>(descriptor, false);
664 if (mQueue->isValid()) {
672 bool readQueue(uint32_t commandLength,
673 const hidl_vec<hidl_handle>& commandHandles)
679 auto quantumCount = mQueue->getQuantumCount();
680 if (mDataMaxSize < quantumCount) {
681 mDataMaxSize = quantumCount;
682 mData = std::make_unique<uint32_t[]>(mDataMaxSize);
685 if (commandLength > mDataMaxSize ||
686 !mQueue->read(mData.get(), commandLength)) {
687 ALOGE("failed to read commands from message queue");
691 mDataSize = commandLength;
695 mDataHandles.setToExternal(
696 const_cast<hidl_handle*>(commandHandles.data()),
697 commandHandles.size());
708 mDataHandles.setToExternal(nullptr, 0);
714 return (mDataRead >= mDataSize);
717 bool beginCommand(IComposerClient::Command* outCommand,
721 LOG_FATAL("endCommand was not called for last command");
724 constexpr uint32_t opcode_mask =
725 static_cast<uint32_t>(IComposerClient::Command::OPCODE_MASK);
726 constexpr uint32_t length_mask =
727 static_cast<uint32_t>(IComposerClient::Command::LENGTH_MASK);
729 uint32_t val = read();
730 *outCommand = static_cast<IComposerClient::Command>(
732 *outLength = static_cast<uint16_t>(val & length_mask);
734 if (mDataRead + *outLength > mDataSize) {
735 ALOGE("command 0x%x has invalid command length %" PRIu16,
736 *outCommand, *outLength);
737 // undo the read() above
742 mCommandEnd = mDataRead + *outLength;
750 LOG_FATAL("beginCommand was not called");
751 } else if (mDataRead > mCommandEnd) {
752 LOG_FATAL("too much data read");
753 mDataRead = mCommandEnd;
754 } else if (mDataRead < mCommandEnd) {
755 LOG_FATAL("too little data read");
756 mDataRead = mCommandEnd;
759 mCommandBegin = mCommandEnd;
763 uint32_t getCommandLoc() const
765 return mCommandBegin;
770 return mData[mDataRead++];
776 memcpy(&val, &mData[mDataRead++], sizeof(val));
783 memcpy(&val, &mData[mDataRead++], sizeof(val));
789 uint32_t lo = read();
790 uint32_t hi = read();
791 return (static_cast<uint64_t>(hi) << 32) | lo;
794 IComposerClient::Color readColor()
796 uint32_t val = read();
797 return IComposerClient::Color{
798 static_cast<uint8_t>((val >> 0) & 0xff),
799 static_cast<uint8_t>((val >> 8) & 0xff),
800 static_cast<uint8_t>((val >> 16) & 0xff),
801 static_cast<uint8_t>((val >> 24) & 0xff),
805 // ownership of handle is not transferred
806 const native_handle_t* readHandle(bool* outUseCache)
808 const native_handle_t* handle = nullptr;
810 int32_t index = readSigned();
812 case static_cast<int32_t>(IComposerClient::HandleIndex::EMPTY):
813 *outUseCache = false;
815 case static_cast<int32_t>(IComposerClient::HandleIndex::CACHED):
819 if (static_cast<size_t>(index) < mDataHandles.size()) {
820 handle = mDataHandles[index].getNativeHandle();
822 ALOGE("invalid handle index %zu", static_cast<size_t>(index));
824 *outUseCache = false;
831 const native_handle_t* readHandle()
834 return readHandle(&useCache);
837 // ownership of fence is transferred
840 auto handle = readHandle();
841 if (!handle || handle->numFds == 0) {
845 if (handle->numFds != 1) {
846 ALOGE("invalid fence handle with %d fds", handle->numFds);
850 int fd = dup(handle->data[0]);
852 ALOGW("failed to dup fence %d", handle->data[0]);
853 sync_wait(handle->data[0], -1);
861 std::unique_ptr<CommandQueueType> mQueue;
862 uint32_t mDataMaxSize;
863 std::unique_ptr<uint32_t[]> mData;
868 // begin/end offsets of the current command
869 uint32_t mCommandBegin;
870 uint32_t mCommandEnd;
872 hidl_vec<hidl_handle> mDataHandles;
876 } // namespace composer
877 } // namespace graphics
878 } // namespace hardware
879 } // namespace android
881 #endif // ANDROID_HARDWARE_GRAPHICS_COMPOSER_COMMAND_BUFFER_H