OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 |
| 8 #include "Test.h" |
| 9 #if SK_SUPPORT_GPU |
| 10 #include "gl/GrGLGpu.h" |
| 11 #include "SkSurface_Gpu.h" |
| 12 #include "../include/gpu/gl/SkGLContext.h" |
| 13 #include "../include/gpu/GrTypes.h" |
| 14 |
| 15 class TestStorageAllocator { |
| 16 public: |
| 17 static GrTextureStorageAllocator::Result allocateTextureStorage(void* ctx, |
| 18 GrBackendObject texture, unsigned width, unsigned height, GrPixelConfi
g config, |
| 19 const void* srcData) { |
| 20 TestStorageAllocator* allocator = static_cast<TestStorageAllocator*>(ctx); |
| 21 if (!allocator->m_allowAllocation) |
| 22 return GrTextureStorageAllocator::Result::kFailed; |
| 23 GrColor pixels[width * height]; |
| 24 for (unsigned y = 0; y < height; ++y) { |
| 25 for (unsigned x = 0; x < width; ++x) { |
| 26 pixels[y * width + x] = 0; |
| 27 } |
| 28 } |
| 29 |
| 30 GrGLuint id; |
| 31 GrGLenum target = GR_GL_TEXTURE_2D; |
| 32 GR_GL_CALL(allocator->m_gl, GenTextures(1, &id)); |
| 33 GR_GL_CALL(allocator->m_gl, BindTexture(target, id)); |
| 34 GR_GL_CALL(allocator->m_gl, TexImage2D(target, 0, GR_GL_RGBA, width, heigh
t, 0, GR_GL_RGBA, |
| 35 GR_GL_UNSIGNED_BYTE, pixels)); |
| 36 GrGLTextureInfo* info = reinterpret_cast<GrGLTextureInfo*>(texture); |
| 37 info->fID = id; |
| 38 info->fTarget = target; |
| 39 allocator->m_mostRecentlyAllocatedStorage = id; |
| 40 return GrTextureStorageAllocator::Result::kSucceededWithoutUpload; |
| 41 } |
| 42 static void deallocateTextureStorage(void* ctx, GrBackendObject texture) { |
| 43 TestStorageAllocator* allocator = static_cast<TestStorageAllocator*>(ctx); |
| 44 GrGLTextureInfo* info = reinterpret_cast<GrGLTextureInfo*>(texture); |
| 45 GR_GL_CALL(allocator->m_gl, DeleteTextures(1, &(info->fID))); |
| 46 } |
| 47 |
| 48 GrGLuint m_mostRecentlyAllocatedStorage; |
| 49 const GrGLInterface* m_gl; |
| 50 bool m_allowAllocation; |
| 51 }; |
| 52 |
| 53 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CustomTexture, reporter, context, glContext)
{ |
| 54 static const int kWidth = 13; |
| 55 static const int kHeight = 13; |
| 56 |
| 57 const GrGLInterface* gl = glContext->gl(); |
| 58 TestStorageAllocator allocator; |
| 59 allocator.m_allowAllocation = true; |
| 60 allocator.m_gl = gl; |
| 61 GrTextureStorageAllocator grAllocator; |
| 62 grAllocator.fAllocateTextureStorage = &TestStorageAllocator::allocateTexture
Storage; |
| 63 grAllocator.fDeallocateTextureStorage= &TestStorageAllocator::deallocateText
ureStorage; |
| 64 grAllocator.fCtx = &allocator; |
| 65 |
| 66 SkSurface* surface = SkSurface_Gpu::NewRenderTarget( |
| 67 context, SkSurface_Gpu::kNo_Budgeted, SkImageInfo::MakeN32Premul(kWi
dth, kHeight), 0, |
| 68 NULL, grAllocator); |
| 69 REPORTER_ASSERT(reporter, surface); |
| 70 GrGLuint id = allocator.m_mostRecentlyAllocatedStorage; |
| 71 |
| 72 SkImage* image = surface->newImageSnapshot(); |
| 73 REPORTER_ASSERT(reporter, image->isTextureBacked()); |
| 74 SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(1,1); |
| 75 GrColor dest = 0x11223344; |
| 76 REPORTER_ASSERT(reporter, image->readPixels(imageInfo, &dest, 4 * kWidth, 0,
0)); |
| 77 REPORTER_ASSERT(reporter, GrColorUnpackG(dest) == 0); |
| 78 |
| 79 surface->getCanvas()->clear(SK_ColorGREEN); |
| 80 SkImage* image2 = surface->newImageSnapshot(); |
| 81 REPORTER_ASSERT(reporter, image2->isTextureBacked()); |
| 82 REPORTER_ASSERT(reporter, allocator.m_mostRecentlyAllocatedStorage != id); |
| 83 |
| 84 REPORTER_ASSERT(reporter, image2->readPixels(imageInfo, &dest, 4 * kWidth, 0
, 0)); |
| 85 REPORTER_ASSERT(reporter, GrColorUnpackG(dest) == 255); |
| 86 } |
| 87 |
| 88 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CustomTextureFailure, reporter, context, glCo
ntext) { |
| 89 static const int kWidth = 13; |
| 90 static const int kHeight = 13; |
| 91 |
| 92 const GrGLInterface* gl = glContext->gl(); |
| 93 TestStorageAllocator allocator; |
| 94 allocator.m_allowAllocation = false; |
| 95 allocator.m_gl = gl; |
| 96 GrTextureStorageAllocator grAllocator; |
| 97 grAllocator.fAllocateTextureStorage = &TestStorageAllocator::allocateTexture
Storage; |
| 98 grAllocator.fDeallocateTextureStorage= &TestStorageAllocator::deallocateText
ureStorage; |
| 99 grAllocator.fCtx = &allocator; |
| 100 SkSurface* surface = SkSurface_Gpu::NewRenderTarget( |
| 101 context, SkSurface_Gpu::kNo_Budgeted, SkImageInfo::MakeN32Premul(kWi
dth, kHeight), 0, |
| 102 NULL, grAllocator); |
| 103 REPORTER_ASSERT(reporter, !surface); |
| 104 } |
| 105 |
| 106 #endif |
OLD | NEW |