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 // write data to queue, optionally resizing it
96 if (mQueue && (mDataMaxSize <= mQueue->getQuantumCount())) {
97 if (!mQueue->write(mData.get(), mDataWritten)) {
98 ALOGE("failed to write commands to message queue");
102 *outQueueChanged = false;
104 auto newQueue = std::make_unique<CommandQueueType>(mDataMaxSize);
105 if (!newQueue->isValid() ||
106 !newQueue->write(mData.get(), mDataWritten)) {
107 ALOGE("failed to prepare a new message queue ");
111 mQueue = std::move(newQueue);
112 *outQueueChanged = true;
115 *outCommandLength = mDataWritten;
116 outCommandHandles->setToExternal(
117 const_cast<hidl_handle*>(mDataHandles.data()),
118 mDataHandles.size());
123 const MQDescriptorSync<uint32_t>* getMQDescriptor() const
125 return (mQueue) ? mQueue->getDesc() : nullptr;
128 static constexpr uint16_t kSelectDisplayLength = 2;
129 void selectDisplay(Display display)
131 beginCommand(IComposerClient::Command::SELECT_DISPLAY,
132 kSelectDisplayLength);
137 static constexpr uint16_t kSelectLayerLength = 2;
138 void selectLayer(Layer layer)
140 beginCommand(IComposerClient::Command::SELECT_LAYER,
146 static constexpr uint16_t kSetErrorLength = 2;
147 void setError(uint32_t location, Error error)
149 beginCommand(IComposerClient::Command::SET_ERROR, kSetErrorLength);
151 writeSigned(static_cast<int32_t>(error));
155 static constexpr uint32_t kPresentOrValidateDisplayResultLength = 1;
156 void setPresentOrValidateResult(uint32_t state) {
157 beginCommand(IComposerClient::Command::SET_PRESENT_OR_VALIDATE_DISPLAY_RESULT, kPresentOrValidateDisplayResultLength);
162 void setChangedCompositionTypes(const std::vector<Layer>& layers,
163 const std::vector<IComposerClient::Composition>& types)
165 size_t totalLayers = std::min(layers.size(), types.size());
166 size_t currentLayer = 0;
168 while (currentLayer < totalLayers) {
169 size_t count = std::min(totalLayers - currentLayer,
170 static_cast<size_t>(kMaxLength) / 3);
173 IComposerClient::Command::SET_CHANGED_COMPOSITION_TYPES,
175 for (size_t i = 0; i < count; i++) {
176 write64(layers[currentLayer + i]);
177 writeSigned(static_cast<int32_t>(types[currentLayer + i]));
181 currentLayer += count;
185 void setDisplayRequests(uint32_t displayRequestMask,
186 const std::vector<Layer>& layers,
187 const std::vector<uint32_t>& layerRequestMasks)
189 size_t totalLayers = std::min(layers.size(),
190 layerRequestMasks.size());
191 size_t currentLayer = 0;
193 while (currentLayer < totalLayers) {
194 size_t count = std::min(totalLayers - currentLayer,
195 static_cast<size_t>(kMaxLength - 1) / 3);
197 beginCommand(IComposerClient::Command::SET_DISPLAY_REQUESTS,
199 write(displayRequestMask);
200 for (size_t i = 0; i < count; i++) {
201 write64(layers[currentLayer + i]);
202 write(static_cast<int32_t>(layerRequestMasks[currentLayer + i]));
206 currentLayer += count;
210 static constexpr uint16_t kSetPresentFenceLength = 1;
211 void setPresentFence(int presentFence)
213 beginCommand(IComposerClient::Command::SET_PRESENT_FENCE,
214 kSetPresentFenceLength);
215 writeFence(presentFence);
219 void setReleaseFences(const std::vector<Layer>& layers,
220 const std::vector<int>& releaseFences)
222 size_t totalLayers = std::min(layers.size(), releaseFences.size());
223 size_t currentLayer = 0;
225 while (currentLayer < totalLayers) {
226 size_t count = std::min(totalLayers - currentLayer,
227 static_cast<size_t>(kMaxLength) / 3);
229 beginCommand(IComposerClient::Command::SET_RELEASE_FENCES,
231 for (size_t i = 0; i < count; i++) {
232 write64(layers[currentLayer + i]);
233 writeFence(releaseFences[currentLayer + i]);
237 currentLayer += count;
241 static constexpr uint16_t kSetColorTransformLength = 17;
242 void setColorTransform(const float* matrix, ColorTransform hint)
244 beginCommand(IComposerClient::Command::SET_COLOR_TRANSFORM,
245 kSetColorTransformLength);
246 for (int i = 0; i < 16; i++) {
247 writeFloat(matrix[i]);
249 writeSigned(static_cast<int32_t>(hint));
253 void setClientTarget(uint32_t slot, const native_handle_t* target,
254 int acquireFence, Dataspace dataspace,
255 const std::vector<IComposerClient::Rect>& damage)
257 bool doWrite = (damage.size() <= (kMaxLength - 4) / 4);
258 size_t length = 4 + ((doWrite) ? damage.size() * 4 : 0);
260 beginCommand(IComposerClient::Command::SET_CLIENT_TARGET, length);
262 writeHandle(target, true);
263 writeFence(acquireFence);
264 writeSigned(static_cast<int32_t>(dataspace));
265 // When there are too many rectangles in the damage region and doWrite
266 // is false, we write no rectangle at all which means the entire
267 // client target is damaged.
274 static constexpr uint16_t kSetOutputBufferLength = 3;
275 void setOutputBuffer(uint32_t slot, const native_handle_t* buffer,
278 beginCommand(IComposerClient::Command::SET_OUTPUT_BUFFER,
279 kSetOutputBufferLength);
281 writeHandle(buffer, true);
282 writeFence(releaseFence);
286 static constexpr uint16_t kValidateDisplayLength = 0;
287 void validateDisplay()
289 beginCommand(IComposerClient::Command::VALIDATE_DISPLAY,
290 kValidateDisplayLength);
294 static constexpr uint16_t kPresentOrValidateDisplayLength = 0;
295 void presentOrvalidateDisplay()
297 beginCommand(IComposerClient::Command::PRESENT_OR_VALIDATE_DISPLAY,
298 kPresentOrValidateDisplayLength);
302 static constexpr uint16_t kAcceptDisplayChangesLength = 0;
303 void acceptDisplayChanges()
305 beginCommand(IComposerClient::Command::ACCEPT_DISPLAY_CHANGES,
306 kAcceptDisplayChangesLength);
310 static constexpr uint16_t kPresentDisplayLength = 0;
311 void presentDisplay()
313 beginCommand(IComposerClient::Command::PRESENT_DISPLAY,
314 kPresentDisplayLength);
318 static constexpr uint16_t kSetLayerCursorPositionLength = 2;
319 void setLayerCursorPosition(int32_t x, int32_t y)
321 beginCommand(IComposerClient::Command::SET_LAYER_CURSOR_POSITION,
322 kSetLayerCursorPositionLength);
328 static constexpr uint16_t kSetLayerBufferLength = 3;
329 void setLayerBuffer(uint32_t slot, const native_handle_t* buffer,
332 beginCommand(IComposerClient::Command::SET_LAYER_BUFFER,
333 kSetLayerBufferLength);
335 writeHandle(buffer, true);
336 writeFence(acquireFence);
340 void setLayerSurfaceDamage(
341 const std::vector<IComposerClient::Rect>& damage)
343 bool doWrite = (damage.size() <= kMaxLength / 4);
344 size_t length = (doWrite) ? damage.size() * 4 : 0;
346 beginCommand(IComposerClient::Command::SET_LAYER_SURFACE_DAMAGE,
348 // When there are too many rectangles in the damage region and doWrite
349 // is false, we write no rectangle at all which means the entire
357 static constexpr uint16_t kSetLayerBlendModeLength = 1;
358 void setLayerBlendMode(IComposerClient::BlendMode mode)
360 beginCommand(IComposerClient::Command::SET_LAYER_BLEND_MODE,
361 kSetLayerBlendModeLength);
362 writeSigned(static_cast<int32_t>(mode));
366 static constexpr uint16_t kSetLayerColorLength = 1;
367 void setLayerColor(IComposerClient::Color color)
369 beginCommand(IComposerClient::Command::SET_LAYER_COLOR,
370 kSetLayerColorLength);
375 static constexpr uint16_t kSetLayerCompositionTypeLength = 1;
376 void setLayerCompositionType(IComposerClient::Composition type)
378 beginCommand(IComposerClient::Command::SET_LAYER_COMPOSITION_TYPE,
379 kSetLayerCompositionTypeLength);
380 writeSigned(static_cast<int32_t>(type));
384 static constexpr uint16_t kSetLayerDataspaceLength = 1;
385 void setLayerDataspace(Dataspace dataspace)
387 beginCommand(IComposerClient::Command::SET_LAYER_DATASPACE,
388 kSetLayerDataspaceLength);
389 writeSigned(static_cast<int32_t>(dataspace));
393 static constexpr uint16_t kSetLayerDisplayFrameLength = 4;
394 void setLayerDisplayFrame(const IComposerClient::Rect& frame)
396 beginCommand(IComposerClient::Command::SET_LAYER_DISPLAY_FRAME,
397 kSetLayerDisplayFrameLength);
402 static constexpr uint16_t kSetLayerPlaneAlphaLength = 1;
403 void setLayerPlaneAlpha(float alpha)
405 beginCommand(IComposerClient::Command::SET_LAYER_PLANE_ALPHA,
406 kSetLayerPlaneAlphaLength);
411 static constexpr uint16_t kSetLayerSidebandStreamLength = 1;
412 void setLayerSidebandStream(const native_handle_t* stream)
414 beginCommand(IComposerClient::Command::SET_LAYER_SIDEBAND_STREAM,
415 kSetLayerSidebandStreamLength);
420 static constexpr uint16_t kSetLayerSourceCropLength = 4;
421 void setLayerSourceCrop(const IComposerClient::FRect& crop)
423 beginCommand(IComposerClient::Command::SET_LAYER_SOURCE_CROP,
424 kSetLayerSourceCropLength);
429 static constexpr uint16_t kSetLayerTransformLength = 1;
430 void setLayerTransform(Transform transform)
432 beginCommand(IComposerClient::Command::SET_LAYER_TRANSFORM,
433 kSetLayerTransformLength);
434 writeSigned(static_cast<int32_t>(transform));
438 void setLayerVisibleRegion(
439 const std::vector<IComposerClient::Rect>& visible)
441 bool doWrite = (visible.size() <= kMaxLength / 4);
442 size_t length = (doWrite) ? visible.size() * 4 : 0;
444 beginCommand(IComposerClient::Command::SET_LAYER_VISIBLE_REGION,
446 // When there are too many rectangles in the visible region and
447 // doWrite is false, we write no rectangle at all which means the
448 // entire layer is visible.
450 writeRegion(visible);
455 static constexpr uint16_t kSetLayerZOrderLength = 1;
456 void setLayerZOrder(uint32_t z)
458 beginCommand(IComposerClient::Command::SET_LAYER_Z_ORDER,
459 kSetLayerZOrderLength);
465 void beginCommand(IComposerClient::Command command, uint16_t length)
468 LOG_FATAL("endCommand was not called before command 0x%x",
472 growData(1 + length);
473 write(static_cast<uint32_t>(command) | length);
475 mCommandEnd = mDataWritten + length;
481 LOG_FATAL("beginCommand was not called");
482 } else if (mDataWritten > mCommandEnd) {
483 LOG_FATAL("too much data written");
484 mDataWritten = mCommandEnd;
485 } else if (mDataWritten < mCommandEnd) {
486 LOG_FATAL("too little data written");
487 while (mDataWritten < mCommandEnd) {
495 void write(uint32_t val)
497 mData[mDataWritten++] = val;
500 void writeSigned(int32_t val)
502 memcpy(&mData[mDataWritten++], &val, sizeof(val));
505 void writeFloat(float val)
507 memcpy(&mData[mDataWritten++], &val, sizeof(val));
510 void write64(uint64_t val)
512 uint32_t lo = static_cast<uint32_t>(val & 0xffffffff);
513 uint32_t hi = static_cast<uint32_t>(val >> 32);
518 void writeRect(const IComposerClient::Rect& rect)
520 writeSigned(rect.left);
521 writeSigned(rect.top);
522 writeSigned(rect.right);
523 writeSigned(rect.bottom);
526 void writeRegion(const std::vector<IComposerClient::Rect>& region)
528 for (const auto& rect : region) {
533 void writeFRect(const IComposerClient::FRect& rect)
535 writeFloat(rect.left);
536 writeFloat(rect.top);
537 writeFloat(rect.right);
538 writeFloat(rect.bottom);
541 void writeColor(const IComposerClient::Color& color)
543 write((color.r << 0) |
549 // ownership of handle is not transferred
550 void writeHandle(const native_handle_t* handle, bool useCache)
553 writeSigned(static_cast<int32_t>((useCache) ?
554 IComposerClient::HandleIndex::CACHED :
555 IComposerClient::HandleIndex::EMPTY));
559 mDataHandles.push_back(handle);
560 writeSigned(mDataHandles.size() - 1);
563 void writeHandle(const native_handle_t* handle)
565 writeHandle(handle, false);
568 // ownership of fence is transferred
569 void writeFence(int fence)
571 native_handle_t* handle = nullptr;
573 handle = getTemporaryHandle(1, 0);
575 handle->data[0] = fence;
577 ALOGW("failed to get temporary handle for fence %d", fence);
578 sync_wait(fence, -1);
586 native_handle_t* getTemporaryHandle(int numFds, int numInts)
588 native_handle_t* handle = native_handle_create(numFds, numInts);
590 mTemporaryHandles.push_back(handle);
595 static constexpr uint16_t kMaxLength =
596 std::numeric_limits<uint16_t>::max();
599 void growData(uint32_t grow)
601 uint32_t newWritten = mDataWritten + grow;
602 if (newWritten < mDataWritten) {
603 LOG_ALWAYS_FATAL("buffer overflowed; data written %" PRIu32
604 ", growing by %" PRIu32, mDataWritten, grow);
607 if (newWritten <= mDataMaxSize) {
611 uint32_t newMaxSize = mDataMaxSize << 1;
612 if (newMaxSize < newWritten) {
613 newMaxSize = newWritten;
616 auto newData = std::make_unique<uint32_t[]>(newMaxSize);
617 std::copy_n(mData.get(), mDataWritten, newData.get());
618 mDataMaxSize = newMaxSize;
619 mData = std::move(newData);
622 uint32_t mDataMaxSize;
623 std::unique_ptr<uint32_t[]> mData;
625 uint32_t mDataWritten;
626 // end offset of the current command
627 uint32_t mCommandEnd;
629 std::vector<hidl_handle> mDataHandles;
630 std::vector<native_handle_t *> mTemporaryHandles;
632 std::unique_ptr<CommandQueueType> mQueue;
635 // This class helps parse a command queue. Note that all sizes/lengths are in
636 // units of uint32_t's.
637 class CommandReaderBase {
639 CommandReaderBase() : mDataMaxSize(0)
644 bool setMQDescriptor(const MQDescriptorSync<uint32_t>& descriptor)
646 mQueue = std::make_unique<CommandQueueType>(descriptor, false);
647 if (mQueue->isValid()) {
655 bool readQueue(uint32_t commandLength,
656 const hidl_vec<hidl_handle>& commandHandles)
662 auto quantumCount = mQueue->getQuantumCount();
663 if (mDataMaxSize < quantumCount) {
664 mDataMaxSize = quantumCount;
665 mData = std::make_unique<uint32_t[]>(mDataMaxSize);
668 if (commandLength > mDataMaxSize ||
669 !mQueue->read(mData.get(), commandLength)) {
670 ALOGE("failed to read commands from message queue");
674 mDataSize = commandLength;
678 mDataHandles.setToExternal(
679 const_cast<hidl_handle*>(commandHandles.data()),
680 commandHandles.size());
691 mDataHandles.setToExternal(nullptr, 0);
697 return (mDataRead >= mDataSize);
700 bool beginCommand(IComposerClient::Command* outCommand,
704 LOG_FATAL("endCommand was not called for last command");
707 constexpr uint32_t opcode_mask =
708 static_cast<uint32_t>(IComposerClient::Command::OPCODE_MASK);
709 constexpr uint32_t length_mask =
710 static_cast<uint32_t>(IComposerClient::Command::LENGTH_MASK);
712 uint32_t val = read();
713 *outCommand = static_cast<IComposerClient::Command>(
715 *outLength = static_cast<uint16_t>(val & length_mask);
717 if (mDataRead + *outLength > mDataSize) {
718 ALOGE("command 0x%x has invalid command length %" PRIu16,
719 *outCommand, *outLength);
720 // undo the read() above
725 mCommandEnd = mDataRead + *outLength;
733 LOG_FATAL("beginCommand was not called");
734 } else if (mDataRead > mCommandEnd) {
735 LOG_FATAL("too much data read");
736 mDataRead = mCommandEnd;
737 } else if (mDataRead < mCommandEnd) {
738 LOG_FATAL("too little data read");
739 mDataRead = mCommandEnd;
742 mCommandBegin = mCommandEnd;
746 uint32_t getCommandLoc() const
748 return mCommandBegin;
753 return mData[mDataRead++];
759 memcpy(&val, &mData[mDataRead++], sizeof(val));
766 memcpy(&val, &mData[mDataRead++], sizeof(val));
772 uint32_t lo = read();
773 uint32_t hi = read();
774 return (static_cast<uint64_t>(hi) << 32) | lo;
777 IComposerClient::Color readColor()
779 uint32_t val = read();
780 return IComposerClient::Color{
781 static_cast<uint8_t>((val >> 0) & 0xff),
782 static_cast<uint8_t>((val >> 8) & 0xff),
783 static_cast<uint8_t>((val >> 16) & 0xff),
784 static_cast<uint8_t>((val >> 24) & 0xff),
788 // ownership of handle is not transferred
789 const native_handle_t* readHandle(bool* outUseCache)
791 const native_handle_t* handle = nullptr;
793 int32_t index = readSigned();
795 case static_cast<int32_t>(IComposerClient::HandleIndex::EMPTY):
796 *outUseCache = false;
798 case static_cast<int32_t>(IComposerClient::HandleIndex::CACHED):
802 if (static_cast<size_t>(index) < mDataHandles.size()) {
803 handle = mDataHandles[index].getNativeHandle();
805 ALOGE("invalid handle index %zu", static_cast<size_t>(index));
807 *outUseCache = false;
814 const native_handle_t* readHandle()
817 return readHandle(&useCache);
820 // ownership of fence is transferred
823 auto handle = readHandle();
824 if (!handle || handle->numFds == 0) {
828 if (handle->numFds != 1) {
829 ALOGE("invalid fence handle with %d fds", handle->numFds);
833 int fd = dup(handle->data[0]);
835 ALOGW("failed to dup fence %d", handle->data[0]);
836 sync_wait(handle->data[0], -1);
844 std::unique_ptr<CommandQueueType> mQueue;
845 uint32_t mDataMaxSize;
846 std::unique_ptr<uint32_t[]> mData;
851 // begin/end offsets of the current command
852 uint32_t mCommandBegin;
853 uint32_t mCommandEnd;
855 hidl_vec<hidl_handle> mDataHandles;
859 } // namespace composer
860 } // namespace graphics
861 } // namespace hardware
862 } // namespace android
864 #endif // ANDROID_HARDWARE_GRAPHICS_COMPOSER_COMMAND_BUFFER_H