blob: 6c448f04fd24e823bf953e8d5159c915001992e0 [file] [log] [blame]
Romain Guycf51a412013-04-08 19:40:31 -07001/*
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
17#define LOG_TAG "OpenGLRenderer"
18
19#include <utils/Log.h>
20
21#include "Caches.h"
Romain Guy9e6f3ac2013-06-20 16:31:35 -070022#include "Debug.h"
Romain Guycf51a412013-04-08 19:40:31 -070023#include "Extensions.h"
24#include "PixelBuffer.h"
25#include "Properties.h"
26
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// CPU pixel buffer
32///////////////////////////////////////////////////////////////////////////////
33
34class CpuPixelBuffer: public PixelBuffer {
35public:
36 CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
Romain Guycf51a412013-04-08 19:40:31 -070037
38 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite);
39 void unmap();
40
41 uint8_t* getMappedPointer() const;
42
43 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset);
44
45private:
Chris Craik51d6a3d2014-12-22 17:16:56 -080046 std::unique_ptr<uint8_t[]> mBuffer;
Romain Guycf51a412013-04-08 19:40:31 -070047};
48
Chris Craik51d6a3d2014-12-22 17:16:56 -080049CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
50 : PixelBuffer(format, width, height)
51 , mBuffer(new uint8_t[width * height * formatSize(format)]) {
Romain Guycf51a412013-04-08 19:40:31 -070052}
53
54uint8_t* CpuPixelBuffer::map(AccessMode mode) {
55 if (mAccessMode == kAccessMode_None) {
56 mAccessMode = mode;
57 }
Chris Craik51d6a3d2014-12-22 17:16:56 -080058 return mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070059}
60
61void CpuPixelBuffer::unmap() {
62 mAccessMode = kAccessMode_None;
63}
64
65uint8_t* CpuPixelBuffer::getMappedPointer() const {
Chris Craik51d6a3d2014-12-22 17:16:56 -080066 return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070067}
68
69void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
70 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
Chris Craik51d6a3d2014-12-22 17:16:56 -080071 mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]);
Romain Guycf51a412013-04-08 19:40:31 -070072}
73
74///////////////////////////////////////////////////////////////////////////////
75// GPU pixel buffer
76///////////////////////////////////////////////////////////////////////////////
77
78class GpuPixelBuffer: public PixelBuffer {
79public:
80 GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
81 ~GpuPixelBuffer();
82
83 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite);
84 void unmap();
85
86 uint8_t* getMappedPointer() const;
87
88 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset);
89
90private:
91 GLuint mBuffer;
92 uint8_t* mMappedPointer;
93 Caches& mCaches;
94};
95
96GpuPixelBuffer::GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height):
97 PixelBuffer(format, width, height), mMappedPointer(0), mCaches(Caches::getInstance()) {
98 glGenBuffers(1, &mBuffer);
99 mCaches.bindPixelBuffer(mBuffer);
100 glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), NULL, GL_DYNAMIC_DRAW);
101 mCaches.unbindPixelBuffer();
102}
103
104GpuPixelBuffer::~GpuPixelBuffer() {
105 glDeleteBuffers(1, &mBuffer);
106}
107
108uint8_t* GpuPixelBuffer::map(AccessMode mode) {
109 if (mAccessMode == kAccessMode_None) {
110 mCaches.bindPixelBuffer(mBuffer);
111 mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode);
Romain Guy9e6f3ac2013-06-20 16:31:35 -0700112#if DEBUG_OPENGL
113 if (!mMappedPointer) {
114 GLenum status = GL_NO_ERROR;
115 while ((status = glGetError()) != GL_NO_ERROR) {
116 ALOGE("Could not map GPU pixel buffer: 0x%x", status);
117 }
118 }
119#endif
Romain Guycf51a412013-04-08 19:40:31 -0700120 mAccessMode = mode;
121 }
122
123 return mMappedPointer;
124}
125
126void GpuPixelBuffer::unmap() {
127 if (mAccessMode != kAccessMode_None) {
128 if (mMappedPointer) {
129 mCaches.bindPixelBuffer(mBuffer);
Romain Guy03c00b52013-06-20 18:30:28 -0700130 GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
131 if (status == GL_FALSE) {
132 ALOGE("Corrupted GPU pixel buffer");
133 }
Romain Guycf51a412013-04-08 19:40:31 -0700134 }
135 mAccessMode = kAccessMode_None;
136 mMappedPointer = NULL;
137 }
138}
139
140uint8_t* GpuPixelBuffer::getMappedPointer() const {
141 return mMappedPointer;
142}
143
144void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
145 // If the buffer is not mapped, unmap() will not bind it
146 mCaches.bindPixelBuffer(mBuffer);
147 unmap();
148 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat,
Kévin PETIT73fc5582014-02-13 11:03:40 +0000149 GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset));
Romain Guycf51a412013-04-08 19:40:31 -0700150}
151
152///////////////////////////////////////////////////////////////////////////////
153// Factory
154///////////////////////////////////////////////////////////////////////////////
155
156PixelBuffer* PixelBuffer::create(GLenum format, uint32_t width, uint32_t height, BufferType type) {
Romain Guyf9f00162013-05-09 11:50:12 -0700157 if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) {
158 return new GpuPixelBuffer(format, width, height);
Romain Guycf51a412013-04-08 19:40:31 -0700159 }
160 return new CpuPixelBuffer(format, width, height);
161}
162
163}; // namespace uirenderer
164}; // namespace android