OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/resources/resource_provider.h" | 5 #include "cc/resources/resource_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 1858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1869 | 1869 |
1870 void ResourceProvider::EnableReadLockFences(ResourceId id) { | 1870 void ResourceProvider::EnableReadLockFences(ResourceId id) { |
1871 Resource* resource = GetResource(id); | 1871 Resource* resource = GetResource(id); |
1872 resource->read_lock_fences_enabled = true; | 1872 resource->read_lock_fences_enabled = true; |
1873 } | 1873 } |
1874 | 1874 |
1875 void ResourceProvider::AcquireImage(ResourceId id) { | 1875 void ResourceProvider::AcquireImage(ResourceId id) { |
1876 Resource* resource = GetResource(id); | 1876 Resource* resource = GetResource(id); |
1877 DCHECK(resource->origin == Resource::Internal); | 1877 DCHECK(resource->origin == Resource::Internal); |
1878 DCHECK_EQ(resource->exported_count, 0); | 1878 DCHECK_EQ(resource->exported_count, 0); |
1879 | 1879 DCHECK_EQ(GLTexture, resource->type); |
1880 if (resource->type != GLTexture) | |
1881 return; | |
1882 | 1880 |
1883 if (resource->image_id) | 1881 if (resource->image_id) |
1884 return; | 1882 return; |
1885 | 1883 |
1886 resource->allocated = true; | 1884 resource->allocated = true; |
1887 GLES2Interface* gl = ContextGL(); | 1885 GLES2Interface* gl = ContextGL(); |
1888 DCHECK(gl); | 1886 DCHECK(gl); |
1889 resource->image_id = | 1887 resource->image_id = |
1890 gl->CreateImageCHROMIUM(resource->size.width(), | 1888 gl->CreateImageCHROMIUM(resource->size.width(), |
1891 resource->size.height(), | 1889 resource->size.height(), |
1892 TextureToStorageFormat(resource->format), | 1890 TextureToStorageFormat(resource->format), |
1893 GL_IMAGE_MAP_CHROMIUM); | 1891 GL_IMAGE_MAP_CHROMIUM); |
1894 DCHECK(resource->image_id); | 1892 DCHECK(resource->image_id); |
1895 } | 1893 } |
1896 | 1894 |
1897 void ResourceProvider::ReleaseImage(ResourceId id) { | 1895 void ResourceProvider::ReleaseImage(ResourceId id) { |
1898 Resource* resource = GetResource(id); | 1896 Resource* resource = GetResource(id); |
1899 DCHECK(resource->origin == Resource::Internal); | 1897 DCHECK(resource->origin == Resource::Internal); |
1900 DCHECK_EQ(resource->exported_count, 0); | 1898 DCHECK_EQ(resource->exported_count, 0); |
| 1899 DCHECK_EQ(GLTexture, resource->type); |
1901 | 1900 |
1902 if (!resource->image_id) | 1901 if (!resource->image_id) |
1903 return; | 1902 return; |
1904 | 1903 |
1905 GLES2Interface* gl = ContextGL(); | 1904 GLES2Interface* gl = ContextGL(); |
1906 DCHECK(gl); | 1905 DCHECK(gl); |
1907 gl->DestroyImageCHROMIUM(resource->image_id); | 1906 gl->DestroyImageCHROMIUM(resource->image_id); |
1908 resource->image_id = 0; | 1907 resource->image_id = 0; |
1909 resource->bound_image_id = 0; | 1908 resource->bound_image_id = 0; |
1910 resource->dirty_image = false; | 1909 resource->dirty_image = false; |
1911 resource->allocated = false; | 1910 resource->allocated = false; |
1912 } | 1911 } |
1913 | 1912 |
1914 uint8_t* ResourceProvider::MapImage(ResourceId id, int* stride) { | 1913 uint8_t* ResourceProvider::MapImage(ResourceId id, int* stride) { |
1915 Resource* resource = GetResource(id); | 1914 Resource* resource = GetResource(id); |
1916 DCHECK(ReadLockFenceHasPassed(resource)); | 1915 DCHECK(ReadLockFenceHasPassed(resource)); |
1917 DCHECK(resource->origin == Resource::Internal); | 1916 DCHECK(resource->origin == Resource::Internal); |
1918 DCHECK_EQ(resource->exported_count, 0); | 1917 DCHECK_EQ(resource->exported_count, 0); |
| 1918 DCHECK(resource->image_id); |
| 1919 |
1919 LockForWrite(id); | 1920 LockForWrite(id); |
1920 | 1921 |
1921 if (resource->type == GLTexture) { | 1922 GLES2Interface* gl = ContextGL(); |
1922 DCHECK(resource->image_id); | 1923 DCHECK(gl); |
1923 GLES2Interface* gl = ContextGL(); | 1924 // MapImageCHROMIUM should be called prior to GetImageParameterivCHROMIUM. |
1924 DCHECK(gl); | 1925 uint8_t* pixels = |
1925 // MapImageCHROMIUM should be called prior to GetImageParameterivCHROMIUM. | 1926 static_cast<uint8_t*>(gl->MapImageCHROMIUM(resource->image_id)); |
1926 uint8_t* pixels = | 1927 gl->GetImageParameterivCHROMIUM( |
1927 static_cast<uint8_t*>(gl->MapImageCHROMIUM(resource->image_id)); | 1928 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, stride); |
1928 gl->GetImageParameterivCHROMIUM( | 1929 return pixels; |
1929 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, stride); | |
1930 return pixels; | |
1931 } | |
1932 DCHECK_EQ(Bitmap, resource->type); | |
1933 *stride = 0; | |
1934 return resource->pixels; | |
1935 } | 1930 } |
1936 | 1931 |
1937 void ResourceProvider::UnmapImage(ResourceId id) { | 1932 void ResourceProvider::UnmapImage(ResourceId id) { |
1938 Resource* resource = GetResource(id); | 1933 Resource* resource = GetResource(id); |
1939 DCHECK(resource->origin == Resource::Internal); | 1934 DCHECK(resource->origin == Resource::Internal); |
1940 DCHECK_EQ(resource->exported_count, 0); | 1935 DCHECK_EQ(resource->exported_count, 0); |
| 1936 DCHECK(resource->image_id); |
1941 DCHECK(resource->locked_for_write); | 1937 DCHECK(resource->locked_for_write); |
1942 | 1938 |
1943 if (resource->image_id) { | 1939 GLES2Interface* gl = ContextGL(); |
1944 GLES2Interface* gl = ContextGL(); | 1940 DCHECK(gl); |
1945 DCHECK(gl); | 1941 gl->UnmapImageCHROMIUM(resource->image_id); |
1946 gl->UnmapImageCHROMIUM(resource->image_id); | 1942 resource->dirty_image = true; |
1947 resource->dirty_image = true; | |
1948 } | |
1949 | 1943 |
1950 UnlockForWrite(id); | 1944 UnlockForWrite(id); |
1951 } | 1945 } |
1952 | 1946 |
1953 void ResourceProvider::AcquireSkSurface(ResourceId id) { | 1947 void ResourceProvider::AcquireSkSurface(ResourceId id) { |
1954 Resource* resource = GetResource(id); | 1948 Resource* resource = GetResource(id); |
1955 DCHECK(resource->origin == Resource::Internal); | 1949 DCHECK(resource->origin == Resource::Internal); |
1956 DCHECK_EQ(resource->exported_count, 0); | 1950 DCHECK_EQ(resource->exported_count, 0); |
1957 | 1951 DCHECK_EQ(GLTexture, resource->type); |
1958 if (resource->type != GLTexture) | |
1959 return; | |
1960 | 1952 |
1961 if (resource->sk_surface) | 1953 if (resource->sk_surface) |
1962 return; | 1954 return; |
1963 | 1955 |
1964 class GrContext* gr_context = GrContext(); | 1956 class GrContext* gr_context = GrContext(); |
1965 // TODO(alokp): Implement TestContextProvider::GrContext(). | 1957 // TODO(alokp): Implement TestContextProvider::GrContext(). |
1966 if (!gr_context) | 1958 if (!gr_context) |
1967 return; | 1959 return; |
1968 | 1960 |
1969 LazyAllocate(resource); | 1961 LazyAllocate(resource); |
(...skipping 11 matching lines...) Expand all Loading... |
1981 use_distance_field_text_ ? SkSurface::kDistanceField_TextRenderMode | 1973 use_distance_field_text_ ? SkSurface::kDistanceField_TextRenderMode |
1982 : SkSurface::kStandard_TextRenderMode; | 1974 : SkSurface::kStandard_TextRenderMode; |
1983 resource->sk_surface = skia::AdoptRef(SkSurface::NewRenderTargetDirect( | 1975 resource->sk_surface = skia::AdoptRef(SkSurface::NewRenderTargetDirect( |
1984 gr_texture->asRenderTarget(), text_render_mode)); | 1976 gr_texture->asRenderTarget(), text_render_mode)); |
1985 } | 1977 } |
1986 | 1978 |
1987 void ResourceProvider::ReleaseSkSurface(ResourceId id) { | 1979 void ResourceProvider::ReleaseSkSurface(ResourceId id) { |
1988 Resource* resource = GetResource(id); | 1980 Resource* resource = GetResource(id); |
1989 DCHECK(resource->origin == Resource::Internal); | 1981 DCHECK(resource->origin == Resource::Internal); |
1990 DCHECK_EQ(resource->exported_count, 0); | 1982 DCHECK_EQ(resource->exported_count, 0); |
| 1983 DCHECK_EQ(GLTexture, resource->type); |
1991 | 1984 |
1992 resource->sk_surface.clear(); | 1985 resource->sk_surface.clear(); |
1993 } | 1986 } |
1994 | 1987 |
1995 SkSurface* ResourceProvider::LockForWriteToSkSurface(ResourceId id) { | 1988 SkSurface* ResourceProvider::LockForWriteToSkSurface(ResourceId id) { |
1996 Resource* resource = GetResource(id); | 1989 Resource* resource = GetResource(id); |
1997 DCHECK(resource->origin == Resource::Internal); | 1990 DCHECK(resource->origin == Resource::Internal); |
1998 DCHECK_EQ(resource->exported_count, 0); | 1991 DCHECK_EQ(resource->exported_count, 0); |
| 1992 DCHECK_EQ(GLTexture, resource->type); |
1999 | 1993 |
2000 LockForWrite(id); | 1994 LockForWrite(id); |
2001 return resource->sk_surface.get(); | 1995 return resource->sk_surface.get(); |
2002 } | 1996 } |
2003 | 1997 |
2004 void ResourceProvider::UnlockForWriteToSkSurface(ResourceId id) { | 1998 void ResourceProvider::UnlockForWriteToSkSurface(ResourceId id) { |
2005 UnlockForWrite(id); | 1999 UnlockForWrite(id); |
2006 } | 2000 } |
2007 | 2001 |
2008 void ResourceProvider::CopyResource(ResourceId source_id, ResourceId dest_id) { | 2002 void ResourceProvider::CopyResource(ResourceId source_id, ResourceId dest_id) { |
2009 TRACE_EVENT0("cc", "ResourceProvider::CopyResource"); | 2003 TRACE_EVENT0("cc", "ResourceProvider::CopyResource"); |
2010 | 2004 |
2011 Resource* source_resource = GetResource(source_id); | 2005 Resource* source_resource = GetResource(source_id); |
2012 DCHECK(!source_resource->lock_for_read_count); | 2006 DCHECK(!source_resource->lock_for_read_count); |
2013 DCHECK(source_resource->origin == Resource::Internal); | 2007 DCHECK(source_resource->origin == Resource::Internal); |
2014 DCHECK_EQ(source_resource->exported_count, 0); | 2008 DCHECK_EQ(source_resource->exported_count, 0); |
| 2009 DCHECK_EQ(GLTexture, source_resource->type); |
2015 DCHECK(source_resource->allocated); | 2010 DCHECK(source_resource->allocated); |
2016 LazyCreate(source_resource); | 2011 LazyCreate(source_resource); |
2017 | 2012 |
2018 Resource* dest_resource = GetResource(dest_id); | 2013 Resource* dest_resource = GetResource(dest_id); |
2019 DCHECK(!dest_resource->locked_for_write); | 2014 DCHECK(!dest_resource->locked_for_write); |
2020 DCHECK(!dest_resource->lock_for_read_count); | 2015 DCHECK(!dest_resource->lock_for_read_count); |
2021 DCHECK(dest_resource->origin == Resource::Internal); | 2016 DCHECK(dest_resource->origin == Resource::Internal); |
2022 DCHECK_EQ(dest_resource->exported_count, 0); | 2017 DCHECK_EQ(dest_resource->exported_count, 0); |
| 2018 DCHECK_EQ(GLTexture, dest_resource->type); |
2023 LazyCreate(dest_resource); | 2019 LazyCreate(dest_resource); |
2024 | 2020 |
2025 DCHECK_EQ(source_resource->type, dest_resource->type); | 2021 DCHECK_EQ(source_resource->type, dest_resource->type); |
2026 DCHECK_EQ(source_resource->format, dest_resource->format); | 2022 DCHECK_EQ(source_resource->format, dest_resource->format); |
2027 DCHECK(source_resource->size == dest_resource->size); | 2023 DCHECK(source_resource->size == dest_resource->size); |
2028 | 2024 |
2029 if (source_resource->type == GLTexture) { | 2025 GLES2Interface* gl = ContextGL(); |
2030 GLES2Interface* gl = ContextGL(); | 2026 DCHECK(gl); |
2031 DCHECK(gl); | 2027 if (source_resource->image_id && source_resource->dirty_image) { |
2032 if (source_resource->image_id && source_resource->dirty_image) { | 2028 gl->BindTexture(source_resource->target, source_resource->gl_id); |
2033 gl->BindTexture(source_resource->target, source_resource->gl_id); | 2029 BindImageForSampling(source_resource); |
2034 BindImageForSampling(source_resource); | |
2035 } | |
2036 DCHECK(use_sync_query_) << "CHROMIUM_sync_query extension missing"; | |
2037 if (!source_resource->gl_read_lock_query_id) | |
2038 gl->GenQueriesEXT(1, &source_resource->gl_read_lock_query_id); | |
2039 gl->BeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM, | |
2040 source_resource->gl_read_lock_query_id); | |
2041 DCHECK(!dest_resource->image_id); | |
2042 dest_resource->allocated = true; | |
2043 gl->CopyTextureCHROMIUM(dest_resource->target, | |
2044 source_resource->gl_id, | |
2045 dest_resource->gl_id, | |
2046 0, | |
2047 GLInternalFormat(dest_resource->format), | |
2048 GLDataType(dest_resource->format)); | |
2049 // End query and create a read lock fence that will prevent access to | |
2050 // source resource until CopyTextureCHROMIUM command has completed. | |
2051 gl->EndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM); | |
2052 source_resource->read_lock_fence = make_scoped_refptr( | |
2053 new QueryFence(gl, source_resource->gl_read_lock_query_id)); | |
2054 } else { | |
2055 DCHECK_EQ(Bitmap, source_resource->type); | |
2056 DCHECK_EQ(RGBA_8888, source_resource->format); | |
2057 LazyAllocate(dest_resource); | |
2058 | |
2059 size_t bytes = SharedBitmap::CheckedSizeInBytes(source_resource->size); | |
2060 memcpy(dest_resource->pixels, source_resource->pixels, bytes); | |
2061 } | 2030 } |
| 2031 DCHECK(use_sync_query_) << "CHROMIUM_sync_query extension missing"; |
| 2032 if (!source_resource->gl_read_lock_query_id) |
| 2033 gl->GenQueriesEXT(1, &source_resource->gl_read_lock_query_id); |
| 2034 gl->BeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM, |
| 2035 source_resource->gl_read_lock_query_id); |
| 2036 DCHECK(!dest_resource->image_id); |
| 2037 dest_resource->allocated = true; |
| 2038 gl->CopyTextureCHROMIUM(dest_resource->target, |
| 2039 source_resource->gl_id, |
| 2040 dest_resource->gl_id, |
| 2041 0, |
| 2042 GLInternalFormat(dest_resource->format), |
| 2043 GLDataType(dest_resource->format)); |
| 2044 // End query and create a read lock fence that will prevent access to |
| 2045 // source resource until CopyTextureCHROMIUM command has completed. |
| 2046 gl->EndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM); |
| 2047 source_resource->read_lock_fence = make_scoped_refptr( |
| 2048 new QueryFence(gl, source_resource->gl_read_lock_query_id)); |
2062 } | 2049 } |
2063 | 2050 |
2064 void ResourceProvider::WaitSyncPointIfNeeded(ResourceId id) { | 2051 void ResourceProvider::WaitSyncPointIfNeeded(ResourceId id) { |
2065 Resource* resource = GetResource(id); | 2052 Resource* resource = GetResource(id); |
2066 DCHECK_EQ(resource->exported_count, 0); | 2053 DCHECK_EQ(resource->exported_count, 0); |
2067 DCHECK(resource->allocated); | 2054 DCHECK(resource->allocated); |
2068 if (resource->type != GLTexture || resource->gl_id) | 2055 if (resource->type != GLTexture || resource->gl_id) |
2069 return; | 2056 return; |
2070 if (!resource->mailbox.sync_point()) | 2057 if (!resource->mailbox.sync_point()) |
2071 return; | 2058 return; |
(...skipping 14 matching lines...) Expand all Loading... |
2086 ContextProvider* context_provider = output_surface_->context_provider(); | 2073 ContextProvider* context_provider = output_surface_->context_provider(); |
2087 return context_provider ? context_provider->ContextGL() : NULL; | 2074 return context_provider ? context_provider->ContextGL() : NULL; |
2088 } | 2075 } |
2089 | 2076 |
2090 class GrContext* ResourceProvider::GrContext() const { | 2077 class GrContext* ResourceProvider::GrContext() const { |
2091 ContextProvider* context_provider = output_surface_->context_provider(); | 2078 ContextProvider* context_provider = output_surface_->context_provider(); |
2092 return context_provider ? context_provider->GrContext() : NULL; | 2079 return context_provider ? context_provider->GrContext() : NULL; |
2093 } | 2080 } |
2094 | 2081 |
2095 } // namespace cc | 2082 } // namespace cc |
OLD | NEW |