Skip to content

Commit

Permalink
Add ResourceCache and ProgramCache classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
domchen committed Feb 11, 2022
1 parent 4138fac commit 60919a0
Show file tree
Hide file tree
Showing 81 changed files with 549 additions and 315 deletions.
1 change: 1 addition & 0 deletions src/platform/android/GPUDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "GPUDrawable.h"
#include "base/utils/Log.h"
#include "gpu/Surface.h"

namespace pag {
Expand Down
4 changes: 2 additions & 2 deletions src/platform/android/VideoSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ jobject VideoSurface::getOutputSurface(JNIEnv* env) const {

bool VideoSurface::attachToContext(Context* context) {
if (oesTexture) {
if (deviceID != context->getDevice()->uniqueID()) {
if (deviceID != context->device()->uniqueID()) {
LOGE("VideoSurface::attachToGLContext(): VideoSurface has already attached to a Context!");
return false;
}
Expand All @@ -146,7 +146,7 @@ bool VideoSurface::attachToContext(Context* context) {
}
auto result = env->CallBooleanMethod(videoSurface.get(), VideoSurface_attachToGLContext,
oesTexture->getGLInfo().id);
deviceID = context->getDevice()->uniqueID();
deviceID = context->device()->uniqueID();
if (!result) {
LOGE("VideoSurface::attachToGLContext(): failed to attached to a Surface!");
oesTexture = nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/platform/cocoa/private/TraceImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "TraceImage.h"
#include "PixelBufferUtils.h"
#include "base/utils/Log.h"
#include "core/Bitmap.h"

namespace pag {
Expand Down
1 change: 1 addition & 0 deletions src/platform/web/GPUDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "GPUDrawable.h"
#include "base/utils/Log.h"
#include "gpu/Surface.h"
#include "gpu/opengl/GLDefines.h"

Expand Down
1 change: 1 addition & 0 deletions src/platform/win/GPUDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "GPUDrawable.h"
#include <windows.h>
#include "base/utils/Log.h"
#include "gpu/opengl/egl/EGLWindow.h"

namespace pag {
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/caches/RenderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ void RenderCache::prepareFrame() {
}

void RenderCache::attachToContext(Context* current, bool forHitTest) {
if (deviceID > 0 && deviceID != current->getDevice()->uniqueID()) {
if (deviceID > 0 && deviceID != current->device()->uniqueID()) {
// Context 改变需要清理内部所有缓存,这里用 uniqueID
// 而不用指针比较,是因为指针析构后再创建可能会地址重合。
releaseAll();
}
context = current;
deviceID = context->getDevice()->uniqueID();
deviceID = context->device()->uniqueID();
hitTestOnly = forHitTest;
if (hitTestOnly) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/graphics/Glyph.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#pragma once

#include "base/utils/BytesKey.h"
#include "core/Font.h"
#include "core/utils/BytesKey.h"
#include "pag/types.h"

namespace pag {
Expand Down
1 change: 1 addition & 0 deletions src/rendering/graphics/Modifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Modifier.h"
#include "Graphic.h"
#include "base/utils/MatrixUtil.h"
#include "base/utils/UniqueID.h"
#include "core/Blend.h"
#include "gpu/Surface.h"

Expand Down
2 changes: 1 addition & 1 deletion src/rendering/graphics/Picture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ class BackendTextureProxy : public TextureProxy {
void* sharedContext = nullptr;

bool checkContext(Context* context) const {
auto glDevice = static_cast<GLDevice*>(context->getDevice());
auto glDevice = static_cast<GLDevice*>(context->device());
if (!glDevice->sharableWith(sharedContext)) {
LOGE(
"A Graphic which made from a texture can not be drawn on to a PAGSurface"
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/graphics/TextureProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include "gpu/Context.h"
#include "gpu/Texture.h"

namespace pag {
class RenderCache;
Expand Down
1 change: 1 addition & 0 deletions src/rendering/utils/MemoryCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "MemoryCalculator.h"
#include "base/utils/Log.h"
#include "rendering/caches/LayerCache.h"

namespace pag {
Expand Down
1 change: 1 addition & 0 deletions test/framework/utils/LzmaUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include <vector>
#include "core/Data.h"

namespace pag {
Expand Down
4 changes: 2 additions & 2 deletions tgfx/include/core/Bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

#pragma once

#include "EncodedFormat.h"
#include "ImageInfo.h"
#include "core/Data.h"
#include "core/EncodedFormat.h"
#include "core/ImageInfo.h"
#include "core/PixelBuffer.h"

namespace pag {
Expand Down
2 changes: 0 additions & 2 deletions tgfx/include/core/Blend.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#pragma once

#include "pag/types.h"

namespace pag {
/**
* Defines constant values for visual blend mode effects.
Expand Down
3 changes: 2 additions & 1 deletion tgfx/include/core/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

#pragma once

#include "pag/types.h"
#include <memory>
#include <string>

namespace pag {
/**
Expand Down
2 changes: 1 addition & 1 deletion tgfx/include/core/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include "Typeface.h"
#include "core/Typeface.h"

namespace pag {
/**
Expand Down
7 changes: 3 additions & 4 deletions tgfx/include/core/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

#pragma once

#include "EncodedFormat.h"
#include "ImageInfo.h"
#include "Orientation.h"
#include "core/Data.h"
#include "pag/types.h"
#include "core/EncodedFormat.h"
#include "core/ImageInfo.h"
#include "core/Orientation.h"

namespace pag {

Expand Down
1 change: 0 additions & 1 deletion tgfx/include/core/ImageInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#pragma once

#include "base/utils/Log.h"
#include "pag/types.h"

namespace pag {
Expand Down
6 changes: 3 additions & 3 deletions tgfx/include/core/Mask.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

#pragma once

#include "Font.h"
#include "Path.h"
#include "TextBlob.h"
#include "core/Font.h"
#include "core/Path.h"
#include "core/Stroke.h"
#include "core/TextBlob.h"
#include "gpu/TextureBuffer.h"

namespace pag {
Expand Down
3 changes: 1 addition & 2 deletions tgfx/include/core/Path.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

#pragma once

#include "PathTypes.h"
#include "base/utils/Log.h"
#include "core/PathTypes.h"
#include "pag/types.h"

namespace pag {
Expand Down
2 changes: 1 addition & 1 deletion tgfx/include/core/PathEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include "Path.h"
#include "core/Path.h"
#include "core/Stroke.h"

namespace pag {
Expand Down
2 changes: 1 addition & 1 deletion tgfx/include/core/PathMeasure.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include "Path.h"
#include "core/Path.h"

namespace pag {
/**
Expand Down
2 changes: 1 addition & 1 deletion tgfx/include/core/PixelBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include "ImageInfo.h"
#include "core/ImageInfo.h"
#include "gpu/TextureBuffer.h"

namespace pag {
Expand Down
2 changes: 1 addition & 1 deletion tgfx/include/core/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include "pag/types.h"
#include <string>

namespace pag {
/**
Expand Down
4 changes: 2 additions & 2 deletions tgfx/include/core/TextBlob.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#pragma once

#include "Font.h"
#include "Path.h"
#include "core/Font.h"
#include "core/Path.h"
#include "core/Stroke.h"

namespace pag {
Expand Down
4 changes: 2 additions & 2 deletions tgfx/include/core/Typeface.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#pragma once

#include "FontMetrics.h"
#include "Path.h"
#include "core/FontMetrics.h"
#include "core/Path.h"
#include "gpu/TextureBuffer.h"

namespace pag {
Expand Down
69 changes: 28 additions & 41 deletions tgfx/include/gpu/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,51 @@

#pragma once

#include <list>
#include <unordered_map>
#include "base/utils/BytesKey.h"
#include "base/utils/UniqueID.h"
#include "core/Color4f.h"
#include "gpu/Caps.h"
#include "core/utils/BytesKey.h"
#include "gpu/Device.h"
#include "pag/gpu.h"

namespace pag {
class ProgramCache;

class Resource;

class Texture;

class Program;
class GradientCache;

class ProgramCreator;
class ResourceCache;

class GradientCache;
class Caps;

class Context {
public:
virtual ~Context();

/**
* Returns the unique ID of the associated device.
* Returns the associated device.
*/
Device* getDevice() const;
Device* device() const {
return _device;
}

/**
* Returns a program cache of specified ProgramMaker. If there is no associated cache available,
* a new program will be created by programMaker. Returns null if the programMaker fails to make a
* new program.
* Returns the associated cache that manages the lifetime of all gradients.
*/
Program* getProgram(const ProgramCreator* programMaker);
GradientCache* gradientCache() const {
return _gradientCache;
}

const Texture* getGradient(const Color4f* colors, const float* positions, int count);
/**
* Returns the associated cache that manages the lifetime of all Program instances.
*/
ProgramCache* programCache() const {
return _programCache;
}

/**
* Returns a reusable resource in the cache.
* Returns the associated cache that manages the lifetime of all Resource instances.
*/
std::shared_ptr<Resource> getRecycledResource(const BytesKey& recycleKey);
ResourceCache* resourceCache() const {
return _resourceCache;
}

/**
* Purges GPU resources that haven't been used in the past 'usNotUsed' microseconds.
Expand All @@ -81,33 +83,18 @@ class Context {
explicit Context(Device* device);

private:
Device* device = nullptr;
bool purgingResource = false;
std::list<Program*> programLRU = {};
std::unordered_map<BytesKey, Program*, BytesHasher> programMap = {};
GradientCache* gradientCache = nullptr;
std::vector<Resource*> nonpurgeableResources = {};
std::vector<std::shared_ptr<Resource>> strongReferences = {};
std::unordered_map<BytesKey, std::vector<Resource*>, BytesHasher> recycledResources = {};
std::mutex removeLocker = {};
std::vector<Resource*> pendingRemovedResources = {};

static void AddToList(std::vector<Resource*>& list, Resource* resource);
static void RemoveFromList(std::vector<Resource*>& list, Resource* resource);
static void NotifyReferenceReachedZero(Resource* resource);

std::shared_ptr<Resource> wrapResource(Resource* resource);
void removeResource(Resource* resource);
void removeOldestProgram(bool releaseGPU = true);
Device* _device = nullptr;
GradientCache* _gradientCache = nullptr;
ProgramCache* _programCache = nullptr;
ResourceCache* _resourceCache = nullptr;

void releaseAll(bool releaseGPU);
void onLocked();
void onUnlocked();

friend class Device;

friend class Resource;

friend class PurgeGuard;
};

} // namespace pag
3 changes: 1 addition & 2 deletions tgfx/include/gpu/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#pragma once

#include "base/utils/Log.h"
#include "pag/types.h"

namespace pag {
Expand Down Expand Up @@ -65,6 +64,6 @@ class Device {
uint32_t _uniqueID = 0;
bool contextLocked = false;

friend class Context;
friend class ResourceCache;
};
} // namespace pag
Loading

0 comments on commit 60919a0

Please sign in to comment.