Index: tests/TextureStorageAllocator.cpp |
diff --git a/tests/TextureStorageAllocator.cpp b/tests/TextureStorageAllocator.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2381a7d460e2e2b6ee6d9942583e14c41566f550 |
--- /dev/null |
+++ b/tests/TextureStorageAllocator.cpp |
@@ -0,0 +1,79 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "Test.h" |
+#if SK_SUPPORT_GPU |
+#include "gl/GrGLGpu.h" |
+#include "SkSurface_Gpu.h" |
+#include "../include/gpu/gl/SkGLContext.h" |
+#include "../include/gpu/GrTypes.h" |
+ |
+class TestStorageAllocator : public TextureStorageAllocator { |
+ public: |
+ unsigned textureStorageTarget() override { |
+ return GR_GL_TEXTURE_2D; |
+ } |
+ bool allocateTextureStorage(GrBackendObject texture, unsigned width, unsigned height) override { |
+ GrColor pixels[width * height]; |
+ for (unsigned y = 0; y < height; ++y) { |
+ for (unsigned x = 0; x < width; ++x) { |
+ pixels[y * width + x] = 0; |
+ } |
+ } |
+ |
+ GrGLuint id; |
+ GR_GL_CALL(m_gl, GenTextures(1, &id)); |
+ GR_GL_CALL(m_gl, BindTexture(textureStorageTarget(), id)); |
+ GR_GL_CALL(m_gl, TexImage2D(textureStorageTarget(), 0, GR_GL_RGBA, width, height, 0, GR_GL_RGBA, |
+ GR_GL_UNSIGNED_BYTE, pixels)); |
+ GrGLTextureInfo* info = reinterpret_cast<GrGLTextureInfo*>(texture); |
+ info->fID = id; |
+ info->fTarget = textureStorageTarget(); |
+ m_mostRecentlyAllocatedStorage = id; |
+ |
+ return true; |
+ } |
+ void deallocateTextureStorage(GrBackendObject) override { |
bsalomon
2016/01/26 22:20:55
why not delete?
erikchen
2016/01/27 21:55:15
Added implementation.
|
+ } |
+ |
+ GrGLuint m_mostRecentlyAllocatedStorage; |
+ const GrGLInterface* m_gl; |
+}; |
+ |
+DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextureStorageAllocator, reporter, context, glContext) { |
+ static const int kWidth = 13; |
+ static const int kHeight = 13; |
+ |
+ const GrGLInterface* gl = glContext->gl(); |
+ TestStorageAllocator allocator; |
+ allocator.m_gl = gl; |
+ SkSurface* surface = SkSurface_Gpu::NewRenderTarget( |
+ context, SkSurface_Gpu::kNo_Budgeted, SkImageInfo::MakeN32Premul(kWidth, kHeight), 0, |
+ NULL, &allocator); |
+ REPORTER_ASSERT(reporter, surface); |
+ GrGLuint id = allocator.m_mostRecentlyAllocatedStorage; |
+ |
+ SkImage* image = surface->newImageSnapshot(); |
+ REPORTER_ASSERT(reporter, image->isTextureBacked()); |
+ SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(1,1); |
+ GrColor dest = 0x11223344; |
+ REPORTER_ASSERT(reporter, image->readPixels(imageInfo, &dest, 4 * kWidth, 0, 0)); |
+ REPORTER_ASSERT(reporter, GrColorUnpackG(dest) == 0); |
+ |
+ surface->getCanvas()->clear(SK_ColorGREEN); |
+ SkImage* image2 = surface->newImageSnapshot(); |
+ REPORTER_ASSERT(reporter, image2->isTextureBacked()); |
+ REPORTER_ASSERT(reporter, allocator.m_mostRecentlyAllocatedStorage != id); |
+ |
+ REPORTER_ASSERT(reporter, image2->readPixels(imageInfo, &dest, 4 * kWidth, 0, 0)); |
+ REPORTER_ASSERT(reporter, GrColorUnpackG(dest) == 255); |
+ |
+ GR_GL_CALL(gl, DeleteTextures(1, &allocator.m_mostRecentlyAllocatedStorage)); |
+ GR_GL_CALL(gl, DeleteTextures(1, &id)); |
+} |
+ |
+#endif |