summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/canvas_resource_test.cc
blob: a042c27bfcf5207605b5239de72a28a0a63f27d9 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/graphics/canvas_resource.h"

#include "base/run_loop.h"
#include "components/viz/test/test_gpu_memory_buffer_manager.h"
#include "gpu/command_buffer/common/gpu_memory_buffer_support.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/graphics/gpu/shared_gpu_context.h"
#include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/test/fake_gles2_interface.h"
#include "third_party/blink/renderer/platform/graphics/test/fake_web_graphics_context_3d_provider.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/skia/include/core/SkSurface.h"

using testing::_;
using testing::Pointee;
using testing::Return;
using testing::SetArrayArgument;
using testing::Test;

namespace blink {

class MockGLES2InterfaceWithMailboxSupport : public FakeGLES2Interface {
 public:
  MOCK_METHOD2(ProduceTextureDirectCHROMIUM, void(GLuint, const GLbyte*));
  MOCK_METHOD1(GenMailboxCHROMIUM, void(GLbyte*));
  MOCK_METHOD1(GenUnverifiedSyncTokenCHROMIUM, void(GLbyte*));
  MOCK_METHOD4(CreateImageCHROMIUM,
               GLuint(ClientBuffer, GLsizei, GLsizei, GLenum));
  MOCK_METHOD2(BindTexture, void(GLenum, GLuint));
};

class FakeCanvasResourcePlatformSupport : public TestingPlatformSupport {
 public:
  void RunUntilStop() const { base::RunLoop().Run(); }

  void StopRunning() const { base::RunLoop().Quit(); }

 private:
  gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() override {
    return &test_gpu_memory_buffer_manager_;
  }

  viz::TestGpuMemoryBufferManager test_gpu_memory_buffer_manager_;
};

class CanvasResourceTest : public Test {
 public:
  void SetUp() override {
    // Install our mock GL context so that it gets served by SharedGpuContext.
    auto factory = [](FakeGLES2Interface* gl, bool* gpu_compositing_disabled)
        -> std::unique_ptr<WebGraphicsContext3DProvider> {
      *gpu_compositing_disabled = false;
      return std::make_unique<FakeWebGraphicsContext3DProvider>(gl);
    };
    SharedGpuContext::SetContextProviderFactoryForTesting(
        WTF::BindRepeating(factory, WTF::Unretained(&gl_)));
    context_provider_wrapper_ = SharedGpuContext::ContextProviderWrapper();
  }

  void TearDown() override { SharedGpuContext::ResetForTesting(); }

  GrContext* GetGrContext() {
    return context_provider_wrapper_->ContextProvider()->GetGrContext();
  }

 protected:
  MockGLES2InterfaceWithMailboxSupport gl_;
  base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper_;
};

gpu::SyncToken GenTestSyncToken(int id) {
  gpu::SyncToken token;
  token.Set(gpu::CommandBufferNamespace::GPU_IO,
            gpu::CommandBufferId::FromUnsafeValue(id), 1);
  return token;
}

TEST_F(CanvasResourceTest, SkiaResourceNoMailboxLeak) {
  testing::InSequence s;
  SkImageInfo image_info =
      SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, nullptr);
  sk_sp<SkSurface> surface =
      SkSurface::MakeRenderTarget(GetGrContext(), SkBudgeted::kYes, image_info);

  testing::Mock::VerifyAndClearExpectations(&gl_);

  EXPECT_TRUE(!!context_provider_wrapper_);
  scoped_refptr<CanvasResource> resource = CanvasResourceBitmap::Create(
      StaticBitmapImage::Create(surface->makeImageSnapshot(),
                                context_provider_wrapper_),
      nullptr, kLow_SkFilterQuality, CanvasColorParams());

  testing::Mock::VerifyAndClearExpectations(&gl_);

  gpu::Mailbox test_mailbox;
  test_mailbox.name[0] = 1;
  EXPECT_CALL(gl_, GenMailboxCHROMIUM(_))
      .WillOnce(SetArrayArgument<0>(
          test_mailbox.name, test_mailbox.name + GL_MAILBOX_SIZE_CHROMIUM));
  EXPECT_CALL(gl_, BindTexture(GL_TEXTURE_2D, _)).Times(2);
  EXPECT_CALL(gl_, ProduceTextureDirectCHROMIUM(_, _));
  EXPECT_CALL(gl_, GenUnverifiedSyncTokenCHROMIUM(_));
  resource->GetOrCreateGpuMailbox();

  testing::Mock::VerifyAndClearExpectations(&gl_);

  // No expected call to DeleteTextures becaus skia recycles
  // No expected call to ProduceTextureDirectCHROMIUM(0, *) because
  // mailbox is cached by GraphicsContext3DUtils and therefore does not need to
  // be orphaned.
  EXPECT_CALL(gl_, ProduceTextureDirectCHROMIUM(0, _)).Times(0);
  resource = nullptr;

  testing::Mock::VerifyAndClearExpectations(&gl_);

  // Purging skia's resource cache will finally delete the GrTexture, resulting
  // in the mailbox being orphaned via ProduceTextureDirectCHROMIUM.
  EXPECT_CALL(gl_,
              ProduceTextureDirectCHROMIUM(0, Pointee(test_mailbox.name[0])))
      .Times(0);
  GetGrContext()->performDeferredCleanup(std::chrono::milliseconds(0));

  testing::Mock::VerifyAndClearExpectations(&gl_);
}

TEST_F(CanvasResourceTest, GpuMemoryBufferSyncTokenRefresh) {
  testing::InSequence s;
  ScopedTestingPlatformSupport<FakeCanvasResourcePlatformSupport> platform;

  constexpr GLuint image_id = 1;
  EXPECT_CALL(gl_, CreateImageCHROMIUM(_, _, _, _)).WillOnce(Return(image_id));
  EXPECT_CALL(gl_, BindTexture(gpu::GetPlatformSpecificTextureTarget(), _));
  scoped_refptr<CanvasResource> resource =
      CanvasResourceGpuMemoryBuffer::Create(
          IntSize(10, 10), CanvasColorParams(),
          SharedGpuContext::ContextProviderWrapper(),
          nullptr,  // Resource provider
          kLow_SkFilterQuality);

  EXPECT_TRUE(bool(resource));

  testing::Mock::VerifyAndClearExpectations(&gl_);

  gpu::Mailbox test_mailbox;
  test_mailbox.name[0] = 1;
  EXPECT_CALL(gl_, GenMailboxCHROMIUM(_))
      .WillOnce(SetArrayArgument<0>(
          test_mailbox.name, test_mailbox.name + GL_MAILBOX_SIZE_CHROMIUM));
  EXPECT_CALL(gl_, ProduceTextureDirectCHROMIUM(_, _));
  resource->GetOrCreateGpuMailbox();

  testing::Mock::VerifyAndClearExpectations(&gl_);

  const gpu::SyncToken reference_token1 = GenTestSyncToken(1);
  EXPECT_CALL(gl_, GenUnverifiedSyncTokenCHROMIUM(_))
      .WillOnce(SetArrayArgument<0>(
          reinterpret_cast<const GLbyte*>(&reference_token1),
          reinterpret_cast<const GLbyte*>(&reference_token1 + 1)));
  gpu::SyncToken actual_token = resource->GetSyncToken();
  EXPECT_EQ(actual_token, reference_token1);

  testing::Mock::VerifyAndClearExpectations(&gl_);

  // Grabbing the mailbox again without making any changes must not result in
  // a sync token refresh
  EXPECT_CALL(gl_, GenMailboxCHROMIUM(_)).Times(0);
  EXPECT_CALL(gl_, GenUnverifiedSyncTokenCHROMIUM(_)).Times(0);
  resource->GetOrCreateGpuMailbox();
  actual_token = resource->GetSyncToken();
  EXPECT_EQ(actual_token, reference_token1);

  testing::Mock::VerifyAndClearExpectations(&gl_);

  // Grabbing the mailbox again after a content change must result in
  // a sync token refresh, but the mailbox gets re-used.
  EXPECT_CALL(gl_, GenMailboxCHROMIUM(_)).Times(0);
  const gpu::SyncToken reference_token2 = GenTestSyncToken(2);
  EXPECT_CALL(gl_, GenUnverifiedSyncTokenCHROMIUM(_))
      .WillOnce(SetArrayArgument<0>(
          reinterpret_cast<const GLbyte*>(&reference_token2),
          reinterpret_cast<const GLbyte*>(&reference_token2 + 1)));
  resource->CopyFromTexture(1,  // source texture id
                            GL_RGBA, GL_UNSIGNED_BYTE);
  resource->GetOrCreateGpuMailbox();
  actual_token = resource->GetSyncToken();
  EXPECT_EQ(actual_token, reference_token2);

  testing::Mock::VerifyAndClearExpectations(&gl_);
}

TEST_F(CanvasResourceTest, MakeAcceleratedFromAcceleratedResourceIsNoOp) {
  ScopedTestingPlatformSupport<FakeCanvasResourcePlatformSupport> platform;

  SkImageInfo image_info =
      SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, nullptr);
  sk_sp<SkSurface> surface =
      SkSurface::MakeRenderTarget(GetGrContext(), SkBudgeted::kYes, image_info);

  testing::Mock::VerifyAndClearExpectations(&gl_);

  EXPECT_TRUE(!!context_provider_wrapper_);
  scoped_refptr<CanvasResource> resource = CanvasResourceBitmap::Create(
      StaticBitmapImage::Create(surface->makeImageSnapshot(),
                                context_provider_wrapper_),
      nullptr, kLow_SkFilterQuality, CanvasColorParams());

  testing::Mock::VerifyAndClearExpectations(&gl_);

  EXPECT_TRUE(resource->IsAccelerated());
  scoped_refptr<CanvasResource> new_resource =
      resource->MakeAccelerated(context_provider_wrapper_);

  testing::Mock::VerifyAndClearExpectations(&gl_);

  EXPECT_EQ(new_resource.get(), resource.get());
  EXPECT_TRUE(new_resource->IsAccelerated());
}

TEST_F(CanvasResourceTest, MakeAcceleratedFromRasterResource) {
  ScopedTestingPlatformSupport<FakeCanvasResourcePlatformSupport> platform;

  SkImageInfo image_info =
      SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, nullptr);
  sk_sp<SkSurface> surface = SkSurface::MakeRaster(image_info);

  EXPECT_TRUE(!!context_provider_wrapper_);
  scoped_refptr<CanvasResource> resource = CanvasResourceBitmap::Create(
      StaticBitmapImage::Create(surface->makeImageSnapshot(),
                                context_provider_wrapper_),
      nullptr, kLow_SkFilterQuality, CanvasColorParams());

  testing::Mock::VerifyAndClearExpectations(&gl_);

  EXPECT_FALSE(resource->IsAccelerated());
  scoped_refptr<CanvasResource> new_resource =
      resource->MakeAccelerated(context_provider_wrapper_);

  testing::Mock::VerifyAndClearExpectations(&gl_);

  EXPECT_NE(new_resource.get(), resource.get());
  EXPECT_FALSE(resource->IsAccelerated());
  EXPECT_TRUE(new_resource->IsAccelerated());
}

TEST_F(CanvasResourceTest, MakeUnacceleratedFromUnacceleratedResourceIsNoOp) {
  ScopedTestingPlatformSupport<FakeCanvasResourcePlatformSupport> platform;

  SkImageInfo image_info =
      SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, nullptr);
  sk_sp<SkSurface> surface = SkSurface::MakeRaster(image_info);

  scoped_refptr<CanvasResource> resource = CanvasResourceBitmap::Create(
      StaticBitmapImage::Create(surface->makeImageSnapshot(),
                                context_provider_wrapper_),
      nullptr, kLow_SkFilterQuality, CanvasColorParams());

  EXPECT_FALSE(resource->IsAccelerated());
  scoped_refptr<CanvasResource> new_resource = resource->MakeUnaccelerated();

  EXPECT_EQ(new_resource.get(), resource.get());
  EXPECT_FALSE(new_resource->IsAccelerated());
}

TEST_F(CanvasResourceTest, MakeUnacceleratedFromAcceleratedResource) {
  ScopedTestingPlatformSupport<FakeCanvasResourcePlatformSupport> platform;

  SkImageInfo image_info =
      SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, nullptr);
  sk_sp<SkSurface> surface =
      SkSurface::MakeRenderTarget(GetGrContext(), SkBudgeted::kYes, image_info);

  EXPECT_TRUE(!!context_provider_wrapper_);
  scoped_refptr<CanvasResource> resource = CanvasResourceBitmap::Create(
      StaticBitmapImage::Create(surface->makeImageSnapshot(),
                                context_provider_wrapper_),
      nullptr, kLow_SkFilterQuality, CanvasColorParams());

  testing::Mock::VerifyAndClearExpectations(&gl_);

  EXPECT_TRUE(resource->IsAccelerated());
  scoped_refptr<CanvasResource> new_resource = resource->MakeUnaccelerated();

  testing::Mock::VerifyAndClearExpectations(&gl_);

  EXPECT_NE(new_resource.get(), resource.get());
  EXPECT_TRUE(resource->IsAccelerated());
  EXPECT_FALSE(new_resource->IsAccelerated());
}

}  // namespace blink