blob: d7a0c57860e952a4922eafe253f510687c124bf7 [file] [log] [blame]
tomhudson@google.com168e6342012-04-18 17:49:20 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/private/SkSpinlock.h"
10#include "src/gpu/GrContextPriv.h"
11#include "src/gpu/GrGeometryProcessor.h"
12#include "src/gpu/GrMemoryPool.h"
13#include "src/gpu/GrProcessor.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040014#include "src/gpu/GrSamplerState.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrXferProcessor.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000017
Hal Canary6f6961e2017-01-31 13:50:44 -050018#if GR_TEST_UTILS
Robert Phillips296b1cc2017-03-15 10:42:12 -040019
20GrResourceProvider* GrProcessorTestData::resourceProvider() {
Robert Phillips9da87e02019-02-04 13:26:26 -050021 return fContext->priv().resourceProvider();
Robert Phillips296b1cc2017-03-15 10:42:12 -040022}
23
Robert Phillips1afd4cd2018-01-08 13:40:32 -050024GrProxyProvider* GrProcessorTestData::proxyProvider() {
Robert Phillips9da87e02019-02-04 13:26:26 -050025 return fContext->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050026}
27
Robert Phillips9da87e02019-02-04 13:26:26 -050028const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); }
Robert Phillips296b1cc2017-03-15 10:42:12 -040029
joshualitteb2a6762014-12-04 11:35:33 -080030class GrFragmentProcessor;
31class GrGeometryProcessor;
32
joshualitt9e87fa72014-10-09 13:12:35 -070033/*
34 * Originally these were both in the processor unit test header, but then it seemed to cause linker
35 * problems on android.
36 */
Brian Salomon1c053642017-07-24 10:16:19 -040037template <>
38SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
39 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
joshualitt9e87fa72014-10-09 13:12:35 -070040 return &gFactories;
41}
42
Brian Salomon1c053642017-07-24 10:16:19 -040043template <>
44SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
45 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
Brian Salomona8f80de2017-01-07 09:37:13 -050046 return &gFactories;
47}
48
Brian Salomona1633922017-01-09 11:46:10 -050049SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
50 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
51 return &gFactories;
52}
53
joshualitt9e87fa72014-10-09 13:12:35 -070054/*
55 * To ensure we always have successful static initialization, before creating from the factories
56 * we verify the count is as expected. If a new factory is added, then these numbers must be
57 * manually adjusted.
58 */
Ben Wagnerf1344ac2019-05-10 15:01:53 -040059#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
Brian Salomonf19f9ca2019-09-18 15:54:26 -040060static const int kFPFactoryCount = 37;
joshualitt4973d9d2014-11-08 09:24:25 -080061static const int kGPFactoryCount = 14;
Brian Salomon1c4717b2017-01-06 12:54:58 -050062static const int kXPFactoryCount = 4;
Ben Wagnerf1344ac2019-05-10 15:01:53 -040063#else
64static const int kFPFactoryCount = 0;
65static const int kGPFactoryCount = 0;
66static const int kXPFactoryCount = 0;
67#endif
joshualitt9e87fa72014-10-09 13:12:35 -070068
Brian Salomon1c053642017-07-24 10:16:19 -040069template <>
70void GrFragmentProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070071 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040072 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
73 kFPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040074 SK_ABORT("Wrong number of fragment processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070075 }
76}
77
Brian Salomon1c053642017-07-24 10:16:19 -040078template <>
79void GrGeometryProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070080 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040081 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
82 kGPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040083 SK_ABORT("Wrong number of geometry processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070084 }
85}
86
Brian Salomona1633922017-01-09 11:46:10 -050087void GrXPFactoryTestFactory::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080088 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040089 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
90 kXPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040091 SK_ABORT("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080092 }
93}
94
joshualitt9e87fa72014-10-09 13:12:35 -070095#endif
96
bsalomon5baedd62015-03-09 12:15:53 -070097
joshualitt23ac62c2015-03-30 09:53:47 -070098// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
99// different threads. The GrContext is not used concurrently on different threads and there is a
100// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -0700101// GrContexts and those contexts may be in use concurrently on different threads.
102namespace {
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400103#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
mtklein15923c92016-02-29 10:14:38 -0800104static SkSpinlock gProcessorSpinlock;
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400105#endif
bsalomon5baedd62015-03-09 12:15:53 -0700106class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000107public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000108
msarett68440f82016-08-29 14:52:24 -0700109// We know in the Android framework there is only one GrContext.
110#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
111 MemoryPoolAccessor() {}
112 ~MemoryPoolAccessor() {}
113#else
114 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700115 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700116#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000117
bsalomon5baedd62015-03-09 12:15:53 -0700118 GrMemoryPool* pool() const {
119 static GrMemoryPool gPool(4096, 4096);
120 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000121 }
122};
bsalomon5baedd62015-03-09 12:15:53 -0700123}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000124
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000125///////////////////////////////////////////////////////////////////////////////
126
Brian Salomonab015ef2017-04-04 10:15:51 -0400127void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000128
Brian Salomonab015ef2017-04-04 10:15:51 -0400129void GrProcessor::operator delete(void* target) {
130 return MemoryPoolAccessor().pool()->release(target);
131}