File: dawn_fallback_image_representation.cc

package info (click to toggle)
chromium 144.0.7559.109-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,915,868 kB
  • sloc: cpp: 35,866,215; ansic: 7,599,035; javascript: 3,623,761; python: 1,639,407; xml: 833,084; asm: 716,173; pascal: 185,323; sh: 88,763; perl: 88,699; objc: 79,984; sql: 58,217; cs: 42,430; fortran: 24,101; makefile: 20,747; tcl: 15,277; php: 14,022; yacc: 9,059; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,717 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/command_buffer/service/shared_image/dawn_fallback_image_representation.h"

#include <dawn/native/DawnNative.h>

#include "base/bits.h"
#include "base/synchronization/atomic_flag.h"
#include "base/threading/platform_thread.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/shared_image/dawn_copy_strategy.h"
#include "gpu/command_buffer/service/shared_image/shared_image_format_service_utils.h"
#include "gpu/config/gpu_finch_features.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "ui/gfx/geometry/skia_conversions.h"

namespace gpu {

DawnFallbackImageRepresentation::DawnFallbackImageRepresentation(
    SharedImageManager* manager,
    SharedImageBacking* backing,
    MemoryTypeTracker* tracker,
    wgpu::Device device,
    wgpu::TextureFormat wgpu_format,
    std::vector<wgpu::TextureFormat> view_formats)
    : DawnImageRepresentation(manager, backing, tracker),
      device_(device),
      wgpu_format_(wgpu_format),
      view_formats_(std::move(view_formats)) {}

DawnFallbackImageRepresentation::~DawnFallbackImageRepresentation() = default;

wgpu::Texture DawnFallbackImageRepresentation::BeginAccess(
    wgpu::TextureUsage wgpu_texture_usage,
    wgpu::TextureUsage internal_usage) {
  const std::string debug_label = "DawnFallbackSharedImageRep(" +
                                  CreateLabelForSharedImageUsage(usage()) + ")";

  wgpu::TextureDescriptor texture_descriptor;
  texture_descriptor.label = debug_label.c_str();
  texture_descriptor.format = wgpu_format_;
  texture_descriptor.usage = wgpu_texture_usage;

  texture_descriptor.dimension = wgpu::TextureDimension::e2D;
  texture_descriptor.size = {static_cast<uint32_t>(size().width()),
                             static_cast<uint32_t>(size().height()), 1};
  texture_descriptor.mipLevelCount = 1;
  texture_descriptor.sampleCount = 1;
  texture_descriptor.viewFormatCount = view_formats_.size();
  texture_descriptor.viewFormats = view_formats_.data();

  // Note: The texture must be internally copyable as this class itself uses the
  // texture as the dest and source of copies for readback from and upload to
  // the backing respectively.
  wgpu::DawnTextureInternalUsageDescriptor internalDesc;
  internalDesc.internalUsage = internal_usage | wgpu::TextureUsage::CopySrc |
                               wgpu::TextureUsage::CopyDst;

  texture_descriptor.nextInChain = &internalDesc;

  texture_ = device_.CreateTexture(&texture_descriptor);

  // Copy data from the image's backing to the texture. We only do it if the
  // image is marked as cleared/initialized.
  if (IsCleared() && !DawnCopyStrategy::CopyFromBackingToTexture(
                         backing(), texture_, device_)) {
    texture_ = nullptr;
  }

  return texture_;
}

void DawnFallbackImageRepresentation::EndAccess() {
  if (!texture_) {
    return;
  }

  // Upload the texture's content to the backing. Only do it if the texture is
  // initialized.
  if (dawn::native::IsTextureSubresourceInitialized(
          texture_.Get(), /*baseMipLevel=*/0, /*levelCount=*/1,
          /*baseArrayLayer=*/0,
          /*layerCount=*/1) &&
      DawnCopyStrategy::CopyFromTextureToBacking(texture_, backing(),
                                                 device_)) {
    SetCleared();
  }

  // All further operations on the textures are errors (they would be racy
  // with other backings).
  texture_.Destroy();

  texture_ = nullptr;
}

}  // namespace gpu