blob: 078cfbdf992c8e3fe0037019e8b565ba9c9df977 [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
joshualittb0a8a372014-09-23 09:50:21 -07008#include "GrProcessor.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +00009#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050010#include "GrContextPriv.h"
joshualitt2e3b3e32014-12-09 13:31:14 -080011#include "GrGeometryProcessor.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000012#include "GrMemoryPool.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040013#include "GrSamplerState.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050014#include "GrTextureProxy.h"
egdaniel915187b2014-12-05 12:58:28 -080015#include "GrXferProcessor.h"
joshualitt23ac62c2015-03-30 09:53:47 -070016#include "SkSpinlock.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 Phillips6be756b2018-01-16 15:07:54 -050021 return fContext->contextPriv().resourceProvider();
Robert Phillips296b1cc2017-03-15 10:42:12 -040022}
23
Robert Phillips1afd4cd2018-01-08 13:40:32 -050024GrProxyProvider* GrProcessorTestData::proxyProvider() {
25 return fContext->contextPriv().proxyProvider();
26}
27
Brian Salomonc7fe0f72018-05-11 10:14:21 -040028const GrCaps* GrProcessorTestData::caps() { return fContext->contextPriv().caps(); }
Robert Phillips296b1cc2017-03-15 10:42:12 -040029
30#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
joshualitteb2a6762014-12-04 11:35:33 -080031class GrFragmentProcessor;
32class GrGeometryProcessor;
33
joshualitt9e87fa72014-10-09 13:12:35 -070034/*
35 * Originally these were both in the processor unit test header, but then it seemed to cause linker
36 * problems on android.
37 */
Brian Salomon1c053642017-07-24 10:16:19 -040038template <>
39SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
40 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
joshualitt9e87fa72014-10-09 13:12:35 -070041 return &gFactories;
42}
43
Brian Salomon1c053642017-07-24 10:16:19 -040044template <>
45SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
46 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
Brian Salomona8f80de2017-01-07 09:37:13 -050047 return &gFactories;
48}
49
Brian Salomona1633922017-01-09 11:46:10 -050050SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
51 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
52 return &gFactories;
53}
54
joshualitt9e87fa72014-10-09 13:12:35 -070055/*
56 * To ensure we always have successful static initialization, before creating from the factories
57 * we verify the count is as expected. If a new factory is added, then these numbers must be
58 * manually adjusted.
59 */
Ethan Nicholas222e2752018-10-11 11:21:34 -040060static const int kFPFactoryCount = 35;
joshualitt4973d9d2014-11-08 09:24:25 -080061static const int kGPFactoryCount = 14;
Brian Salomon1c4717b2017-01-06 12:54:58 -050062static const int kXPFactoryCount = 4;
joshualitt9e87fa72014-10-09 13:12:35 -070063
Brian Salomon1c053642017-07-24 10:16:19 -040064template <>
65void GrFragmentProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070066 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040067 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
68 kFPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040069 SK_ABORT("Wrong number of fragment processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070070 }
71}
72
Brian Salomon1c053642017-07-24 10:16:19 -040073template <>
74void GrGeometryProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070075 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040076 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
77 kGPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040078 SK_ABORT("Wrong number of geometry processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070079 }
80}
81
Brian Salomona1633922017-01-09 11:46:10 -050082void GrXPFactoryTestFactory::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080083 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040084 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
85 kXPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040086 SK_ABORT("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080087 }
88}
89
joshualitt9e87fa72014-10-09 13:12:35 -070090#endif
Hal Canary6f6961e2017-01-31 13:50:44 -050091#endif
joshualitt9e87fa72014-10-09 13:12:35 -070092
bsalomon5baedd62015-03-09 12:15:53 -070093
joshualitt23ac62c2015-03-30 09:53:47 -070094// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
95// different threads. The GrContext is not used concurrently on different threads and there is a
96// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070097// GrContexts and those contexts may be in use concurrently on different threads.
98namespace {
Leon Scroggins III981a31e2017-10-06 11:53:53 -040099#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
mtklein15923c92016-02-29 10:14:38 -0800100static SkSpinlock gProcessorSpinlock;
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400101#endif
bsalomon5baedd62015-03-09 12:15:53 -0700102class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000103public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000104
msarett68440f82016-08-29 14:52:24 -0700105// We know in the Android framework there is only one GrContext.
106#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
107 MemoryPoolAccessor() {}
108 ~MemoryPoolAccessor() {}
109#else
110 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700111 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700112#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000113
bsalomon5baedd62015-03-09 12:15:53 -0700114 GrMemoryPool* pool() const {
115 static GrMemoryPool gPool(4096, 4096);
116 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000117 }
118};
bsalomon5baedd62015-03-09 12:15:53 -0700119}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000120
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000121///////////////////////////////////////////////////////////////////////////////
122
Brian Salomonab015ef2017-04-04 10:15:51 -0400123void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000124
Brian Salomonab015ef2017-04-04 10:15:51 -0400125void GrProcessor::operator delete(void* target) {
126 return MemoryPoolAccessor().pool()->release(target);
127}