QGVAPConfigModel.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // QGVAPConfigModel.m
  2. // Tencent is pleased to support the open source community by making vap available.
  3. //
  4. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. //
  6. // Licensed under the MIT License (the "License"); you may not use this file except in
  7. // compliance with the License. You may obtain a copy of the License at
  8. //
  9. // http://opensource.org/licenses/MIT
  10. //
  11. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. // either express or implied. See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import "QGVAPConfigModel.h"
  16. #import "NSDictionary+VAPUtil.h"
  17. #import "QGVAPMetalUtil.h"
  18. #import "QGVAPLogger.h"
  19. #import "UIDevice+VAPUtil.h"
  20. //资源适配类型
  21. QGAGAttachmentFitType const kQGAGAttachmentFitTypeFitXY = @"fitXY"; //按指定尺寸缩放
  22. QGAGAttachmentFitType const kQGAGAttachmentFitTypeCenterFull = @"centerFull"; //默认按资源尺寸展示,如果资源尺寸小于遮罩,则等比缩放至可填满
  23. //资源类型
  24. QGAGAttachmentSourceType const kQGAGAttachmentSourceTypeTextStr = @"textStr"; //文字
  25. QGAGAttachmentSourceType const kQGAGAttachmentSourceTypeImgUrl = @"imgUrl"; //图片
  26. QGAGAttachmentSourceType const kQGAGAttachmentSourceTypeText = @"txt"; //文字
  27. QGAGAttachmentSourceType const kQGAGAttachmentSourceTypeImg = @"img"; //图片
  28. QGAGAttachmentSourceLoadType const QGAGAttachmentSourceLoadTypeLocal = @"local";
  29. QGAGAttachmentSourceLoadType const QGAGAttachmentSourceLoadTypeNet = @"net";
  30. //字体
  31. QGAGAttachmentSourceStyle const kQGAGAttachmentSourceStyleBoldText = @"b"; //粗体
  32. @implementation QGVAPConfigModel
  33. - (NSString *)description {
  34. return [NSString stringWithFormat:@"<%@: %p> {info:%@, configs:%@}", self.class, self, _info, _mergedConfig];
  35. }
  36. @end
  37. @implementation QGVAPCommonInfo
  38. - (NSString *)description {
  39. return [NSString stringWithFormat:@"<%@: %p> {version:%@, frames:%@, size:(%@,%@), videoSize:(%@,%@) orien:%@, fps:%@, merged:%@, alpha:(%@,%@,%@,%@), rgb:(%@,%@,%@,%@)}", self.class, self, @(_version), @(_framesCount), @(_size.width), @(_size.height), @(_videoSize.width), @(_videoSize.height), @(_targetOrientaion), @(_fps), @(_isMerged), @(_alphaAreaRect.origin.x), @(_alphaAreaRect.origin.y), @(_alphaAreaRect.size.width), @(_alphaAreaRect.size.height), @(_rgbAreaRect.origin.x), @(_rgbAreaRect.origin.y), @(_rgbAreaRect.size.width), @(_rgbAreaRect.size.height)];
  40. }
  41. @end
  42. @implementation QGVAPSourceInfo
  43. - (NSString *)description {
  44. return [NSString stringWithFormat:@"<%@: %p> {type:%@, tag:%@-%@ color:%@, style:%@, size:(%@,%@), fitType:%@}", self.class, self, _type, _contentTag, _contentTagValue, _color, _style, @(_size.width), @(_size.height), _fitType];
  45. }
  46. @end
  47. @implementation QGVAPSourceDisplayItem
  48. @end
  49. @implementation QGVAPMergedInfo
  50. - (id<MTLBuffer>)vertexBufferWithContainerSize:(CGSize)size maskContianerSize:(CGSize)mSize device:(id<MTLDevice>)device {
  51. if (size.width <= 0 || size.height <= 0 || mSize.width <= 0 || mSize.height <= 0) {
  52. VAP_Error(kQGVAPModuleCommon, @"vertexBufferWithContainerSize size error! :%@ - %@", [NSValue valueWithCGSize:size], [NSValue valueWithCGSize:mSize]);
  53. NSAssert(0, @"vertexBufferWithContainerSize size error!");
  54. return nil;
  55. }
  56. const int colunmCountForVertices = 4, colunmCountForCoordinate = 2, vertexDataLength = 32;
  57. float vertices[16], maskCoordinates[8], sourceCoordinates[8];
  58. genMTLVertices(self.renderRect, size, vertices, NO);
  59. genMTLTextureCoordinates(self.maskRect, mSize, maskCoordinates,YES, self.maskRotation);
  60. if ([self.source.fitType isEqualToString:kQGAGAttachmentFitTypeCenterFull]) {
  61. CGRect sourceRect = vapRectForCenterFull(self.source.size, self.renderRect.size);
  62. CGSize sourceSize = vapSourceSizeForCenterFull(self.source.size, self.renderRect.size);
  63. genMTLTextureCoordinates(sourceRect, sourceSize, sourceCoordinates,NO, 0);
  64. } else {
  65. replaceArrayElements(sourceCoordinates, (void*)kVAPMTLTextureCoordinatesIdentity, 8);
  66. }
  67. static float vertexData[vertexDataLength];
  68. int indexForVertexData = 0;
  69. //顶点数据+纹理坐标+遮罩纹理坐标
  70. for (int i = 0; i < 16; i ++) {
  71. vertexData[indexForVertexData++] = ((float*)vertices)[i];
  72. if (i%colunmCountForVertices == colunmCountForVertices-1) {
  73. int row = i/colunmCountForVertices;
  74. vertexData[indexForVertexData++] = ((float*)sourceCoordinates)[row*colunmCountForCoordinate];
  75. vertexData[indexForVertexData++] = ((float*)sourceCoordinates)[row*colunmCountForCoordinate+1];
  76. vertexData[indexForVertexData++] = ((float*)maskCoordinates)[row*colunmCountForCoordinate];
  77. vertexData[indexForVertexData++] = ((float*)maskCoordinates)[row*colunmCountForCoordinate+1];
  78. }
  79. }
  80. NSUInteger allocationSize = vertexDataLength * sizeof(float);
  81. id<MTLBuffer> vertexBuffer = [device newBufferWithBytes:vertexData length:allocationSize options:kDefaultMTLResourceOption];
  82. return vertexBuffer;
  83. }
  84. - (NSString *)description {
  85. return [NSString stringWithFormat:@"<%@: %p> {index:%@, rect:(%@,%@,%@,%@), mask:%@, maskRect:(%@,%@,%@,%@), maskRotation:%@, source:%@}", self.class, self, @(_renderIndex), @(_renderRect.origin.x), @(_renderRect.origin.y), @(_renderRect.size.width), @(_renderRect.size.height), @(_needMask), @(_maskRect.origin.x), @(_maskRect.origin.y), @(_maskRect.size.width), @(_maskRect.size.height), @(_maskRotation), _source];
  86. }
  87. @end