[go: nahoru, domu]

blob: c4a58851485bc2d965d1e7413d910e1a14e9ff4a [file] [log] [blame]
prashant.nb4d4f492016-04-29 12:51:281// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "cc/raster/zero_copy_raster_buffer_provider.h"
6
7#include <stdint.h>
8
9#include <algorithm>
10
11#include "base/macros.h"
12#include "base/memory/ptr_util.h"
13#include "base/strings/stringprintf.h"
14#include "base/trace_event/trace_event.h"
15#include "base/trace_event/trace_event_argument.h"
Xu Xingc101c862017-08-24 17:26:4516#include "cc/resources/layer_tree_resource_provider.h"
prashant.nb4d4f492016-04-29 12:51:2817#include "cc/resources/resource.h"
Fady Samuel555c8d12017-07-07 23:14:0918#include "components/viz/common/resources/platform_color.h"
prashant.nb4d4f492016-04-29 12:51:2819#include "ui/gfx/buffer_format_util.h"
20#include "ui/gfx/gpu_memory_buffer.h"
21
22namespace cc {
23namespace {
24
Daniel Bratell1bddcb332017-11-21 11:08:2925class ZeroCopyRasterBufferImpl : public RasterBuffer {
prashant.nb4d4f492016-04-29 12:51:2826 public:
Daniel Bratell1bddcb332017-11-21 11:08:2927 ZeroCopyRasterBufferImpl(LayerTreeResourceProvider* resource_provider,
28 const Resource* resource)
prashant.nb4d4f492016-04-29 12:51:2829 : lock_(resource_provider, resource->id()), resource_(resource) {}
30
31 // Overridden from RasterBuffer:
32 void Playback(
33 const RasterSource* raster_source,
34 const gfx::Rect& raster_full_rect,
35 const gfx::Rect& raster_dirty_rect,
36 uint64_t new_content_id,
trchen178ac912017-04-04 10:11:1037 const gfx::AxisTransform2d& transform,
prashant.nb4d4f492016-04-29 12:51:2838 const RasterSource::PlaybackSettings& playback_settings) override {
prashant.n60e135b02016-06-08 04:12:2339 TRACE_EVENT0("cc", "ZeroCopyRasterBuffer::Playback");
prashant.nb4d4f492016-04-29 12:51:2840 gfx::GpuMemoryBuffer* buffer = lock_.GetGpuMemoryBuffer();
41 if (!buffer)
42 return;
43
44 DCHECK_EQ(1u, gfx::NumberOfPlanesForBufferFormat(buffer->GetFormat()));
45 bool rv = buffer->Map();
46 DCHECK(rv);
47 DCHECK(buffer->memory(0));
48 // RasterBufferProvider::PlaybackToMemory only supports unsigned strides.
49 DCHECK_GE(buffer->stride(0), 0);
50
51 // TODO(danakj): Implement partial raster with raster_dirty_rect.
52 RasterBufferProvider::PlaybackToMemory(
53 buffer->memory(0), resource_->format(), resource_->size(),
54 buffer->stride(0), raster_source, raster_full_rect, raster_full_rect,
trchen178ac912017-04-04 10:11:1055 transform, lock_.color_space_for_raster(), playback_settings);
prashant.nb4d4f492016-04-29 12:51:2856 buffer->Unmap();
57 }
58
59 private:
Xu Xingc101c862017-08-24 17:26:4560 LayerTreeResourceProvider::ScopedWriteLockGpuMemoryBuffer lock_;
prashant.nb4d4f492016-04-29 12:51:2861 const Resource* resource_;
62
Daniel Bratell1bddcb332017-11-21 11:08:2963 DISALLOW_COPY_AND_ASSIGN(ZeroCopyRasterBufferImpl);
prashant.nb4d4f492016-04-29 12:51:2864};
65
66} // namespace
67
68// static
69std::unique_ptr<RasterBufferProvider> ZeroCopyRasterBufferProvider::Create(
Xu Xingc101c862017-08-24 17:26:4570 LayerTreeResourceProvider* resource_provider,
Fady Samuel555c8d12017-07-07 23:14:0971 viz::ResourceFormat preferred_tile_format) {
prashant.nb4d4f492016-04-29 12:51:2872 return base::WrapUnique<RasterBufferProvider>(
73 new ZeroCopyRasterBufferProvider(resource_provider,
74 preferred_tile_format));
75}
76
77ZeroCopyRasterBufferProvider::ZeroCopyRasterBufferProvider(
Xu Xingc101c862017-08-24 17:26:4578 LayerTreeResourceProvider* resource_provider,
Fady Samuel555c8d12017-07-07 23:14:0979 viz::ResourceFormat preferred_tile_format)
prashant.nb4d4f492016-04-29 12:51:2880 : resource_provider_(resource_provider),
81 preferred_tile_format_(preferred_tile_format) {}
82
Chris Watkinsf6353292017-12-04 02:36:0583ZeroCopyRasterBufferProvider::~ZeroCopyRasterBufferProvider() = default;
prashant.nb4d4f492016-04-29 12:51:2884
85std::unique_ptr<RasterBuffer>
86ZeroCopyRasterBufferProvider::AcquireBufferForRaster(
87 const Resource* resource,
88 uint64_t resource_content_id,
89 uint64_t previous_content_id) {
90 return base::WrapUnique<RasterBuffer>(
Daniel Bratell1bddcb332017-11-21 11:08:2991 new ZeroCopyRasterBufferImpl(resource_provider_, resource));
prashant.nb4d4f492016-04-29 12:51:2892}
93
sunnyps12074d3d2016-06-21 01:57:4894void ZeroCopyRasterBufferProvider::OrderingBarrier() {
prashant.nb4d4f492016-04-29 12:51:2895 // No need to sync resources as this provider does not use GL context.
prashant.nb4d4f492016-04-29 12:51:2896}
97
Sunny Sachanandani5f5419e22017-05-12 20:35:3098void ZeroCopyRasterBufferProvider::Flush() {}
99
Fady Samuel555c8d12017-07-07 23:14:09100viz::ResourceFormat ZeroCopyRasterBufferProvider::GetResourceFormat(
prashant.nb4d4f492016-04-29 12:51:28101 bool must_support_alpha) const {
ericrkd03c005d2017-03-24 18:48:52102 if (resource_provider_->IsTextureFormatSupported(preferred_tile_format_) &&
prashant.nb4d4f492016-04-29 12:51:28103 (DoesResourceFormatSupportAlpha(preferred_tile_format_) ||
104 !must_support_alpha)) {
105 return preferred_tile_format_;
106 }
107
108 return resource_provider_->best_texture_format();
109}
110
ericrkeeda58992016-07-07 02:34:27111bool ZeroCopyRasterBufferProvider::IsResourceSwizzleRequired(
prashant.nb4d4f492016-04-29 12:51:28112 bool must_support_alpha) const {
113 return ResourceFormatRequiresSwizzle(GetResourceFormat(must_support_alpha));
114}
115
ericrk5ac42f322016-07-14 01:06:51116bool ZeroCopyRasterBufferProvider::CanPartialRasterIntoProvidedResource()
117 const {
ericrkeeda58992016-07-07 02:34:27118 return false;
119}
120
ericrk7f6a27f2017-01-31 22:34:32121bool ZeroCopyRasterBufferProvider::IsResourceReadyToDraw(
Fady Samuelc80a4a862017-07-28 10:23:36122 viz::ResourceId resource_id) const {
ericrk7f6a27f2017-01-31 22:34:32123 // Zero-copy resources are immediately ready to draw.
124 return true;
125}
126
127uint64_t ZeroCopyRasterBufferProvider::SetReadyToDrawCallback(
128 const ResourceProvider::ResourceIdArray& resource_ids,
129 const base::Closure& callback,
130 uint64_t pending_callback_id) const {
131 // Zero-copy resources are immediately ready to draw.
132 return 0;
133}
134
prashant.nb4d4f492016-04-29 12:51:28135void ZeroCopyRasterBufferProvider::Shutdown() {}
136
137} // namespace cc