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 : public TextureStorageAllocator { |
| 16 public: |
| 17 unsigned textureStorageTarget() override { |
| 18 return GR_GL_TEXTURE_2D; |
| 19 } |
| 20 bool allocateTextureStorage(unsigned textureId, unsigned width, unsigned heigh
t) override { |
| 21 m_mostRecentlyAllocatedStorage = textureId; |
| 22 |
| 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 GR_GL_CALL(m_gl, TexImage2D(textureStorageTarget(), 0, GR_GL_RGBA, width, he
ight, 0, GR_GL_RGBA, |
| 31 GR_GL_UNSIGNED_BYTE, pixels)); |
| 32 return true; |
| 33 } |
| 34 void deallocateTextureStorage(unsigned textureId) override { |
| 35 } |
| 36 |
| 37 unsigned m_mostRecentlyAllocatedStorage; |
| 38 const GrGLInterface* m_gl; |
| 39 }; |
| 40 |
| 41 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextureStorageAllocator, reporter, context, g
lContext) { |
| 42 static const int kWidth = 13; |
| 43 static const int kHeight = 13; |
| 44 |
| 45 const GrGLInterface* gl = glContext->gl(); |
| 46 TestStorageAllocator allocator; |
| 47 allocator.m_gl = gl; |
| 48 unsigned target = allocator.textureStorageTarget(); |
| 49 |
| 50 GrGLuint id; |
| 51 GR_GL_CALL(gl, GenTextures(1, &id)); |
| 52 GR_GL_CALL(gl, BindTexture(target, id)); |
| 53 REPORTER_ASSERT(reporter, allocator.allocateTextureStorage(id, kWidth, kHeig
ht)); |
| 54 REPORTER_ASSERT(reporter, allocator.m_mostRecentlyAllocatedStorage == id); |
| 55 |
| 56 GrBackendTextureDesc desc; |
| 57 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 58 desc.fWidth = kWidth; |
| 59 desc.fHeight = kHeight; |
| 60 desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 61 desc.fSampleCnt = 0; |
| 62 struct GrGLTextureInfo info; |
| 63 info.fTarget = target; |
| 64 info.fID = id; |
| 65 desc.fTextureHandle = reinterpret_cast<GrBackendObject>(&info); |
| 66 desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 67 desc.fTextureStorageAllocator = &allocator; |
| 68 SkSurfaceProps disableLCDProps(0, kUnknown_SkPixelGeometry); |
| 69 SkSurface* surface = SkSurface_Gpu::NewFromBackendTexture(context, desc, &di
sableLCDProps); |
| 70 |
| 71 REPORTER_ASSERT(reporter, surface); |
| 72 |
| 73 SkImage* image = surface->newImageSnapshot(); |
| 74 REPORTER_ASSERT(reporter, image->isTextureBacked()); |
| 75 SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(1,1); |
| 76 GrColor dest = 0x11223344; |
| 77 REPORTER_ASSERT(reporter, image->readPixels(imageInfo, &dest, 4 * kWidth, 0,
0)); |
| 78 REPORTER_ASSERT(reporter, GrColorUnpackG(dest) == 0); |
| 79 |
| 80 surface->getCanvas()->clear(SK_ColorGREEN); |
| 81 SkImage* image2 = surface->newImageSnapshot(); |
| 82 REPORTER_ASSERT(reporter, image2->isTextureBacked()); |
| 83 REPORTER_ASSERT(reporter, allocator.m_mostRecentlyAllocatedStorage != id); |
| 84 |
| 85 REPORTER_ASSERT(reporter, image2->readPixels(imageInfo, &dest, 4 * kWidth, 0
, 0)); |
| 86 REPORTER_ASSERT(reporter, GrColorUnpackG(dest) == 255); |
| 87 |
| 88 GR_GL_CALL(gl, DeleteTextures(1, &allocator.m_mostRecentlyAllocatedStorage))
; |
| 89 GR_GL_CALL(gl, DeleteTextures(1, &id)); |
| 90 } |
| 91 |
| 92 #endif |
OLD | NEW |