OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2013, Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "platform/graphics/gpu/TextureImageBufferSurface.h" |
| 32 |
| 33 #include "gpu/command_buffer/client/gles2_interface.h" |
| 34 #include "gpu/command_buffer/common/mailbox.h" |
| 35 #include "gpu/command_buffer/common/sync_token.h" |
| 36 #include "platform/graphics/gpu/SharedGpuContext.h" |
| 37 #include "platform/graphics/skia/SkiaUtils.h" |
| 38 #include "platform/wtf/PtrUtil.h" |
| 39 #include "platform/wtf/RefPtr.h" |
| 40 #include "skia/ext/texture_handle.h" |
| 41 #include "third_party/skia/include/gpu/GrBackendSurface.h" |
| 42 #include "third_party/skia/include/gpu/GrContext.h" |
| 43 |
| 44 namespace blink { |
| 45 |
| 46 TextureImageBufferSurface::TextureImageBufferSurface( |
| 47 gpu::gles2::GLES2Interface* webgl_gl, |
| 48 GLenum texture_target, |
| 49 GLuint webgl_texture_id, |
| 50 const IntSize& size, |
| 51 OpacityMode opacity_mode, |
| 52 const CanvasColorParams& color_params) |
| 53 : ImageBufferSurface(size, opacity_mode, color_params) { |
| 54 if (!SharedGpuContext::IsValid()) |
| 55 return; |
| 56 gpu::gles2::GLES2Interface* gl = SharedGpuContext::Gl(); |
| 57 GrContext* gr_context = SharedGpuContext::Gr(); |
| 58 context_id_ = SharedGpuContext::ContextId(); |
| 59 CHECK(gr_context); |
| 60 |
| 61 gpu::Mailbox mailbox; |
| 62 webgl_gl->GenMailboxCHROMIUM(mailbox.name); |
| 63 DVLOG(1) << webgl_texture_id << " / " << texture_target; |
| 64 webgl_gl->ProduceTextureDirectCHROMIUM(webgl_texture_id, texture_target, |
| 65 mailbox.name); |
| 66 |
| 67 gpu::SyncToken sync_token; |
| 68 const GLuint64 fence_sync = webgl_gl->InsertFenceSyncCHROMIUM(); |
| 69 webgl_gl->Flush(); |
| 70 webgl_gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); |
| 71 gl->WaitSyncTokenCHROMIUM(sync_token.GetData()); |
| 72 |
| 73 GLuint texture = |
| 74 gl->CreateAndConsumeTextureCHROMIUM(texture_target, mailbox.name); |
| 75 |
| 76 SkAlphaType alpha_type = |
| 77 (kOpaque == opacity_mode) ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 78 SkImageInfo info = SkImageInfo::Make( |
| 79 size.Width(), size.Height(), color_params.GetSkColorType(), alpha_type, |
| 80 color_params.GetSkColorSpaceForSkSurfaces()); |
| 81 SkSurfaceProps disable_lcd_props(0, kUnknown_SkPixelGeometry); |
| 82 |
| 83 GrGLTextureInfo tinfo; |
| 84 tinfo.fTarget = texture_target; |
| 85 tinfo.fID = texture; |
| 86 GrBackendTextureDesc desc; |
| 87 desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 88 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 89 desc.fWidth = size.Width(); |
| 90 desc.fHeight = size.Height(); |
| 91 desc.fSampleCnt = 0; |
| 92 desc.fConfig = kSkia8888_GrPixelConfig; |
| 93 desc.fTextureHandle = skia::GrGLTextureInfoToGrBackendObject(tinfo); |
| 94 surface_ = SkSurface::MakeFromBackendTexture( |
| 95 gr_context, desc, color_params.GetSkColorSpaceForSkSurfaces(), |
| 96 kOpaque == opacity_mode ? nullptr : &disable_lcd_props); |
| 97 |
| 98 if (!surface_) |
| 99 return; |
| 100 |
| 101 DVLOG(1) << "texture: " << texture; |
| 102 DVLOG(1) << "Skia texture: " << GetBackingTextureHandleForOverwrite(); |
| 103 |
| 104 canvas_ = WTF::WrapUnique(new SkiaPaintCanvas(surface_->getCanvas())); |
| 105 Clear(); |
| 106 |
| 107 // Always save an initial frame, to support resetting the top level matrix |
| 108 // and clip. |
| 109 canvas_->save(); |
| 110 } |
| 111 |
| 112 bool TextureImageBufferSurface::IsValid() const { |
| 113 return surface_ && SharedGpuContext::IsValid() && |
| 114 context_id_ == SharedGpuContext::ContextId(); |
| 115 } |
| 116 |
| 117 sk_sp<SkImage> TextureImageBufferSurface::NewImageSnapshot(AccelerationHint, |
| 118 SnapshotReason) { |
| 119 return surface_->makeImageSnapshot(); |
| 120 } |
| 121 |
| 122 GLuint TextureImageBufferSurface::GetBackingTextureHandleForOverwrite() { |
| 123 if (!surface_) |
| 124 return 0; |
| 125 return skia::GrBackendObjectToGrGLTextureInfo( |
| 126 surface_->getTextureHandle( |
| 127 SkSurface::kDiscardWrite_TextureHandleAccess)) |
| 128 ->fID; |
| 129 } |
| 130 |
| 131 bool TextureImageBufferSurface::WritePixels(const SkImageInfo& orig_info, |
| 132 const void* pixels, |
| 133 size_t row_bytes, |
| 134 int x, |
| 135 int y) { |
| 136 return surface_->getCanvas()->writePixels(orig_info, pixels, row_bytes, x, y); |
| 137 } |
| 138 |
| 139 } // namespace blink |
OLD | NEW |