Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 17 | #include "PixelBuffer.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 18 | |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 19 | #include "Debug.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 20 | #include "Extensions.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 21 | #include "Properties.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 22 | #include "renderstate/RenderState.h" |
John Reck | 2de7771 | 2016-01-20 11:09:53 -0800 | [diff] [blame] | 23 | #include "utils/GLUtils.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 24 | |
| 25 | #include <utils/Log.h> |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // CPU pixel buffer |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | class CpuPixelBuffer: public PixelBuffer { |
| 35 | public: |
| 36 | CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 37 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 38 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 39 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 40 | uint8_t* getMappedPointer() const override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 41 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 42 | void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 43 | |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame^] | 44 | protected: |
| 45 | void unmap() override; |
| 46 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 47 | private: |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 48 | std::unique_ptr<uint8_t[]> mBuffer; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 51 | CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height) |
| 52 | : PixelBuffer(format, width, height) |
| 53 | , mBuffer(new uint8_t[width * height * formatSize(format)]) { |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | uint8_t* CpuPixelBuffer::map(AccessMode mode) { |
| 57 | if (mAccessMode == kAccessMode_None) { |
| 58 | mAccessMode = mode; |
| 59 | } |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 60 | return mBuffer.get(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void CpuPixelBuffer::unmap() { |
| 64 | mAccessMode = kAccessMode_None; |
| 65 | } |
| 66 | |
| 67 | uint8_t* CpuPixelBuffer::getMappedPointer() const { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 68 | return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
| 72 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 73 | mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /////////////////////////////////////////////////////////////////////////////// |
| 77 | // GPU pixel buffer |
| 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
| 80 | class GpuPixelBuffer: public PixelBuffer { |
| 81 | public: |
| 82 | GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
| 83 | ~GpuPixelBuffer(); |
| 84 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 85 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 86 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 87 | uint8_t* getMappedPointer() const override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 88 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 89 | void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 90 | |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame^] | 91 | protected: |
| 92 | void unmap() override; |
| 93 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 94 | private: |
| 95 | GLuint mBuffer; |
| 96 | uint8_t* mMappedPointer; |
| 97 | Caches& mCaches; |
| 98 | }; |
| 99 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 100 | GpuPixelBuffer::GpuPixelBuffer(GLenum format, |
| 101 | uint32_t width, uint32_t height) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 102 | : PixelBuffer(format, width, height) |
| 103 | , mMappedPointer(nullptr) |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 104 | , mCaches(Caches::getInstance()){ |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 105 | glGenBuffers(1, &mBuffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 106 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 107 | mCaches.pixelBufferState().bind(mBuffer); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 108 | glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 109 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | GpuPixelBuffer::~GpuPixelBuffer() { |
| 113 | glDeleteBuffers(1, &mBuffer); |
| 114 | } |
| 115 | |
| 116 | uint8_t* GpuPixelBuffer::map(AccessMode mode) { |
| 117 | if (mAccessMode == kAccessMode_None) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 118 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 119 | mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode); |
John Reck | 2de7771 | 2016-01-20 11:09:53 -0800 | [diff] [blame] | 120 | if (CC_UNLIKELY(!mMappedPointer)) { |
| 121 | GLUtils::dumpGLErrors(); |
| 122 | LOG_ALWAYS_FATAL("Failed to map PBO"); |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 123 | } |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 124 | mAccessMode = mode; |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame^] | 125 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return mMappedPointer; |
| 129 | } |
| 130 | |
| 131 | void GpuPixelBuffer::unmap() { |
| 132 | if (mAccessMode != kAccessMode_None) { |
| 133 | if (mMappedPointer) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 134 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 135 | GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); |
| 136 | if (status == GL_FALSE) { |
| 137 | ALOGE("Corrupted GPU pixel buffer"); |
| 138 | } |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 139 | } |
| 140 | mAccessMode = kAccessMode_None; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 141 | mMappedPointer = nullptr; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | uint8_t* GpuPixelBuffer::getMappedPointer() const { |
| 146 | return mMappedPointer; |
| 147 | } |
| 148 | |
| 149 | void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
| 150 | // If the buffer is not mapped, unmap() will not bind it |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 151 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 152 | unmap(); |
| 153 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, |
Kévin PETIT | 73fc558 | 2014-02-13 11:03:40 +0000 | [diff] [blame] | 154 | GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset)); |
John Reck | f3ad324 | 2016-02-24 15:36:35 -0800 | [diff] [blame^] | 155 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /////////////////////////////////////////////////////////////////////////////// |
| 159 | // Factory |
| 160 | /////////////////////////////////////////////////////////////////////////////// |
| 161 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 162 | PixelBuffer* PixelBuffer::create(GLenum format, |
| 163 | uint32_t width, uint32_t height, BufferType type) { |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 164 | if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) { |
| 165 | return new GpuPixelBuffer(format, width, height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 166 | } |
| 167 | return new CpuPixelBuffer(format, width, height); |
| 168 | } |
| 169 | |
| 170 | }; // namespace uirenderer |
| 171 | }; // namespace android |