Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2015 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Image.cpp: Implements the egl::Image class representing the EGLimage object. |
| 8 | |
| 9 | #include "libANGLE/Image.h" |
| 10 | |
| 11 | #include "common/debug.h" |
| 12 | #include "common/utilities.h" |
Geoff Lang | e1a057e | 2018-06-07 15:09:00 -0400 | [diff] [blame] | 13 | #include "libANGLE/Renderbuffer.h" |
| 14 | #include "libANGLE/Texture.h" |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 15 | #include "libANGLE/angletypes.h" |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 16 | #include "libANGLE/formatutils.h" |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 17 | #include "libANGLE/renderer/EGLImplFactory.h" |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/ImageImpl.h" |
| 19 | |
| 20 | namespace egl |
| 21 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 22 | |
| 23 | namespace |
| 24 | { |
| 25 | gl::ImageIndex GetImageIndex(EGLenum eglTarget, const egl::AttributeMap &attribs) |
| 26 | { |
| 27 | if (eglTarget == EGL_GL_RENDERBUFFER) |
| 28 | { |
Jamie Madill | cc12937 | 2018-04-12 09:13:18 -0400 | [diff] [blame] | 29 | return gl::ImageIndex(); |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 32 | gl::TextureTarget target = egl_gl::EGLImageTargetToTextureTarget(eglTarget); |
Geoff Lang | e1a057e | 2018-06-07 15:09:00 -0400 | [diff] [blame] | 33 | GLint mip = static_cast<GLint>(attribs.get(EGL_GL_TEXTURE_LEVEL_KHR, 0)); |
| 34 | GLint layer = static_cast<GLint>(attribs.get(EGL_GL_TEXTURE_ZOFFSET_KHR, 0)); |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 35 | |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 36 | if (target == gl::TextureTarget::_3D) |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 37 | { |
| 38 | return gl::ImageIndex::Make3D(mip, layer); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | ASSERT(layer == 0); |
Jamie Madill | cc12937 | 2018-04-12 09:13:18 -0400 | [diff] [blame] | 43 | return gl::ImageIndex::MakeFromTarget(target, mip); |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | } // anonymous namespace |
| 47 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 48 | ImageSibling::ImageSibling(GLuint id) |
| 49 | : RefCountObject(id), FramebufferAttachmentObject(), mSourcesOf(), mTargetOf() |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 50 | { |
| 51 | } |
| 52 | |
| 53 | ImageSibling::~ImageSibling() |
| 54 | { |
| 55 | // EGL images should hold a ref to their targets and siblings, a Texture should not be deletable |
| 56 | // while it is attached to an EGL image. |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 57 | // Child class should orphan images before destruction. |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 58 | ASSERT(mSourcesOf.empty()); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 59 | ASSERT(mTargetOf.get() == nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 62 | void ImageSibling::setTargetImage(const gl::Context *context, egl::Image *imageTarget) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 63 | { |
| 64 | ASSERT(imageTarget != nullptr); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 65 | mTargetOf.set(context, imageTarget); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 66 | imageTarget->addTargetSibling(this); |
| 67 | } |
| 68 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 69 | gl::Error ImageSibling::orphanImages(const gl::Context *context) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 70 | { |
| 71 | if (mTargetOf.get() != nullptr) |
| 72 | { |
| 73 | // Can't be a target and have sources. |
| 74 | ASSERT(mSourcesOf.empty()); |
| 75 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 76 | ANGLE_TRY(mTargetOf->orphanSibling(context, this)); |
| 77 | mTargetOf.set(context, nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 78 | } |
| 79 | else |
| 80 | { |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 81 | for (egl::Image *sourceImage : mSourcesOf) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 82 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 83 | ANGLE_TRY(sourceImage->orphanSibling(context, this)); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 84 | } |
| 85 | mSourcesOf.clear(); |
| 86 | } |
| 87 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 88 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void ImageSibling::addImageSource(egl::Image *imageSource) |
| 92 | { |
| 93 | ASSERT(imageSource != nullptr); |
| 94 | mSourcesOf.insert(imageSource); |
| 95 | } |
| 96 | |
| 97 | void ImageSibling::removeImageSource(egl::Image *imageSource) |
| 98 | { |
| 99 | ASSERT(mSourcesOf.find(imageSource) != mSourcesOf.end()); |
| 100 | mSourcesOf.erase(imageSource); |
| 101 | } |
| 102 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 103 | bool ImageSibling::isEGLImageTarget() const |
| 104 | { |
| 105 | return (mTargetOf.get() != nullptr); |
| 106 | } |
| 107 | |
| 108 | gl::InitState ImageSibling::sourceEGLImageInitState() const |
| 109 | { |
| 110 | ASSERT(isEGLImageTarget()); |
| 111 | return mTargetOf->sourceInitState(); |
| 112 | } |
| 113 | |
| 114 | void ImageSibling::setSourceEGLImageInitState(gl::InitState initState) const |
| 115 | { |
| 116 | ASSERT(isEGLImageTarget()); |
| 117 | mTargetOf->setInitState(initState); |
| 118 | } |
| 119 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 120 | ImageState::ImageState(EGLenum target, ImageSibling *buffer, const AttributeMap &attribs) |
Geoff Lang | 7535966 | 2018-04-11 01:42:27 -0400 | [diff] [blame] | 121 | : label(nullptr), imageIndex(GetImageIndex(target, attribs)), source(buffer), targets() |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 122 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 125 | ImageState::~ImageState() |
| 126 | { |
| 127 | } |
| 128 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 129 | Image::Image(rx::EGLImplFactory *factory, |
Geoff Lang | cd7cd2a | 2018-07-19 11:25:54 -0400 | [diff] [blame] | 130 | const gl::Context *context, |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 131 | EGLenum target, |
| 132 | ImageSibling *buffer, |
| 133 | const AttributeMap &attribs) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 134 | : RefCountObject(0), |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 135 | mState(target, buffer, attribs), |
Geoff Lang | cd7cd2a | 2018-07-19 11:25:54 -0400 | [diff] [blame] | 136 | mImplementation(factory->createImage(mState, context, target, attribs)), |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 137 | mOrphanedAndNeedsInit(false) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 138 | { |
| 139 | ASSERT(mImplementation != nullptr); |
| 140 | ASSERT(buffer != nullptr); |
| 141 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 142 | mState.source->addImageSource(this); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Jamie Madill | 71c88b3 | 2017-09-14 22:20:29 -0400 | [diff] [blame] | 145 | gl::Error Image::onDestroy(const gl::Context *context) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 146 | { |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 147 | // All targets should hold a ref to the egl image and it should not be deleted until there are |
| 148 | // no siblings left. |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 149 | ASSERT(mState.targets.empty()); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 150 | |
| 151 | // Tell the source that it is no longer used by this image |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 152 | if (mState.source.get() != nullptr) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 153 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 154 | mState.source->removeImageSource(this); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 155 | mState.source.set(context, nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 156 | } |
Jamie Madill | 71c88b3 | 2017-09-14 22:20:29 -0400 | [diff] [blame] | 157 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 160 | Image::~Image() |
| 161 | { |
Jamie Madill | 71c88b3 | 2017-09-14 22:20:29 -0400 | [diff] [blame] | 162 | SafeDelete(mImplementation); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Geoff Lang | 7535966 | 2018-04-11 01:42:27 -0400 | [diff] [blame] | 165 | void Image::setLabel(EGLLabelKHR label) |
| 166 | { |
| 167 | mState.label = label; |
| 168 | } |
| 169 | |
| 170 | EGLLabelKHR Image::getLabel() const |
| 171 | { |
| 172 | return mState.label; |
| 173 | } |
| 174 | |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 175 | void Image::addTargetSibling(ImageSibling *sibling) |
| 176 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 177 | mState.targets.insert(sibling); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 180 | gl::Error Image::orphanSibling(const gl::Context *context, ImageSibling *sibling) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 181 | { |
| 182 | // notify impl |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 183 | ANGLE_TRY(mImplementation->orphan(context, sibling)); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 184 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 185 | if (mState.source.get() == sibling) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 186 | { |
| 187 | // If the sibling is the source, it cannot be a target. |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 188 | ASSERT(mState.targets.find(sibling) == mState.targets.end()); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 189 | mState.source.set(context, nullptr); |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 190 | mOrphanedAndNeedsInit = |
| 191 | (sibling->initState(mState.imageIndex) == gl::InitState::MayNeedInit); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 192 | } |
| 193 | else |
| 194 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 195 | mState.targets.erase(sibling); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 198 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Jiawei Shao | a880247 | 2018-05-28 11:17:47 +0800 | [diff] [blame] | 201 | gl::Format Image::getFormat() const |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 202 | { |
Jamie Madill | d75dd26 | 2017-04-20 17:01:19 -0400 | [diff] [blame] | 203 | return mState.source->getAttachmentFormat(GL_NONE, mState.imageIndex); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | size_t Image::getWidth() const |
| 207 | { |
Jamie Madill | d75dd26 | 2017-04-20 17:01:19 -0400 | [diff] [blame] | 208 | return mState.source->getAttachmentSize(mState.imageIndex).width; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | size_t Image::getHeight() const |
| 212 | { |
Jamie Madill | d75dd26 | 2017-04-20 17:01:19 -0400 | [diff] [blame] | 213 | return mState.source->getAttachmentSize(mState.imageIndex).height; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | size_t Image::getSamples() const |
| 217 | { |
Jamie Madill | d75dd26 | 2017-04-20 17:01:19 -0400 | [diff] [blame] | 218 | return mState.source->getAttachmentSamples(mState.imageIndex); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 221 | rx::ImageImpl *Image::getImplementation() const |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 222 | { |
| 223 | return mImplementation; |
| 224 | } |
| 225 | |
Geoff Lang | cd7cd2a | 2018-07-19 11:25:54 -0400 | [diff] [blame] | 226 | Error Image::initialize(const Display *display) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 227 | { |
Geoff Lang | cd7cd2a | 2018-07-19 11:25:54 -0400 | [diff] [blame] | 228 | return mImplementation->initialize(display); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 229 | } |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 230 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 231 | bool Image::orphaned() const |
| 232 | { |
| 233 | return (mState.source.get() == nullptr); |
| 234 | } |
| 235 | |
| 236 | gl::InitState Image::sourceInitState() const |
| 237 | { |
| 238 | if (orphaned()) |
| 239 | { |
| 240 | return mOrphanedAndNeedsInit ? gl::InitState::MayNeedInit : gl::InitState::Initialized; |
| 241 | } |
| 242 | |
| 243 | return mState.source->initState(mState.imageIndex); |
| 244 | } |
| 245 | |
| 246 | void Image::setInitState(gl::InitState initState) |
| 247 | { |
| 248 | if (orphaned()) |
| 249 | { |
| 250 | mOrphanedAndNeedsInit = false; |
| 251 | } |
| 252 | |
| 253 | return mState.source->setInitState(mState.imageIndex, initState); |
| 254 | } |
| 255 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 256 | } // namespace egl |