1168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com/*
2168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com * Copyright 2012 Google Inc.
3168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com *
4168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com * Use of this source code is governed by a BSD-style license that can be
5168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com * found in the LICENSE file.
6168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com */
7168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
8b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt#include "GrProcessor.h"
92eaaefd7e6a58339b3f93333f1e9cc92252cc303bsalomon@google.com#include "GrContext.h"
102e3b3e369d79e78f7635d4c20e83a47ab571bdf2joshualitt#include "GrGeometryProcessor.h"
11dcba4c2cc30cc64f08def991376c6dab65cfb51ctomhudson@google.com#include "GrMemoryPool.h"
12514baff8be7f71111aa7bfb9b099a096b31e16ecBrian Salomon#include "GrSamplerParams.h"
130bbecb21ab82b3d742c491780bcc2e74be03efedBrian Salomon#include "GrTexturePriv.h"
14bc7a4fb06780f9829b4b21470fe6f0503d2297cdRobert Phillips#include "GrTextureProxy.h"
15915187b71d4068f21a15a1b8f4d919e1179d8b74egdaniel#include "GrXferProcessor.h"
1623ac62c83a49d675a38f1c20462b5537f3c8af01joshualitt#include "SkSpinlock.h"
17168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
186f6961ebad65c582318564b3688e78e5c99f3935Hal Canary#if GR_TEST_UTILS
19296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert Phillips
20296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert PhillipsGrResourceProvider* GrProcessorTestData::resourceProvider() {
21296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert Phillips    return fContext->resourceProvider();
22296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert Phillips}
23296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert Phillips
24296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert Phillipsconst GrCaps* GrProcessorTestData::caps() {
25296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert Phillips    return fContext->caps();
26296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert Phillips}
27296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert Phillips
28296b1ccf9b8e9c8b945645efcbaa9c71c7135f58Robert Phillips#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
29eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualittclass GrFragmentProcessor;
30eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualittclass GrGeometryProcessor;
31eb2a6761654307e8aeeeaabdd63c6bf9ab0411e9joshualitt
329e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt/*
339e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt * Originally these were both in the processor unit test header, but then it seemed to cause linker
349e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt * problems on android.
359e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt */
369e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitttemplate<>
379e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualittSkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>*
389e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualittGrProcessorTestFactory<GrFragmentProcessor>::GetFactories() {
399e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt    static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories;
409e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt    return &gFactories;
419e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt}
429e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt
439e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitttemplate<>
44003312a211e65f35e402d6fe80a32e23d4c94ac4Brian SalomonSkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
45003312a211e65f35e402d6fe80a32e23d4c94ac4Brian SalomonGrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
46003312a211e65f35e402d6fe80a32e23d4c94ac4Brian Salomon    static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
47a8f80de2bc17672b4b6f26d3cf6b38123ac850c9Brian Salomon    return &gFactories;
48a8f80de2bc17672b4b6f26d3cf6b38123ac850c9Brian Salomon}
49a8f80de2bc17672b4b6f26d3cf6b38123ac850c9Brian Salomon
50a16339297859f37df69230e64f05624cef511ad3Brian SalomonSkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
51a16339297859f37df69230e64f05624cef511ad3Brian Salomon    static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
52a16339297859f37df69230e64f05624cef511ad3Brian Salomon    return &gFactories;
53a16339297859f37df69230e64f05624cef511ad3Brian Salomon}
54a16339297859f37df69230e64f05624cef511ad3Brian Salomon
559e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt/*
569e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt * To ensure we always have successful static initialization, before creating from the factories
579e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt * we verify the count is as expected.  If a new factory is added, then these numbers must be
589e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt * manually adjusted.
599e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt */
60f2ae2b2dbcbdcc31b72fa86ed79f1cc02220528dMike Reedstatic const int kFPFactoryCount = 42;
614973d9da4aeb7c4d8b8e67e167586c7cc9534eeejoshualittstatic const int kGPFactoryCount = 14;
621c4717b54b21bc6c640864caf600ef16496803ecBrian Salomonstatic const int kXPFactoryCount = 4;
639e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt
649e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitttemplate<>
659e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualittvoid GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
669e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt    if (kFPFactoryCount != GetFactories()->count()) {
67c03e1c55a79f00d02ab528945425ff50cb700402Ben Wagner        SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
68c03e1c55a79f00d02ab528945425ff50cb700402Ben Wagner                 kFPFactoryCount, GetFactories()->count());
699e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt        SkFAIL("Wrong number of fragment processor factories!");
709e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt    }
719e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt}
729e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt
739e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitttemplate<>
749e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualittvoid GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
759e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt    if (kGPFactoryCount != GetFactories()->count()) {
76c03e1c55a79f00d02ab528945425ff50cb700402Ben Wagner        SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
77c03e1c55a79f00d02ab528945425ff50cb700402Ben Wagner                 kGPFactoryCount, GetFactories()->count());
789e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt        SkFAIL("Wrong number of geometry processor factories!");
799e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt    }
809e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt}
819e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt
82a16339297859f37df69230e64f05624cef511ad3Brian Salomonvoid GrXPFactoryTestFactory::VerifyFactoryCount() {
83378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel    if (kXPFactoryCount != GetFactories()->count()) {
84c03e1c55a79f00d02ab528945425ff50cb700402Ben Wagner        SkDebugf("\nExpected %d xp factory factories, found %d.\n",
85c03e1c55a79f00d02ab528945425ff50cb700402Ben Wagner                 kXPFactoryCount, GetFactories()->count());
86c230414861558ce20b74281b1ca363e56c441832egdaniel        SkFAIL("Wrong number of xp factory factories!");
87378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel    }
88378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel}
89378092f3d10b1dd62967f419c35cfefec7c10ee7egdaniel
909e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt#endif
916f6961ebad65c582318564b3688e78e5c99f3935Hal Canary#endif
929e87fa7c0e52a79ad85bf32eeb71570938e357bdjoshualitt
935baedd637806293e9da600af42ada8f75e7db580bsalomon
9423ac62c83a49d675a38f1c20462b5537f3c8af01joshualitt// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
9523ac62c83a49d675a38f1c20462b5537f3c8af01joshualitt// different threads. The GrContext is not used concurrently on different threads and there is a
9623ac62c83a49d675a38f1c20462b5537f3c8af01joshualitt// memory barrier between accesses of a context on different threads. Also, there may be multiple
975baedd637806293e9da600af42ada8f75e7db580bsalomon// GrContexts and those contexts may be in use concurrently on different threads.
985baedd637806293e9da600af42ada8f75e7db580bsalomonnamespace {
9915923c9e475894d89028b7a6a0b38aeeb9f9e645mtkleinstatic SkSpinlock gProcessorSpinlock;
1005baedd637806293e9da600af42ada8f75e7db580bsalomonclass MemoryPoolAccessor {
101dcba4c2cc30cc64f08def991376c6dab65cfb51ctomhudson@google.compublic:
102dcba4c2cc30cc64f08def991376c6dab65cfb51ctomhudson@google.com
10368440f8ce90a48e14e79b6fe5a6dd47c8c09bfb4msarett// We know in the Android framework there is only one GrContext.
10468440f8ce90a48e14e79b6fe5a6dd47c8c09bfb4msarett#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
10568440f8ce90a48e14e79b6fe5a6dd47c8c09bfb4msarett    MemoryPoolAccessor() {}
10668440f8ce90a48e14e79b6fe5a6dd47c8c09bfb4msarett    ~MemoryPoolAccessor() {}
10768440f8ce90a48e14e79b6fe5a6dd47c8c09bfb4msarett#else
10868440f8ce90a48e14e79b6fe5a6dd47c8c09bfb4msarett    MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
10923ac62c83a49d675a38f1c20462b5537f3c8af01joshualitt    ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
11068440f8ce90a48e14e79b6fe5a6dd47c8c09bfb4msarett#endif
111dcba4c2cc30cc64f08def991376c6dab65cfb51ctomhudson@google.com
1125baedd637806293e9da600af42ada8f75e7db580bsalomon    GrMemoryPool* pool() const {
1135baedd637806293e9da600af42ada8f75e7db580bsalomon        static GrMemoryPool gPool(4096, 4096);
1145baedd637806293e9da600af42ada8f75e7db580bsalomon        return &gPool;
115dcba4c2cc30cc64f08def991376c6dab65cfb51ctomhudson@google.com    }
116dcba4c2cc30cc64f08def991376c6dab65cfb51ctomhudson@google.com};
1175baedd637806293e9da600af42ada8f75e7db580bsalomon}
118dcba4c2cc30cc64f08def991376c6dab65cfb51ctomhudson@google.com
1195baedd637806293e9da600af42ada8f75e7db580bsalomonint32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
120168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
1210ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com///////////////////////////////////////////////////////////////////////////////
1220ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com
123ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomonvoid* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
124168e63418cadba4018aadf95c091d40d9deb13b9tomhudson@google.com
125ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomonvoid GrProcessor::operator delete(void* target) {
126ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon    return MemoryPoolAccessor().pool()->release(target);
127ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon}
128ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon
129ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon///////////////////////////////////////////////////////////////////////////////
130ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon
131ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomonvoid GrResourceIOProcessor::addTextureSampler(const TextureSampler* access) {
1320bbecb21ab82b3d742c491780bcc2e74be03efedBrian Salomon    fTextureSamplers.push_back(access);
133a5e65ec434fed44dc616e4f64950b835b541181btwiz@google.com}
134a5e65ec434fed44dc616e4f64950b835b541181btwiz@google.com
135ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomonvoid GrResourceIOProcessor::addBufferAccess(const BufferAccess* access) {
13674b8d323323c8533e3e5cc7719e0bd127aacd829cdalton    fBufferAccesses.push_back(access);
137f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon}
138f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon
139fbcef6eb8abad142daf45418516550f7635b4a52Robert Phillipsvoid GrResourceIOProcessor::addImageStorageAccess(const ImageStorageAccess* access) {
140f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon    fImageStorageAccesses.push_back(access);
141e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon}
142e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon
143ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomonvoid GrResourceIOProcessor::addPendingIOs() const {
144e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    for (const auto& sampler : fTextureSamplers) {
14518166eeaf2d088d494c4273b88107544166046fdRobert Phillips        sampler->programProxy()->markPendingIO();
146e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    }
147e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    for (const auto& buffer : fBufferAccesses) {
148e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon        buffer->programBuffer()->markPendingIO();
149e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    }
150e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    for (const auto& imageStorage : fImageStorageAccesses) {
1518a02f65c5cc16d010f188c34861b03d96cb8ec10Robert Phillips        imageStorage->programProxy()->markPendingIO();
152e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    }
153e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon}
154e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon
155ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomonvoid GrResourceIOProcessor::removeRefs() const {
156e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    for (const auto& sampler : fTextureSamplers) {
15718166eeaf2d088d494c4273b88107544166046fdRobert Phillips        sampler->programProxy()->removeRef();
158e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    }
159e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    for (const auto& buffer : fBufferAccesses) {
160e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon        buffer->programBuffer()->removeRef();
161e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    }
162e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    for (const auto& imageStorage : fImageStorageAccesses) {
1638a02f65c5cc16d010f188c34861b03d96cb8ec10Robert Phillips        imageStorage->programProxy()->removeRef();
164e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    }
165e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon}
166e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon
167ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomonvoid GrResourceIOProcessor::pendingIOComplete() const {
168e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    for (const auto& sampler : fTextureSamplers) {
16918166eeaf2d088d494c4273b88107544166046fdRobert Phillips        sampler->programProxy()->pendingIOComplete();
170e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    }
171e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    for (const auto& buffer : fBufferAccesses) {
172e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon        buffer->programBuffer()->pendingIOComplete();
173e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    }
174e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    for (const auto& imageStorage : fImageStorageAccesses) {
1758a02f65c5cc16d010f188c34861b03d96cb8ec10Robert Phillips        imageStorage->programProxy()->pendingIOComplete();
176e57194f74d125c7b0ba767ae5357b97d102792efBrian Salomon    }
17774b8d323323c8533e3e5cc7719e0bd127aacd829cdalton}
17874b8d323323c8533e3e5cc7719e0bd127aacd829cdalton
1799bee2e5894bb8dd374392f238bc429e16f239583Robert Phillipsbool GrResourceIOProcessor::instantiate(GrResourceProvider* resourceProvider) const {
1809bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips    for (const auto& sampler : fTextureSamplers) {
1819bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips        if (!sampler->instantiate(resourceProvider)) {
1829bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips            return false;
1839bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips        }
1849bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips    }
1859bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips
1865efd5ea4e146c47394d16c8b6f876264822a0beeRobert Phillips    // MDB TODO: instantiate 'fBufferAccesses' here as well
1875efd5ea4e146c47394d16c8b6f876264822a0beeRobert Phillips
1889bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips    for (const auto& imageStorage : fImageStorageAccesses) {
1899bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips        if (!imageStorage->instantiate(resourceProvider)) {
1909bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips            return false;
1919bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips        }
1929bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips    }
1939bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips
1949bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips    return true;
1959bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips}
1969bee2e5894bb8dd374392f238bc429e16f239583Robert Phillips
197ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomonbool GrResourceIOProcessor::hasSameSamplersAndAccesses(const GrResourceIOProcessor& that) const {
1980bbecb21ab82b3d742c491780bcc2e74be03efedBrian Salomon    if (this->numTextureSamplers() != that.numTextureSamplers() ||
199f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon        this->numBuffers() != that.numBuffers() ||
200f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon        this->numImageStorages() != that.numImageStorages()) {
201420d7e9a79358908850c74192b4949375563449absalomon        return false;
202420d7e9a79358908850c74192b4949375563449absalomon    }
2030bbecb21ab82b3d742c491780bcc2e74be03efedBrian Salomon    for (int i = 0; i < this->numTextureSamplers(); ++i) {
2040bbecb21ab82b3d742c491780bcc2e74be03efedBrian Salomon        if (this->textureSampler(i) != that.textureSampler(i)) {
205420d7e9a79358908850c74192b4949375563449absalomon            return false;
206420d7e9a79358908850c74192b4949375563449absalomon        }
20777af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    }
20874b8d323323c8533e3e5cc7719e0bd127aacd829cdalton    for (int i = 0; i < this->numBuffers(); ++i) {
20974b8d323323c8533e3e5cc7719e0bd127aacd829cdalton        if (this->bufferAccess(i) != that.bufferAccess(i)) {
21074b8d323323c8533e3e5cc7719e0bd127aacd829cdalton            return false;
21174b8d323323c8533e3e5cc7719e0bd127aacd829cdalton        }
21274b8d323323c8533e3e5cc7719e0bd127aacd829cdalton    }
213f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon    for (int i = 0; i < this->numImageStorages(); ++i) {
214f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon        if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) {
215f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            return false;
216f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon        }
217f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon    }
218420d7e9a79358908850c74192b4949375563449absalomon    return true;
21977af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com}
2201a8ecdfb73a15de600d5779b75d7c4b61863c50begdaniel
221a5305a110ab5201d5dadd40cbe711582d5ac4996joshualitt///////////////////////////////////////////////////////////////////////////////////////////////////
2221a8ecdfb73a15de600d5779b75d7c4b61863c50begdaniel
223ab015efc48c462ffdffebb45c02cd19efb254983Brian SalomonGrResourceIOProcessor::TextureSampler::TextureSampler() {}
2240bbecb21ab82b3d742c491780bcc2e74be03efedBrian Salomon
225fbcef6eb8abad142daf45418516550f7635b4a52Robert PhillipsGrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
226ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                      const GrSamplerParams& params) {
227fbcef6eb8abad142daf45418516550f7635b4a52Robert Phillips    this->reset(std::move(proxy), params);
228901f29ad3e38b7072a2abef5ff1665cd755d21a2Robert Phillips}
229901f29ad3e38b7072a2abef5ff1665cd755d21a2Robert Phillips
230fbcef6eb8abad142daf45418516550f7635b4a52Robert PhillipsGrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
231ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                      GrSamplerParams::FilterMode filterMode,
232ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                      SkShader::TileMode tileXAndY,
233ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                      GrShaderFlags visibility) {
234fbcef6eb8abad142daf45418516550f7635b4a52Robert Phillips    this->reset(std::move(proxy), filterMode, tileXAndY, visibility);
235bc7a4fb06780f9829b4b21470fe6f0503d2297cdRobert Phillips}
236bc7a4fb06780f9829b4b21470fe6f0503d2297cdRobert Phillips
237fbcef6eb8abad142daf45418516550f7635b4a52Robert Phillipsvoid GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
238ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                  const GrSamplerParams& params,
239ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                  GrShaderFlags visibility) {
240a91e0b7cc2537dc57ccf67914638e13b4612ffd1Robert Phillips    fParams = params;
24118166eeaf2d088d494c4273b88107544166046fdRobert Phillips    fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
24218166eeaf2d088d494c4273b88107544166046fdRobert Phillips    fParams.setFilterMode(SkTMin(params.filterMode(), this->proxy()->highestFilterMode()));
24330f9bc69cfc506075e1fce8e7934f941c0203023Robert Phillips    fVisibility = visibility;
24430f9bc69cfc506075e1fce8e7934f941c0203023Robert Phillips}
24530f9bc69cfc506075e1fce8e7934f941c0203023Robert Phillips
246fbcef6eb8abad142daf45418516550f7635b4a52Robert Phillipsvoid GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
247ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                  GrSamplerParams::FilterMode filterMode,
248ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                  SkShader::TileMode tileXAndY,
249ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                  GrShaderFlags visibility) {
25018166eeaf2d088d494c4273b88107544166046fdRobert Phillips    fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
25118166eeaf2d088d494c4273b88107544166046fdRobert Phillips    filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
25230f9bc69cfc506075e1fce8e7934f941c0203023Robert Phillips    fParams.reset(tileXAndY, filterMode);
25330f9bc69cfc506075e1fce8e7934f941c0203023Robert Phillips    fVisibility = visibility;
25430f9bc69cfc506075e1fce8e7934f941c0203023Robert Phillips}
25530f9bc69cfc506075e1fce8e7934f941c0203023Robert Phillips
2560bbecb21ab82b3d742c491780bcc2e74be03efedBrian Salomon///////////////////////////////////////////////////////////////////////////////////////////////////
2570bbecb21ab82b3d742c491780bcc2e74be03efedBrian Salomon
2588a02f65c5cc16d010f188c34861b03d96cb8ec10Robert PhillipsGrResourceIOProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTextureProxy> proxy,
259ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                              GrIOType ioType,
260ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                              GrSLMemoryModel memoryModel,
261ab015efc48c462ffdffebb45c02cd19efb254983Brian Salomon                                                              GrSLRestrict restrict,
2628a02f65c5cc16d010f188c34861b03d96cb8ec10Robert Phillips                                                              GrShaderFlags visibility)
2638a02f65c5cc16d010f188c34861b03d96cb8ec10Robert Phillips        : fProxyRef(std::move(proxy), ioType) {
2645efd5ea4e146c47394d16c8b6f876264822a0beeRobert Phillips    SkASSERT(fProxyRef.get());
2658a02f65c5cc16d010f188c34861b03d96cb8ec10Robert Phillips
266f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon    fMemoryModel = memoryModel;
267f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon    fRestrict = restrict;
268f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon    fVisibility = visibility;
269f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon    // We currently infer this from the config. However, we could allow the client to specify
270f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon    // a format that is different but compatible with the config.
2715efd5ea4e146c47394d16c8b6f876264822a0beeRobert Phillips    switch (fProxyRef.get()->config()) {
272f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon        case kRGBA_8888_GrPixelConfig:
273f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            fFormat = GrImageStorageFormat::kRGBA8;
274f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            break;
275f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon        case kRGBA_8888_sint_GrPixelConfig:
276f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            fFormat = GrImageStorageFormat::kRGBA8i;
277f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            break;
278f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon        case kRGBA_half_GrPixelConfig:
279f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            fFormat = GrImageStorageFormat::kRGBA16f;
280f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            break;
281f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon        case kRGBA_float_GrPixelConfig:
282f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            fFormat = GrImageStorageFormat::kRGBA32f;
283f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            break;
284f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon        default:
285f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            SkFAIL("Config is not (yet) supported as image storage.");
286f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon            break;
287f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon    }
288f9f451213a3951d8a61568998de2ddbd643f6693Brian Salomon}
289