Index: tests/TextureStorageAllocator.cpp |
diff --git a/tests/TextureStorageAllocator.cpp b/tests/TextureStorageAllocator.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..633fa999d8e51d29a74054d9ae035c8f75df8835 |
--- /dev/null |
+++ b/tests/TextureStorageAllocator.cpp |
@@ -0,0 +1,92 @@ |
+/* |
+ * 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(unsigned textureId, unsigned width, unsigned height) override { |
+ m_mostRecentlyAllocatedStorage = textureId; |
+ |
+ GrColor pixels[width * height]; |
+ for (unsigned y = 0; y < height; ++y) { |
+ for (unsigned x = 0; x < width; ++x) { |
+ pixels[y * width + x] = 0; |
+ } |
+ } |
+ |
+ GR_GL_CALL(m_gl, TexImage2D(textureStorageTarget(), 0, GR_GL_RGBA, width, height, 0, GR_GL_RGBA, |
+ GR_GL_UNSIGNED_BYTE, pixels)); |
+ return true; |
+ } |
+ void deallocateTextureStorage(unsigned textureId) override { |
+ } |
+ |
+ unsigned 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; |
+ unsigned target = allocator.textureStorageTarget(); |
+ |
+ GrGLuint id; |
+ GR_GL_CALL(gl, GenTextures(1, &id)); |
+ GR_GL_CALL(gl, BindTexture(target, id)); |
+ REPORTER_ASSERT(reporter, allocator.allocateTextureStorage(id, kWidth, kHeight)); |
+ REPORTER_ASSERT(reporter, allocator.m_mostRecentlyAllocatedStorage == id); |
+ |
+ GrBackendTextureDesc desc; |
+ desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
+ desc.fWidth = kWidth; |
+ desc.fHeight = kHeight; |
+ desc.fConfig = kRGBA_8888_GrPixelConfig; |
+ desc.fSampleCnt = 0; |
+ struct GrGLTextureInfo info; |
+ info.fTarget = target; |
+ info.fID = id; |
+ desc.fTextureHandle = reinterpret_cast<GrBackendObject>(&info); |
+ desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
+ desc.fTextureStorageAllocator = &allocator; |
+ SkSurfaceProps disableLCDProps(0, kUnknown_SkPixelGeometry); |
+ SkSurface* surface = SkSurface_Gpu::NewFromBackendTexture(context, desc, &disableLCDProps); |
+ |
+ REPORTER_ASSERT(reporter, surface); |
+ |
+ 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 |